Warning, file /include/boost/outcome/success_failure.hpp was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 #ifndef BOOST_OUTCOME_SUCCESS_FAILURE_HPP
0032 #define BOOST_OUTCOME_SUCCESS_FAILURE_HPP
0033
0034 #include "config.hpp"
0035
0036 BOOST_OUTCOME_V2_NAMESPACE_BEGIN
0037
0038
0039
0040
0041 template <class T> struct BOOST_OUTCOME_NODISCARD success_type
0042 {
0043 using value_type = T;
0044
0045 private:
0046 value_type _value;
0047 uint16_t _spare_storage{0};
0048
0049 public:
0050 success_type() = default;
0051 success_type(const success_type &) = default;
0052 success_type(success_type &&) = default;
0053 success_type &operator=(const success_type &) = default;
0054 success_type &operator=(success_type &&) = default;
0055 ~success_type() = default;
0056 BOOST_OUTCOME_TEMPLATE(class U)
0057 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(!std::is_same<success_type, std::decay_t<U>>::value))
0058 constexpr explicit success_type(U &&v, uint16_t spare_storage = 0)
0059 : _value(static_cast<U &&>(v))
0060 , _spare_storage(spare_storage)
0061 {
0062 }
0063
0064 constexpr value_type &value() & { return _value; }
0065 constexpr const value_type &value() const & { return _value; }
0066 constexpr value_type &&value() && { return static_cast<value_type &&>(_value); }
0067 constexpr const value_type &&value() const && { return static_cast<value_type &&>(_value); }
0068
0069 constexpr uint16_t spare_storage() const { return _spare_storage; }
0070 };
0071 template <> struct BOOST_OUTCOME_NODISCARD success_type<void>
0072 {
0073 using value_type = void;
0074
0075 constexpr uint16_t spare_storage() const { return 0; }
0076 };
0077
0078
0079
0080 inline constexpr success_type<void> success() noexcept
0081 {
0082 return success_type<void>{};
0083 }
0084
0085
0086
0087 BOOST_OUTCOME_TEMPLATE(class T)
0088 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_copy_constructible<T>::value))
0089 inline constexpr success_type<std::decay_t<T>> success(const T &v, uint16_t spare_storage = 0)
0090 {
0091 return success_type<std::decay_t<T>>{v, spare_storage};
0092 }
0093
0094
0095
0096 BOOST_OUTCOME_TEMPLATE(class T)
0097 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_move_constructible<T>::value))
0098 inline constexpr success_type<std::decay_t<T>> success(T &&v, uint16_t spare_storage = 0)
0099 {
0100 return success_type<std::decay_t<T>>{static_cast<T &&>(v), spare_storage};
0101 }
0102
0103
0104
0105
0106 template <class EC, class E = void> struct BOOST_OUTCOME_NODISCARD failure_type
0107 {
0108 using error_type = EC;
0109 using exception_type = E;
0110
0111 private:
0112 error_type _error;
0113 exception_type _exception;
0114 bool _have_error{false}, _have_exception{false};
0115 uint16_t _spare_storage{0};
0116
0117 struct error_init_tag
0118 {
0119 };
0120 struct exception_init_tag
0121 {
0122 };
0123
0124 public:
0125 failure_type() = default;
0126 failure_type(const failure_type &) = default;
0127 failure_type(failure_type &&) = default;
0128 failure_type &operator=(const failure_type &) = default;
0129 failure_type &operator=(failure_type &&) = default;
0130 ~failure_type() = default;
0131 template <class U, class V>
0132 constexpr explicit failure_type(U &&u, V &&v, uint16_t spare_storage = 0)
0133 : _error(static_cast<U &&>(u))
0134 , _exception(static_cast<V &&>(v))
0135 , _have_error(true)
0136 , _have_exception(true)
0137 , _spare_storage(spare_storage)
0138 {
0139 }
0140 template <class U>
0141 constexpr explicit failure_type(in_place_type_t<error_type> , U &&u, uint16_t spare_storage = 0, error_init_tag = error_init_tag())
0142 : _error(static_cast<U &&>(u))
0143 , _exception()
0144 , _have_error(true)
0145 , _spare_storage(spare_storage)
0146 {
0147 }
0148 template <class U>
0149 constexpr explicit failure_type(in_place_type_t<exception_type> , U &&u, uint16_t spare_storage = 0,
0150 exception_init_tag = exception_init_tag())
0151 : _error()
0152 , _exception(static_cast<U &&>(u))
0153 , _have_exception(true)
0154 , _spare_storage(spare_storage)
0155 {
0156 }
0157
0158 constexpr bool has_error() const { return _have_error; }
0159 constexpr bool has_exception() const { return _have_exception; }
0160
0161 constexpr error_type &error() & { return _error; }
0162 constexpr const error_type &error() const & { return _error; }
0163 constexpr error_type &&error() && { return static_cast<error_type &&>(_error); }
0164 constexpr const error_type &&error() const && { return static_cast<error_type &&>(_error); }
0165
0166 constexpr exception_type &exception() & { return _exception; }
0167 constexpr const exception_type &exception() const & { return _exception; }
0168 constexpr exception_type &&exception() && { return static_cast<exception_type &&>(_exception); }
0169 constexpr const exception_type &&exception() const && { return static_cast<exception_type &&>(_exception); }
0170
0171 constexpr uint16_t spare_storage() const { return _spare_storage; }
0172 };
0173 template <class EC> struct BOOST_OUTCOME_NODISCARD failure_type<EC, void>
0174 {
0175 using error_type = EC;
0176 using exception_type = void;
0177
0178 private:
0179 error_type _error;
0180 uint16_t _spare_storage{0};
0181
0182 public:
0183 failure_type() = default;
0184 failure_type(const failure_type &) = default;
0185 failure_type(failure_type &&) = default;
0186 failure_type &operator=(const failure_type &) = default;
0187 failure_type &operator=(failure_type &&) = default;
0188 ~failure_type() = default;
0189 BOOST_OUTCOME_TEMPLATE(class U)
0190 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(!std::is_same<failure_type, std::decay_t<U>>::value))
0191 constexpr explicit failure_type(U &&u, uint16_t spare_storage = 0)
0192 : _error(static_cast<U &&>(u))
0193 , _spare_storage(spare_storage)
0194 {
0195 }
0196
0197 constexpr error_type &error() & { return _error; }
0198 constexpr const error_type &error() const & { return _error; }
0199 constexpr error_type &&error() && { return static_cast<error_type &&>(_error); }
0200 constexpr const error_type &&error() const && { return static_cast<error_type &&>(_error); }
0201
0202 constexpr uint16_t spare_storage() const { return _spare_storage; }
0203 };
0204 template <class E> struct BOOST_OUTCOME_NODISCARD failure_type<void, E>
0205 {
0206 using error_type = void;
0207 using exception_type = E;
0208
0209 private:
0210 exception_type _exception;
0211 uint16_t _spare_storage{0};
0212
0213 public:
0214 failure_type() = default;
0215 failure_type(const failure_type &) = default;
0216 failure_type(failure_type &&) = default;
0217 failure_type &operator=(const failure_type &) = default;
0218 failure_type &operator=(failure_type &&) = default;
0219 ~failure_type() = default;
0220 BOOST_OUTCOME_TEMPLATE(class V)
0221 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(!std::is_same<failure_type, std::decay_t<V>>::value))
0222 constexpr explicit failure_type(V &&v, uint16_t spare_storage = 0)
0223 : _exception(static_cast<V &&>(v))
0224 , _spare_storage(spare_storage)
0225 {
0226 }
0227
0228 constexpr exception_type &exception() & { return _exception; }
0229 constexpr const exception_type &exception() const & { return _exception; }
0230 constexpr exception_type &&exception() && { return static_cast<exception_type &&>(_exception); }
0231 constexpr const exception_type &&exception() const && { return static_cast<exception_type &&>(_exception); }
0232
0233 constexpr uint16_t spare_storage() const { return _spare_storage; }
0234 };
0235
0236
0237
0238 BOOST_OUTCOME_TEMPLATE(class EC)
0239 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_copy_constructible<EC>::value))
0240 inline constexpr failure_type<std::decay_t<EC>> failure(const EC &v, uint16_t spare_storage = 0)
0241 {
0242 return failure_type<std::decay_t<EC>>{v, spare_storage};
0243 }
0244
0245
0246
0247 BOOST_OUTCOME_TEMPLATE(class EC)
0248 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_move_constructible<EC>::value))
0249 inline constexpr failure_type<std::decay_t<EC>> failure(EC &&v, uint16_t spare_storage = 0)
0250 {
0251 return failure_type<std::decay_t<EC>>{static_cast<EC &&>(v), spare_storage};
0252 }
0253
0254
0255
0256 BOOST_OUTCOME_TEMPLATE(class EC, class E)
0257 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_copy_constructible<EC>::value &&std::is_copy_constructible<E>::value))
0258 inline constexpr failure_type<std::decay_t<EC>, std::decay_t<E>> failure(const EC &v, const E &w, uint16_t spare_storage = 0)
0259 {
0260 return failure_type<std::decay_t<EC>, std::decay_t<E>>{v, w, spare_storage};
0261 }
0262
0263
0264
0265 BOOST_OUTCOME_TEMPLATE(class EC, class E)
0266 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_copy_constructible<EC>::value &&std::is_move_constructible<E>::value))
0267 inline constexpr failure_type<std::decay_t<EC>, std::decay_t<E>> failure(const EC &v, E &&w, uint16_t spare_storage = 0)
0268 {
0269 return failure_type<std::decay_t<EC>, std::decay_t<E>>{v, static_cast<E &&>(w), spare_storage};
0270 }
0271
0272
0273
0274 BOOST_OUTCOME_TEMPLATE(class EC, class E)
0275 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_move_constructible<EC>::value &&std::is_copy_constructible<E>::value))
0276 inline constexpr failure_type<std::decay_t<EC>, std::decay_t<E>> failure(EC &&v, const E &w, uint16_t spare_storage = 0)
0277 {
0278 return failure_type<std::decay_t<EC>, std::decay_t<E>>{static_cast<EC &&>(v), w, spare_storage};
0279 }
0280
0281
0282
0283 BOOST_OUTCOME_TEMPLATE(class EC, class E)
0284 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_move_constructible<EC>::value &&std::is_move_constructible<E>::value))
0285 inline constexpr failure_type<std::decay_t<EC>, std::decay_t<E>> failure(EC &&v, E &&w, uint16_t spare_storage = 0)
0286 {
0287 return failure_type<std::decay_t<EC>, std::decay_t<E>>{static_cast<EC &&>(v), static_cast<E &&>(w), spare_storage};
0288 }
0289
0290 namespace detail
0291 {
0292 template <class T> struct is_success_type
0293 {
0294 static constexpr bool value = false;
0295 };
0296 template <class T> struct is_success_type<success_type<T>>
0297 {
0298 static constexpr bool value = true;
0299 };
0300 template <class T> struct is_failure_type
0301 {
0302 static constexpr bool value = false;
0303 };
0304 template <class EC, class E> struct is_failure_type<failure_type<EC, E>>
0305 {
0306 static constexpr bool value = true;
0307 };
0308 }
0309
0310
0311
0312
0313 template <class T> static constexpr bool is_success_type = detail::is_success_type<std::decay_t<T>>::value;
0314
0315
0316
0317
0318 template <class T> static constexpr bool is_failure_type = detail::is_failure_type<std::decay_t<T>>::value;
0319
0320 BOOST_OUTCOME_V2_NAMESPACE_END
0321
0322 #endif