diff --git a/CMakeLists.txt b/CMakeLists.txt index 1ee8b13..8ced0ee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,12 +15,11 @@ FetchContent_MakeAvailable(googletest) enable_testing() -file(GLOB_RECURSE SOURCES "src/**/*.cpp" "src/**/*.hpp") -file(GLOB_RECURSE TESTS_SOURCES "tests/*.cpp") +file(GLOB_RECURSE TESTS_SOURCES "tests/*.cpp") add_executable( tests - ${SOURCES} + src/result.h ${TESTS_SOURCES} ) target_link_libraries(tests GTest::gtest_main) diff --git a/src/Result/Result.hpp b/src/result.h similarity index 82% rename from src/Result/Result.hpp rename to src/result.h index 78bb86a..c1ee436 100644 --- a/src/Result/Result.hpp +++ b/src/result.h @@ -28,16 +28,16 @@ template class Result { enum ContentType { Value, Error }; std::variant content_; - auto emplace_value(const T &value) noexcept -> void; - auto emplace_error(const E &error) noexcept -> void; + auto emplace_value(const T &value) -> void; + auto emplace_error(const E &error) -> void; public: - [[nodiscard]] static auto Ok(const T &value) noexcept -> Result; - [[nodiscard]] static auto Ok(T &&value) noexcept -> Result; + [[nodiscard]] static auto Ok(const T &value) -> Result; + [[nodiscard]] static auto Ok(T &&value) -> Result; [[nodiscard]] static auto Ok() noexcept -> Result; - [[nodiscard]] static auto Err(const E &error) noexcept -> Result; - [[nodiscard]] static auto Err(E &&error) noexcept -> Result; + [[nodiscard]] static auto Err(const E &error) -> Result; + [[nodiscard]] static auto Err(E &&error) -> Result; [[nodiscard]] auto is_ok() const noexcept -> bool; [[nodiscard]] auto is_err() const noexcept -> bool; @@ -46,25 +46,19 @@ template class Result { [[nodiscard]] auto unwrap_err() const -> E; }; -/// @brief Sets the Result content to T(value) with index based std::variant::get. -/// @details This is necessary in case of T and E being the same type. -/// @param value The value to be set. -/// @return void -template inline auto Result::emplace_value(const T &value) noexcept -> void { +template inline auto Result::emplace_value(const T &value) -> void { content_.template emplace(value); } -/// @brief Analogous to `emplace_value` but for error type. -/// @param error The error to be set. -/// @return void -template inline auto Result::emplace_error(const E &error) noexcept -> void { +template inline auto Result::emplace_error(const E &error) -> void { content_.template emplace(error); } /// @brief Creates a successful Result object containing a value. /// @param value /// @return Result -template inline auto Result::Ok(const T &value) noexcept -> Result { +/// @throws Any exception thrown during the initialization of the T value. +template inline auto Result::Ok(const T &value) -> Result { Result result; result.emplace_value(value); return result; @@ -73,7 +67,8 @@ template inline auto Result::Ok(const T &value) n /// @brief Analogous to `Ok(const T &value)` but for rvalue reference. /// @param value /// @return Result -template inline auto Result::Ok(T &&value) noexcept -> Result { +/// @throws Any exception thrown during the initialization of the T value. +template inline auto Result::Ok(T &&value) -> Result { Result result; result.emplace_value(std::move(value)); return result; @@ -92,7 +87,8 @@ template inline auto Result::Ok() noexcept -> Res /// @note Although it's possible to create a Result with an empty value, it's impossible to create it with an empty /// error. This is enforced by static_assert. /// @return Result -template inline auto Result::Err(const E &error) noexcept -> Result { +/// @throws Any exception thrown during the initialization of the E value. +template inline auto Result::Err(const E &error) -> Result { Result result; result.emplace_error(error); return result; @@ -101,7 +97,8 @@ template inline auto Result::Err(const E &error) /// @brief Analogous to `Err(const E &error)` but for rvalue reference. /// @param error /// @return Result -template inline auto Result::Err(E &&error) noexcept -> Result { +/// @throws Any exception thrown during the initialization of the E value. +template inline auto Result::Err(E &&error) -> Result { Result result; result.emplace_error(std::move(error)); return result; diff --git a/tests/is_ok.cpp b/tests/is_ok.cpp index 6f6a4ba..7998369 100644 --- a/tests/is_ok.cpp +++ b/tests/is_ok.cpp @@ -1,4 +1,4 @@ -#include "../src/Result/Result.hpp" +#include "../src/result.h" #include #include diff --git a/tests/type_edgecases.cpp b/tests/type_edgecases.cpp index 2b41e46..f6c0c75 100644 --- a/tests/type_edgecases.cpp +++ b/tests/type_edgecases.cpp @@ -1,4 +1,4 @@ -#include "../src/Result/Result.hpp" +#include "../src/result.h" #include #include diff --git a/tests/unwrap.cpp b/tests/unwrap.cpp index a68c332..75c5e76 100644 --- a/tests/unwrap.cpp +++ b/tests/unwrap.cpp @@ -1,4 +1,4 @@ -#include "../src/Result/Result.hpp" +#include "../src/result.h" #include #include