From 39cc0f907932b5fb308fcc66bb2b46e7a5f15118 Mon Sep 17 00:00:00 2001 From: altalk23 <45172705+altalk23@users.noreply.github.com> Date: Tue, 3 Dec 2024 15:15:56 +0300 Subject: [PATCH] add geode unwrap either --- include/Geode/Result.hpp | 13 +++++++++++++ test/Misc.cpp | 16 ++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/include/Geode/Result.hpp b/include/Geode/Result.hpp index 3cafdf2..537627c 100644 --- a/include/Geode/Result.hpp +++ b/include/Geode/Result.hpp @@ -7,6 +7,7 @@ #include #include #include + #include #include #include @@ -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{}, \ + geode::impl::ResultErrType{}, \ + (__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 Result; diff --git a/test/Misc.cpp b/test/Misc.cpp index 309d1a8..8abaec5 100644 --- a/test/Misc.cpp +++ b/test/Misc.cpp @@ -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());