Skip to content

Commit

Permalink
adds geode let macros
Browse files Browse the repository at this point in the history
  • Loading branch information
altalk23 committed Nov 27, 2024
1 parent 641e722 commit 36ca69f
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
cmake_policy(SET CMP0097 NEW)

project(GeodeResult VERSION 1.2.1 LANGUAGES C CXX)
project(GeodeResult VERSION 1.2.3 LANGUAGES C CXX)

add_library(GeodeResult INTERFACE)

Expand Down
42 changes: 36 additions & 6 deletions include/Geode/Result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@
variable = std::move(GEODE_CONCAT(res, __LINE__)).unwrap()
#endif

#if !defined(GEODE_LET_OK)
#define GEODE_LET_OK(variable, ...) \
auto [variable, GEODE_CONCAT(res, __LINE__)] = \
std::make_pair(geode::impl::ResultOkType<decltype(__VA_ARGS__)>{}, (__VA_ARGS__)); \
GEODE_CONCAT(res, __LINE__).isOk() && \
(variable = std::move(GEODE_CONCAT(res, __LINE__)).unwrap(), true)
#endif

#if !defined(GEODE_LET_ERR)
#define GEODE_LET_ERR(variable, ...) \
auto [variable, GEODE_CONCAT(res, __LINE__)] = \
std::make_pair(geode::impl::ResultErrType<decltype(__VA_ARGS__)>{}, (__VA_ARGS__)); \
GEODE_CONCAT(res, __LINE__).isErr() && \
(variable = std::move(GEODE_CONCAT(res, __LINE__)).unwrapErr(), true)
#endif

#if !defined(GEODE_LET_SOME)
#define GEODE_LET_SOME(variable, ...) \
auto [variable, GEODE_CONCAT(res, __LINE__)] = \
std::make_pair(geode::impl::OptionalType<decltype(__VA_ARGS__)>{}, (__VA_ARGS__)); \
GEODE_CONCAT(res, __LINE__).has_value() && \
(variable = std::move(GEODE_CONCAT(res, __LINE__)).value(), true)
#endif

namespace geode {
template <class OkType, class ErrType>
class Result;
Expand Down Expand Up @@ -556,7 +580,8 @@ namespace geode {
}
}

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

/// @brief Unwraps the Ok value from the Result, returning the result of an operation if unavailable
/// @brief Unwraps the Ok value from the Result, returning the result of an
/// operation if unavailable
/// @param operation the operation to perform if the Result is Err
/// @return the Ok value if available, otherwise the result of the operation
constexpr OkType unwrapOrElse(std::invocable auto&& operation
Expand Down Expand Up @@ -918,7 +944,8 @@ namespace geode {
}
}

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

/// @brief Unwraps the Ok value from the Result, returning the result of an operation if unavailable
/// @brief Unwraps the Ok value from the Result, returning the result of an
/// operation if unavailable
/// @param operation the operation to perform if the Result is Err
/// @return the Ok value if available, otherwise the result of the operation
constexpr OkType unwrapOrElse(std::invocable auto&& operation
Expand Down Expand Up @@ -1775,7 +1803,8 @@ namespace geode {
}
}

/// @brief Flattens the Result from Result<Result<OkType, ErrType>, ErrType> to Result<OkType, ErrType>
/// @brief Flattens the Result from Result<Result<OkType, ErrType>, ErrType> to
/// Result<OkType, ErrType>
/// @return the inner Result if the Result is Ok, otherwise the outer Result
constexpr Result<impl::ResultOkType<OkType>, ErrType> flatten(
) && noexcept(std::is_nothrow_move_constructible_v<OkType> && std::is_nothrow_move_constructible_v<ErrType>)
Expand All @@ -1789,7 +1818,8 @@ namespace geode {
}
}

/// @brief Flattens the Result from Result<Result<OkType, ErrType>, ErrType> to Result<OkType, ErrType>
/// @brief Flattens the Result from Result<Result<OkType, ErrType>, ErrType> to
/// Result<OkType, ErrType>
/// @return the inner Result if the Result is Ok, otherwise the outer Result
constexpr Result<impl::ResultOkType<OkType>, ErrType> flatten(
) const& noexcept(std::is_nothrow_move_constructible_v<OkType> && std::is_nothrow_move_constructible_v<ErrType>)
Expand Down
53 changes: 53 additions & 0 deletions test/Misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,59 @@ TEST_CASE("Misc") {
}
}

SECTION("Let macros") {
SECTION("Ok") {
auto res = divideConstexpr(32, 2);
if (GEODE_LET_OK(value, res)) {
REQUIRE(value == 16);
}
else {
FAIL("Expected the block to be executed");
}

if (GEODE_LET_ERR(value, res)) {
FAIL("Expected the block to not be executed");
}
else {
REQUIRE(true);
}
}

SECTION("Err") {
auto res = divideConstexpr(32, 0);
if (GEODE_LET_ERR(value, res)) {
REQUIRE(value == -1);
}
else {
FAIL("Expected the block to be executed");
}

if (GEODE_LET_OK(value, res)) {
FAIL("Expected the block to not be executed");
}
else {
REQUIRE(true);
}
}

SECTION("Some") {
auto res = divideConstexpr(32, 2);
if (GEODE_LET_SOME(value, res.ok())) {
REQUIRE(value == 16);
}
else {
FAIL("Expected the block to be executed");
}

if (GEODE_LET_SOME(value, res.err())) {
FAIL("Expected the block to not be executed");
}
else {
REQUIRE(true);
}
}
}

SECTION("Operator*") {
auto res = divideConstRefErrRef(32, 2);
REQUIRE(res.isOk());
Expand Down

0 comments on commit 36ca69f

Please sign in to comment.