Skip to content

Commit 8c1ecd8

Browse files
author
xwei19
committed
Fix
1 parent 10c3ead commit 8c1ecd8

File tree

6 files changed

+50
-32
lines changed

6 files changed

+50
-32
lines changed

CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ if(BUILD_TESTING AND YSTDLIB_CPP_BUILD_TESTING)
5353
set(YSTDLIB_CPP_ENABLE_TESTS ON)
5454
endif()
5555

56-
find_package(Boost REQUIRED)
57-
if(Boost_FOUND)
58-
message(STATUS "Found Boost ${Boost_VERSION}.")
56+
find_package(outcome REQUIRED)
57+
if(outcome_FOUND)
58+
message(STATUS "Found outcome.")
5959
else()
60-
message(STATUS "Could not find libraries for Boost.")
60+
message(FATAL_ERROR "Could not find libraries for outcome.")
6161
endif()
6262

6363
if(YSTDLIB_CPP_ENABLE_TESTS)

src/ystdlib/error_handling/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ cpp_library(
77
Result.hpp
88
utils.hpp
99
PUBLIC_LINK_LIBRARIES
10-
Boost::headers
10+
outcome::hl
1111
TESTS_SOURCES
1212
test/constants.hpp
1313
test/test_ErrorCode.cpp

src/ystdlib/error_handling/Result.hpp

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#ifndef YSTDLIB_ERROR_HANDLING_RESULT_HPP
22
#define YSTDLIB_ERROR_HANDLING_RESULT_HPP
33

4-
#include <boost/outcome/config.hpp>
5-
#include <boost/outcome/std_result.hpp>
6-
#include <boost/outcome/success_failure.hpp>
7-
#include <boost/outcome/try.hpp>
84
#include <system_error>
95

6+
#include <outcome/config.hpp>
7+
#include <outcome/std_result.hpp>
8+
#include <outcome/success_failure.hpp>
9+
#include <outcome/try.hpp>
10+
1011
namespace ystdlib::error_handling {
1112
/**
1213
* A Rust-style `Result<T, E>` type for standardized, exception-free error handling.
@@ -19,16 +20,34 @@ namespace ystdlib::error_handling {
1920
* @tparam ErrorType The type used to represent errors.
2021
*/
2122
template <typename ReturnType, typename ErrorType = std::error_code>
22-
using Result = BOOST_OUTCOME_V2_NAMESPACE::std_result<ReturnType, ErrorType>;
23+
using Result = OUTCOME_V2_NAMESPACE::std_result<ReturnType, ErrorType>;
2324

2425
/**
2526
* @return A value indicating successful completion of a function that returns a void result (i.e.,
2627
* `Result<void, E>`).
2728
*/
28-
[[nodiscard]] inline auto success() -> BOOST_OUTCOME_V2_NAMESPACE::success_type<void> {
29-
return BOOST_OUTCOME_V2_NAMESPACE::success();
29+
[[nodiscard]] inline auto success() -> OUTCOME_V2_NAMESPACE::success_type<void> {
30+
return OUTCOME_V2_NAMESPACE::success();
3031
}
3132

33+
/**
34+
* A function-style macro that emulates Rust’s try (`?`) operator for error propagation.
35+
*
36+
* @param expr An expression that evaluates to a `Result` object.
37+
*
38+
* Behavior:
39+
* - If `expr` represents an error (i.e., `expr.has_error()` returns true), the macro performs an
40+
* early return from the enclosing function with the contained error.
41+
* - Otherwise, it unwraps and yields the successful value as an rvalue reference (`expr.value()`).
42+
*
43+
* NOTE: This macro is only supported on GCC and Clang due to reliance on compiler-specific
44+
* extensions.
45+
*/
46+
#ifdef OUTCOME_TRYX
47+
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
48+
#define YSTDLIB_ERROR_HANDLING_TRYX(expr) OUTCOME_TRYX(expr)
49+
#endif
50+
3251
/**
3352
* A function-style macro for propagating errors from expressions that evaluate to a void result
3453
* (`Result<void, E>`).
@@ -41,8 +60,7 @@ using Result = BOOST_OUTCOME_V2_NAMESPACE::std_result<ReturnType, ErrorType>;
4160
* - Otherwise, execution continues normally.
4261
*/
4362
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
44-
#define YSTDLIB_ERROR_HANDLING_TRYV(expr) BOOST_OUTCOME_TRYV(expr)
45-
#define YSTDLIB_ERROR_HANDLING_TRY(val, expr) BOOST_OUTCOME_TRY(auto&& val, expr)
63+
#define YSTDLIB_ERROR_HANDLING_TRYV(expr) OUTCOME_TRYV(expr)
4664
} // namespace ystdlib::error_handling
4765

4866
#endif // YSTDLIB_ERROR_HANDLING_RESULT_HPP

src/ystdlib/error_handling/test/test_Result.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ TEST_CASE("test_result_int_in_main", "[error_handling][Result]") {
8888

8989
TEST_CASE("test_result_int_propagate", "[error_handling][Result]") {
9090
auto main_func = [&](bool is_error) -> Result<int> {
91-
YSTDLIB_ERROR_HANDLING_TRY(value, cIntFunc(is_error));
92-
return value;
91+
return YSTDLIB_ERROR_HANDLING_TRYX(cIntFunc(is_error));
9392
};
9493
auto const main_no_error{main_func(false)};
9594
REQUIRE_FALSE(main_no_error.has_error());
@@ -107,8 +106,8 @@ TEST_CASE("test_result_unique_ptr", "[error_handling][Result]") {
107106

108107
auto const result_has_error{cUniquePtrFunc(true)};
109108
REQUIRE(result_has_error.has_error());
110-
REQUIRE(AlwaysSuccessErrorCode{AlwaysSuccessErrorCodeEnum::Success}
111-
== result_has_error.error());
109+
REQUIRE(AlwaysSuccessErrorCode{AlwaysSuccessErrorCodeEnum::Success} == result_has_error.error()
110+
);
112111
}
113112

114113
TEST_CASE("test_result_unique_ptr_in_main", "[error_handling][Result]") {
@@ -127,8 +126,7 @@ TEST_CASE("test_result_unique_ptr_in_main", "[error_handling][Result]") {
127126

128127
TEST_CASE("test_result_unique_ptr_propagate", "[error_handling][Result]") {
129128
auto main_func = [&](bool is_error) -> Result<std::unique_ptr<int>> {
130-
YSTDLIB_ERROR_HANDLING_TRY(value, cUniquePtrFunc(is_error));
131-
return value;
129+
return YSTDLIB_ERROR_HANDLING_TRYX(cUniquePtrFunc(is_error));
132130
};
133131
auto const main_no_error{main_func(false)};
134132
REQUIRE_FALSE(main_no_error.has_error());

taskfiles/deps.yaml

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ version: "3"
33
vars:
44
G_CATCH2_LIB_NAME: "Catch2"
55
G_CATCH2_WORK_DIR: "{{.G_DEPS_DIR}}/{{.G_CATCH2_LIB_NAME}}"
6+
G_OUTCOME_LIB_NAME: "outcome"
7+
G_OUTCOME_WORK_DIR: "{{.G_DEPS_DIR}}/{{.G_OUTCOME_LIB_NAME}}"
68
G_QUICKCPPLIB_LIB_NAME: "quickcpplib"
79
G_QUICKCPPLIB_WORK_DIR: "{{.G_DEPS_DIR}}/{{.G_QUICKCPPLIB_LIB_NAME}}"
810

@@ -24,8 +26,8 @@ tasks:
2426
install-all-run:
2527
internal: true
2628
deps:
27-
- "install-boost"
2829
- "install-Catch2"
30+
- "install-outcome"
2931

3032
install-all-finish:
3133
internal: true
@@ -73,13 +75,13 @@ tasks:
7375
deps:
7476
- "install-quickcpplib"
7577
cmds:
76-
- task: ":utils:cmake-install-remote-tar"
78+
- task: ":utils:cmake:install-remote-tar"
7779
vars:
78-
NAME: "{{.G_OUTCOME_LIB_NAME}}"
80+
CMAKE_PACKAGE_NAME: "{{.G_OUTCOME_LIB_NAME}}"
7981
WORK_DIR: "{{.G_OUTCOME_WORK_DIR}}"
80-
FILE_SHA256: "0382248cbb00806ce4b5f3ce6939797dc3b597c85fd3531614959e31ef488b39"
81-
URL: "https://github.com/ned14/outcome/archive/refs/tags/v2.2.11.tar.gz"
82-
GEN_ARGS:
82+
TAR_SHA256: "0382248cbb00806ce4b5f3ce6939797dc3b597c85fd3531614959e31ef488b39"
83+
TAR_URL: "https://github.com/ned14/outcome/archive/refs/tags/v2.2.11.tar.gz"
84+
CMAKE_GEN_ARGS:
8385
- "-C {{.G_DEPS_CMAKE_SETTINGS_DIR}}/{{.G_QUICKCPPLIB_LIB_NAME}}.cmake"
8486
- "-DBUILD_TESTING=OFF"
8587
- "-DCMAKE_BUILD_TYPE=Release"
@@ -93,13 +95,13 @@ tasks:
9395
internal: true
9496
run: "once"
9597
cmds:
96-
- task: ":utils:cmake-install-remote-tar"
98+
- task: ":utils:cmake:install-remote-tar"
9799
vars:
98-
NAME: "{{.G_QUICKCPPLIB_LIB_NAME}}"
100+
CMAKE_PACKAGE_NAME: "{{.G_QUICKCPPLIB_LIB_NAME}}"
99101
WORK_DIR: "{{.G_QUICKCPPLIB_WORK_DIR}}"
100-
FILE_SHA256: "5d4c9b2d6fa177d3fb14f3fe3086867e43b44f4a7a944eb10ee4616b2b0f3c05"
101-
URL: "https://github.com/ned14/quickcpplib/archive/f3e452e.tar.gz"
102-
GEN_ARGS:
102+
TAR_SHA256: "5d4c9b2d6fa177d3fb14f3fe3086867e43b44f4a7a944eb10ee4616b2b0f3c05"
103+
TAR_URL: "https://github.com/ned14/quickcpplib/archive/f3e452e.tar.gz"
104+
CMAKE_GEN_ARGS:
103105
- "-DBUILD_TESTING=OFF"
104106
- "-DCMAKE_BUILD_TYPE=Release"
105107
- task: "add-package-root-to-cmake-settings"

0 commit comments

Comments
 (0)