]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/outcome/experimental/status-code/errored_status_code.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / outcome / experimental / status-code / errored_status_code.hpp
1 /* Proposed SG14 status_code
2 (C) 2018-2020 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
3 File Created: Jun 2018
4
5
6 Boost Software License - Version 1.0 - August 17th, 2003
7
8 Permission is hereby granted, free of charge, to any person or organization
9 obtaining a copy of the software and accompanying documentation covered by
10 this license (the "Software") to use, reproduce, display, distribute,
11 execute, and transmit the Software, and to prepare derivative works of the
12 Software, and to permit third-parties to whom the Software is furnished to
13 do so, all subject to the following:
14
15 The copyright notices in the Software and this entire statement, including
16 the above license grant, this restriction and the following disclaimer,
17 must be included in all copies of the Software, in whole or in part, and
18 all derivative works of the Software, unless such copies or derivative
19 works are solely in the form of machine-executable object code generated by
20 a source language processor.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
25 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
26 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
27 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 DEALINGS IN THE SOFTWARE.
29 */
30
31 #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_ERRORED_STATUS_CODE_HPP
32 #define BOOST_OUTCOME_SYSTEM_ERROR2_ERRORED_STATUS_CODE_HPP
33
34 #include "quick_status_code_from_enum.hpp"
35 #include "status_code_ptr.hpp"
36
37 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
38
39 /*! A `status_code` which is always a failure. The closest equivalent to
40 `std::error_code`, except it cannot be modified, and is templated.
41
42 Differences from `status_code`:
43
44 - Never successful (this contract is checked on construction, if fails then it
45 terminates the process).
46 - Is immutable.
47 */
48 template <class DomainType> class errored_status_code : public status_code<DomainType>
49 {
50 using _base = status_code<DomainType>;
51 using _base::clear;
52 using _base::success;
53
54 void _check()
55 {
56 if(_base::success())
57 {
58 std::terminate();
59 }
60 }
61
62 public:
63 //! The type of the erased error code.
64 using typename _base::value_type;
65 //! The type of a reference to a message string.
66 using typename _base::string_ref;
67
68 //! Default constructor.
69 errored_status_code() = default;
70 //! Copy constructor.
71 errored_status_code(const errored_status_code &) = default;
72 //! Move constructor.
73 errored_status_code(errored_status_code &&) = default; // NOLINT
74 //! Copy assignment.
75 errored_status_code &operator=(const errored_status_code &) = default;
76 //! Move assignment.
77 errored_status_code &operator=(errored_status_code &&) = default; // NOLINT
78 ~errored_status_code() = default;
79
80 //! Explicitly construct from any similarly erased status code
81 explicit errored_status_code(const _base &o) noexcept(std::is_nothrow_copy_constructible<_base>::value)
82 : _base(o)
83 {
84 _check();
85 }
86 //! Explicitly construct from any similarly erased status code
87 explicit errored_status_code(_base &&o) noexcept(std::is_nothrow_move_constructible<_base>::value)
88 : _base(static_cast<_base &&>(o))
89 {
90 _check();
91 }
92
93 /***** KEEP THESE IN SYNC WITH STATUS_CODE *****/
94 //! Implicit construction from any type where an ADL discovered `make_status_code(T, Args ...)` returns a `status_code`.
95 template <class T, class... Args, //
96 class MakeStatusCodeResult = typename detail::safe_get_make_status_code_result<T, Args...>::type, // Safe ADL lookup of make_status_code(), returns void if not found
97 typename std::enable_if<!std::is_same<typename std::decay<T>::type, errored_status_code>::value // not copy/move of self
98 && !std::is_same<typename std::decay<T>::type, in_place_t>::value // not in_place_t
99 && is_status_code<MakeStatusCodeResult>::value // ADL makes a status code
100 && std::is_constructible<errored_status_code, MakeStatusCodeResult>::value, // ADLed status code is compatible
101 bool>::type = true>
102 errored_status_code(T &&v, Args &&... args) noexcept(noexcept(make_status_code(std::declval<T>(), std::declval<Args>()...))) // NOLINT
103 : errored_status_code(make_status_code(static_cast<T &&>(v), static_cast<Args &&>(args)...))
104 {
105 _check();
106 }
107 //! Implicit construction from any `quick_status_code_from_enum<Enum>` enumerated type.
108 template <class Enum, //
109 class QuickStatusCodeType = typename quick_status_code_from_enum<Enum>::code_type, // Enumeration has been activated
110 typename std::enable_if<std::is_constructible<errored_status_code, QuickStatusCodeType>::value, // Its status code is compatible
111
112 bool>::type = true>
113 errored_status_code(Enum &&v) noexcept(std::is_nothrow_constructible<errored_status_code, QuickStatusCodeType>::value) // NOLINT
114 : errored_status_code(QuickStatusCodeType(static_cast<Enum &&>(v)))
115 {
116 _check();
117 }
118 //! Explicit in-place construction.
119 template <class... Args>
120 explicit errored_status_code(in_place_t _, Args &&... args) noexcept(std::is_nothrow_constructible<value_type, Args &&...>::value)
121 : _base(_, static_cast<Args &&>(args)...)
122 {
123 _check();
124 }
125 //! Explicit in-place construction from initialiser list.
126 template <class T, class... Args>
127 explicit errored_status_code(in_place_t _, std::initializer_list<T> il, Args &&... args) noexcept(std::is_nothrow_constructible<value_type, std::initializer_list<T>, Args &&...>::value)
128 : _base(_, il, static_cast<Args &&>(args)...)
129 {
130 _check();
131 }
132 //! Explicit copy construction from a `value_type`.
133 explicit errored_status_code(const value_type &v) noexcept(std::is_nothrow_copy_constructible<value_type>::value)
134 : _base(v)
135 {
136 _check();
137 }
138 //! Explicit move construction from a `value_type`.
139 explicit errored_status_code(value_type &&v) noexcept(std::is_nothrow_move_constructible<value_type>::value)
140 : _base(static_cast<value_type &&>(v))
141 {
142 _check();
143 }
144 /*! Explicit construction from an erased status code. Available only if
145 `value_type` is trivially destructible and `sizeof(status_code) <= sizeof(status_code<erased<>>)`.
146 Does not check if domains are equal.
147 */
148 template <class ErasedType, //
149 typename std::enable_if<detail::type_erasure_is_safe<ErasedType, value_type>::value, bool>::type = true>
150 explicit errored_status_code(const status_code<erased<ErasedType>> &v) noexcept(std::is_nothrow_copy_constructible<value_type>::value)
151 : errored_status_code(detail::erasure_cast<value_type>(v.value())) // NOLINT
152 {
153 assert(v.domain() == this->domain()); // NOLINT
154 _check();
155 }
156
157 //! Always false (including at compile time), as errored status codes are never successful.
158 constexpr bool success() const noexcept { return false; }
159 //! Return a const reference to the `value_type`.
160 constexpr const value_type &value() const &noexcept { return this->_value; }
161 };
162
163 namespace traits
164 {
165 template <class DomainType> struct is_move_bitcopying<errored_status_code<DomainType>>
166 {
167 static constexpr bool value = is_move_bitcopying<typename DomainType::value_type>::value;
168 };
169 } // namespace traits
170
171 template <class ErasedType> class errored_status_code<erased<ErasedType>> : public status_code<erased<ErasedType>>
172 {
173 using _base = status_code<erased<ErasedType>>;
174 using _base::success;
175
176 void _check()
177 {
178 if(_base::success())
179 {
180 std::terminate();
181 }
182 }
183
184 public:
185 using value_type = typename _base::value_type;
186 using string_ref = typename _base::string_ref;
187
188 //! Default construction to empty
189 errored_status_code() = default;
190 //! Copy constructor
191 errored_status_code(const errored_status_code &) = default;
192 //! Move constructor
193 errored_status_code(errored_status_code &&) = default; // NOLINT
194 //! Copy assignment
195 errored_status_code &operator=(const errored_status_code &) = default;
196 //! Move assignment
197 errored_status_code &operator=(errored_status_code &&) = default; // NOLINT
198 ~errored_status_code() = default;
199
200 //! Explicitly construct from any similarly erased status code
201 explicit errored_status_code(const _base &o) noexcept(std::is_nothrow_copy_constructible<_base>::value)
202 : _base(o)
203 {
204 _check();
205 }
206 //! Explicitly construct from any similarly erased status code
207 explicit errored_status_code(_base &&o) noexcept(std::is_nothrow_move_constructible<_base>::value)
208 : _base(static_cast<_base &&>(o))
209 {
210 _check();
211 }
212
213 /***** KEEP THESE IN SYNC WITH STATUS_CODE *****/
214 //! Implicit copy construction from any other status code if its value type is trivially copyable and it would fit into our storage
215 template <class DomainType, //
216 typename std::enable_if<std::is_trivially_copyable<typename DomainType::value_type>::value //
217 && detail::type_erasure_is_safe<value_type, typename DomainType::value_type>::value,
218 bool>::type = true>
219 errored_status_code(const status_code<DomainType> &v) noexcept
220 : _base(v) // NOLINT
221 {
222 _check();
223 }
224 //! Implicit copy construction from any other status code if its value type is trivially copyable and it would fit into our storage
225 template <class DomainType, //
226 typename std::enable_if<std::is_trivially_copyable<typename DomainType::value_type>::value //
227 && detail::type_erasure_is_safe<value_type, typename DomainType::value_type>::value,
228 bool>::type = true>
229 errored_status_code(const errored_status_code<DomainType> &v) noexcept
230 : _base(static_cast<const status_code<DomainType> &>(v)) // NOLINT
231 {
232 _check();
233 }
234 //! Implicit move construction from any other status code if its value type is trivially copyable or move bitcopying and it would fit into our storage
235 template <class DomainType, //
236 typename std::enable_if<detail::type_erasure_is_safe<value_type, typename DomainType::value_type>::value,
237 bool>::type = true>
238 errored_status_code(status_code<DomainType> &&v) noexcept
239 : _base(static_cast<status_code<DomainType> &&>(v)) // NOLINT
240 {
241 _check();
242 }
243 //! Implicit move construction from any other status code if its value type is trivially copyable or move bitcopying and it would fit into our storage
244 template <class DomainType, //
245 typename std::enable_if<detail::type_erasure_is_safe<value_type, typename DomainType::value_type>::value,
246 bool>::type = true>
247 errored_status_code(errored_status_code<DomainType> &&v) noexcept
248 : _base(static_cast<status_code<DomainType> &&>(v)) // NOLINT
249 {
250 _check();
251 }
252 //! Implicit construction from any type where an ADL discovered `make_status_code(T, Args ...)` returns a `status_code`.
253 template <class T, class... Args, //
254 class MakeStatusCodeResult = typename detail::safe_get_make_status_code_result<T, Args...>::type, // Safe ADL lookup of make_status_code(), returns void if not found
255 typename std::enable_if<!std::is_same<typename std::decay<T>::type, errored_status_code>::value // not copy/move of self
256 && !std::is_same<typename std::decay<T>::type, value_type>::value // not copy/move of value type
257 && is_status_code<MakeStatusCodeResult>::value // ADL makes a status code
258 && std::is_constructible<errored_status_code, MakeStatusCodeResult>::value, // ADLed status code is compatible
259 bool>::type = true>
260 errored_status_code(T &&v, Args &&... args) noexcept(noexcept(make_status_code(std::declval<T>(), std::declval<Args>()...))) // NOLINT
261 : errored_status_code(make_status_code(static_cast<T &&>(v), static_cast<Args &&>(args)...))
262 {
263 _check();
264 }
265 //! Implicit construction from any `quick_status_code_from_enum<Enum>` enumerated type.
266 template <class Enum, //
267 class QuickStatusCodeType = typename quick_status_code_from_enum<Enum>::code_type, // Enumeration has been activated
268 typename std::enable_if<std::is_constructible<errored_status_code, QuickStatusCodeType>::value, // Its status code is compatible
269
270 bool>::type = true>
271 errored_status_code(Enum &&v) noexcept(std::is_nothrow_constructible<errored_status_code, QuickStatusCodeType>::value) // NOLINT
272 : errored_status_code(QuickStatusCodeType(static_cast<Enum &&>(v)))
273 {
274 _check();
275 }
276
277 //! Always false (including at compile time), as errored status codes are never successful.
278 constexpr bool success() const noexcept { return false; }
279 //! Return the erased `value_type` by value.
280 constexpr value_type value() const noexcept { return this->_value; }
281 };
282
283 namespace traits
284 {
285 template <class ErasedType> struct is_move_bitcopying<errored_status_code<erased<ErasedType>>>
286 {
287 static constexpr bool value = true;
288 };
289 } // namespace traits
290
291
292 //! True if the status code's are semantically equal via `equivalent()`.
293 template <class DomainType1, class DomainType2> inline bool operator==(const errored_status_code<DomainType1> &a, const errored_status_code<DomainType2> &b) noexcept
294 {
295 return a.equivalent(static_cast<const status_code<DomainType2> &>(b));
296 }
297 //! True if the status code's are semantically equal via `equivalent()`.
298 template <class DomainType1, class DomainType2> inline bool operator==(const status_code<DomainType1> &a, const errored_status_code<DomainType2> &b) noexcept
299 {
300 return a.equivalent(static_cast<const status_code<DomainType2> &>(b));
301 }
302 //! True if the status code's are semantically equal via `equivalent()`.
303 template <class DomainType1, class DomainType2> inline bool operator==(const errored_status_code<DomainType1> &a, const status_code<DomainType2> &b) noexcept
304 {
305 return static_cast<const status_code<DomainType1> &>(a).equivalent(b);
306 }
307 //! True if the status code's are not semantically equal via `equivalent()`.
308 template <class DomainType1, class DomainType2> inline bool operator!=(const errored_status_code<DomainType1> &a, const errored_status_code<DomainType2> &b) noexcept
309 {
310 return !a.equivalent(static_cast<const status_code<DomainType2> &>(b));
311 }
312 //! True if the status code's are not semantically equal via `equivalent()`.
313 template <class DomainType1, class DomainType2> inline bool operator!=(const status_code<DomainType1> &a, const errored_status_code<DomainType2> &b) noexcept
314 {
315 return !a.equivalent(static_cast<const status_code<DomainType2> &>(b));
316 }
317 //! True if the status code's are not semantically equal via `equivalent()`.
318 template <class DomainType1, class DomainType2> inline bool operator!=(const errored_status_code<DomainType1> &a, const status_code<DomainType2> &b) noexcept
319 {
320 return !static_cast<const status_code<DomainType1> &>(a).equivalent(b);
321 }
322 //! True if the status code's are semantically equal via `equivalent()` to `make_status_code(T)`.
323 template <class DomainType1, class T, //
324 class MakeStatusCodeResult = typename detail::safe_get_make_status_code_result<const T &>::type, // Safe ADL lookup of make_status_code(), returns void if not found
325 typename std::enable_if<is_status_code<MakeStatusCodeResult>::value, bool>::type = true> // ADL makes a status code
326 inline bool operator==(const errored_status_code<DomainType1> &a, const T &b)
327 {
328 return a.equivalent(make_status_code(b));
329 }
330 //! True if the status code's are semantically equal via `equivalent()` to `make_status_code(T)`.
331 template <class T, class DomainType1, //
332 class MakeStatusCodeResult = typename detail::safe_get_make_status_code_result<const T &>::type, // Safe ADL lookup of make_status_code(), returns void if not found
333 typename std::enable_if<is_status_code<MakeStatusCodeResult>::value, bool>::type = true> // ADL makes a status code
334 inline bool operator==(const T &a, const errored_status_code<DomainType1> &b)
335 {
336 return b.equivalent(make_status_code(a));
337 }
338 //! True if the status code's are not semantically equal via `equivalent()` to `make_status_code(T)`.
339 template <class DomainType1, class T, //
340 class MakeStatusCodeResult = typename detail::safe_get_make_status_code_result<const T &>::type, // Safe ADL lookup of make_status_code(), returns void if not found
341 typename std::enable_if<is_status_code<MakeStatusCodeResult>::value, bool>::type = true> // ADL makes a status code
342 inline bool operator!=(const errored_status_code<DomainType1> &a, const T &b)
343 {
344 return !a.equivalent(make_status_code(b));
345 }
346 //! True if the status code's are semantically equal via `equivalent()` to `make_status_code(T)`.
347 template <class T, class DomainType1, //
348 class MakeStatusCodeResult = typename detail::safe_get_make_status_code_result<const T &>::type, // Safe ADL lookup of make_status_code(), returns void if not found
349 typename std::enable_if<is_status_code<MakeStatusCodeResult>::value, bool>::type = true> // ADL makes a status code
350 inline bool operator!=(const T &a, const errored_status_code<DomainType1> &b)
351 {
352 return !b.equivalent(make_status_code(a));
353 }
354 //! True if the status code's are semantically equal via `equivalent()` to `quick_status_code_from_enum<T>::code_type(b)`.
355 template <class DomainType1, class T, //
356 class QuickStatusCodeType = typename quick_status_code_from_enum<T>::code_type // Enumeration has been activated
357 >
358 inline bool operator==(const errored_status_code<DomainType1> &a, const T &b)
359 {
360 return a.equivalent(QuickStatusCodeType(b));
361 }
362 //! True if the status code's are semantically equal via `equivalent()` to `quick_status_code_from_enum<T>::code_type(a)`.
363 template <class T, class DomainType1, //
364 class QuickStatusCodeType = typename quick_status_code_from_enum<T>::code_type // Enumeration has been activated
365 >
366 inline bool operator==(const T &a, const errored_status_code<DomainType1> &b)
367 {
368 return b.equivalent(QuickStatusCodeType(a));
369 }
370 //! True if the status code's are not semantically equal via `equivalent()` to `quick_status_code_from_enum<T>::code_type(b)`.
371 template <class DomainType1, class T, //
372 class QuickStatusCodeType = typename quick_status_code_from_enum<T>::code_type // Enumeration has been activated
373 >
374 inline bool operator!=(const errored_status_code<DomainType1> &a, const T &b)
375 {
376 return !a.equivalent(QuickStatusCodeType(b));
377 }
378 //! True if the status code's are not semantically equal via `equivalent()` to `quick_status_code_from_enum<T>::code_type(a)`.
379 template <class T, class DomainType1, //
380 class QuickStatusCodeType = typename quick_status_code_from_enum<T>::code_type // Enumeration has been activated
381 >
382 inline bool operator!=(const T &a, const errored_status_code<DomainType1> &b)
383 {
384 return !b.equivalent(QuickStatusCodeType(a));
385 }
386
387
388 namespace detail
389 {
390 template <class T> struct is_errored_status_code
391 {
392 static constexpr bool value = false;
393 };
394 template <class T> struct is_errored_status_code<errored_status_code<T>>
395 {
396 static constexpr bool value = true;
397 };
398 template <class T> struct is_erased_errored_status_code
399 {
400 static constexpr bool value = false;
401 };
402 template <class T> struct is_erased_errored_status_code<errored_status_code<erased<T>>>
403 {
404 static constexpr bool value = true;
405 };
406 } // namespace detail
407
408 //! Trait returning true if the type is an errored status code.
409 template <class T> struct is_errored_status_code
410 {
411 static constexpr bool value = detail::is_errored_status_code<typename std::decay<T>::type>::value || detail::is_erased_errored_status_code<typename std::decay<T>::type>::value;
412 };
413
414
415 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
416
417 #endif