]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/outcome/experimental/status-code/status_code.hpp
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / boost / boost / outcome / experimental / status-code / status_code.hpp
1 /* Proposed SG14 status_code
2 (C) 2018 - 2019 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
3 File Created: Feb 2018
4
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License in the accompanying file
9 Licence.txt or at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18
19
20 Distributed under the Boost Software License, Version 1.0.
21 (See accompanying file Licence.txt or copy at
22 http://www.boost.org/LICENSE_1_0.txt)
23 */
24
25 #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_STATUS_CODE_HPP
26 #define BOOST_OUTCOME_SYSTEM_ERROR2_STATUS_CODE_HPP
27
28 #include "status_code_domain.hpp"
29
30 #if(__cplusplus >= 201700 || _HAS_CXX17) && !defined(BOOST_OUTCOME_SYSTEM_ERROR2_DISABLE_STD_IN_PLACE)
31 // 0.26
32 #include <utility> // for in_place
33
34 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
35 using in_place_t = std::in_place_t;
36 using std::in_place;
37 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
38
39 #else
40
41 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
42 //! Aliases `std::in_place_t` if on C++ 17 or later, else defined locally.
43 struct in_place_t
44 {
45 explicit in_place_t() = default;
46 };
47 //! Aliases `std::in_place` if on C++ 17 or later, else defined locally.
48 constexpr in_place_t in_place{};
49 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
50 #endif
51
52 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
53
54 //! Namespace for user injected mixins
55 namespace mixins
56 {
57 template <class Base, class T> struct mixin : public Base
58 {
59 using Base::Base;
60 };
61 } // namespace mixins
62
63 /*! A tag for an erased value type for `status_code<D>`.
64 Available only if `ErasedType` satisfies `traits::is_move_bitcopying<ErasedType>::value`.
65 */
66 template <class ErasedType, //
67 typename std::enable_if<traits::is_move_bitcopying<ErasedType>::value, bool>::type = true>
68 struct erased
69 {
70 using value_type = ErasedType;
71 };
72
73 namespace detail
74 {
75 template <class T> struct is_status_code
76 {
77 static constexpr bool value = false;
78 };
79 template <class T> struct is_status_code<status_code<T>>
80 {
81 static constexpr bool value = true;
82 };
83 template <class T> struct is_erased_status_code
84 {
85 static constexpr bool value = false;
86 };
87 template <class T> struct is_erased_status_code<status_code<erased<T>>>
88 {
89 static constexpr bool value = true;
90 };
91
92 // From http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4436.pdf
93 namespace impl
94 {
95 template <typename... Ts> struct make_void
96 {
97 using type = void;
98 };
99 template <typename... Ts> using void_t = typename make_void<Ts...>::type;
100 template <class...> struct types
101 {
102 using type = types;
103 };
104 template <template <class...> class T, class types, class = void> struct test_apply
105 {
106 using type = void;
107 };
108 template <template <class...> class T, class... Ts> struct test_apply<T, types<Ts...>, void_t<T<Ts...>>>
109 {
110 using type = T<Ts...>;
111 };
112 } // namespace impl
113 template <template <class...> class T, class... Ts> using test_apply = impl::test_apply<T, impl::types<Ts...>>;
114
115 template <class T, class... Args> using get_make_status_code_result = decltype(make_status_code(std::declval<T>(), std::declval<Args>()...));
116 template <class... Args> using safe_get_make_status_code_result = test_apply<get_make_status_code_result, Args...>;
117 } // namespace detail
118
119 //! Trait returning true if the type is a status code.
120 template <class T> struct is_status_code
121 {
122 static constexpr bool value = detail::is_status_code<typename std::decay<T>::type>::value || detail::is_erased_status_code<typename std::decay<T>::type>::value;
123 };
124
125 /*! A type erased lightweight status code reflecting empty, success, or failure.
126 Differs from `status_code<erased<>>` by being always available irrespective of
127 the domain's value type, but cannot be copied, moved, nor destructed. Thus one
128 always passes this around by const lvalue reference.
129 */
130 template <> class BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI status_code<void>
131 {
132 template <class T> friend class status_code;
133
134 public:
135 //! The type of the domain.
136 using domain_type = void;
137 //! The type of the status code.
138 using value_type = void;
139 //! The type of a reference to a message string.
140 using string_ref = typename status_code_domain::string_ref;
141
142 protected:
143 const status_code_domain *_domain{nullptr};
144
145 protected:
146 //! No default construction at type erased level
147 status_code() = default;
148 //! No public copying at type erased level
149 status_code(const status_code &) = default;
150 //! No public moving at type erased level
151 status_code(status_code &&) = default;
152 //! No public assignment at type erased level
153 status_code &operator=(const status_code &) = default;
154 //! No public assignment at type erased level
155 status_code &operator=(status_code &&) = default;
156 //! No public destruction at type erased level
157 ~status_code() = default;
158
159 //! Used to construct a non-empty type erased status code
160 constexpr explicit status_code(const status_code_domain *v) noexcept
161 : _domain(v)
162 {
163 }
164
165 constexpr const status_code_domain *_domain_ptr() const noexcept { return _domain; }
166
167 public:
168 //! Return the status code domain.
169 constexpr const status_code_domain &domain() const noexcept { return *_domain; }
170 //! True if the status code is empty.
171 BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD constexpr bool empty() const noexcept { return _domain == nullptr; }
172
173 //! Return a reference to a string textually representing a code.
174 string_ref message() const noexcept { return (_domain != nullptr) ? _domain->_do_message(*this) : string_ref("(empty)"); }
175 //! True if code means success.
176 bool success() const noexcept { return (_domain != nullptr) ? !_domain->_do_failure(*this) : false; }
177 //! True if code means failure.
178 bool failure() const noexcept { return (_domain != nullptr) ? _domain->_do_failure(*this) : false; }
179 /*! True if code is strictly (and potentially non-transitively) semantically equivalent to another code in another domain.
180 Note that usually non-semantic i.e. pure value comparison is used when the other status code has the same domain.
181 As `equivalent()` will try mapping to generic code, this usually captures when two codes have the same semantic
182 meaning in `equivalent()`.
183 */
184 template <class T> bool strictly_equivalent(const status_code<T> &o) const noexcept
185 {
186 if(_domain && o._domain)
187 {
188 return _domain->_do_equivalent(*this, o);
189 }
190 // If we are both empty, we are equivalent
191 if(!_domain && !o._domain)
192 {
193 return true; // NOLINT
194 }
195 // Otherwise not equivalent
196 return false;
197 }
198 /*! True if code is equivalent, by any means, to another code in another domain (guaranteed transitive).
199 Firstly `strictly_equivalent()` is run in both directions. If neither succeeds, each domain is asked
200 for the equivalent generic code and those are compared.
201 */
202 template <class T> inline bool equivalent(const status_code<T> &o) const noexcept;
203 #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
204 //! Throw a code as a C++ exception.
205 BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN void throw_exception() const
206 {
207 _domain->_do_throw_exception(*this);
208 abort(); // suppress buggy GCC warning
209 }
210 #endif
211 };
212
213 namespace detail
214 {
215 template <class DomainType> struct get_domain_value_type
216 {
217 using domain_type = DomainType;
218 using value_type = typename domain_type::value_type;
219 };
220 template <class ErasedType> struct get_domain_value_type<erased<ErasedType>>
221 {
222 using domain_type = status_code_domain;
223 using value_type = ErasedType;
224 };
225 template <class DomainType> class BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI status_code_storage : public status_code<void>
226 {
227 using _base = status_code<void>;
228
229 public:
230 //! The type of the domain.
231 using domain_type = typename get_domain_value_type<DomainType>::domain_type;
232 //! The type of the status code.
233 using value_type = typename get_domain_value_type<DomainType>::value_type;
234 //! The type of a reference to a message string.
235 using string_ref = typename domain_type::string_ref;
236
237 #ifndef NDEBUG
238 static_assert(std::is_move_constructible<value_type>::value || std::is_copy_constructible<value_type>::value, "DomainType::value_type is neither move nor copy constructible!");
239 static_assert(!std::is_default_constructible<value_type>::value || std::is_nothrow_default_constructible<value_type>::value, "DomainType::value_type is not nothrow default constructible!");
240 static_assert(!std::is_move_constructible<value_type>::value || std::is_nothrow_move_constructible<value_type>::value, "DomainType::value_type is not nothrow move constructible!");
241 static_assert(std::is_nothrow_destructible<value_type>::value, "DomainType::value_type is not nothrow destructible!");
242 #endif
243
244 // Replace the type erased implementations with type aware implementations for better codegen
245 //! Return the status code domain.
246 constexpr const domain_type &domain() const noexcept { return *static_cast<const domain_type *>(this->_domain); }
247
248 //! Reset the code to empty.
249 BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 void clear() noexcept
250 {
251 this->_value.~value_type();
252 this->_domain = nullptr;
253 new(&this->_value) value_type();
254 }
255
256 #if __cplusplus >= 201400 || _MSC_VER >= 1910 /* VS2017 */
257 //! Return a reference to the `value_type`.
258 constexpr value_type &value() & noexcept { return this->_value; }
259 //! Return a reference to the `value_type`.
260 constexpr value_type &&value() && noexcept { return this->_value; }
261 #endif
262 //! Return a reference to the `value_type`.
263 constexpr const value_type &value() const &noexcept { return this->_value; }
264 //! Return a reference to the `value_type`.
265 constexpr const value_type &&value() const &&noexcept { return this->_value; }
266
267 protected:
268 status_code_storage() = default;
269 status_code_storage(const status_code_storage &) = default;
270 BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code_storage(status_code_storage &&o) noexcept
271 : _base(static_cast<status_code_storage &&>(o))
272 , _value(static_cast<status_code_storage &&>(o)._value)
273 {
274 o._domain = nullptr;
275 }
276 status_code_storage &operator=(const status_code_storage &) = default;
277 BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code_storage &operator=(status_code_storage &&o) noexcept
278 {
279 this->~status_code_storage();
280 new(this) status_code_storage(static_cast<status_code_storage &&>(o));
281 return *this;
282 }
283 ~status_code_storage() = default;
284
285 value_type _value{};
286 struct _value_type_constructor
287 {
288 };
289 template <class... Args>
290 constexpr status_code_storage(_value_type_constructor /*unused*/, const status_code_domain *v, Args &&... args)
291 : _base(v)
292 , _value(static_cast<Args &&>(args)...)
293 {
294 }
295 };
296 } // namespace detail
297
298 /*! A lightweight, typed, status code reflecting empty, success, or failure.
299 This is the main workhorse of the system_error2 library. Its characteristics reflect the value type
300 set by its domain type, so if that value type is move-only or trivial, so is this.
301
302 An ADL discovered helper function `make_status_code(T, Args...)` is looked up by one of the constructors.
303 If it is found, and it generates a status code compatible with this status code, implicit construction
304 is made available.
305
306 You may mix in custom member functions and member function overrides by injecting a specialisation of
307 `mixins::mixin<Base, YourDomainType>`. Your mixin must inherit from `Base`.
308 */
309 template <class DomainType> class BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI status_code : public mixins::mixin<detail::status_code_storage<DomainType>, DomainType>
310 {
311 template <class T> friend class status_code;
312 using _base = mixins::mixin<detail::status_code_storage<DomainType>, DomainType>;
313
314 public:
315 //! The type of the domain.
316 using domain_type = DomainType;
317 //! The type of the status code.
318 using value_type = typename domain_type::value_type;
319 //! The type of a reference to a message string.
320 using string_ref = typename domain_type::string_ref;
321
322 public:
323 //! Default construction to empty
324 status_code() = default;
325 //! Copy constructor
326 status_code(const status_code &) = default;
327 //! Move constructor
328 status_code(status_code &&) = default; // NOLINT
329 //! Copy assignment
330 status_code &operator=(const status_code &) = default;
331 //! Move assignment
332 status_code &operator=(status_code &&) = default; // NOLINT
333 ~status_code() = default;
334
335 //! Return a copy of the code.
336 BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code clone() const { return *this; }
337
338 /***** KEEP THESE IN SYNC WITH ERRORED_STATUS_CODE *****/
339 //! Implicit construction from any type where an ADL discovered `make_status_code(T, Args ...)` returns a `status_code`.
340 template <class T, class... Args, //
341 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
342 typename std::enable_if<!std::is_same<typename std::decay<T>::type, status_code>::value // not copy/move of self
343 && !std::is_same<typename std::decay<T>::type, in_place_t>::value // not in_place_t
344 && is_status_code<MakeStatusCodeResult>::value // ADL makes a status code
345 && std::is_constructible<status_code, MakeStatusCodeResult>::value, // ADLed status code is compatible
346
347 bool>::type = true>
348 constexpr status_code(T &&v, Args &&... args) noexcept(noexcept(make_status_code(std::declval<T>(), std::declval<Args>()...))) // NOLINT
349 : status_code(make_status_code(static_cast<T &&>(v), static_cast<Args &&>(args)...))
350 {
351 }
352 //! Explicit in-place construction.
353 template <class... Args>
354 constexpr explicit status_code(in_place_t /*unused */, Args &&... args) noexcept(std::is_nothrow_constructible<value_type, Args &&...>::value)
355 : _base(typename _base::_value_type_constructor{}, &domain_type::get(), static_cast<Args &&>(args)...)
356 {
357 }
358 //! Explicit in-place construction from initialiser list.
359 template <class T, class... Args>
360 constexpr explicit status_code(in_place_t /*unused */, std::initializer_list<T> il, Args &&... args) noexcept(std::is_nothrow_constructible<value_type, std::initializer_list<T>, Args &&...>::value)
361 : _base(typename _base::_value_type_constructor{}, &domain_type::get(), il, static_cast<Args &&>(args)...)
362 {
363 }
364 //! Explicit copy construction from a `value_type`.
365 constexpr explicit status_code(const value_type &v) noexcept(std::is_nothrow_copy_constructible<value_type>::value)
366 : _base(typename _base::_value_type_constructor{}, &domain_type::get(), v)
367 {
368 }
369 //! Explicit move construction from a `value_type`.
370 constexpr explicit status_code(value_type &&v) noexcept(std::is_nothrow_move_constructible<value_type>::value)
371 : _base(typename _base::_value_type_constructor{}, &domain_type::get(), static_cast<value_type &&>(v))
372 {
373 }
374 /*! Explicit construction from an erased status code. Available only if
375 `value_type` is trivially copyable or move bitcopying, and `sizeof(status_code) <= sizeof(status_code<erased<>>)`.
376 Does not check if domains are equal.
377 */
378 template <class ErasedType, //
379 typename std::enable_if<detail::type_erasure_is_safe<ErasedType, value_type>::value, bool>::type = true>
380 constexpr explicit status_code(const status_code<erased<ErasedType>> &v) noexcept(std::is_nothrow_copy_constructible<value_type>::value)
381 : status_code(detail::erasure_cast<value_type>(v.value()))
382 {
383 #if __cplusplus >= 201400
384 assert(v.domain() == this->domain());
385 #endif
386 }
387
388 //! Assignment from a `value_type`.
389 BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code &operator=(const value_type &v) noexcept(std::is_nothrow_copy_assignable<value_type>::value)
390 {
391 this->_value = v;
392 return *this;
393 }
394
395 //! Return a reference to a string textually representing a code.
396 string_ref message() const noexcept { return this->_domain ? string_ref(this->domain()._do_message(*this)) : string_ref("(empty)"); }
397 };
398
399 namespace traits
400 {
401 template <class DomainType> struct is_move_bitcopying<status_code<DomainType>>
402 {
403 static constexpr bool value = is_move_bitcopying<typename DomainType::value_type>::value;
404 };
405 } // namespace traits
406
407
408 /*! Type erased, move-only status_code, unlike `status_code<void>` which cannot be moved nor destroyed. Available
409 only if `erased<>` is available, which is when the domain's type is trivially
410 copyable or is move relocatable, and if the size of the domain's typed error code is less than or equal to
411 this erased error code. Copy construction is disabled, but if you want a copy call `.clone()`.
412
413 An ADL discovered helper function `make_status_code(T, Args...)` is looked up by one of the constructors.
414 If it is found, and it generates a status code compatible with this status code, implicit construction
415 is made available.
416 */
417 template <class ErasedType> class BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI status_code<erased<ErasedType>> : public mixins::mixin<detail::status_code_storage<erased<ErasedType>>, erased<ErasedType>>
418 {
419 template <class T> friend class status_code;
420 using _base = mixins::mixin<detail::status_code_storage<erased<ErasedType>>, erased<ErasedType>>;
421
422 public:
423 //! The type of the domain (void, as it is erased).
424 using domain_type = void;
425 //! The type of the erased status code.
426 using value_type = ErasedType;
427 //! The type of a reference to a message string.
428 using string_ref = typename _base::string_ref;
429
430 public:
431 //! Default construction to empty
432 status_code() = default;
433 //! Copy constructor
434 status_code(const status_code &) = delete;
435 //! Move constructor
436 status_code(status_code &&) = default; // NOLINT
437 //! Copy assignment
438 status_code &operator=(const status_code &) = delete;
439 //! Move assignment
440 status_code &operator=(status_code &&) = default; // NOLINT
441 ~status_code()
442 {
443 if(nullptr != this->_domain)
444 {
445 this->_domain->_do_erased_destroy(*this, sizeof(*this));
446 }
447 }
448
449 //! Return a copy of the erased code by asking the domain to perform the erased copy.
450 status_code clone() const
451 {
452 if(nullptr == this->_domain)
453 {
454 return {};
455 }
456 status_code x;
457 this->_domain->_do_erased_copy(x, *this, sizeof(*this));
458 return x;
459 }
460
461 /***** KEEP THESE IN SYNC WITH ERRORED_STATUS_CODE *****/
462 //! Implicit copy construction from any other status code if its value type is trivially copyable and it would fit into our storage
463 template <class DomainType, //
464 typename std::enable_if<!detail::is_erased_status_code<status_code<DomainType>>::value //
465 && std::is_trivially_copyable<typename DomainType::value_type>::value //
466 && detail::type_erasure_is_safe<value_type, typename DomainType::value_type>::value,
467 bool>::type = true>
468 constexpr status_code(const status_code<DomainType> &v) noexcept // NOLINT
469 : _base(typename _base::_value_type_constructor{}, v._domain_ptr(), detail::erasure_cast<value_type>(v.value()))
470 {
471 }
472 //! 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
473 template <class DomainType, //
474 typename std::enable_if<detail::type_erasure_is_safe<value_type, typename DomainType::value_type>::value, bool>::type = true>
475 BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code(status_code<DomainType> &&v) noexcept // NOLINT
476 : _base(typename _base::_value_type_constructor{}, v._domain_ptr(), detail::erasure_cast<value_type>(v.value()))
477 {
478 v._domain = nullptr;
479 }
480 //! Implicit construction from any type where an ADL discovered `make_status_code(T, Args ...)` returns a `status_code`.
481 template <class T, class... Args, //
482 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
483 typename std::enable_if<!std::is_same<typename std::decay<T>::type, status_code>::value // not copy/move of self
484 && !std::is_same<typename std::decay<T>::type, value_type>::value // not copy/move of value type
485 && is_status_code<MakeStatusCodeResult>::value // ADL makes a status code
486 && std::is_constructible<status_code, MakeStatusCodeResult>::value, // ADLed status code is compatible
487 bool>::type = true>
488 constexpr status_code(T &&v, Args &&... args) noexcept(noexcept(make_status_code(std::declval<T>(), std::declval<Args>()...))) // NOLINT
489 : status_code(make_status_code(static_cast<T &&>(v), static_cast<Args &&>(args)...))
490 {
491 }
492
493 /**** By rights ought to be removed in any formal standard ****/
494 //! Reset the code to empty.
495 BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 void clear() noexcept { *this = status_code(); }
496 //! Return the erased `value_type` by value.
497 constexpr value_type value() const noexcept { return this->_value; }
498 };
499
500 namespace traits
501 {
502 template <class ErasedType> struct is_move_bitcopying<status_code<erased<ErasedType>>>
503 {
504 static constexpr bool value = true;
505 };
506 } // namespace traits
507
508 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
509
510 #endif