Imported Upstream version 1.72.0
[platform/upstream/boost.git] / 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
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_relocating<ErasedType>::value`.
65 */
66 template <class ErasedType,  //
67           typename std::enable_if<traits::is_move_relocating<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 public:
166   //! Return the status code domain.
167   constexpr const status_code_domain &domain() const noexcept { return *_domain; }
168   //! True if the status code is empty.
169   BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD constexpr bool empty() const noexcept { return _domain == nullptr; }
170
171   //! Return a reference to a string textually representing a code.
172   string_ref message() const noexcept { return (_domain != nullptr) ? _domain->_do_message(*this) : string_ref("(empty)"); }
173   //! True if code means success.
174   bool success() const noexcept { return (_domain != nullptr) ? !_domain->_do_failure(*this) : false; }
175   //! True if code means failure.
176   bool failure() const noexcept { return (_domain != nullptr) ? _domain->_do_failure(*this) : false; }
177   /*! True if code is strictly (and potentially non-transitively) semantically equivalent to another code in another domain.
178   Note that usually non-semantic i.e. pure value comparison is used when the other status code has the same domain.
179   As `equivalent()` will try mapping to generic code, this usually captures when two codes have the same semantic
180   meaning in `equivalent()`.
181   */
182   template <class T> bool strictly_equivalent(const status_code<T> &o) const noexcept
183   {
184     if(_domain && o._domain)
185     {
186       return _domain->_do_equivalent(*this, o);
187     }
188     // If we are both empty, we are equivalent
189     if(!_domain && !o._domain)
190     {
191       return true;  // NOLINT
192     }
193     // Otherwise not equivalent
194     return false;
195   }
196   /*! True if code is equivalent, by any means, to another code in another domain (guaranteed transitive).
197   Firstly `strictly_equivalent()` is run in both directions. If neither succeeds, each domain is asked
198   for the equivalent generic code and those are compared.
199   */
200   template <class T> inline bool equivalent(const status_code<T> &o) const noexcept;
201 #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
202   //! Throw a code as a C++ exception.
203   BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN void throw_exception() const { _domain->_do_throw_exception(*this); }
204 #endif
205 };
206
207 namespace detail
208 {
209   template <class DomainType> struct get_domain_value_type
210   {
211     using domain_type = DomainType;
212     using value_type = typename domain_type::value_type;
213   };
214   template <class ErasedType> struct get_domain_value_type<erased<ErasedType>>
215   {
216     using domain_type = status_code_domain;
217     using value_type = ErasedType;
218   };
219   template <class DomainType> class BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI status_code_storage : public status_code<void>
220   {
221     using _base = status_code<void>;
222
223   public:
224     //! The type of the domain.
225     using domain_type = typename get_domain_value_type<DomainType>::domain_type;
226     //! The type of the status code.
227     using value_type = typename get_domain_value_type<DomainType>::value_type;
228     //! The type of a reference to a message string.
229     using string_ref = typename domain_type::string_ref;
230
231 #ifndef NDEBUG
232     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!");
233     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!");
234     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!");
235     static_assert(std::is_nothrow_destructible<value_type>::value, "DomainType::value_type is not nothrow destructible!");
236 #endif
237
238     // Replace the type erased implementations with type aware implementations for better codegen
239     //! Return the status code domain.
240     constexpr const domain_type &domain() const noexcept { return *static_cast<const domain_type *>(this->_domain); }
241
242     //! Reset the code to empty.
243     BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 void clear() noexcept
244     {
245       this->_value.~value_type();
246       this->_domain = nullptr;
247       new(&this->_value) value_type();
248     }
249
250 #if __cplusplus >= 201400 || _MSC_VER >= 1910 /* VS2017 */
251     //! Return a reference to the `value_type`.
252     constexpr value_type &value() & noexcept { return this->_value; }
253     //! Return a reference to the `value_type`.
254     constexpr value_type &&value() && noexcept { return this->_value; }
255 #endif
256     //! Return a reference to the `value_type`.
257     constexpr const value_type &value() const &noexcept { return this->_value; }
258     //! Return a reference to the `value_type`.
259     constexpr const value_type &&value() const &&noexcept { return this->_value; }
260
261   protected:
262     status_code_storage() = default;
263     status_code_storage(const status_code_storage &) = default;
264     BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code_storage(status_code_storage &&o) noexcept
265         : _base(static_cast<status_code_storage &&>(o))
266         , _value(static_cast<status_code_storage &&>(o)._value)
267     {
268       o._domain = nullptr;
269     }
270     status_code_storage &operator=(const status_code_storage &) = default;
271     BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code_storage &operator=(status_code_storage &&o) noexcept
272     {
273       this->~status_code_storage();
274       new(this) status_code_storage(static_cast<status_code_storage &&>(o));
275       return *this;
276     }
277     ~status_code_storage() = default;
278
279     value_type _value{};
280     struct _value_type_constructor
281     {
282     };
283     template <class... Args>
284     constexpr status_code_storage(_value_type_constructor /*unused*/, const status_code_domain *v, Args &&... args)
285         : _base(v)
286         , _value(static_cast<Args &&>(args)...)
287     {
288     }
289   };
290 }  // namespace detail
291
292 /*! A lightweight, typed, status code reflecting empty, success, or failure.
293 This is the main workhorse of the system_error2 library. Its characteristics reflect the value type
294 set by its domain type, so if that value type is move-only or trivial, so is this.
295
296 An ADL discovered helper function `make_status_code(T, Args...)` is looked up by one of the constructors.
297 If it is found, and it generates a status code compatible with this status code, implicit construction
298 is made available.
299
300 You may mix in custom member functions and member function overrides by injecting a specialisation of
301 `mixins::mixin<Base, YourDomainType>`. Your mixin must inherit from `Base`.
302 */
303 template <class DomainType> class BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI status_code : public mixins::mixin<detail::status_code_storage<DomainType>, DomainType>
304 {
305   template <class T> friend class status_code;
306   using _base = mixins::mixin<detail::status_code_storage<DomainType>, DomainType>;
307
308 public:
309   //! The type of the domain.
310   using domain_type = DomainType;
311   //! The type of the status code.
312   using value_type = typename domain_type::value_type;
313   //! The type of a reference to a message string.
314   using string_ref = typename domain_type::string_ref;
315
316 public:
317   //! Default construction to empty
318   status_code() = default;
319   //! Copy constructor
320   status_code(const status_code &) = default;
321   //! Move constructor
322   status_code(status_code &&) = default;  // NOLINT
323   //! Copy assignment
324   status_code &operator=(const status_code &) = default;
325   //! Move assignment
326   status_code &operator=(status_code &&) = default;  // NOLINT
327   ~status_code() = default;
328
329   //! Return a copy of the code.
330   BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code clone() const { return *this; }
331
332   /***** KEEP THESE IN SYNC WITH ERRORED_STATUS_CODE *****/
333   //! Implicit construction from any type where an ADL discovered `make_status_code(T, Args ...)` returns a `status_code`.
334   template <class T, class... Args,                                                                            //
335             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
336             typename std::enable_if<!std::is_same<typename std::decay<T>::type, status_code>::value            // not copy/move of self
337                                     && !std::is_same<typename std::decay<T>::type, in_place_t>::value          // not in_place_t
338                                     && is_status_code<MakeStatusCodeResult>::value                             // ADL makes a status code
339                                     && std::is_constructible<status_code, MakeStatusCodeResult>::value,        // ADLed status code is compatible
340
341                                     bool>::type = true>
342   constexpr status_code(T &&v, Args &&... args) noexcept(noexcept(make_status_code(std::declval<T>(), std::declval<Args>()...)))  // NOLINT
343       : status_code(make_status_code(static_cast<T &&>(v), static_cast<Args &&>(args)...))
344   {
345   }
346   //! Explicit in-place construction.
347   template <class... Args>
348   constexpr explicit status_code(in_place_t /*unused */, Args &&... args) noexcept(std::is_nothrow_constructible<value_type, Args &&...>::value)
349       : _base(typename _base::_value_type_constructor{}, &domain_type::get(), static_cast<Args &&>(args)...)
350   {
351   }
352   //! Explicit in-place construction from initialiser list.
353   template <class T, class... Args>
354   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)
355       : _base(typename _base::_value_type_constructor{}, &domain_type::get(), il, static_cast<Args &&>(args)...)
356   {
357   }
358   //! Explicit copy construction from a `value_type`.
359   constexpr explicit status_code(const value_type &v) noexcept(std::is_nothrow_copy_constructible<value_type>::value)
360       : _base(typename _base::_value_type_constructor{}, &domain_type::get(), v)
361   {
362   }
363   //! Explicit move construction from a `value_type`.
364   constexpr explicit status_code(value_type &&v) noexcept(std::is_nothrow_move_constructible<value_type>::value)
365       : _base(typename _base::_value_type_constructor{}, &domain_type::get(), static_cast<value_type &&>(v))
366   {
367   }
368   /*! Explicit construction from an erased status code. Available only if
369   `value_type` is trivially copyable or move relocating, and `sizeof(status_code) <= sizeof(status_code<erased<>>)`.
370   Does not check if domains are equal.
371   */
372   template <class ErasedType,  //
373             typename std::enable_if<detail::type_erasure_is_safe<ErasedType, value_type>::value, bool>::type = true>
374   constexpr explicit status_code(const status_code<erased<ErasedType>> &v) noexcept(std::is_nothrow_copy_constructible<value_type>::value)
375       : status_code(detail::erasure_cast<value_type>(v.value()))
376   {
377 #if __cplusplus >= 201400
378     assert(v.domain() == this->domain());
379 #endif
380   }
381
382   //! Assignment from a `value_type`.
383   BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code &operator=(const value_type &v) noexcept(std::is_nothrow_copy_assignable<value_type>::value)
384   {
385     this->_value = v;
386     return *this;
387   }
388
389   //! Return a reference to a string textually representing a code.
390   string_ref message() const noexcept { return this->_domain ? string_ref(this->domain()._do_message(*this)) : string_ref("(empty)"); }
391 };
392
393 namespace traits
394 {
395   template <class DomainType> struct is_move_relocating<status_code<DomainType>>
396   {
397     static constexpr bool value = is_move_relocating<typename DomainType::value_type>::value;
398   };
399 }  // namespace traits
400
401
402 /*! Type erased, move-only status_code, unlike `status_code<void>` which cannot be moved nor destroyed. Available
403 only if `erased<>` is available, which is when the domain's type is trivially
404 copyable or is move relocatable, and if the size of the domain's typed error code is less than or equal to
405 this erased error code. Copy construction is disabled, but if you want a copy call `.clone()`.
406
407 An ADL discovered helper function `make_status_code(T, Args...)` is looked up by one of the constructors.
408 If it is found, and it generates a status code compatible with this status code, implicit construction
409 is made available.
410 */
411 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>>
412 {
413   template <class T> friend class status_code;
414   using _base = mixins::mixin<detail::status_code_storage<erased<ErasedType>>, erased<ErasedType>>;
415
416 public:
417   //! The type of the domain (void, as it is erased).
418   using domain_type = void;
419   //! The type of the erased status code.
420   using value_type = ErasedType;
421   //! The type of a reference to a message string.
422   using string_ref = typename _base::string_ref;
423
424 public:
425   //! Default construction to empty
426   status_code() = default;
427   //! Copy constructor
428   status_code(const status_code &) = delete;
429   //! Move constructor
430   status_code(status_code &&) = default;  // NOLINT
431   //! Copy assignment
432   status_code &operator=(const status_code &) = delete;
433   //! Move assignment
434   status_code &operator=(status_code &&) = default;  // NOLINT
435   ~status_code()
436   {
437     if(nullptr != this->_domain)
438     {
439       this->_domain->_do_erased_destroy(*this, sizeof(*this));
440     }
441   }
442
443   //! Return a copy of the erased code by asking the domain to perform the erased copy.
444   status_code clone() const
445   {
446     if(nullptr == this->_domain)
447     {
448       return {};
449     }
450     status_code x;
451     this->_domain->_do_erased_copy(x, *this, sizeof(*this));
452     return x;
453   }
454
455   /***** KEEP THESE IN SYNC WITH ERRORED_STATUS_CODE *****/
456   //! Implicit copy construction from any other status code if its value type is trivially copyable and it would fit into our storage
457   template <class DomainType,                                                                              //
458             typename std::enable_if<!detail::is_erased_status_code<status_code<DomainType>>::value         //
459                                     && std::is_trivially_copyable<typename DomainType::value_type>::value  //
460                                     && detail::type_erasure_is_safe<value_type, typename DomainType::value_type>::value,
461                                     bool>::type = true>
462   constexpr status_code(const status_code<DomainType> &v) noexcept  // NOLINT
463       : _base(typename _base::_value_type_constructor{}, &v.domain(), detail::erasure_cast<value_type>(v.value()))
464   {
465   }
466   //! Implicit move construction from any other status code if its value type is trivially copyable or move relocating and it would fit into our storage
467   template <class DomainType,  //
468             typename std::enable_if<detail::type_erasure_is_safe<value_type, typename DomainType::value_type>::value, bool>::type = true>
469   BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code(status_code<DomainType> &&v) noexcept  // NOLINT
470       : _base(typename _base::_value_type_constructor{}, &v.domain(), detail::erasure_cast<value_type>(v.value()))
471   {
472     v._domain = nullptr;
473   }
474   //! Implicit construction from any type where an ADL discovered `make_status_code(T, Args ...)` returns a `status_code`.
475   template <class T, class... Args,                                                                            //
476             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
477             typename std::enable_if<!std::is_same<typename std::decay<T>::type, status_code>::value            // not copy/move of self
478                                     && !std::is_same<typename std::decay<T>::type, value_type>::value          // not copy/move of value type
479                                     && is_status_code<MakeStatusCodeResult>::value                             // ADL makes a status code
480                                     && std::is_constructible<status_code, MakeStatusCodeResult>::value,        // ADLed status code is compatible
481                                     bool>::type = true>
482   constexpr status_code(T &&v, Args &&... args) noexcept(noexcept(make_status_code(std::declval<T>(), std::declval<Args>()...)))  // NOLINT
483       : status_code(make_status_code(static_cast<T &&>(v), static_cast<Args &&>(args)...))
484   {
485   }
486
487   /**** By rights ought to be removed in any formal standard ****/
488   //! Reset the code to empty.
489   BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 void clear() noexcept { *this = status_code(); }
490   //! Return the erased `value_type` by value.
491   constexpr value_type value() const noexcept { return this->_value; }
492 };
493
494 namespace traits
495 {
496   template <class ErasedType> struct is_move_relocating<status_code<erased<ErasedType>>>
497   {
498     static constexpr bool value = true;
499   };
500 }  // namespace traits
501
502 BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
503
504 #endif