errored_status_code.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /* Proposed SG14 status_code
  2. (C) 2018-2019 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
  3. File Created: Jun 2018
  4. Boost Software License - Version 1.0 - August 17th, 2003
  5. Permission is hereby granted, free of charge, to any person or organization
  6. obtaining a copy of the software and accompanying documentation covered by
  7. this license (the "Software") to use, reproduce, display, distribute,
  8. execute, and transmit the Software, and to prepare derivative works of the
  9. Software, and to permit third-parties to whom the Software is furnished to
  10. do so, all subject to the following:
  11. The copyright notices in the Software and this entire statement, including
  12. the above license grant, this restriction and the following disclaimer,
  13. must be included in all copies of the Software, in whole or in part, and
  14. all derivative works of the Software, unless such copies or derivative
  15. works are solely in the form of machine-executable object code generated by
  16. a source language processor.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  20. SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  21. FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  22. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_ERRORED_STATUS_CODE_HPP
  26. #define BOOST_OUTCOME_SYSTEM_ERROR2_ERRORED_STATUS_CODE_HPP
  27. #include "generic_code.hpp"
  28. #include "status_code_ptr.hpp"
  29. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
  30. /*! A `status_code` which is always a failure. The closest equivalent to
  31. `std::error_code`, except it cannot be modified, and is templated.
  32. Differences from `status_code`:
  33. - Never successful (this contract is checked on construction, if fails then it
  34. terminates the process).
  35. - Is immutable.
  36. */
  37. template <class DomainType> class errored_status_code : public status_code<DomainType>
  38. {
  39. using _base = status_code<DomainType>;
  40. using _base::clear;
  41. using _base::success;
  42. void _check()
  43. {
  44. if(_base::success())
  45. {
  46. std::terminate();
  47. }
  48. }
  49. public:
  50. //! The type of the erased error code.
  51. using typename _base::value_type;
  52. //! The type of a reference to a message string.
  53. using typename _base::string_ref;
  54. //! Default constructor.
  55. errored_status_code() = default;
  56. //! Copy constructor.
  57. errored_status_code(const errored_status_code &) = default;
  58. //! Move constructor.
  59. errored_status_code(errored_status_code &&) = default; // NOLINT
  60. //! Copy assignment.
  61. errored_status_code &operator=(const errored_status_code &) = default;
  62. //! Move assignment.
  63. errored_status_code &operator=(errored_status_code &&) = default; // NOLINT
  64. ~errored_status_code() = default;
  65. //! Explicitly construct from any similarly erased status code
  66. explicit errored_status_code(const _base &o) noexcept(std::is_nothrow_copy_constructible<_base>::value)
  67. : _base(o)
  68. {
  69. _check();
  70. }
  71. //! Explicitly construct from any similarly erased status code
  72. explicit errored_status_code(_base &&o) noexcept(std::is_nothrow_move_constructible<_base>::value)
  73. : _base(static_cast<_base &&>(o))
  74. {
  75. _check();
  76. }
  77. /***** KEEP THESE IN SYNC WITH STATUS_CODE *****/
  78. //! Implicit construction from any type where an ADL discovered `make_status_code(T, Args ...)` returns a `status_code`.
  79. template <class T, class... Args, //
  80. 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
  81. typename std::enable_if<!std::is_same<typename std::decay<T>::type, errored_status_code>::value // not copy/move of self
  82. && !std::is_same<typename std::decay<T>::type, in_place_t>::value // not in_place_t
  83. && is_status_code<MakeStatusCodeResult>::value // ADL makes a status code
  84. && std::is_constructible<errored_status_code, MakeStatusCodeResult>::value, // ADLed status code is compatible
  85. bool>::type = true>
  86. errored_status_code(T &&v, Args &&... args) noexcept(noexcept(make_status_code(std::declval<T>(), std::declval<Args>()...))) // NOLINT
  87. : errored_status_code(make_status_code(static_cast<T &&>(v), static_cast<Args &&>(args)...))
  88. {
  89. _check();
  90. }
  91. //! Explicit in-place construction.
  92. template <class... Args>
  93. explicit errored_status_code(in_place_t _, Args &&... args) noexcept(std::is_nothrow_constructible<value_type, Args &&...>::value)
  94. : _base(_, static_cast<Args &&>(args)...)
  95. {
  96. _check();
  97. }
  98. //! Explicit in-place construction from initialiser list.
  99. template <class T, class... Args>
  100. explicit errored_status_code(in_place_t _, std::initializer_list<T> il, Args &&... args) noexcept(std::is_nothrow_constructible<value_type, std::initializer_list<T>, Args &&...>::value)
  101. : _base(_, il, static_cast<Args &&>(args)...)
  102. {
  103. _check();
  104. }
  105. //! Explicit copy construction from a `value_type`.
  106. explicit errored_status_code(const value_type &v) noexcept(std::is_nothrow_copy_constructible<value_type>::value)
  107. : _base(v)
  108. {
  109. _check();
  110. }
  111. //! Explicit move construction from a `value_type`.
  112. explicit errored_status_code(value_type &&v) noexcept(std::is_nothrow_move_constructible<value_type>::value)
  113. : _base(static_cast<value_type &&>(v))
  114. {
  115. _check();
  116. }
  117. /*! Explicit construction from an erased status code. Available only if
  118. `value_type` is trivially destructible and `sizeof(status_code) <= sizeof(status_code<erased<>>)`.
  119. Does not check if domains are equal.
  120. */
  121. template <class ErasedType, //
  122. typename std::enable_if<detail::type_erasure_is_safe<ErasedType, value_type>::value, bool>::type = true>
  123. explicit errored_status_code(const status_code<erased<ErasedType>> &v) noexcept(std::is_nothrow_copy_constructible<value_type>::value)
  124. : errored_status_code(detail::erasure_cast<value_type>(v.value())) // NOLINT
  125. {
  126. assert(v.domain() == this->domain()); // NOLINT
  127. _check();
  128. }
  129. //! Return a const reference to the `value_type`.
  130. constexpr const value_type &value() const &noexcept { return this->_value; }
  131. };
  132. namespace traits
  133. {
  134. template <class DomainType> struct is_move_relocating<errored_status_code<DomainType>>
  135. {
  136. static constexpr bool value = is_move_relocating<typename DomainType::value_type>::value;
  137. };
  138. } // namespace traits
  139. template <class ErasedType> class errored_status_code<erased<ErasedType>> : public status_code<erased<ErasedType>>
  140. {
  141. using _base = status_code<erased<ErasedType>>;
  142. using _base::success;
  143. void _check()
  144. {
  145. if(_base::success())
  146. {
  147. std::terminate();
  148. }
  149. }
  150. public:
  151. using value_type = typename _base::value_type;
  152. using string_ref = typename _base::string_ref;
  153. //! Default construction to empty
  154. errored_status_code() = default;
  155. //! Copy constructor
  156. errored_status_code(const errored_status_code &) = default;
  157. //! Move constructor
  158. errored_status_code(errored_status_code &&) = default; // NOLINT
  159. //! Copy assignment
  160. errored_status_code &operator=(const errored_status_code &) = default;
  161. //! Move assignment
  162. errored_status_code &operator=(errored_status_code &&) = default; // NOLINT
  163. ~errored_status_code() = default;
  164. //! Explicitly construct from any similarly erased status code
  165. explicit errored_status_code(const _base &o) noexcept(std::is_nothrow_copy_constructible<_base>::value)
  166. : _base(o)
  167. {
  168. _check();
  169. }
  170. //! Explicitly construct from any similarly erased status code
  171. explicit errored_status_code(_base &&o) noexcept(std::is_nothrow_move_constructible<_base>::value)
  172. : _base(static_cast<_base &&>(o))
  173. {
  174. _check();
  175. }
  176. /***** KEEP THESE IN SYNC WITH STATUS_CODE *****/
  177. //! Implicit copy construction from any other status code if its value type is trivially copyable and it would fit into our storage
  178. template <class DomainType, //
  179. typename std::enable_if<!detail::is_erased_status_code<status_code<DomainType>>::value //
  180. && std::is_trivially_copyable<typename DomainType::value_type>::value //
  181. && detail::type_erasure_is_safe<value_type, typename DomainType::value_type>::value,
  182. bool>::type = true>
  183. errored_status_code(const status_code<DomainType> &v) noexcept : _base(v) // NOLINT
  184. {
  185. _check();
  186. }
  187. //! 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
  188. template <class DomainType, //
  189. typename std::enable_if<detail::type_erasure_is_safe<value_type, typename DomainType::value_type>::value,
  190. bool>::type = true>
  191. errored_status_code(status_code<DomainType> &&v) noexcept : _base(static_cast<status_code<DomainType> &&>(v)) // NOLINT
  192. {
  193. _check();
  194. }
  195. //! Implicit construction from any type where an ADL discovered `make_status_code(T, Args ...)` returns a `status_code`.
  196. template <class T, class... Args, //
  197. 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
  198. typename std::enable_if<!std::is_same<typename std::decay<T>::type, errored_status_code>::value // not copy/move of self
  199. && !std::is_same<typename std::decay<T>::type, value_type>::value // not copy/move of value type
  200. && is_status_code<MakeStatusCodeResult>::value // ADL makes a status code
  201. && std::is_constructible<errored_status_code, MakeStatusCodeResult>::value, // ADLed status code is compatible
  202. bool>::type = true>
  203. errored_status_code(T &&v, Args &&... args) noexcept(noexcept(make_status_code(std::declval<T>(), std::declval<Args>()...))) // NOLINT
  204. : errored_status_code(make_status_code(static_cast<T &&>(v), static_cast<Args &&>(args)...))
  205. {
  206. _check();
  207. }
  208. //! Return the erased `value_type` by value.
  209. constexpr value_type value() const noexcept { return this->_value; }
  210. };
  211. namespace traits
  212. {
  213. template <class ErasedType> struct is_move_relocating<errored_status_code<erased<ErasedType>>>
  214. {
  215. static constexpr bool value = true;
  216. };
  217. } // namespace traits
  218. //! True if the status code's are semantically equal via `equivalent()`.
  219. template <class DomainType1, class DomainType2> inline bool operator==(const errored_status_code<DomainType1> &a, const errored_status_code<DomainType2> &b) noexcept
  220. {
  221. return a.equivalent(static_cast<const status_code<DomainType2> &>(b));
  222. }
  223. //! True if the status code's are semantically equal via `equivalent()`.
  224. template <class DomainType1, class DomainType2> inline bool operator==(const status_code<DomainType1> &a, const errored_status_code<DomainType2> &b) noexcept
  225. {
  226. return a.equivalent(static_cast<const status_code<DomainType2> &>(b));
  227. }
  228. //! True if the status code's are semantically equal via `equivalent()`.
  229. template <class DomainType1, class DomainType2> inline bool operator==(const errored_status_code<DomainType1> &a, const status_code<DomainType2> &b) noexcept
  230. {
  231. return static_cast<const status_code<DomainType1> &>(a).equivalent(b);
  232. }
  233. //! True if the status code's are not semantically equal via `equivalent()`.
  234. template <class DomainType1, class DomainType2> inline bool operator!=(const errored_status_code<DomainType1> &a, const errored_status_code<DomainType2> &b) noexcept
  235. {
  236. return !a.equivalent(static_cast<const status_code<DomainType2> &>(b));
  237. }
  238. //! True if the status code's are not semantically equal via `equivalent()`.
  239. template <class DomainType1, class DomainType2> inline bool operator!=(const status_code<DomainType1> &a, const errored_status_code<DomainType2> &b) noexcept
  240. {
  241. return !a.equivalent(static_cast<const status_code<DomainType2> &>(b));
  242. }
  243. //! True if the status code's are not semantically equal via `equivalent()`.
  244. template <class DomainType1, class DomainType2> inline bool operator!=(const errored_status_code<DomainType1> &a, const status_code<DomainType2> &b) noexcept
  245. {
  246. return !static_cast<const status_code<DomainType1> &>(a).equivalent(b);
  247. }
  248. //! True if the status code's are semantically equal via `equivalent()` to `make_status_code(T)`.
  249. template <class DomainType1, class T, //
  250. class MakeStatusCodeResult = typename detail::safe_get_make_status_code_result<const T &>::type, // Safe ADL lookup of make_status_code(), returns void if not found
  251. typename std::enable_if<is_status_code<MakeStatusCodeResult>::value, bool>::type = true> // ADL makes a status code
  252. inline bool
  253. operator==(const errored_status_code<DomainType1> &a, const T &b)
  254. {
  255. return a.equivalent(make_status_code(b));
  256. }
  257. //! True if the status code's are semantically equal via `equivalent()` to `make_status_code(T)`.
  258. template <class T, class DomainType1, //
  259. class MakeStatusCodeResult = typename detail::safe_get_make_status_code_result<const T &>::type, // Safe ADL lookup of make_status_code(), returns void if not found
  260. typename std::enable_if<is_status_code<MakeStatusCodeResult>::value, bool>::type = true> // ADL makes a status code
  261. inline bool
  262. operator==(const T &a, const errored_status_code<DomainType1> &b)
  263. {
  264. return b.equivalent(make_status_code(a));
  265. }
  266. //! True if the status code's are not semantically equal via `equivalent()` to `make_status_code(T)`.
  267. template <class DomainType1, class T, //
  268. class MakeStatusCodeResult = typename detail::safe_get_make_status_code_result<const T &>::type, // Safe ADL lookup of make_status_code(), returns void if not found
  269. typename std::enable_if<is_status_code<MakeStatusCodeResult>::value, bool>::type = true> // ADL makes a status code
  270. inline bool
  271. operator!=(const errored_status_code<DomainType1> &a, const T &b)
  272. {
  273. return !a.equivalent(make_status_code(b));
  274. }
  275. //! True if the status code's are semantically equal via `equivalent()` to `make_status_code(T)`.
  276. template <class T, class DomainType1, //
  277. class MakeStatusCodeResult = typename detail::safe_get_make_status_code_result<const T &>::type, // Safe ADL lookup of make_status_code(), returns void if not found
  278. typename std::enable_if<is_status_code<MakeStatusCodeResult>::value, bool>::type = true> // ADL makes a status code
  279. inline bool
  280. operator!=(const T &a, const errored_status_code<DomainType1> &b)
  281. {
  282. return !b.equivalent(make_status_code(a));
  283. }
  284. namespace detail
  285. {
  286. template <class T> struct is_errored_status_code
  287. {
  288. static constexpr bool value = false;
  289. };
  290. template <class T> struct is_errored_status_code<errored_status_code<T>>
  291. {
  292. static constexpr bool value = true;
  293. };
  294. template <class T> struct is_erased_errored_status_code
  295. {
  296. static constexpr bool value = false;
  297. };
  298. template <class T> struct is_erased_errored_status_code<errored_status_code<erased<T>>>
  299. {
  300. static constexpr bool value = true;
  301. };
  302. } // namespace detail
  303. //! Trait returning true if the type is an errored status code.
  304. template <class T> struct is_errored_status_code
  305. {
  306. static constexpr bool value = detail::is_errored_status_code<typename std::decay<T>::type>::value || detail::is_erased_errored_status_code<typename std::decay<T>::type>::value;
  307. };
  308. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
  309. #endif