1/* Type sugar for success and failure
2(C) 2017-2024 Niall Douglas <http://www.nedproductions.biz/> (25 commits)
3File Created: July 2017
4
5
6Boost Software License - Version 1.0 - August 17th, 2003
7
8Permission is hereby granted, free of charge, to any person or organization
9obtaining a copy of the software and accompanying documentation covered by
10this license (the "Software") to use, reproduce, display, distribute,
11execute, and transmit the Software, and to prepare derivative works of the
12Software, and to permit third-parties to whom the Software is furnished to
13do so, all subject to the following:
14
15The copyright notices in the Software and this entire statement, including
16the above license grant, this restriction and the following disclaimer,
17must be included in all copies of the Software, in whole or in part, and
18all derivative works of the Software, unless such copies or derivative
19works are solely in the form of machine-executable object code generated by
20a source language processor.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
25SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
26FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
27ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28DEALINGS IN THE SOFTWARE.
29*/
30
31#ifndef BOOST_OUTCOME_SUCCESS_FAILURE_HPP
32#define BOOST_OUTCOME_SUCCESS_FAILURE_HPP
33
34#include "config.hpp"
35
36BOOST_OUTCOME_V2_NAMESPACE_BEGIN
37
38/*! AWAITING HUGO JSON CONVERSION TOOL
39type definition template <class T> success_type. Potential doc page: `success_type<T>`
40*/
41template <class T> struct BOOST_OUTCOME_NODISCARD success_type
42{
43 using value_type = T;
44
45private:
46 value_type _value;
47 uint16_t _spare_storage{0};
48
49public:
50 success_type() = default;
51 success_type(const success_type &) = default;
52 success_type(success_type &&) = default; // NOLINT
53 success_type &operator=(const success_type &) = default;
54 success_type &operator=(success_type &&) = default; // NOLINT
55 ~success_type() = default;
56 BOOST_OUTCOME_TEMPLATE(class U)
57 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(!std::is_same<success_type, std::decay_t<U>>::value))
58 constexpr explicit success_type(U &&v, uint16_t spare_storage = 0)
59 : _value(static_cast<U &&>(v)) // NOLINT
60 , _spare_storage(spare_storage)
61 {
62 }
63
64 constexpr value_type &value() & { return _value; }
65 constexpr const value_type &value() const & { return _value; }
66 constexpr value_type &&value() && { return static_cast<value_type &&>(_value); }
67 constexpr const value_type &&value() const && { return static_cast<value_type &&>(_value); }
68
69 constexpr uint16_t spare_storage() const { return _spare_storage; }
70};
71template <> struct BOOST_OUTCOME_NODISCARD success_type<void>
72{
73 using value_type = void;
74
75 constexpr uint16_t spare_storage() const { return 0; }
76};
77/*! Returns type sugar for implicitly constructing a `basic_result<T>` with a successful state,
78default constructing `T` if necessary.
79*/
80inline constexpr success_type<void> success() noexcept
81{
82 return success_type<void>{};
83}
84/*! Returns type sugar for implicitly constructing a `basic_result<T>` with a successful state.
85\effects Copies the successful state supplied into the returned type sugar.
86*/
87BOOST_OUTCOME_TEMPLATE(class T)
88BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_copy_constructible<T>::value))
89inline constexpr success_type<std::decay_t<T>> success(const T &v, uint16_t spare_storage = 0)
90{
91 return success_type<std::decay_t<T>>{v, spare_storage};
92}
93/*! Returns type sugar for implicitly constructing a `basic_result<T>` with a successful state.
94\effects Moves the successful state supplied into the returned type sugar.
95*/
96BOOST_OUTCOME_TEMPLATE(class T)
97BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_move_constructible<T>::value))
98inline constexpr success_type<std::decay_t<T>> success(T &&v, uint16_t spare_storage = 0)
99{
100 return success_type<std::decay_t<T>>{static_cast<T &&>(v), spare_storage};
101}
102
103/*! AWAITING HUGO JSON CONVERSION TOOL
104type definition template <class EC, class E = void> failure_type. Potential doc page: `failure_type<EC, EP = void>`
105*/
106template <class EC, class E = void> struct BOOST_OUTCOME_NODISCARD failure_type
107{
108 using error_type = EC;
109 using exception_type = E;
110
111private:
112 error_type _error;
113 exception_type _exception;
114 bool _have_error{false}, _have_exception{false};
115 uint16_t _spare_storage{0};
116
117 struct error_init_tag
118 {
119 };
120 struct exception_init_tag
121 {
122 };
123
124public:
125 failure_type() = default;
126 failure_type(const failure_type &) = default;
127 failure_type(failure_type &&) = default; // NOLINT
128 failure_type &operator=(const failure_type &) = default;
129 failure_type &operator=(failure_type &&) = default; // NOLINT
130 ~failure_type() = default;
131 template <class U, class V>
132 constexpr explicit failure_type(U &&u, V &&v, uint16_t spare_storage = 0)
133 : _error(static_cast<U &&>(u))
134 , _exception(static_cast<V &&>(v))
135 , _have_error(true)
136 , _have_exception(true)
137 , _spare_storage(spare_storage)
138 {
139 }
140 template <class U>
141 constexpr explicit failure_type(in_place_type_t<error_type> /*unused*/, U &&u, uint16_t spare_storage = 0, error_init_tag /*unused*/ = error_init_tag())
142 : _error(static_cast<U &&>(u))
143 , _exception()
144 , _have_error(true)
145 , _spare_storage(spare_storage)
146 {
147 }
148 template <class U>
149 constexpr explicit failure_type(in_place_type_t<exception_type> /*unused*/, U &&u, uint16_t spare_storage = 0,
150 exception_init_tag /*unused*/ = exception_init_tag())
151 : _error()
152 , _exception(static_cast<U &&>(u))
153 , _have_exception(true)
154 , _spare_storage(spare_storage)
155 {
156 }
157
158 constexpr bool has_error() const { return _have_error; }
159 constexpr bool has_exception() const { return _have_exception; }
160
161 constexpr error_type &error() & { return _error; }
162 constexpr const error_type &error() const & { return _error; }
163 constexpr error_type &&error() && { return static_cast<error_type &&>(_error); }
164 constexpr const error_type &&error() const && { return static_cast<error_type &&>(_error); }
165
166 constexpr exception_type &exception() & { return _exception; }
167 constexpr const exception_type &exception() const & { return _exception; }
168 constexpr exception_type &&exception() && { return static_cast<exception_type &&>(_exception); }
169 constexpr const exception_type &&exception() const && { return static_cast<exception_type &&>(_exception); }
170
171 constexpr uint16_t spare_storage() const { return _spare_storage; }
172};
173template <class EC> struct BOOST_OUTCOME_NODISCARD failure_type<EC, void>
174{
175 using error_type = EC;
176 using exception_type = void;
177
178private:
179 error_type _error;
180 uint16_t _spare_storage{0};
181
182public:
183 failure_type() = default;
184 failure_type(const failure_type &) = default;
185 failure_type(failure_type &&) = default; // NOLINT
186 failure_type &operator=(const failure_type &) = default;
187 failure_type &operator=(failure_type &&) = default; // NOLINT
188 ~failure_type() = default;
189 BOOST_OUTCOME_TEMPLATE(class U)
190 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(!std::is_same<failure_type, std::decay_t<U>>::value))
191 constexpr explicit failure_type(U &&u, uint16_t spare_storage = 0)
192 : _error(static_cast<U &&>(u)) // NOLINT
193 , _spare_storage(spare_storage)
194 {
195 }
196
197 constexpr error_type &error() & { return _error; }
198 constexpr const error_type &error() const & { return _error; }
199 constexpr error_type &&error() && { return static_cast<error_type &&>(_error); }
200 constexpr const error_type &&error() const && { return static_cast<error_type &&>(_error); }
201
202 constexpr uint16_t spare_storage() const { return _spare_storage; }
203};
204template <class E> struct BOOST_OUTCOME_NODISCARD failure_type<void, E>
205{
206 using error_type = void;
207 using exception_type = E;
208
209private:
210 exception_type _exception;
211 uint16_t _spare_storage{0};
212
213public:
214 failure_type() = default;
215 failure_type(const failure_type &) = default;
216 failure_type(failure_type &&) = default; // NOLINT
217 failure_type &operator=(const failure_type &) = default;
218 failure_type &operator=(failure_type &&) = default; // NOLINT
219 ~failure_type() = default;
220 BOOST_OUTCOME_TEMPLATE(class V)
221 BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(!std::is_same<failure_type, std::decay_t<V>>::value))
222 constexpr explicit failure_type(V &&v, uint16_t spare_storage = 0)
223 : _exception(static_cast<V &&>(v)) // NOLINT
224 , _spare_storage(spare_storage)
225 {
226 }
227
228 constexpr exception_type &exception() & { return _exception; }
229 constexpr const exception_type &exception() const & { return _exception; }
230 constexpr exception_type &&exception() && { return static_cast<exception_type &&>(_exception); }
231 constexpr const exception_type &&exception() const && { return static_cast<exception_type &&>(_exception); }
232
233 constexpr uint16_t spare_storage() const { return _spare_storage; }
234};
235/*! AWAITING HUGO JSON CONVERSION TOOL
236SIGNATURE NOT RECOGNISED
237*/
238BOOST_OUTCOME_TEMPLATE(class EC)
239BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_copy_constructible<EC>::value))
240inline constexpr failure_type<std::decay_t<EC>> failure(const EC &v, uint16_t spare_storage = 0)
241{
242 return failure_type<std::decay_t<EC>>{v, spare_storage};
243}
244/*! AWAITING HUGO JSON CONVERSION TOOL
245SIGNATURE NOT RECOGNISED
246*/
247BOOST_OUTCOME_TEMPLATE(class EC)
248BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_move_constructible<EC>::value))
249inline constexpr failure_type<std::decay_t<EC>> failure(EC &&v, uint16_t spare_storage = 0)
250{
251 return failure_type<std::decay_t<EC>>{static_cast<EC &&>(v), spare_storage};
252}
253/*! AWAITING HUGO JSON CONVERSION TOOL
254SIGNATURE NOT RECOGNISED
255*/
256BOOST_OUTCOME_TEMPLATE(class EC, class E)
257BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_copy_constructible<EC>::value &&std::is_copy_constructible<E>::value))
258inline constexpr failure_type<std::decay_t<EC>, std::decay_t<E>> failure(const EC &v, const E &w, uint16_t spare_storage = 0)
259{
260 return failure_type<std::decay_t<EC>, std::decay_t<E>>{v, w, spare_storage};
261}
262/*! AWAITING HUGO JSON CONVERSION TOOL
263SIGNATURE NOT RECOGNISED
264*/
265BOOST_OUTCOME_TEMPLATE(class EC, class E)
266BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_copy_constructible<EC>::value &&std::is_move_constructible<E>::value))
267inline constexpr failure_type<std::decay_t<EC>, std::decay_t<E>> failure(const EC &v, E &&w, uint16_t spare_storage = 0)
268{
269 return failure_type<std::decay_t<EC>, std::decay_t<E>>{v, static_cast<E &&>(w), spare_storage};
270}
271/*! AWAITING HUGO JSON CONVERSION TOOL
272SIGNATURE NOT RECOGNISED
273*/
274BOOST_OUTCOME_TEMPLATE(class EC, class E)
275BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_move_constructible<EC>::value &&std::is_copy_constructible<E>::value))
276inline constexpr failure_type<std::decay_t<EC>, std::decay_t<E>> failure(EC &&v, const E &w, uint16_t spare_storage = 0)
277{
278 return failure_type<std::decay_t<EC>, std::decay_t<E>>{static_cast<EC &&>(v), w, spare_storage};
279}
280/*! AWAITING HUGO JSON CONVERSION TOOL
281SIGNATURE NOT RECOGNISED
282*/
283BOOST_OUTCOME_TEMPLATE(class EC, class E)
284BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_move_constructible<EC>::value &&std::is_move_constructible<E>::value))
285inline constexpr failure_type<std::decay_t<EC>, std::decay_t<E>> failure(EC &&v, E &&w, uint16_t spare_storage = 0)
286{
287 return failure_type<std::decay_t<EC>, std::decay_t<E>>{static_cast<EC &&>(v), static_cast<E &&>(w), spare_storage};
288}
289
290namespace detail
291{
292 template <class T> struct is_success_type
293 {
294 static constexpr bool value = false;
295 };
296 template <class T> struct is_success_type<success_type<T>>
297 {
298 static constexpr bool value = true;
299 };
300 template <class T> struct is_failure_type
301 {
302 static constexpr bool value = false;
303 };
304 template <class EC, class E> struct is_failure_type<failure_type<EC, E>>
305 {
306 static constexpr bool value = true;
307 };
308} // namespace detail
309
310/*! AWAITING HUGO JSON CONVERSION TOOL
311SIGNATURE NOT RECOGNISED
312*/
313template <class T> static constexpr bool is_success_type = detail::is_success_type<std::decay_t<T>>::value;
314
315/*! AWAITING HUGO JSON CONVERSION TOOL
316SIGNATURE NOT RECOGNISED
317*/
318template <class T> static constexpr bool is_failure_type = detail::is_failure_type<std::decay_t<T>>::value;
319
320BOOST_OUTCOME_V2_NAMESPACE_END
321
322#endif
323

source code of boost/libs/outcome/include/boost/outcome/success_failure.hpp