status_code.hpp 25 KB

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