status_code.hpp 22 KB

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