Skip to content

Commit 36ca69f

Browse files
committed
adds geode let macros
1 parent 641e722 commit 36ca69f

File tree

3 files changed

+90
-7
lines changed

3 files changed

+90
-7
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
22
cmake_policy(SET CMP0097 NEW)
33

4-
project(GeodeResult VERSION 1.2.1 LANGUAGES C CXX)
4+
project(GeodeResult VERSION 1.2.3 LANGUAGES C CXX)
55

66
add_library(GeodeResult INTERFACE)
77

include/Geode/Result.hpp

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,30 @@
4040
variable = std::move(GEODE_CONCAT(res, __LINE__)).unwrap()
4141
#endif
4242

43+
#if !defined(GEODE_LET_OK)
44+
#define GEODE_LET_OK(variable, ...) \
45+
auto [variable, GEODE_CONCAT(res, __LINE__)] = \
46+
std::make_pair(geode::impl::ResultOkType<decltype(__VA_ARGS__)>{}, (__VA_ARGS__)); \
47+
GEODE_CONCAT(res, __LINE__).isOk() && \
48+
(variable = std::move(GEODE_CONCAT(res, __LINE__)).unwrap(), true)
49+
#endif
50+
51+
#if !defined(GEODE_LET_ERR)
52+
#define GEODE_LET_ERR(variable, ...) \
53+
auto [variable, GEODE_CONCAT(res, __LINE__)] = \
54+
std::make_pair(geode::impl::ResultErrType<decltype(__VA_ARGS__)>{}, (__VA_ARGS__)); \
55+
GEODE_CONCAT(res, __LINE__).isErr() && \
56+
(variable = std::move(GEODE_CONCAT(res, __LINE__)).unwrapErr(), true)
57+
#endif
58+
59+
#if !defined(GEODE_LET_SOME)
60+
#define GEODE_LET_SOME(variable, ...) \
61+
auto [variable, GEODE_CONCAT(res, __LINE__)] = \
62+
std::make_pair(geode::impl::OptionalType<decltype(__VA_ARGS__)>{}, (__VA_ARGS__)); \
63+
GEODE_CONCAT(res, __LINE__).has_value() && \
64+
(variable = std::move(GEODE_CONCAT(res, __LINE__)).value(), true)
65+
#endif
66+
4367
namespace geode {
4468
template <class OkType, class ErrType>
4569
class Result;
@@ -556,7 +580,8 @@ namespace geode {
556580
}
557581
}
558582

559-
/// @brief Unwraps the Ok value from the Result, returning the result of an operation if unavailable
583+
/// @brief Unwraps the Ok value from the Result, returning the result of an
584+
/// operation if unavailable
560585
/// @param operation the operation to perform if the Result is Err
561586
/// @return the Ok value if available, otherwise the result of the operation
562587
constexpr OkType unwrapOrElse(std::invocable auto&& operation
@@ -571,7 +596,8 @@ namespace geode {
571596
}
572597
}
573598

574-
/// @brief Unwraps the Ok value from the Result, returning the result of an operation if unavailable
599+
/// @brief Unwraps the Ok value from the Result, returning the result of an
600+
/// operation if unavailable
575601
/// @param operation the operation to perform if the Result is Err
576602
/// @return the Ok value if available, otherwise the result of the operation
577603
constexpr OkType unwrapOrElse(std::invocable auto&& operation
@@ -918,7 +944,8 @@ namespace geode {
918944
}
919945
}
920946

921-
/// @brief Unwraps the Ok value from the Result, returning the result of an operation if unavailable
947+
/// @brief Unwraps the Ok value from the Result, returning the result of an
948+
/// operation if unavailable
922949
/// @param operation the operation to perform if the Result is Err
923950
/// @return the Ok value if available, otherwise the result of the operation
924951
constexpr OkType unwrapOrElse(std::invocable auto&& operation
@@ -933,7 +960,8 @@ namespace geode {
933960
}
934961
}
935962

936-
/// @brief Unwraps the Ok value from the Result, returning the result of an operation if unavailable
963+
/// @brief Unwraps the Ok value from the Result, returning the result of an
964+
/// operation if unavailable
937965
/// @param operation the operation to perform if the Result is Err
938966
/// @return the Ok value if available, otherwise the result of the operation
939967
constexpr OkType unwrapOrElse(std::invocable auto&& operation
@@ -1775,7 +1803,8 @@ namespace geode {
17751803
}
17761804
}
17771805

1778-
/// @brief Flattens the Result from Result<Result<OkType, ErrType>, ErrType> to Result<OkType, ErrType>
1806+
/// @brief Flattens the Result from Result<Result<OkType, ErrType>, ErrType> to
1807+
/// Result<OkType, ErrType>
17791808
/// @return the inner Result if the Result is Ok, otherwise the outer Result
17801809
constexpr Result<impl::ResultOkType<OkType>, ErrType> flatten(
17811810
) && noexcept(std::is_nothrow_move_constructible_v<OkType> && std::is_nothrow_move_constructible_v<ErrType>)
@@ -1789,7 +1818,8 @@ namespace geode {
17891818
}
17901819
}
17911820

1792-
/// @brief Flattens the Result from Result<Result<OkType, ErrType>, ErrType> to Result<OkType, ErrType>
1821+
/// @brief Flattens the Result from Result<Result<OkType, ErrType>, ErrType> to
1822+
/// Result<OkType, ErrType>
17931823
/// @return the inner Result if the Result is Ok, otherwise the outer Result
17941824
constexpr Result<impl::ResultOkType<OkType>, ErrType> flatten(
17951825
) const& noexcept(std::is_nothrow_move_constructible_v<OkType> && std::is_nothrow_move_constructible_v<ErrType>)

test/Misc.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,59 @@ TEST_CASE("Misc") {
178178
}
179179
}
180180

181+
SECTION("Let macros") {
182+
SECTION("Ok") {
183+
auto res = divideConstexpr(32, 2);
184+
if (GEODE_LET_OK(value, res)) {
185+
REQUIRE(value == 16);
186+
}
187+
else {
188+
FAIL("Expected the block to be executed");
189+
}
190+
191+
if (GEODE_LET_ERR(value, res)) {
192+
FAIL("Expected the block to not be executed");
193+
}
194+
else {
195+
REQUIRE(true);
196+
}
197+
}
198+
199+
SECTION("Err") {
200+
auto res = divideConstexpr(32, 0);
201+
if (GEODE_LET_ERR(value, res)) {
202+
REQUIRE(value == -1);
203+
}
204+
else {
205+
FAIL("Expected the block to be executed");
206+
}
207+
208+
if (GEODE_LET_OK(value, res)) {
209+
FAIL("Expected the block to not be executed");
210+
}
211+
else {
212+
REQUIRE(true);
213+
}
214+
}
215+
216+
SECTION("Some") {
217+
auto res = divideConstexpr(32, 2);
218+
if (GEODE_LET_SOME(value, res.ok())) {
219+
REQUIRE(value == 16);
220+
}
221+
else {
222+
FAIL("Expected the block to be executed");
223+
}
224+
225+
if (GEODE_LET_SOME(value, res.err())) {
226+
FAIL("Expected the block to not be executed");
227+
}
228+
else {
229+
REQUIRE(true);
230+
}
231+
}
232+
}
233+
181234
SECTION("Operator*") {
182235
auto res = divideConstRefErrRef(32, 2);
183236
REQUIRE(res.isOk());

0 commit comments

Comments
 (0)