Skip to content

Commit

Permalink
add geode unwrap either
Browse files Browse the repository at this point in the history
  • Loading branch information
altalk23 committed Dec 3, 2024
1 parent 16e30ab commit 39cc0f9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
13 changes: 13 additions & 0 deletions include/Geode/Result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <sstream>
#include <stdexcept>
#include <string>
#include <tuple>
#include <utility>
#include <variant>

Expand Down Expand Up @@ -73,6 +74,18 @@
else
#endif

#if !defined(GEODE_UNWRAP_EITHER)
#define GEODE_UNWRAP_EITHER(okVariable, errVariable, ...) \
auto [okVariable, errVariable, GEODE_CONCAT(res, __LINE__)] = std::make_tuple( \
geode::impl::ResultOkType<decltype(__VA_ARGS__)>{}, \
geode::impl::ResultErrType<decltype(__VA_ARGS__)>{}, \
(__VA_ARGS__) \
); \
GEODE_CONCAT(res, __LINE__).isOk() && \
(okVariable = std::move(GEODE_CONCAT(res, __LINE__)).unwrap(), true) || \
(errVariable = std::move(GEODE_CONCAT(res, __LINE__)).unwrapErr(), false)
#endif

namespace geode {
template <class OkType, class ErrType>
class Result;
Expand Down
16 changes: 16 additions & 0 deletions test/Misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,22 @@ TEST_CASE("Misc") {
REQUIRE(value == 16);
}

SECTION("Unwrap Either") {
if (GEODE_UNWRAP_EITHER(value, err, divideConstexpr(32, 2))) {
REQUIRE(value == 16);
}
else {
FAIL("Expected the block to be executed");
}

if (GEODE_UNWRAP_EITHER(value, err, divideConstexpr(32, 0))) {
FAIL("Expected the block to not be executed");
}
else {
REQUIRE(err == -1);
}
}

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

0 comments on commit 39cc0f9

Please sign in to comment.