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
1011namespace 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
0 commit comments