Skip to content

Commit 8a18f96

Browse files
committed
Fix
1 parent 68f8bef commit 8a18f96

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

src/ystdlib/error_handling/Result.hpp

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

4+
#include <system_error>
5+
46
#include <boost/outcome/config.hpp>
57
#include <boost/outcome/std_result.hpp>
68
#include <boost/outcome/success_failure.hpp>
79
#include <boost/outcome/try.hpp>
8-
#include <system_error>
910

1011
namespace ystdlib::error_handling {
1112
/**
@@ -18,16 +19,34 @@ namespace ystdlib::error_handling {
1819
* @tparam ReturnType The type returned on success.
1920
* @tparam ErrorType The type used to represent errors.
2021
*/
21-
template <typename ReturnType, typename ErrorType = std::error_code>
22-
using Result = BOOST_OUTCOME_V2_NAMESPACE::std_result<ReturnType, ErrorType>;
22+
template <typename ReturnType, typename ErrorType = std::error_code>
23+
using Result = BOOST_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();
30-
}
29+
[[nodiscard]] inline auto success() -> BOOST_OUTCOME_V2_NAMESPACE::success_type<void> {
30+
return BOOST_OUTCOME_V2_NAMESPACE::success();
31+
}
32+
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 BOOST_OUTCOME_TRYX
47+
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
48+
#define YSTDLIB_ERROR_HANDLING_TRYX(expr) BOOST_OUTCOME_TRYX(expr)
49+
#endif
3150

3251
/**
3352
* A function-style macro for propagating errors from expressions that evaluate to a void result
@@ -42,8 +61,6 @@ using Result = BOOST_OUTCOME_V2_NAMESPACE::std_result<ReturnType, ErrorType>;
4261
*/
4362
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
4463
#define YSTDLIB_ERROR_HANDLING_TRYV(expr) BOOST_OUTCOME_TRYV(expr)
45-
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
46-
#define YSTDLIB_ERROR_HANDLING_TRY(val, expr) BOOST_OUTCOME_TRY(auto &&(val), (expr))
4764
} // namespace ystdlib::error_handling
4865

4966
#endif // YSTDLIB_ERROR_HANDLING_RESULT_HPP

src/ystdlib/error_handling/test/test_Result.cpp

Lines changed: 2 additions & 4 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());
@@ -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/lint-cmake.yaml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ tasks:
1515
- exclude: "{{.ROOT_DIR}}/**/CMakeFiles/*"
1616
- exclude: "{{.ROOT_DIR}}/**/submodules/*"
1717
- exclude: "{{.ROOT_DIR}}/**/tools/*"
18+
19+
# Exclude CMake build directories generated by CLion
20+
- exclude: "{{.ROOT_DIR}}/**/cmake-build-*"
1821
deps:
1922
- "venv"
2023
cmds:
@@ -48,8 +51,14 @@ tasks:
4851
- |-
4952
. "{{.G_LINT_VENV_DIR}}/bin/activate"
5053
find . \
51-
\( -path '**/build' -o -path '**/cmake_install.cmake' -o -path '**/CMakeFiles' \
52-
-o -path '**/submodules' -o -path '**/tools' \) -prune -o \
54+
\( \
55+
-path '**/build' \
56+
-o -path '**/cmake-build-*' \
57+
-o -path '**/cmake_install.cmake' \
58+
-o -path '**/CMakeFiles' \
59+
-o -path '**/submodules' \
60+
-o -path '**/tools' \
61+
\) -prune -o \
5362
\( -iname "CMakeLists.txt" -o -iname "*.cmake" -o -iname "*.cmake.in" \) \
5463
-print0 | \
5564
xargs -0 gersemi {{.FLAGS}}

0 commit comments

Comments
 (0)