status_code.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /* Proposed SG14 status_code
  2. (C) 2018 - 2019 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. namespace detail
  58. {
  59. template <class T> struct is_status_code
  60. {
  61. static constexpr bool value = false;
  62. };
  63. template <class T> struct is_status_code<status_code<T>>
  64. {
  65. static constexpr bool value = true;
  66. };
  67. template <class T> struct is_erased_status_code
  68. {
  69. static constexpr bool value = false;
  70. };
  71. template <class T> struct is_erased_status_code<status_code<erased<T>>>
  72. {
  73. static constexpr bool value = true;
  74. };
  75. // From http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4436.pdf
  76. namespace impl
  77. {
  78. template <typename... Ts> struct make_void
  79. {
  80. using type = void;
  81. };
  82. template <typename... Ts> using void_t = typename make_void<Ts...>::type;
  83. template <class...> struct types
  84. {
  85. using type = types;
  86. };
  87. template <template <class...> class T, class types, class = void> struct test_apply
  88. {
  89. using type = void;
  90. };
  91. template <template <class...> class T, class... Ts> struct test_apply<T, types<Ts...>, void_t<T<Ts...>>>
  92. {
  93. using type = T<Ts...>;
  94. };
  95. } // namespace impl
  96. template <template <class...> class T, class... Ts> using test_apply = impl::test_apply<T, impl::types<Ts...>>;
  97. template <class T, class... Args> using get_make_status_code_result = decltype(make_status_code(std::declval<T>(), std::declval<Args>()...));
  98. template <class... Args> using safe_get_make_status_code_result = test_apply<get_make_status_code_result, Args...>;
  99. } // namespace detail
  100. //! Trait returning true if the type is a status code.
  101. template <class T> struct is_status_code
  102. {
  103. 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;
  104. };
  105. /*! A type erased lightweight status code reflecting empty, success, or failure.
  106. Differs from `status_code<erased<>>` by being always available irrespective of
  107. the domain's value type, but cannot be copied, moved, nor destructed. Thus one
  108. always passes this around by const lvalue reference.
  109. */
  110. template <> class BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI status_code<void>
  111. {
  112. template <class T> friend class status_code;
  113. public:
  114. //! The type of the domain.
  115. using domain_type = void;
  116. //! The type of the status code.
  117. using value_type = void;
  118. //! The type of a reference to a message string.
  119. using string_ref = typename status_code_domain::string_ref;
  120. protected:
  121. const status_code_domain *_domain{nullptr};
  122. protected:
  123. //! No default construction at type erased level
  124. status_code() = default;
  125. //! No public copying at type erased level
  126. status_code(const status_code &) = default;
  127. //! No public moving at type erased level
  128. status_code(status_code &&) = default;
  129. //! No public assignment at type erased level
  130. status_code &operator=(const status_code &) = default;
  131. //! No public assignment at type erased level
  132. status_code &operator=(status_code &&) = default;
  133. //! No public destruction at type erased level
  134. ~status_code() = default;
  135. //! Used to construct a non-empty type erased status code
  136. constexpr explicit status_code(const status_code_domain *v) noexcept
  137. : _domain(v)
  138. {
  139. }
  140. constexpr const status_code_domain *_domain_ptr() const noexcept { return _domain; }
  141. public:
  142. //! Return the status code domain.
  143. constexpr const status_code_domain &domain() const noexcept { return *_domain; }
  144. //! True if the status code is empty.
  145. BOOST_OUTCOME_SYSTEM_ERROR2_NODISCARD constexpr bool empty() const noexcept { return _domain == nullptr; }
  146. //! Return a reference to a string textually representing a code.
  147. string_ref message() const noexcept { return (_domain != nullptr) ? _domain->_do_message(*this) : string_ref("(empty)"); }
  148. //! True if code means success.
  149. bool success() const noexcept { return (_domain != nullptr) ? !_domain->_do_failure(*this) : false; }
  150. //! True if code means failure.
  151. bool failure() const noexcept { return (_domain != nullptr) ? _domain->_do_failure(*this) : false; }
  152. /*! True if code is strictly (and potentially non-transitively) semantically equivalent to another code in another domain.
  153. Note that usually non-semantic i.e. pure value comparison is used when the other status code has the same domain.
  154. As `equivalent()` will try mapping to generic code, this usually captures when two codes have the same semantic
  155. meaning in `equivalent()`.
  156. */
  157. template <class T> bool strictly_equivalent(const status_code<T> &o) const noexcept
  158. {
  159. if(_domain && o._domain)
  160. {
  161. return _domain->_do_equivalent(*this, o);
  162. }
  163. // If we are both empty, we are equivalent
  164. if(!_domain && !o._domain)
  165. {
  166. return true; // NOLINT
  167. }
  168. // Otherwise not equivalent
  169. return false;
  170. }
  171. /*! True if code is equivalent, by any means, to another code in another domain (guaranteed transitive).
  172. Firstly `strictly_equivalent()` is run in both directions. If neither succeeds, each domain is asked
  173. for the equivalent generic code and those are compared.
  174. */
  175. template <class T> inline bool equivalent(const status_code<T> &o) const noexcept;
  176. #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
  177. //! Throw a code as a C++ exception.
  178. BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN void throw_exception() const
  179. {
  180. _domain->_do_throw_exception(*this);
  181. abort(); // suppress buggy GCC warning
  182. }
  183. #endif
  184. };
  185. namespace detail
  186. {
  187. template <class DomainType> struct get_domain_value_type
  188. {
  189. using domain_type = DomainType;
  190. using value_type = typename domain_type::value_type;
  191. };
  192. template <class ErasedType> struct get_domain_value_type<erased<ErasedType>>
  193. {
  194. using domain_type = status_code_domain;
  195. using value_type = ErasedType;
  196. };
  197. template <class DomainType> class BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI status_code_storage : public status_code<void>
  198. {
  199. using _base = status_code<void>;
  200. public:
  201. //! The type of the domain.
  202. using domain_type = typename get_domain_value_type<DomainType>::domain_type;
  203. //! The type of the status code.
  204. using value_type = typename get_domain_value_type<DomainType>::value_type;
  205. //! The type of a reference to a message string.
  206. using string_ref = typename domain_type::string_ref;
  207. #ifndef NDEBUG
  208. 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!");
  209. 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!");
  210. 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!");
  211. static_assert(std::is_nothrow_destructible<value_type>::value, "DomainType::value_type is not nothrow destructible!");
  212. #endif
  213. // Replace the type erased implementations with type aware implementations for better codegen
  214. //! Return the status code domain.
  215. constexpr const domain_type &domain() const noexcept { return *static_cast<const domain_type *>(this->_domain); }
  216. //! Reset the code to empty.
  217. BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 void clear() noexcept
  218. {
  219. this->_value.~value_type();
  220. this->_domain = nullptr;
  221. new(&this->_value) value_type();
  222. }
  223. #if __cplusplus >= 201400 || _MSC_VER >= 1910 /* VS2017 */
  224. //! Return a reference to the `value_type`.
  225. constexpr value_type &value() & noexcept { return this->_value; }
  226. //! Return a reference to the `value_type`.
  227. constexpr value_type &&value() && noexcept { return this->_value; }
  228. #endif
  229. //! Return a reference to the `value_type`.
  230. constexpr const value_type &value() const &noexcept { return this->_value; }
  231. //! Return a reference to the `value_type`.
  232. constexpr const value_type &&value() const &&noexcept { return this->_value; }
  233. protected:
  234. status_code_storage() = default;
  235. status_code_storage(const status_code_storage &) = default;
  236. BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code_storage(status_code_storage &&o) noexcept
  237. : _base(static_cast<status_code_storage &&>(o))
  238. , _value(static_cast<status_code_storage &&>(o)._value)
  239. {
  240. o._domain = nullptr;
  241. }
  242. status_code_storage &operator=(const status_code_storage &) = default;
  243. BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code_storage &operator=(status_code_storage &&o) noexcept
  244. {
  245. this->~status_code_storage();
  246. new(this) status_code_storage(static_cast<status_code_storage &&>(o));
  247. return *this;
  248. }
  249. ~status_code_storage() = default;
  250. value_type _value{};
  251. struct _value_type_constructor
  252. {
  253. };
  254. template <class... Args>
  255. constexpr status_code_storage(_value_type_constructor /*unused*/, const status_code_domain *v, Args &&... args)
  256. : _base(v)
  257. , _value(static_cast<Args &&>(args)...)
  258. {
  259. }
  260. };
  261. } // namespace detail
  262. /*! A lightweight, typed, status code reflecting empty, success, or failure.
  263. This is the main workhorse of the system_error2 library. Its characteristics reflect the value type
  264. set by its domain type, so if that value type is move-only or trivial, so is this.
  265. An ADL discovered helper function `make_status_code(T, Args...)` is looked up by one of the constructors.
  266. If it is found, and it generates a status code compatible with this status code, implicit construction
  267. is made available.
  268. You may mix in custom member functions and member function overrides by injecting a specialisation of
  269. `mixins::mixin<Base, YourDomainType>`. Your mixin must inherit from `Base`.
  270. */
  271. template <class DomainType> class BOOST_OUTCOME_SYSTEM_ERROR2_TRIVIAL_ABI status_code : public mixins::mixin<detail::status_code_storage<DomainType>, DomainType>
  272. {
  273. template <class T> friend class status_code;
  274. using _base = mixins::mixin<detail::status_code_storage<DomainType>, DomainType>;
  275. public:
  276. //! The type of the domain.
  277. using domain_type = DomainType;
  278. //! The type of the status code.
  279. using value_type = typename domain_type::value_type;
  280. //! The type of a reference to a message string.
  281. using string_ref = typename domain_type::string_ref;
  282. public:
  283. //! Default construction to empty
  284. status_code() = default;
  285. //! Copy constructor
  286. status_code(const status_code &) = default;
  287. //! Move constructor
  288. status_code(status_code &&) = default; // NOLINT
  289. //! Copy assignment
  290. status_code &operator=(const status_code &) = default;
  291. //! Move assignment
  292. status_code &operator=(status_code &&) = default; // NOLINT
  293. ~status_code() = default;
  294. //! Return a copy of the code.
  295. BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code clone() const { return *this; }
  296. /***** KEEP THESE IN SYNC WITH ERRORED_STATUS_CODE *****/
  297. //! Implicit construction from any type where an ADL discovered `make_status_code(T, Args ...)` returns a `status_code`.
  298. template <class T, class... Args, //
  299. 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
  300. typename std::enable_if<!std::is_same<typename std::decay<T>::type, status_code>::value // not copy/move of self
  301. && !std::is_same<typename std::decay<T>::type, in_place_t>::value // not in_place_t
  302. && is_status_code<MakeStatusCodeResult>::value // ADL makes a status code
  303. && std::is_constructible<status_code, MakeStatusCodeResult>::value, // ADLed status code is compatible
  304. bool>::type = true>
  305. constexpr status_code(T &&v, Args &&... args) noexcept(noexcept(make_status_code(std::declval<T>(), std::declval<Args>()...))) // NOLINT
  306. : status_code(make_status_code(static_cast<T &&>(v), static_cast<Args &&>(args)...))
  307. {
  308. }
  309. //! Explicit in-place construction.
  310. template <class... Args>
  311. constexpr explicit status_code(in_place_t /*unused */, Args &&... args) noexcept(std::is_nothrow_constructible<value_type, Args &&...>::value)
  312. : _base(typename _base::_value_type_constructor{}, &domain_type::get(), static_cast<Args &&>(args)...)
  313. {
  314. }
  315. //! Explicit in-place construction from initialiser list.
  316. template <class T, class... Args>
  317. 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)
  318. : _base(typename _base::_value_type_constructor{}, &domain_type::get(), il, static_cast<Args &&>(args)...)
  319. {
  320. }
  321. //! Explicit copy construction from a `value_type`.
  322. constexpr explicit status_code(const value_type &v) noexcept(std::is_nothrow_copy_constructible<value_type>::value)
  323. : _base(typename _base::_value_type_constructor{}, &domain_type::get(), v)
  324. {
  325. }
  326. //! Explicit move construction from a `value_type`.
  327. constexpr explicit status_code(value_type &&v) noexcept(std::is_nothrow_move_constructible<value_type>::value)
  328. : _base(typename _base::_value_type_constructor{}, &domain_type::get(), static_cast<value_type &&>(v))
  329. {
  330. }
  331. /*! Explicit construction from an erased status code. Available only if
  332. `value_type` is trivially copyable or move bitcopying, and `sizeof(status_code) <= sizeof(status_code<erased<>>)`.
  333. Does not check if domains are equal.
  334. */
  335. template <class ErasedType, //
  336. typename std::enable_if<detail::type_erasure_is_safe<ErasedType, value_type>::value, bool>::type = true>
  337. constexpr explicit status_code(const status_code<erased<ErasedType>> &v) noexcept(std::is_nothrow_copy_constructible<value_type>::value)
  338. : status_code(detail::erasure_cast<value_type>(v.value()))
  339. {
  340. #if __cplusplus >= 201400
  341. assert(v.domain() == this->domain());
  342. #endif
  343. }
  344. //! Assignment from a `value_type`.
  345. BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code &operator=(const value_type &v) noexcept(std::is_nothrow_copy_assignable<value_type>::value)
  346. {
  347. this->_value = v;
  348. return *this;
  349. }
  350. //! Return a reference to a string textually representing a code.
  351. string_ref message() const noexcept { return this->_domain ? string_ref(this->domain()._do_message(*this)) : string_ref("(empty)"); }
  352. };
  353. namespace traits
  354. {
  355. template <class DomainType> struct is_move_bitcopying<status_code<DomainType>>
  356. {
  357. static constexpr bool value = is_move_bitcopying<typename DomainType::value_type>::value;
  358. };
  359. } // namespace traits
  360. /*! Type erased, move-only status_code, unlike `status_code<void>` which cannot be moved nor destroyed. Available
  361. only if `erased<>` is available, which is when the domain's type is trivially
  362. copyable or is move relocatable, and if the size of the domain's typed error code is less than or equal to
  363. this erased error code. Copy construction is disabled, but if you want a copy call `.clone()`.
  364. An ADL discovered helper function `make_status_code(T, Args...)` is looked up by one of the constructors.
  365. If it is found, and it generates a status code compatible with this status code, implicit construction
  366. is made available.
  367. */
  368. 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>>
  369. {
  370. template <class T> friend class status_code;
  371. using _base = mixins::mixin<detail::status_code_storage<erased<ErasedType>>, erased<ErasedType>>;
  372. public:
  373. //! The type of the domain (void, as it is erased).
  374. using domain_type = void;
  375. //! The type of the erased status code.
  376. using value_type = ErasedType;
  377. //! The type of a reference to a message string.
  378. using string_ref = typename _base::string_ref;
  379. public:
  380. //! Default construction to empty
  381. status_code() = default;
  382. //! Copy constructor
  383. status_code(const status_code &) = delete;
  384. //! Move constructor
  385. status_code(status_code &&) = default; // NOLINT
  386. //! Copy assignment
  387. status_code &operator=(const status_code &) = delete;
  388. //! Move assignment
  389. status_code &operator=(status_code &&) = default; // NOLINT
  390. ~status_code()
  391. {
  392. if(nullptr != this->_domain)
  393. {
  394. this->_domain->_do_erased_destroy(*this, sizeof(*this));
  395. }
  396. }
  397. //! Return a copy of the erased code by asking the domain to perform the erased copy.
  398. status_code clone() const
  399. {
  400. if(nullptr == this->_domain)
  401. {
  402. return {};
  403. }
  404. status_code x;
  405. this->_domain->_do_erased_copy(x, *this, sizeof(*this));
  406. return x;
  407. }
  408. /***** KEEP THESE IN SYNC WITH ERRORED_STATUS_CODE *****/
  409. //! Implicit copy construction from any other status code if its value type is trivially copyable and it would fit into our storage
  410. template <class DomainType, //
  411. typename std::enable_if<!detail::is_erased_status_code<status_code<DomainType>>::value //
  412. && std::is_trivially_copyable<typename DomainType::value_type>::value //
  413. && detail::type_erasure_is_safe<value_type, typename DomainType::value_type>::value,
  414. bool>::type = true>
  415. constexpr status_code(const status_code<DomainType> &v) noexcept // NOLINT
  416. : _base(typename _base::_value_type_constructor{}, v._domain_ptr(), detail::erasure_cast<value_type>(v.value()))
  417. {
  418. }
  419. //! 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
  420. template <class DomainType, //
  421. typename std::enable_if<detail::type_erasure_is_safe<value_type, typename DomainType::value_type>::value, bool>::type = true>
  422. BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 status_code(status_code<DomainType> &&v) noexcept // NOLINT
  423. : _base(typename _base::_value_type_constructor{}, v._domain_ptr(), detail::erasure_cast<value_type>(v.value()))
  424. {
  425. v._domain = nullptr;
  426. }
  427. //! Implicit construction from any type where an ADL discovered `make_status_code(T, Args ...)` returns a `status_code`.
  428. template <class T, class... Args, //
  429. 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
  430. typename std::enable_if<!std::is_same<typename std::decay<T>::type, status_code>::value // not copy/move of self
  431. && !std::is_same<typename std::decay<T>::type, value_type>::value // not copy/move of value type
  432. && is_status_code<MakeStatusCodeResult>::value // ADL makes a status code
  433. && std::is_constructible<status_code, MakeStatusCodeResult>::value, // ADLed status code is compatible
  434. bool>::type = true>
  435. constexpr status_code(T &&v, Args &&... args) noexcept(noexcept(make_status_code(std::declval<T>(), std::declval<Args>()...))) // NOLINT
  436. : status_code(make_status_code(static_cast<T &&>(v), static_cast<Args &&>(args)...))
  437. {
  438. }
  439. /**** By rights ought to be removed in any formal standard ****/
  440. //! Reset the code to empty.
  441. BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 void clear() noexcept { *this = status_code(); }
  442. //! Return the erased `value_type` by value.
  443. constexpr value_type value() const noexcept { return this->_value; }
  444. };
  445. namespace traits
  446. {
  447. template <class ErasedType> struct is_move_bitcopying<status_code<erased<ErasedType>>>
  448. {
  449. static constexpr bool value = true;
  450. };
  451. } // namespace traits
  452. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
  453. #endif