status_code.hpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /* Proposed SG14 status_code
  2. (C) 2018 - 2023 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. namespace detail
  49. {
  50. BOOST_OUTCOME_SYSTEM_ERROR2_TEMPLATE(class ErasedType) //
  51. BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES(BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(traits::is_move_bitcopying<ErasedType>::value))
  52. struct erased
  53. {
  54. using value_type = ErasedType;
  55. };
  56. } // namespace detail
  57. /*! The tag type used to specialise erased editions of `status_code<D>`.
  58. Available only if `ErasedType` satisfies `traits::is_move_bitcopying<ErasedType>::value`.
  59. */
  60. template <class ErasedType> //
  61. using status_code_erased_tag_type = detail::erased<ErasedType>;
  62. /*! Specialise this template to quickly wrap a third party enumeration into a
  63. custom status code domain.
  64. Use like this:
  65. ```c++
  66. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
  67. template <> struct quick_status_code_from_enum<AnotherCode> : quick_status_code_from_enum_defaults<AnotherCode>
  68. {
  69. // Text name of the enum
  70. static constexpr const auto domain_name = "Another Code";
  71. // Unique UUID for the enum. PLEASE use https://www.random.org/cgi-bin/randbyte?nbytes=16&format=h
  72. static constexpr const auto domain_uuid = "{be201f65-3962-dd0e-1266-a72e63776a42}";
  73. // Map of each enum value to its text string, and list of semantically equivalent errc's
  74. static const std::initializer_list<mapping> &value_mappings()
  75. {
  76. static const std::initializer_list<mapping<AnotherCode>> v = {
  77. // Format is: { enum value, "string representation", { list of errc mappings ... } }
  78. {AnotherCode::success1, "Success 1", {errc::success}}, //
  79. {AnotherCode::goaway, "Go away", {errc::permission_denied}}, //
  80. {AnotherCode::success2, "Success 2", {errc::success}}, //
  81. {AnotherCode::error2, "Error 2", {}}, //
  82. };
  83. return v;
  84. }
  85. // Completely optional definition of mixin for the status code synthesised from `Enum`. It can be omitted.
  86. template <class Base> struct mixin : Base
  87. {
  88. using Base::Base;
  89. constexpr int custom_method() const { return 42; }
  90. };
  91. };
  92. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
  93. ```
  94. Note that if the `errc` mapping contains `errc::success`, then
  95. the enumeration value is considered to be a successful value.
  96. Otherwise it is considered to be a failure value.
  97. The first value in the `errc` mapping is the one chosen as the
  98. `generic_code` conversion. Other values are used during equivalence
  99. comparisons.
  100. */
  101. template <class Enum> struct quick_status_code_from_enum;
  102. namespace detail
  103. {
  104. template <class T> struct is_status_code
  105. {
  106. static constexpr bool value = false;
  107. };
  108. template <class T> struct is_status_code<status_code<T>>
  109. {
  110. static constexpr bool value = true;
  111. };
  112. template <class T> struct is_erased_status_code
  113. {
  114. static constexpr bool value = false;
  115. };
  116. template <class T> struct is_erased_status_code<status_code<detail::erased<T>>>
  117. {
  118. static constexpr bool value = true;
  119. };
  120. #if !defined(__GNUC__) || defined(__clang__) || __GNUC__ >= 8
  121. // From http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4436.pdf
  122. namespace impl
  123. {
  124. template <typename... Ts> struct make_void
  125. {
  126. using type = void;
  127. };
  128. template <typename... Ts> using void_t = typename make_void<Ts...>::type;
  129. template <class...> struct types
  130. {
  131. using type = types;
  132. };
  133. template <template <class...> class T, class types, class = void> struct test_apply
  134. {
  135. using type = void;
  136. };
  137. template <template <class...> class T, class... Ts> struct test_apply<T, types<Ts...>, void_t<T<Ts...>>>
  138. {
  139. using type = T<Ts...>;
  140. };
  141. } // namespace impl
  142. template <template <class...> class T, class... Ts> using test_apply = impl::test_apply<T, impl::types<Ts...>>;
  143. template <class T, class... Args> using get_make_status_code_result = decltype(make_status_code(std::declval<T>(), std::declval<Args>()...));
  144. template <class... Args> using safe_get_make_status_code_result = test_apply<get_make_status_code_result, Args...>;
  145. #else
  146. // ICE avoidance form for GCCs before 8. Note this form doesn't prevent recursive make_status_code ADL instantation,
  147. // so in certain corner cases this will break. On the other hand, more useful than an ICE.
  148. namespace impl
  149. {
  150. template <typename... Ts> struct make_void
  151. {
  152. using type = void;
  153. };
  154. template <typename... Ts> using void_t = typename make_void<Ts...>::type;
  155. template <class...> struct types
  156. {
  157. using type = types;
  158. };
  159. template <typename Types, typename = void> struct make_status_code_rettype
  160. {
  161. using type = void;
  162. };
  163. template <typename... Args> using get_make_status_code_result = decltype(make_status_code(std::declval<Args>()...));
  164. template <typename... Args> struct make_status_code_rettype<types<Args...>, void_t<get_make_status_code_result<Args...>>>
  165. {
  166. using type = get_make_status_code_result<Args...>;
  167. };
  168. } // namespace impl
  169. template <class... Args> struct safe_get_make_status_code_result
  170. {
  171. using type = typename impl::make_status_code_rettype<impl::types<Args...>>::type;
  172. };
  173. #endif
  174. } // namespace detail
  175. //! Trait returning true if the type is a status code.
  176. template <class T> struct is_status_code
  177. {
  178. static constexpr bool value =
  179. detail::is_status_code<typename std::decay<T>::type>::value || detail::is_erased_status_code<typename std::decay<T>::type>::value;
  180. };
  181. /*! A type erased lightweight status code reflecting empty, success, or failure.
  182. Differs from `status_code<erased<>>` by being always available irrespective of
  183. the domain's value type, but cannot be copied, moved, nor destructed. Thus one
  184. always passes this around by const lvalue reference.
  185. */
  186. template <> class BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI status_code<void>
  187. {
  188. template <class T> friend class status_code;
  189. public:
  190. //! The type of the domain.
  191. using domain_type = void;
  192. //! The type of the status code.
  193. using value_type = void;
  194. //! The type of a reference to a message string.
  195. using string_ref = typename status_code_domain::string_ref;
  196. protected:
  197. const status_code_domain *_domain{nullptr};
  198. protected:
  199. //! No default construction at type erased level
  200. status_code() = default;
  201. //! No public copying at type erased level
  202. status_code(const status_code &) = default;
  203. //! No public moving at type erased level
  204. status_code(status_code &&) = default;
  205. //! No public assignment at type erased level
  206. status_code &operator=(const status_code &) = default;
  207. //! No public assignment at type erased level
  208. status_code &operator=(status_code &&) = default;
  209. //! No public destruction at type erased level
  210. ~status_code() = default;
  211. //! Used to construct a non-empty type erased status code
  212. constexpr explicit status_code(const status_code_domain *v) noexcept
  213. : _domain(v)
  214. {
  215. }
  216. // Used to work around triggering a ubsan failure. Do NOT remove!
  217. constexpr const status_code_domain *_domain_ptr() const noexcept { return _domain; }
  218. public:
  219. //! Return the status code domain.
  220. constexpr const status_code_domain &domain() const noexcept { return *_domain; }
  221. //! True if the status code is empty.
  222. BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD constexpr bool empty() const noexcept { return _domain == nullptr; }
  223. //! Return a reference to a string textually representing a code.
  224. BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR20 string_ref message() const noexcept
  225. {
  226. // Avoid MSVC's buggy ternary operator for expensive to destruct things
  227. if(_domain != nullptr)
  228. {
  229. return _domain->_do_message(*this);
  230. }
  231. return string_ref("(empty)");
  232. }
  233. //! True if code means success.
  234. BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR20 bool success() const noexcept { return (_domain != nullptr) ? !_domain->_do_failure(*this) : false; }
  235. //! True if code means failure.
  236. BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR20 bool failure() const noexcept { return (_domain != nullptr) ? _domain->_do_failure(*this) : false; }
  237. /*! True if code is strictly (and potentially non-transitively) semantically equivalent to another code in another domain.
  238. Note that usually non-semantic i.e. pure value comparison is used when the other status code has the same domain.
  239. As `equivalent()` will try mapping to generic code, this usually captures when two codes have the same semantic
  240. meaning in `equivalent()`.
  241. */
  242. template <class T> BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 bool strictly_equivalent(const status_code<T> &o) const noexcept
  243. {
  244. if(_domain && o._domain)
  245. {
  246. return _domain->_do_equivalent(*this, o);
  247. }
  248. // If we are both empty, we are equivalent
  249. if(!_domain && !o._domain)
  250. {
  251. return true; // NOLINT
  252. }
  253. // Otherwise not equivalent
  254. return false;
  255. }
  256. /*! True if code is equivalent, by any means, to another code in another domain (guaranteed transitive).
  257. Firstly `strictly_equivalent()` is run in both directions. If neither succeeds, each domain is asked
  258. for the equivalent generic code and those are compared.
  259. */
  260. template <class T> BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 inline bool equivalent(const status_code<T> &o) const noexcept;
  261. #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
  262. //! Throw a code as a C++ exception.
  263. BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN void throw_exception() const
  264. {
  265. _domain->_do_throw_exception(*this);
  266. abort(); // suppress buggy GCC warning
  267. }
  268. #endif
  269. };
  270. namespace detail
  271. {
  272. template <class DomainType> struct get_domain_value_type
  273. {
  274. using domain_type = DomainType;
  275. using value_type = typename domain_type::value_type;
  276. };
  277. template <class ErasedType> struct get_domain_value_type<erased<ErasedType>>
  278. {
  279. using domain_type = status_code_domain;
  280. using value_type = ErasedType;
  281. };
  282. template <class DomainType> class BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI status_code_storage : public status_code<void>
  283. {
  284. static_assert(!std::is_void<DomainType>::value, "status_code_storage<void> should never occur!");
  285. using _base = status_code<void>;
  286. public:
  287. //! The type of the domain.
  288. using domain_type = typename get_domain_value_type<DomainType>::domain_type;
  289. //! The type of the status code.
  290. using value_type = typename get_domain_value_type<DomainType>::value_type;
  291. //! The type of a reference to a message string.
  292. using string_ref = typename domain_type::string_ref;
  293. #ifndef NDEBUG
  294. static_assert(std::is_move_constructible<value_type>::value || std::is_copy_constructible<value_type>::value,
  295. "DomainType::value_type is neither move nor copy constructible!");
  296. static_assert(!std::is_default_constructible<value_type>::value || std::is_nothrow_default_constructible<value_type>::value,
  297. "DomainType::value_type is not nothrow default constructible!");
  298. static_assert(!std::is_move_constructible<value_type>::value || std::is_nothrow_move_constructible<value_type>::value,
  299. "DomainType::value_type is not nothrow move constructible!");
  300. static_assert(std::is_nothrow_destructible<value_type>::value, "DomainType::value_type is not nothrow destructible!");
  301. #endif
  302. // Replace the type erased implementations with type aware implementations for better codegen
  303. //! Return the status code domain.
  304. constexpr const domain_type &domain() const noexcept { return *static_cast<const domain_type *>(this->_domain); }
  305. //! Reset the code to empty.
  306. BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 void clear() noexcept
  307. {
  308. this->_value.~value_type();
  309. this->_domain = nullptr;
  310. new(std::addressof(this->_value)) value_type();
  311. }
  312. #if __cplusplus >= 201400 || _MSC_VER >= 1910 /* VS2017 */
  313. //! Return a reference to the `value_type`.
  314. constexpr value_type &value() & noexcept { return this->_value; }
  315. //! Return a reference to the `value_type`.
  316. constexpr value_type &&value() && noexcept { return static_cast<value_type &&>(this->_value); }
  317. #endif
  318. //! Return a reference to the `value_type`.
  319. constexpr const value_type &value() const & noexcept { return this->_value; }
  320. //! Return a reference to the `value_type`.
  321. constexpr const value_type &&value() const && noexcept { return static_cast<const value_type &&>(this->_value); }
  322. protected:
  323. status_code_storage() = default;
  324. status_code_storage(const status_code_storage &) = default;
  325. BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code_storage(status_code_storage &&o) noexcept
  326. : _base(static_cast<status_code_storage &&>(o))
  327. , _value(static_cast<status_code_storage &&>(o)._value)
  328. {
  329. o._domain = nullptr;
  330. }
  331. status_code_storage &operator=(const status_code_storage &) = default;
  332. BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code_storage &operator=(status_code_storage &&o) noexcept
  333. {
  334. this->~status_code_storage();
  335. new(this) status_code_storage(static_cast<status_code_storage &&>(o));
  336. return *this;
  337. }
  338. ~status_code_storage() = default;
  339. value_type _value{};
  340. struct _value_type_constructor
  341. {
  342. };
  343. template <class... Args>
  344. constexpr status_code_storage(_value_type_constructor /*unused*/, const status_code_domain *v, Args &&...args)
  345. : _base(v)
  346. , _value(static_cast<Args &&>(args)...)
  347. {
  348. }
  349. };
  350. template <class DomainType> struct has_stateful_mixin
  351. {
  352. static constexpr bool value = (sizeof(status_code_storage<DomainType>) != sizeof(mixins::mixin<status_code_storage<DomainType>, DomainType>));
  353. };
  354. template <class ToDomain, class FromDomain> struct domain_value_type_erasure_is_safe
  355. {
  356. using to_value_type = typename get_domain_value_type<ToDomain>::value_type;
  357. using from_value_type = typename get_domain_value_type<FromDomain>::value_type;
  358. static constexpr bool value = traits::is_move_bitcopying<to_value_type>::value //
  359. && traits::is_move_bitcopying<from_value_type>::value //
  360. && sizeof(status_code_storage<FromDomain>) <= sizeof(status_code_storage<ToDomain>) //
  361. && !has_stateful_mixin<FromDomain>::value;
  362. };
  363. template <class ToDomain> struct domain_value_type_erasure_is_safe<ToDomain, void>
  364. {
  365. static constexpr bool value = false;
  366. };
  367. } // namespace detail
  368. namespace traits
  369. {
  370. //! Determines whether the mixin contained in `StatusCode` contains non-static member variables.
  371. template <class StatusCode> using has_stateful_mixin = detail::has_stateful_mixin<typename detail::remove_cvref<StatusCode>::type::value_type>;
  372. //! Determines whether the status code `From` can be type erased into the status code `To`.
  373. template <class To, class From>
  374. using is_type_erasable_to =
  375. detail::domain_value_type_erasure_is_safe<typename detail::remove_cvref<To>::type::domain_type, typename detail::remove_cvref<From>::type::domain_type>;
  376. } // namespace traits
  377. /*! A lightweight, typed, status code reflecting empty, success, or failure.
  378. This is the main workhorse of the system_error2 library. Its characteristics reflect the value type
  379. set by its domain type, so if that value type is move-only or trivial, so is this.
  380. An ADL discovered helper function `make_status_code(T, Args...)` is looked up by one of the constructors.
  381. If it is found, and it generates a status code compatible with this status code, implicit construction
  382. is made available.
  383. You may mix in custom member functions and member function overrides by injecting a specialisation of
  384. `mixins::mixin<Base, YourDomainType>`. Your mixin must inherit from `Base`. Your mixin can carry state,
  385. but if it does, it will no longer be possible to construct erased status codes from such unerased status
  386. codes.
  387. */
  388. template <class DomainType> class BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI status_code : public mixins::mixin<detail::status_code_storage<DomainType>, DomainType>
  389. {
  390. template <class T> friend class status_code;
  391. using _base = mixins::mixin<detail::status_code_storage<DomainType>, DomainType>;
  392. public:
  393. //! The type of the domain.
  394. using domain_type = DomainType;
  395. //! The type of the status code.
  396. using value_type = typename domain_type::value_type;
  397. //! The type of a reference to a message string.
  398. using string_ref = typename domain_type::string_ref;
  399. protected:
  400. using _base::_base;
  401. public:
  402. //! Default construction to empty
  403. status_code() = default;
  404. //! Copy constructor
  405. status_code(const status_code &) = default;
  406. //! Move constructor
  407. status_code(status_code &&) = default; // NOLINT
  408. //! Copy assignment
  409. status_code &operator=(const status_code &) = default;
  410. //! Move assignment
  411. status_code &operator=(status_code &&) = default; // NOLINT
  412. ~status_code() = default;
  413. //! Return a copy of the code.
  414. BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code clone() const { return *this; }
  415. /***** KEEP THESE IN SYNC WITH ERRORED_STATUS_CODE *****/
  416. //! Implicit construction from any type where an ADL discovered `make_status_code(T, Args ...)` returns a `status_code`.
  417. BOOST_OUTCOME_SYSTEM_ERROR2_TEMPLATE(
  418. class T, class... Args, //
  419. class MakeStatusCodeResult =
  420. typename detail::safe_get_make_status_code_result<T, Args...>::type) // Safe ADL lookup of make_status_code(), returns void if not found
  421. BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES(BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(!std::is_same<typename std::decay<T>::type, status_code>::value // not copy/move of self
  422. && !std::is_same<typename std::decay<T>::type, in_place_t>::value // not in_place_t
  423. && is_status_code<MakeStatusCodeResult>::value // ADL makes a status code
  424. && std::is_constructible<status_code, MakeStatusCodeResult>::value)) // ADLed status code is compatible
  425. constexpr status_code(T &&v, Args &&...args) noexcept(noexcept(make_status_code(std::declval<T>(), std::declval<Args>()...))) // NOLINT
  426. : status_code(make_status_code(static_cast<T &&>(v), static_cast<Args &&>(args)...))
  427. {
  428. }
  429. //! Implicit construction from any `quick_status_code_from_enum<Enum>` enumerated type.
  430. BOOST_OUTCOME_SYSTEM_ERROR2_TEMPLATE(class Enum, //
  431. class QuickStatusCodeType = typename quick_status_code_from_enum<Enum>::code_type) // Enumeration has been activated
  432. BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES(BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(std::is_constructible<status_code, QuickStatusCodeType>::value)) // Its status code is compatible
  433. constexpr status_code(Enum &&v) noexcept(std::is_nothrow_constructible<status_code, QuickStatusCodeType>::value) // NOLINT
  434. : status_code(QuickStatusCodeType(static_cast<Enum &&>(v)))
  435. {
  436. }
  437. //! Explicit in-place construction. Disables if `domain_type::get()` is not a valid expression.
  438. template <class... Args>
  439. constexpr explicit status_code(in_place_t /*unused */, Args &&...args) noexcept(std::is_nothrow_constructible<value_type, Args &&...>::value)
  440. : _base(typename _base::_value_type_constructor{}, &domain_type::get(), static_cast<Args &&>(args)...)
  441. {
  442. }
  443. //! Explicit in-place construction from initialiser list. Disables if `domain_type::get()` is not a valid expression.
  444. template <class T, class... Args>
  445. constexpr explicit status_code(in_place_t /*unused */, std::initializer_list<T> il,
  446. Args &&...args) noexcept(std::is_nothrow_constructible<value_type, std::initializer_list<T>, Args &&...>::value)
  447. : _base(typename _base::_value_type_constructor{}, &domain_type::get(), il, static_cast<Args &&>(args)...)
  448. {
  449. }
  450. //! Explicit copy construction from a `value_type`. Disables if `domain_type::get()` is not a valid expression.
  451. constexpr explicit status_code(const value_type &v) noexcept(std::is_nothrow_copy_constructible<value_type>::value)
  452. : _base(typename _base::_value_type_constructor{}, &domain_type::get(), v)
  453. {
  454. }
  455. //! Explicit move construction from a `value_type`. Disables if `domain_type::get()` is not a valid expression.
  456. constexpr explicit status_code(value_type &&v) noexcept(std::is_nothrow_move_constructible<value_type>::value)
  457. : _base(typename _base::_value_type_constructor{}, &domain_type::get(), static_cast<value_type &&>(v))
  458. {
  459. }
  460. /*! Explicit construction from an erased status code. Available only if
  461. `value_type` is trivially copyable or move bitcopying, and `sizeof(status_code) <= sizeof(status_code<erased<>>)`.
  462. Does not check if domains are equal.
  463. */
  464. BOOST_OUTCOME_SYSTEM_ERROR2_TEMPLATE(class ErasedType) //
  465. BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES(BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(detail::domain_value_type_erasure_is_safe<domain_type, detail::erased<ErasedType>>::value))
  466. constexpr explicit status_code(const status_code<detail::erased<ErasedType>> &v) noexcept(std::is_nothrow_copy_constructible<value_type>::value)
  467. : status_code(detail::erasure_cast<value_type>(v.value()))
  468. {
  469. #if __cplusplus >= 201400
  470. assert(v.domain() == this->domain());
  471. #endif
  472. }
  473. //! Return a reference to a string textually representing a code.
  474. BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR20 string_ref message() const noexcept
  475. {
  476. // Avoid MSVC's buggy ternary operator for expensive to destruct things
  477. if(this->_domain != nullptr)
  478. {
  479. return string_ref(this->domain()._do_message(*this));
  480. }
  481. return string_ref("(empty)");
  482. }
  483. };
  484. namespace traits
  485. {
  486. template <class DomainType> struct is_move_bitcopying<status_code<DomainType>>
  487. {
  488. static constexpr bool value = is_move_bitcopying<typename DomainType::value_type>::value;
  489. };
  490. } // namespace traits
  491. /*! Type erased, move-only status_code, unlike `status_code<void>` which cannot be moved nor destroyed. Available
  492. only if `erased<>` is available, which is when the domain's type is trivially
  493. copyable or is move relocatable, and if the size of the domain's typed error code is less than or equal to
  494. this erased error code. Copy construction is disabled, but if you want a copy call `.clone()`.
  495. An ADL discovered helper function `make_status_code(T, Args...)` is looked up by one of the constructors.
  496. If it is found, and it generates a status code compatible with this status code, implicit construction
  497. is made available.
  498. */
  499. template <class ErasedType>
  500. class BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI status_code<detail::erased<ErasedType>>
  501. : public mixins::mixin<detail::status_code_storage<detail::erased<ErasedType>>, detail::erased<ErasedType>>
  502. {
  503. template <class T> friend class status_code;
  504. using _base = mixins::mixin<detail::status_code_storage<detail::erased<ErasedType>>, detail::erased<ErasedType>>;
  505. public:
  506. //! The type of the domain (void, as it is erased).
  507. using domain_type = void;
  508. //! The type of the erased status code.
  509. using value_type = ErasedType;
  510. //! The type of a reference to a message string.
  511. using string_ref = typename _base::string_ref;
  512. public:
  513. //! Default construction to empty
  514. status_code() = default;
  515. //! Copy constructor
  516. status_code(const status_code &) = delete;
  517. //! Move constructor
  518. status_code(status_code &&) = default; // NOLINT
  519. //! Copy assignment
  520. status_code &operator=(const status_code &) = delete;
  521. //! Move assignment
  522. status_code &operator=(status_code &&) = default; // NOLINT
  523. BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR20 ~status_code()
  524. {
  525. if(nullptr != this->_domain)
  526. {
  527. status_code_domain::payload_info_t info{sizeof(value_type), sizeof(status_code), alignof(status_code)};
  528. this->_domain->_do_erased_destroy(*this, info);
  529. }
  530. }
  531. //! Return a copy of the erased code by asking the domain to perform the erased copy.
  532. BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR20 status_code clone() const
  533. {
  534. if(nullptr == this->_domain)
  535. {
  536. return {};
  537. }
  538. status_code x;
  539. if(!this->_domain->_do_erased_copy(x, *this, this->_domain->payload_info()))
  540. {
  541. abort(); // should not be possible
  542. }
  543. return x;
  544. }
  545. /***** KEEP THESE IN SYNC WITH ERRORED_STATUS_CODE *****/
  546. //! Implicit copy construction from any other status code if its value type is trivially copyable, it would fit into our storage, and it is not an erased
  547. //! status code.
  548. BOOST_OUTCOME_SYSTEM_ERROR2_TEMPLATE(class DomainType) //
  549. BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES(BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(detail::domain_value_type_erasure_is_safe<detail::erased<ErasedType>, DomainType>::value),
  550. BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(!detail::is_erased_status_code<status_code<typename std::decay<DomainType>::type>>::value))
  551. constexpr status_code(const status_code<DomainType> &v) noexcept // NOLINT
  552. : _base(typename _base::_value_type_constructor{}, v._domain_ptr(), detail::erasure_cast<value_type>(v.value()))
  553. {
  554. }
  555. //! 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
  556. BOOST_OUTCOME_SYSTEM_ERROR2_TEMPLATE(class DomainType) //
  557. BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES(BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(detail::domain_value_type_erasure_is_safe<detail::erased<ErasedType>, DomainType>::value))
  558. BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code(status_code<DomainType> &&v) noexcept // NOLINT
  559. : _base(typename _base::_value_type_constructor{}, v._domain_ptr(), detail::erasure_cast<value_type>(v.value()))
  560. {
  561. alignas(alignof(typename DomainType::value_type)) char buffer[sizeof(typename DomainType::value_type)];
  562. new(buffer) typename DomainType::value_type(static_cast<status_code<DomainType> &&>(v).value());
  563. // deliberately do not destruct value moved into buffer
  564. (void) buffer;
  565. v._domain = nullptr;
  566. }
  567. //! Implicit construction from any type where an ADL discovered `make_status_code(T, Args ...)` returns a `status_code`.
  568. BOOST_OUTCOME_SYSTEM_ERROR2_TEMPLATE(
  569. class T, class... Args, //
  570. class MakeStatusCodeResult =
  571. typename detail::safe_get_make_status_code_result<T, Args...>::type) // Safe ADL lookup of make_status_code(), returns void if not found
  572. BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES(BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(!std::is_same<typename std::decay<T>::type, status_code>::value // not copy/move of self
  573. && !std::is_same<typename std::decay<T>::type, value_type>::value // not copy/move of value type
  574. && is_status_code<MakeStatusCodeResult>::value // ADL makes a status code
  575. && std::is_constructible<status_code, MakeStatusCodeResult>::value)) // ADLed status code is compatible
  576. constexpr status_code(T &&v, Args &&...args) noexcept(noexcept(make_status_code(std::declval<T>(), std::declval<Args>()...))) // NOLINT
  577. : status_code(make_status_code(static_cast<T &&>(v), static_cast<Args &&>(args)...))
  578. {
  579. }
  580. //! Implicit construction from any `quick_status_code_from_enum<Enum>` enumerated type.
  581. BOOST_OUTCOME_SYSTEM_ERROR2_TEMPLATE(class Enum, //
  582. class QuickStatusCodeType = typename quick_status_code_from_enum<Enum>::code_type) // Enumeration has been activated
  583. BOOST_OUTCOME_SYSTEM_ERROR2_TREQUIRES(BOOST_OUTCOME_SYSTEM_ERROR2_TPRED(std::is_constructible<status_code, QuickStatusCodeType>::value)) // Its status code is compatible
  584. constexpr status_code(Enum &&v) noexcept(std::is_nothrow_constructible<status_code, QuickStatusCodeType>::value) // NOLINT
  585. : status_code(QuickStatusCodeType(static_cast<Enum &&>(v)))
  586. {
  587. }
  588. #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
  589. //! Explicit copy construction from an unknown status code. Note that this will throw an exception if its value type is not trivially copyable or would not
  590. //! fit into our storage or the source domain's `_do_erased_copy()` refused the copy.
  591. explicit BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code(in_place_t, const status_code<void> &v) // NOLINT
  592. : _base(typename _base::_value_type_constructor{}, v._domain_ptr(), value_type{})
  593. {
  594. status_code_domain::payload_info_t info{sizeof(value_type), sizeof(status_code), alignof(status_code)};
  595. if(this->_domain->_do_erased_copy(*this, v, info))
  596. {
  597. return;
  598. }
  599. struct _ final : public std::exception
  600. {
  601. virtual const char *what() const noexcept override { return "status_code: source domain's erased copy function returned failure or refusal"; }
  602. };
  603. throw _{};
  604. }
  605. #endif
  606. //! Tagged copy construction from an unknown status code. Note that this will be empty if its value type is not trivially copyable or would not fit into our
  607. //! storage or the source domain's `_do_erased_copy()` refused the copy.
  608. BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR20 status_code(std::nothrow_t, const status_code<void> &v) noexcept // NOLINT
  609. : _base(typename _base::_value_type_constructor{}, v._domain_ptr(), value_type{})
  610. {
  611. #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
  612. try
  613. #endif
  614. {
  615. status_code_domain::payload_info_t info{sizeof(value_type), sizeof(status_code), alignof(status_code)};
  616. if(this->_domain->_do_erased_copy(*this, v, info))
  617. {
  618. return;
  619. }
  620. this->_domain = nullptr;
  621. }
  622. #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
  623. catch(...)
  624. {
  625. this->_domain = nullptr;
  626. }
  627. #endif
  628. }
  629. /**** By rights ought to be removed in any formal standard ****/
  630. //! Reset the code to empty.
  631. BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 void clear() noexcept { *this = status_code(); }
  632. //! Return the erased `value_type` by value.
  633. constexpr value_type value() const noexcept { return this->_value; }
  634. };
  635. /*! An erased type specialisation of `status_code<D>`.
  636. Available only if `ErasedType` satisfies `traits::is_move_bitcopying<ErasedType>::value`.
  637. */
  638. template <class ErasedType> using erased_status_code = status_code<detail::erased<ErasedType>>;
  639. namespace traits
  640. {
  641. template <class ErasedType> struct is_move_bitcopying<status_code<detail::erased<ErasedType>>>
  642. {
  643. static constexpr bool value = true;
  644. };
  645. } // namespace traits
  646. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
  647. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_DISABLE_INLINE_GDB_PRETTY_PRINTERS
  648. #if defined(__ELF__)
  649. __asm__(
  650. ".pushsection \".debug_gdb_scripts\", \"MS\",@progbits,1\n"
  651. ".byte 4 /* Python Text */\n"
  652. ".ascii \"gdb.inlined-script\\n\"\n"
  653. ".ascii \"import gdb.printing\\n\"\n"
  654. ".ascii \"import gdb\\n\"\n"
  655. ".ascii \"import os\\n\"\n"
  656. ".ascii \"def synthesise_gdb_value_from_string(s):\\n\"\n"
  657. ".ascii \" '''For when you want to return a synthetic string from children()'''\\n\"\n"
  658. ".ascii \" return gdb.Value(s + '\\0').cast(gdb.lookup_type('char').pointer())\\n\"\n"
  659. ".ascii \"class StatusCodePrinter(object):\\n\"\n"
  660. ".ascii \" '''Print a system_error2::status_code<T>'''\\n\"\n"
  661. ".ascii \" def __init__(self, val):\\n\"\n"
  662. ".ascii \" self.val = val\\n\"\n"
  663. ".ascii \" def children(self):\\n\"\n"
  664. ".ascii \" s = str(self.val['_domain'])\\n\"\n"
  665. ".ascii \" if 'posix_code_domain' in s or 'generic_code_domain' in s:\\n\"\n"
  666. ".ascii \" yield ('msg', synthesise_gdb_value_from_string(str(self.val['_value']) + ' (' + os.strerror(int(self.val['_value'])) + ')'))\\n\"\n"
  667. ".ascii \" yield ('domain', self.val['_domain'])\\n\"\n"
  668. ".ascii \" yield ('value', self.val['_value'])\\n\"\n"
  669. ".ascii \" def display_hint(self):\\n\"\n"
  670. ".ascii \" return None\\n\"\n"
  671. ".ascii \" def to_string(self):\\n\"\n"
  672. ".ascii \" s = str(self.val['_domain'])\\n\"\n"
  673. ".ascii \" if 'posix_code_domain' in s or 'generic_code_domain' in s:\\n\"\n"
  674. ".ascii \" return str(self.val['_value']) + ' (' + os.strerror(int(self.val['_value'])) + ')'\\n\"\n"
  675. ".ascii \" else:\\n\"\n"
  676. ".ascii \" return self.val['_value']\\n\"\n"
  677. ".ascii \"def build_pretty_printer():\\n\"\n"
  678. ".ascii \" pp = gdb.printing.RegexpCollectionPrettyPrinter('system_error2')\\n\"\n"
  679. ".ascii \" pp.add_printer('system_error2::status_code', '^(boost::)?system_error2::status_code<.*>$', StatusCodePrinter)\\n\"\n"
  680. ".ascii \" return pp\\n\"\n"
  681. ".ascii \"def register_printers(obj = None):\\n\"\n"
  682. ".ascii \" gdb.printing.register_pretty_printer(obj, build_pretty_printer(), replace = True)\\n\"\n"
  683. ".ascii \"register_printers(gdb.current_objfile())\\n\"\n"
  684. ".byte 0\n"
  685. ".popsection\n");
  686. #endif
  687. #endif
  688. #endif