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+
1011namespace 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 */
2122template <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
0 commit comments