From 36ca69f14cd05b74956a79ec581e2f52ecdeddf0 Mon Sep 17 00:00:00 2001 From: altalk23 <45172705+altalk23@users.noreply.github.com> Date: Wed, 27 Nov 2024 16:29:46 +0300 Subject: [PATCH] adds geode let macros --- CMakeLists.txt | 2 +- include/Geode/Result.hpp | 42 ++++++++++++++++++++++++++----- test/Misc.cpp | 53 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8099805..22f07d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/include/Geode/Result.hpp b/include/Geode/Result.hpp index 5bc3b8a..01272df 100644 --- a/include/Geode/Result.hpp +++ b/include/Geode/Result.hpp @@ -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{}, (__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{}, (__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{}, (__VA_ARGS__)); \ + GEODE_CONCAT(res, __LINE__).has_value() && \ + (variable = std::move(GEODE_CONCAT(res, __LINE__)).value(), true) + #endif + namespace geode { template class Result; @@ -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 @@ -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 @@ -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 @@ -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 @@ -1775,7 +1803,8 @@ namespace geode { } } - /// @brief Flattens the Result from Result, ErrType> to Result + /// @brief Flattens the Result from Result, ErrType> to + /// Result /// @return the inner Result if the Result is Ok, otherwise the outer Result constexpr Result, ErrType> flatten( ) && noexcept(std::is_nothrow_move_constructible_v && std::is_nothrow_move_constructible_v) @@ -1789,7 +1818,8 @@ namespace geode { } } - /// @brief Flattens the Result from Result, ErrType> to Result + /// @brief Flattens the Result from Result, ErrType> to + /// Result /// @return the inner Result if the Result is Ok, otherwise the outer Result constexpr Result, ErrType> flatten( ) const& noexcept(std::is_nothrow_move_constructible_v && std::is_nothrow_move_constructible_v) diff --git a/test/Misc.cpp b/test/Misc.cpp index 5198f87..0275718 100644 --- a/test/Misc.cpp +++ b/test/Misc.cpp @@ -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());