success_failure.hpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* Type sugar for success and failure
  2. (C) 2017-2021 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 or moves the successful state supplied into the returned type sugar.
  72. */
  73. template <class T> inline constexpr success_type<std::decay_t<T>> success(T &&v, uint16_t spare_storage = 0)
  74. {
  75. return success_type<std::decay_t<T>>{static_cast<T &&>(v), spare_storage};
  76. }
  77. /*! AWAITING HUGO JSON CONVERSION TOOL
  78. type definition template <class EC, class E = void> failure_type. Potential doc page: `failure_type<EC, EP = void>`
  79. */
  80. template <class EC, class E = void> struct BOOST_OUTCOME_NODISCARD failure_type
  81. {
  82. using error_type = EC;
  83. using exception_type = E;
  84. private:
  85. error_type _error;
  86. exception_type _exception;
  87. bool _have_error{false}, _have_exception{false};
  88. uint16_t _spare_storage{0};
  89. struct error_init_tag
  90. {
  91. };
  92. struct exception_init_tag
  93. {
  94. };
  95. public:
  96. failure_type() = default;
  97. failure_type(const failure_type &) = default;
  98. failure_type(failure_type &&) = default; // NOLINT
  99. failure_type &operator=(const failure_type &) = default;
  100. failure_type &operator=(failure_type &&) = default; // NOLINT
  101. ~failure_type() = default;
  102. template <class U, class V>
  103. constexpr explicit failure_type(U &&u, V &&v, uint16_t spare_storage = 0)
  104. : _error(static_cast<U &&>(u))
  105. , _exception(static_cast<V &&>(v))
  106. , _have_error(true)
  107. , _have_exception(true)
  108. , _spare_storage(spare_storage)
  109. {
  110. }
  111. template <class U>
  112. 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())
  113. : _error(static_cast<U &&>(u))
  114. , _exception()
  115. , _have_error(true)
  116. , _spare_storage(spare_storage)
  117. {
  118. }
  119. template <class U>
  120. constexpr explicit failure_type(in_place_type_t<exception_type> /*unused*/, U &&u, uint16_t spare_storage = 0,
  121. exception_init_tag /*unused*/ = exception_init_tag())
  122. : _error()
  123. , _exception(static_cast<U &&>(u))
  124. , _have_exception(true)
  125. , _spare_storage(spare_storage)
  126. {
  127. }
  128. constexpr bool has_error() const { return _have_error; }
  129. constexpr bool has_exception() const { return _have_exception; }
  130. constexpr error_type &error() & { return _error; }
  131. constexpr const error_type &error() const & { return _error; }
  132. constexpr error_type &&error() && { return static_cast<error_type &&>(_error); }
  133. constexpr const error_type &&error() const && { return static_cast<error_type &&>(_error); }
  134. constexpr exception_type &exception() & { return _exception; }
  135. constexpr const exception_type &exception() const & { return _exception; }
  136. constexpr exception_type &&exception() && { return static_cast<exception_type &&>(_exception); }
  137. constexpr const exception_type &&exception() const && { return static_cast<exception_type &&>(_exception); }
  138. constexpr uint16_t spare_storage() const { return _spare_storage; }
  139. };
  140. template <class EC> struct BOOST_OUTCOME_NODISCARD failure_type<EC, void>
  141. {
  142. using error_type = EC;
  143. using exception_type = void;
  144. private:
  145. error_type _error;
  146. uint16_t _spare_storage{0};
  147. public:
  148. failure_type() = default;
  149. failure_type(const failure_type &) = default;
  150. failure_type(failure_type &&) = default; // NOLINT
  151. failure_type &operator=(const failure_type &) = default;
  152. failure_type &operator=(failure_type &&) = default; // NOLINT
  153. ~failure_type() = default;
  154. BOOST_OUTCOME_TEMPLATE(class U)
  155. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(!std::is_same<failure_type, std::decay_t<U>>::value))
  156. constexpr explicit failure_type(U &&u, uint16_t spare_storage = 0)
  157. : _error(static_cast<U &&>(u)) // NOLINT
  158. , _spare_storage(spare_storage)
  159. {
  160. }
  161. constexpr error_type &error() & { return _error; }
  162. constexpr const error_type &error() const & { return _error; }
  163. constexpr error_type &&error() && { return static_cast<error_type &&>(_error); }
  164. constexpr const error_type &&error() const && { return static_cast<error_type &&>(_error); }
  165. constexpr uint16_t spare_storage() const { return _spare_storage; }
  166. };
  167. template <class E> struct BOOST_OUTCOME_NODISCARD failure_type<void, E>
  168. {
  169. using error_type = void;
  170. using exception_type = E;
  171. private:
  172. exception_type _exception;
  173. uint16_t _spare_storage{0};
  174. public:
  175. failure_type() = default;
  176. failure_type(const failure_type &) = default;
  177. failure_type(failure_type &&) = default; // NOLINT
  178. failure_type &operator=(const failure_type &) = default;
  179. failure_type &operator=(failure_type &&) = default; // NOLINT
  180. ~failure_type() = default;
  181. BOOST_OUTCOME_TEMPLATE(class V)
  182. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(!std::is_same<failure_type, std::decay_t<V>>::value))
  183. constexpr explicit failure_type(V &&v, uint16_t spare_storage = 0)
  184. : _exception(static_cast<V &&>(v)) // NOLINT
  185. , _spare_storage(spare_storage)
  186. {
  187. }
  188. constexpr exception_type &exception() & { return _exception; }
  189. constexpr const exception_type &exception() const & { return _exception; }
  190. constexpr exception_type &&exception() && { return static_cast<exception_type &&>(_exception); }
  191. constexpr const exception_type &&exception() const && { return static_cast<exception_type &&>(_exception); }
  192. constexpr uint16_t spare_storage() const { return _spare_storage; }
  193. };
  194. /*! AWAITING HUGO JSON CONVERSION TOOL
  195. SIGNATURE NOT RECOGNISED
  196. */
  197. template <class EC> inline constexpr failure_type<std::decay_t<EC>> failure(EC &&v, uint16_t spare_storage = 0)
  198. {
  199. return failure_type<std::decay_t<EC>>{static_cast<EC &&>(v), spare_storage};
  200. }
  201. /*! AWAITING HUGO JSON CONVERSION TOOL
  202. SIGNATURE NOT RECOGNISED
  203. */
  204. template <class EC, class E> inline constexpr failure_type<std::decay_t<EC>, std::decay_t<E>> failure(EC &&v, E &&w, uint16_t spare_storage = 0)
  205. {
  206. return failure_type<std::decay_t<EC>, std::decay_t<E>>{static_cast<EC &&>(v), static_cast<E &&>(w), spare_storage};
  207. }
  208. namespace detail
  209. {
  210. template <class T> struct is_success_type
  211. {
  212. static constexpr bool value = false;
  213. };
  214. template <class T> struct is_success_type<success_type<T>>
  215. {
  216. static constexpr bool value = true;
  217. };
  218. template <class T> struct is_failure_type
  219. {
  220. static constexpr bool value = false;
  221. };
  222. template <class EC, class E> struct is_failure_type<failure_type<EC, E>>
  223. {
  224. static constexpr bool value = true;
  225. };
  226. } // namespace detail
  227. /*! AWAITING HUGO JSON CONVERSION TOOL
  228. SIGNATURE NOT RECOGNISED
  229. */
  230. template <class T> static constexpr bool is_success_type = detail::is_success_type<std::decay_t<T>>::value;
  231. /*! AWAITING HUGO JSON CONVERSION TOOL
  232. SIGNATURE NOT RECOGNISED
  233. */
  234. template <class T> static constexpr bool is_failure_type = detail::is_failure_type<std::decay_t<T>>::value;
  235. BOOST_OUTCOME_V2_NAMESPACE_END
  236. #endif