success_failure.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* Type sugar for success and failure
  2. (C) 2017-2024 Niall Douglas <http://www.nedproductions.biz/> (25 commits)
  3. File Created: July 2017
  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_SUCCESS_FAILURE_HPP
  26. #define BOOST_OUTCOME_SUCCESS_FAILURE_HPP
  27. #include "config.hpp"
  28. BOOST_OUTCOME_V2_NAMESPACE_BEGIN
  29. /*! AWAITING HUGO JSON CONVERSION TOOL
  30. type definition template <class T> success_type. Potential doc page: `success_type<T>`
  31. */
  32. template <class T> struct BOOST_OUTCOME_NODISCARD success_type
  33. {
  34. using value_type = T;
  35. private:
  36. value_type _value;
  37. uint16_t _spare_storage{0};
  38. public:
  39. success_type() = default;
  40. success_type(const success_type &) = default;
  41. success_type(success_type &&) = default; // NOLINT
  42. success_type &operator=(const success_type &) = default;
  43. success_type &operator=(success_type &&) = default; // NOLINT
  44. ~success_type() = default;
  45. BOOST_OUTCOME_TEMPLATE(class U)
  46. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(!std::is_same<success_type, std::decay_t<U>>::value))
  47. constexpr explicit success_type(U &&v, uint16_t spare_storage = 0)
  48. : _value(static_cast<U &&>(v)) // NOLINT
  49. , _spare_storage(spare_storage)
  50. {
  51. }
  52. constexpr value_type &value() & { return _value; }
  53. constexpr const value_type &value() const & { return _value; }
  54. constexpr value_type &&value() && { return static_cast<value_type &&>(_value); }
  55. constexpr const value_type &&value() const && { return static_cast<value_type &&>(_value); }
  56. constexpr uint16_t spare_storage() const { return _spare_storage; }
  57. };
  58. template <> struct BOOST_OUTCOME_NODISCARD success_type<void>
  59. {
  60. using value_type = void;
  61. constexpr uint16_t spare_storage() const { return 0; }
  62. };
  63. /*! Returns type sugar for implicitly constructing a `basic_result<T>` with a successful state,
  64. default constructing `T` if necessary.
  65. */
  66. inline constexpr success_type<void> success() noexcept
  67. {
  68. return success_type<void>{};
  69. }
  70. /*! Returns type sugar for implicitly constructing a `basic_result<T>` with a successful state.
  71. \effects Copies the successful state supplied into the returned type sugar.
  72. */
  73. BOOST_OUTCOME_TEMPLATE(class T)
  74. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_copy_constructible<T>::value))
  75. inline constexpr success_type<std::decay_t<T>> success(const T &v, uint16_t spare_storage = 0)
  76. {
  77. return success_type<std::decay_t<T>>{v, spare_storage};
  78. }
  79. /*! Returns type sugar for implicitly constructing a `basic_result<T>` with a successful state.
  80. \effects Moves the successful state supplied into the returned type sugar.
  81. */
  82. BOOST_OUTCOME_TEMPLATE(class T)
  83. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_move_constructible<T>::value))
  84. inline constexpr success_type<std::decay_t<T>> success(T &&v, uint16_t spare_storage = 0)
  85. {
  86. return success_type<std::decay_t<T>>{static_cast<T &&>(v), spare_storage};
  87. }
  88. /*! AWAITING HUGO JSON CONVERSION TOOL
  89. type definition template <class EC, class E = void> failure_type. Potential doc page: `failure_type<EC, EP = void>`
  90. */
  91. template <class EC, class E = void> struct BOOST_OUTCOME_NODISCARD failure_type
  92. {
  93. using error_type = EC;
  94. using exception_type = E;
  95. private:
  96. error_type _error;
  97. exception_type _exception;
  98. bool _have_error{false}, _have_exception{false};
  99. uint16_t _spare_storage{0};
  100. struct error_init_tag
  101. {
  102. };
  103. struct exception_init_tag
  104. {
  105. };
  106. public:
  107. failure_type() = default;
  108. failure_type(const failure_type &) = default;
  109. failure_type(failure_type &&) = default; // NOLINT
  110. failure_type &operator=(const failure_type &) = default;
  111. failure_type &operator=(failure_type &&) = default; // NOLINT
  112. ~failure_type() = default;
  113. template <class U, class V>
  114. constexpr explicit failure_type(U &&u, V &&v, uint16_t spare_storage = 0)
  115. : _error(static_cast<U &&>(u))
  116. , _exception(static_cast<V &&>(v))
  117. , _have_error(true)
  118. , _have_exception(true)
  119. , _spare_storage(spare_storage)
  120. {
  121. }
  122. template <class U>
  123. constexpr explicit failure_type(in_place_type_t<error_type> /*unused*/, U &&u, uint16_t spare_storage = 0, error_init_tag /*unused*/ = error_init_tag())
  124. : _error(static_cast<U &&>(u))
  125. , _exception()
  126. , _have_error(true)
  127. , _spare_storage(spare_storage)
  128. {
  129. }
  130. template <class U>
  131. constexpr explicit failure_type(in_place_type_t<exception_type> /*unused*/, U &&u, uint16_t spare_storage = 0,
  132. exception_init_tag /*unused*/ = exception_init_tag())
  133. : _error()
  134. , _exception(static_cast<U &&>(u))
  135. , _have_exception(true)
  136. , _spare_storage(spare_storage)
  137. {
  138. }
  139. constexpr bool has_error() const { return _have_error; }
  140. constexpr bool has_exception() const { return _have_exception; }
  141. constexpr error_type &error() & { return _error; }
  142. constexpr const error_type &error() const & { return _error; }
  143. constexpr error_type &&error() && { return static_cast<error_type &&>(_error); }
  144. constexpr const error_type &&error() const && { return static_cast<error_type &&>(_error); }
  145. constexpr exception_type &exception() & { return _exception; }
  146. constexpr const exception_type &exception() const & { return _exception; }
  147. constexpr exception_type &&exception() && { return static_cast<exception_type &&>(_exception); }
  148. constexpr const exception_type &&exception() const && { return static_cast<exception_type &&>(_exception); }
  149. constexpr uint16_t spare_storage() const { return _spare_storage; }
  150. };
  151. template <class EC> struct BOOST_OUTCOME_NODISCARD failure_type<EC, void>
  152. {
  153. using error_type = EC;
  154. using exception_type = void;
  155. private:
  156. error_type _error;
  157. uint16_t _spare_storage{0};
  158. public:
  159. failure_type() = default;
  160. failure_type(const failure_type &) = default;
  161. failure_type(failure_type &&) = default; // NOLINT
  162. failure_type &operator=(const failure_type &) = default;
  163. failure_type &operator=(failure_type &&) = default; // NOLINT
  164. ~failure_type() = default;
  165. BOOST_OUTCOME_TEMPLATE(class U)
  166. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(!std::is_same<failure_type, std::decay_t<U>>::value))
  167. constexpr explicit failure_type(U &&u, uint16_t spare_storage = 0)
  168. : _error(static_cast<U &&>(u)) // NOLINT
  169. , _spare_storage(spare_storage)
  170. {
  171. }
  172. constexpr error_type &error() & { return _error; }
  173. constexpr const error_type &error() const & { return _error; }
  174. constexpr error_type &&error() && { return static_cast<error_type &&>(_error); }
  175. constexpr const error_type &&error() const && { return static_cast<error_type &&>(_error); }
  176. constexpr uint16_t spare_storage() const { return _spare_storage; }
  177. };
  178. template <class E> struct BOOST_OUTCOME_NODISCARD failure_type<void, E>
  179. {
  180. using error_type = void;
  181. using exception_type = E;
  182. private:
  183. exception_type _exception;
  184. uint16_t _spare_storage{0};
  185. public:
  186. failure_type() = default;
  187. failure_type(const failure_type &) = default;
  188. failure_type(failure_type &&) = default; // NOLINT
  189. failure_type &operator=(const failure_type &) = default;
  190. failure_type &operator=(failure_type &&) = default; // NOLINT
  191. ~failure_type() = default;
  192. BOOST_OUTCOME_TEMPLATE(class V)
  193. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(!std::is_same<failure_type, std::decay_t<V>>::value))
  194. constexpr explicit failure_type(V &&v, uint16_t spare_storage = 0)
  195. : _exception(static_cast<V &&>(v)) // NOLINT
  196. , _spare_storage(spare_storage)
  197. {
  198. }
  199. constexpr exception_type &exception() & { return _exception; }
  200. constexpr const exception_type &exception() const & { return _exception; }
  201. constexpr exception_type &&exception() && { return static_cast<exception_type &&>(_exception); }
  202. constexpr const exception_type &&exception() const && { return static_cast<exception_type &&>(_exception); }
  203. constexpr uint16_t spare_storage() const { return _spare_storage; }
  204. };
  205. /*! AWAITING HUGO JSON CONVERSION TOOL
  206. SIGNATURE NOT RECOGNISED
  207. */
  208. BOOST_OUTCOME_TEMPLATE(class EC)
  209. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_copy_constructible<EC>::value))
  210. inline constexpr failure_type<std::decay_t<EC>> failure(const EC &v, uint16_t spare_storage = 0)
  211. {
  212. return failure_type<std::decay_t<EC>>{v, spare_storage};
  213. }
  214. /*! AWAITING HUGO JSON CONVERSION TOOL
  215. SIGNATURE NOT RECOGNISED
  216. */
  217. BOOST_OUTCOME_TEMPLATE(class EC)
  218. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_move_constructible<EC>::value))
  219. inline constexpr failure_type<std::decay_t<EC>> failure(EC &&v, uint16_t spare_storage = 0)
  220. {
  221. return failure_type<std::decay_t<EC>>{static_cast<EC &&>(v), spare_storage};
  222. }
  223. /*! AWAITING HUGO JSON CONVERSION TOOL
  224. SIGNATURE NOT RECOGNISED
  225. */
  226. BOOST_OUTCOME_TEMPLATE(class EC, class E)
  227. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_copy_constructible<EC>::value &&std::is_copy_constructible<E>::value))
  228. inline constexpr failure_type<std::decay_t<EC>, std::decay_t<E>> failure(const EC &v, const E &w, uint16_t spare_storage = 0)
  229. {
  230. return failure_type<std::decay_t<EC>, std::decay_t<E>>{v, w, spare_storage};
  231. }
  232. /*! AWAITING HUGO JSON CONVERSION TOOL
  233. SIGNATURE NOT RECOGNISED
  234. */
  235. BOOST_OUTCOME_TEMPLATE(class EC, class E)
  236. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_copy_constructible<EC>::value &&std::is_move_constructible<E>::value))
  237. inline constexpr failure_type<std::decay_t<EC>, std::decay_t<E>> failure(const EC &v, E &&w, uint16_t spare_storage = 0)
  238. {
  239. return failure_type<std::decay_t<EC>, std::decay_t<E>>{v, static_cast<E &&>(w), spare_storage};
  240. }
  241. /*! AWAITING HUGO JSON CONVERSION TOOL
  242. SIGNATURE NOT RECOGNISED
  243. */
  244. BOOST_OUTCOME_TEMPLATE(class EC, class E)
  245. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_move_constructible<EC>::value &&std::is_copy_constructible<E>::value))
  246. inline constexpr failure_type<std::decay_t<EC>, std::decay_t<E>> failure(EC &&v, const E &w, uint16_t spare_storage = 0)
  247. {
  248. return failure_type<std::decay_t<EC>, std::decay_t<E>>{static_cast<EC &&>(v), w, spare_storage};
  249. }
  250. /*! AWAITING HUGO JSON CONVERSION TOOL
  251. SIGNATURE NOT RECOGNISED
  252. */
  253. BOOST_OUTCOME_TEMPLATE(class EC, class E)
  254. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_move_constructible<EC>::value &&std::is_move_constructible<E>::value))
  255. inline constexpr failure_type<std::decay_t<EC>, std::decay_t<E>> failure(EC &&v, E &&w, uint16_t spare_storage = 0)
  256. {
  257. return failure_type<std::decay_t<EC>, std::decay_t<E>>{static_cast<EC &&>(v), static_cast<E &&>(w), spare_storage};
  258. }
  259. namespace detail
  260. {
  261. template <class T> struct is_success_type
  262. {
  263. static constexpr bool value = false;
  264. };
  265. template <class T> struct is_success_type<success_type<T>>
  266. {
  267. static constexpr bool value = true;
  268. };
  269. template <class T> struct is_failure_type
  270. {
  271. static constexpr bool value = false;
  272. };
  273. template <class EC, class E> struct is_failure_type<failure_type<EC, E>>
  274. {
  275. static constexpr bool value = true;
  276. };
  277. } // namespace detail
  278. /*! AWAITING HUGO JSON CONVERSION TOOL
  279. SIGNATURE NOT RECOGNISED
  280. */
  281. template <class T> static constexpr bool is_success_type = detail::is_success_type<std::decay_t<T>>::value;
  282. /*! AWAITING HUGO JSON CONVERSION TOOL
  283. SIGNATURE NOT RECOGNISED
  284. */
  285. template <class T> static constexpr bool is_failure_type = detail::is_failure_type<std::decay_t<T>>::value;
  286. BOOST_OUTCOME_V2_NAMESPACE_END
  287. #endif