success_failure.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* Type sugar for success and failure
  2. (C) 2017-2019 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. public:
  38. success_type() = default;
  39. success_type(const success_type &) = default;
  40. success_type(success_type &&) = default; // NOLINT
  41. success_type &operator=(const success_type &) = default;
  42. success_type &operator=(success_type &&) = default; // NOLINT
  43. ~success_type() = default;
  44. BOOST_OUTCOME_TEMPLATE(class U)
  45. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(!std::is_same<success_type, std::decay_t<U>>::value))
  46. constexpr explicit success_type(U &&v)
  47. : _value(static_cast<U &&>(v)) // NOLINT
  48. {
  49. }
  50. constexpr value_type &value() & { return _value; }
  51. constexpr const value_type &value() const & { return _value; }
  52. constexpr value_type &&value() && { return static_cast<value_type &&>(_value); }
  53. constexpr const value_type &&value() const && { return static_cast<value_type &&>(_value); }
  54. };
  55. template <> struct BOOST_OUTCOME_NODISCARD success_type<void>
  56. {
  57. using value_type = void;
  58. };
  59. /*! Returns type sugar for implicitly constructing a `basic_result<T>` with a successful state,
  60. default constructing `T` if necessary.
  61. */
  62. inline constexpr success_type<void> success() noexcept
  63. {
  64. return success_type<void>{};
  65. }
  66. /*! Returns type sugar for implicitly constructing a `basic_result<T>` with a successful state.
  67. \effects Copies or moves the successful state supplied into the returned type sugar.
  68. */
  69. template <class T> inline constexpr success_type<std::decay_t<T>> success(T &&v)
  70. {
  71. return success_type<std::decay_t<T>>{static_cast<T &&>(v)};
  72. }
  73. /*! AWAITING HUGO JSON CONVERSION TOOL
  74. type definition template <class EC, class E = void> failure_type. Potential doc page: `failure_type<EC, EP = void>`
  75. */
  76. template <class EC, class E = void> struct BOOST_OUTCOME_NODISCARD failure_type
  77. {
  78. using error_type = EC;
  79. using exception_type = E;
  80. private:
  81. bool _have_error{}, _have_exception{};
  82. error_type _error;
  83. exception_type _exception;
  84. struct error_init_tag
  85. {
  86. };
  87. struct exception_init_tag
  88. {
  89. };
  90. public:
  91. failure_type() = default;
  92. failure_type(const failure_type &) = default;
  93. failure_type(failure_type &&) = default; // NOLINT
  94. failure_type &operator=(const failure_type &) = default;
  95. failure_type &operator=(failure_type &&) = default; // NOLINT
  96. ~failure_type() = default;
  97. template <class U, class V>
  98. constexpr explicit failure_type(U &&u, V &&v)
  99. : _have_error(true)
  100. , _have_exception(true)
  101. , _error(static_cast<U &&>(u))
  102. , _exception(static_cast<V &&>(v))
  103. {
  104. }
  105. template <class U>
  106. constexpr explicit failure_type(in_place_type_t<error_type> /*unused*/, U &&u, error_init_tag /*unused*/ = error_init_tag())
  107. : _have_error(true)
  108. , _error(static_cast<U &&>(u))
  109. , _exception()
  110. {
  111. }
  112. template <class U>
  113. constexpr explicit failure_type(in_place_type_t<exception_type> /*unused*/, U &&u, exception_init_tag /*unused*/ = exception_init_tag())
  114. : _have_exception(true)
  115. , _error()
  116. , _exception(static_cast<U &&>(u))
  117. {
  118. }
  119. constexpr bool has_error() const { return _have_error; }
  120. constexpr bool has_exception() const { return _have_exception; }
  121. constexpr error_type &error() & { return _error; }
  122. constexpr const error_type &error() const & { return _error; }
  123. constexpr error_type &&error() && { return static_cast<error_type &&>(_error); }
  124. constexpr const error_type &&error() const && { return static_cast<error_type &&>(_error); }
  125. constexpr exception_type &exception() & { return _exception; }
  126. constexpr const exception_type &exception() const & { return _exception; }
  127. constexpr exception_type &&exception() && { return static_cast<exception_type &&>(_exception); }
  128. constexpr const exception_type &&exception() const && { return static_cast<exception_type &&>(_exception); }
  129. };
  130. template <class EC> struct BOOST_OUTCOME_NODISCARD failure_type<EC, void>
  131. {
  132. using error_type = EC;
  133. using exception_type = void;
  134. private:
  135. error_type _error;
  136. public:
  137. failure_type() = default;
  138. failure_type(const failure_type &) = default;
  139. failure_type(failure_type &&) = default; // NOLINT
  140. failure_type &operator=(const failure_type &) = default;
  141. failure_type &operator=(failure_type &&) = default; // NOLINT
  142. ~failure_type() = default;
  143. BOOST_OUTCOME_TEMPLATE(class U)
  144. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(!std::is_same<failure_type, std::decay_t<U>>::value))
  145. constexpr explicit failure_type(U &&u)
  146. : _error(static_cast<U &&>(u)) // NOLINT
  147. {
  148. }
  149. constexpr error_type &error() & { return _error; }
  150. constexpr const error_type &error() const & { return _error; }
  151. constexpr error_type &&error() && { return static_cast<error_type &&>(_error); }
  152. constexpr const error_type &&error() const && { return static_cast<error_type &&>(_error); }
  153. };
  154. template <class E> struct BOOST_OUTCOME_NODISCARD failure_type<void, E>
  155. {
  156. using error_type = void;
  157. using exception_type = E;
  158. private:
  159. exception_type _exception;
  160. public:
  161. failure_type() = default;
  162. failure_type(const failure_type &) = default;
  163. failure_type(failure_type &&) = default; // NOLINT
  164. failure_type &operator=(const failure_type &) = default;
  165. failure_type &operator=(failure_type &&) = default; // NOLINT
  166. ~failure_type() = default;
  167. BOOST_OUTCOME_TEMPLATE(class V)
  168. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(!std::is_same<failure_type, std::decay_t<V>>::value))
  169. constexpr explicit failure_type(V &&v)
  170. : _exception(static_cast<V &&>(v)) // NOLINT
  171. {
  172. }
  173. constexpr exception_type &exception() & { return _exception; }
  174. constexpr const exception_type &exception() const & { return _exception; }
  175. constexpr exception_type &&exception() && { return static_cast<exception_type &&>(_exception); }
  176. constexpr const exception_type &&exception() const && { return static_cast<exception_type &&>(_exception); }
  177. };
  178. /*! AWAITING HUGO JSON CONVERSION TOOL
  179. SIGNATURE NOT RECOGNISED
  180. */
  181. template <class EC> inline constexpr failure_type<std::decay_t<EC>> failure(EC &&v)
  182. {
  183. return failure_type<std::decay_t<EC>>{static_cast<EC &&>(v)};
  184. }
  185. /*! AWAITING HUGO JSON CONVERSION TOOL
  186. SIGNATURE NOT RECOGNISED
  187. */
  188. template <class EC, class E> inline constexpr failure_type<std::decay_t<EC>, std::decay_t<E>> failure(EC &&v, E &&w)
  189. {
  190. return failure_type<std::decay_t<EC>, std::decay_t<E>>{static_cast<EC &&>(v), static_cast<E &&>(w)};
  191. }
  192. namespace detail
  193. {
  194. template <class T> struct is_success_type
  195. {
  196. static constexpr bool value = false;
  197. };
  198. template <class T> struct is_success_type<success_type<T>>
  199. {
  200. static constexpr bool value = true;
  201. };
  202. template <class T> struct is_failure_type
  203. {
  204. static constexpr bool value = false;
  205. };
  206. template <class EC, class E> struct is_failure_type<failure_type<EC, E>>
  207. {
  208. static constexpr bool value = true;
  209. };
  210. } // namespace detail
  211. /*! AWAITING HUGO JSON CONVERSION TOOL
  212. SIGNATURE NOT RECOGNISED
  213. */
  214. template <class T> static constexpr bool is_success_type = detail::is_success_type<std::decay_t<T>>::value;
  215. /*! AWAITING HUGO JSON CONVERSION TOOL
  216. SIGNATURE NOT RECOGNISED
  217. */
  218. template <class T> static constexpr bool is_failure_type = detail::is_failure_type<std::decay_t<T>>::value;
  219. BOOST_OUTCOME_V2_NAMESPACE_END
  220. #endif