STX  1.0.0
try.h
Go to the documentation of this file.
1 
30 
36 #pragma once
37 
39 
41 
42 template <typename Tp, typename Er>
44  Result<Tp, Er>& result) {
45  return std::move(result.value_ref_());
46 }
47 
48 template <typename Tp, typename Er>
50  Result<Tp, Er>& result) {
51  return std::move(result.err_ref_());
52 }
53 
54 template <typename Tp>
56  return std::move(option.value_ref_());
57 }
58 
60 
62 
63 #define STX_TRY__UTIL_JOIN_(x, y) x##_##y
64 #define STX_WITH_UNIQUE_SUFFIX_(x, y) STX_TRY__UTIL_JOIN_(x, y)
65 
66 #define STX_TRY_OK_IMPL_(STX_ARG_UNIQUE_PLACEHOLDER, qualifier_identifier, \
67  result_expr) \
68  static_assert(!::std::is_const_v<decltype((result_expr))>, \
69  "the expression: ' " #result_expr \
70  " ' evaluates to a const and is not mutable"); \
71  static_assert( \
72  !::std::is_lvalue_reference_v<decltype((result_expr))>, \
73  "the expression: ' " #result_expr \
74  " ' evaluates to an l-value reference, 'TRY_OK' only accepts r-values " \
75  "and r-value references "); \
76  decltype((result_expr))&& STX_ARG_UNIQUE_PLACEHOLDER = (result_expr); \
77  \
78  if (STX_ARG_UNIQUE_PLACEHOLDER.is_err()) \
79  return ::stx::Err<decltype((result_expr))::error_type>( \
80  ::stx::internal::result::unsafe_err_move(STX_ARG_UNIQUE_PLACEHOLDER)); \
81  \
82  decltype((result_expr))::value_type qualifier_identifier = \
83  ::stx::internal::result::unsafe_value_move(STX_ARG_UNIQUE_PLACEHOLDER);
84 
85 #define STX_TRY_SOME_IMPL_(STX_ARG_UNIQUE_PLACEHOLDER, qualifier_identifier, \
86  option_expr) \
87  static_assert(!::std::is_const_v<decltype((option_expr))>, \
88  "the expression: ' " #option_expr \
89  " ' evaluates to a const and is not mutable"); \
90  static_assert(!::std::is_lvalue_reference_v<decltype((option_expr))>, \
91  "the expression: ' " #option_expr \
92  " ' evaluates to an l-value reference, 'TRY_SOME' only " \
93  "accepts r-values " \
94  "and r-value references "); \
95  decltype((option_expr))&& STX_ARG_UNIQUE_PLACEHOLDER = (option_expr); \
96  \
97  if (STX_ARG_UNIQUE_PLACEHOLDER.is_none()) return ::stx::None; \
98  \
99  decltype((option_expr))::value_type qualifier_identifier = \
100  ::stx::internal::option::unsafe_value_move(STX_ARG_UNIQUE_PLACEHOLDER);
101 
107 #define TRY_OK(qualifier_identifier, result_expr) \
108  STX_TRY_OK_IMPL_( \
109  STX_WITH_UNIQUE_SUFFIX_(STX_TRY_OK_PLACEHOLDER, __COUNTER__), \
110  qualifier_identifier, result_expr)
111 
117 #define TRY_SOME(qualifier_identifier, option_expr) \
118  STX_TRY_SOME_IMPL_( \
119  STX_WITH_UNIQUE_SUFFIX_(STX_TRY_SOME_PLACEHOLDER, __COUNTER__), \
120  qualifier_identifier, option_expr)
121 
122 // Coroutines
123 // this feature is experimental and not widely tested yet
124 #if defined(__cpp_coroutines) || defined(__cpp_lib_coroutine)
125 
126 #define STX_CO_TRY_OK_IMPL_(STX_ARG_UNIQUE_PLACEHOLDER, qualifier_identifier, \
127  result_expr) \
128  static_assert(!::std::is_const_v<decltype((result_expr))>, \
129  "the expression: ' " #result_expr \
130  " ' evaluates to a const and is not mutable"); \
131  static_assert(!::std::is_lvalue_reference_v<decltype((result_expr))>, \
132  "the expression: ' " #result_expr \
133  " ' evaluates to an l-value reference, 'CO_TRY_OK' only " \
134  "accepts r-values " \
135  "and r-value references "); \
136  decltype((result_expr))&& STX_ARG_UNIQUE_PLACEHOLDER = (result_expr); \
137  \
138  if (STX_ARG_UNIQUE_PLACEHOLDER.is_err()) \
139  co_return ::stx::Err<decltype((result_expr))::error_type>( \
140  ::stx::internal::result::unsafe_err_move(STX_ARG_UNIQUE_PLACEHOLDER)); \
141  \
142  decltype((result_expr))::value_type qualifier_identifier = \
143  ::stx::internal::result::unsafe_value_move(STX_ARG_UNIQUE_PLACEHOLDER);
144 
145 #define STX_CO_TRY_SOME_IMPL_(STX_ARG_UNIQUE_PLACEHOLDER, \
146  qualifier_identifier, option_expr) \
147  static_assert(!::std::is_const_v<decltype((option_expr))>, \
148  "the expression: ' " #option_expr \
149  " ' evaluates to a const and is not mutable"); \
150  static_assert(!::std::is_lvalue_reference_v<decltype((option_expr))>, \
151  "the expression: ' " #option_expr \
152  " ' evaluates to an l-value reference, 'CO_TRY_SOME' only " \
153  "accepts r-values " \
154  "and r-value references "); \
155  decltype((option_expr))&& STX_ARG_UNIQUE_PLACEHOLDER = (option_expr); \
156  \
157  if (STX_ARG_UNIQUE_PLACEHOLDER.is_none()) co_return ::stx::None; \
158  \
159  decltype((option_expr))::value_type qualifier_identifier = \
160  ::stx::internal::option::unsafe_value_move(STX_ARG_UNIQUE_PLACEHOLDER);
161 
167 #define CO_TRY_OK(qualifier_identifier, result_expr) \
168  STX_CO_TRY_OK_IMPL_( \
169  STX_WITH_UNIQUE_SUFFIX_(STX_CO_TRY_OK_PLACEHOLDER, __COUNTER__), \
170  qualifier_identifier, result_expr)
171 
177 #define CO_TRY_SOME(qualifier_identifier, option_expr) \
178  STX_CO_TRY_SOME_IMPL_( \
179  STX_WITH_UNIQUE_SUFFIX_(STX_CO_TRY_SOME_PLACEHOLDER, __COUNTER__), \
180  qualifier_identifier, option_expr)
181 
182 #endif
#define STX_FORCE_INLINE
Definition: config.h:254
Tp && unsafe_value_move(Option< Tp > &)
Er && unsafe_err_move(Result< Tp, Er > &)
#define STX_END_NAMESPACE
Definition: config.h:329
#define STX_BEGIN_NAMESPACE
Definition: config.h:325