status_result.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* A very simple result type
  2. (C) 2018-2024 Niall Douglas <http://www.nedproductions.biz/> (11 commits)
  3. File Created: Apr 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_EXPERIMENTAL_STATUS_RESULT_HPP
  26. #define BOOST_OUTCOME_EXPERIMENTAL_STATUS_RESULT_HPP
  27. #include "../basic_result.hpp"
  28. #include "../policy/fail_to_compile_observers.hpp"
  29. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_USE_STD_ADDRESSOF
  30. #if BOOST_OUTCOME_USE_STD_ADDRESSOF
  31. #define BOOST_OUTCOME_SYSTEM_ERROR2_USE_STD_ADDRESSOF 1
  32. #endif
  33. #endif
  34. #if __PCPP_ALWAYS_TRUE__
  35. #include "status-code/system_error2.hpp"
  36. #elif !BOOST_OUTCOME_USE_SYSTEM_STATUS_CODE && __has_include("status-code/system_error2.hpp")
  37. #include "status-code/system_error2.hpp"
  38. #else
  39. #include <status-code/system_error2.hpp>
  40. #endif
  41. BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
  42. namespace trait
  43. {
  44. namespace detail
  45. {
  46. // Shortcut this for lower build impact. Used to tell outcome's converting constructors
  47. // that they can do E => EC or E => EP as necessary.
  48. template <class DomainType> struct _is_error_code_available<BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code<DomainType>>
  49. {
  50. static constexpr bool value = true;
  51. using type = BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code<DomainType>;
  52. };
  53. template <class DomainType> struct _is_error_code_available<BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::errored_status_code<DomainType>>
  54. {
  55. static constexpr bool value = true;
  56. using type = BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::errored_status_code<DomainType>;
  57. };
  58. } // namespace detail
  59. #if 0 // Do NOT enable weakened implicit construction for these types
  60. template <class DomainType> struct is_error_type<BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code<DomainType>>
  61. {
  62. static constexpr bool value = true;
  63. };
  64. template <> struct is_error_type<BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::errc>
  65. {
  66. static constexpr bool value = true;
  67. };
  68. template <class DomainType, class Enum> struct is_error_type_enum<BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code<DomainType>, Enum>
  69. {
  70. static constexpr bool value = boost::system::is_error_condition_enum<Enum>::value;
  71. };
  72. #endif
  73. template <class DomainType> struct is_move_bitcopying<BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code<DomainType>>
  74. {
  75. static constexpr bool value = BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::traits::is_move_bitcopying<BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::status_code<DomainType>>::value;
  76. };
  77. template <class DomainType> struct is_move_bitcopying<BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::errored_status_code<DomainType>>
  78. {
  79. static constexpr bool value = BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::traits::is_move_bitcopying<BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::errored_status_code<DomainType>>::value;
  80. };
  81. } // namespace trait
  82. namespace detail
  83. {
  84. // Customise _set_error_is_errno
  85. template <class State> constexpr inline void _set_error_is_errno(State &state, const BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::generic_code & /*unused*/)
  86. {
  87. state._status.set_have_error_is_errno(true);
  88. }
  89. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_NOT_POSIX
  90. template <class State> constexpr inline void _set_error_is_errno(State &state, const BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::posix_code & /*unused*/)
  91. {
  92. state._status.set_have_error_is_errno(true);
  93. }
  94. #endif
  95. template <class State> constexpr inline void _set_error_is_errno(State &state, const BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE::errc & /*unused*/)
  96. {
  97. state._status.set_have_error_is_errno(true);
  98. }
  99. } // namespace detail
  100. namespace experimental
  101. {
  102. using namespace BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE;
  103. using BOOST_OUTCOME_V2_NAMESPACE::failure;
  104. using BOOST_OUTCOME_V2_NAMESPACE::success;
  105. namespace policy
  106. {
  107. using namespace BOOST_OUTCOME_V2_NAMESPACE::policy;
  108. template <class T, class EC, class E> struct status_code_throw
  109. {
  110. static_assert(!std::is_same<T, T>::value,
  111. "policy::status_code_throw not specialised for these types, did you use status_result<T, status_code<DomainType>, E>?");
  112. };
  113. template <class T, class DomainType> struct status_code_throw<T, status_code<DomainType>, void> : base
  114. {
  115. using _base = base;
  116. template <class Impl> static constexpr void wide_value_check(Impl &&self)
  117. {
  118. if(!base::_has_value(static_cast<Impl &&>(self)))
  119. {
  120. if(base::_has_error(static_cast<Impl &&>(self)))
  121. {
  122. #ifndef BOOST_NO_EXCEPTIONS
  123. base::_error(static_cast<Impl &&>(self)).throw_exception();
  124. #else
  125. BOOST_OUTCOME_THROW_EXCEPTION("wide value check failed");
  126. #endif
  127. }
  128. }
  129. }
  130. template <class Impl> static constexpr void wide_error_check(Impl &&self) { _base::narrow_error_check(static_cast<Impl &&>(self)); }
  131. };
  132. template <class T, class DomainType>
  133. struct status_code_throw<T, errored_status_code<DomainType>, void> : status_code_throw<T, status_code<DomainType>, void>
  134. {
  135. status_code_throw() = default;
  136. using status_code_throw<T, status_code<DomainType>, void>::status_code_throw;
  137. };
  138. template <class T, class EC>
  139. using default_status_result_policy = std::conditional_t< //
  140. std::is_void<EC>::value, //
  141. BOOST_OUTCOME_V2_NAMESPACE::policy::terminate, //
  142. std::conditional_t<is_status_code<EC>::value || is_errored_status_code<EC>::value, //
  143. status_code_throw<T, EC, void>, //
  144. BOOST_OUTCOME_V2_NAMESPACE::policy::fail_to_compile_observers //
  145. >>;
  146. } // namespace policy
  147. /*! AWAITING HUGO JSON CONVERSION TOOL
  148. SIGNATURE NOT RECOGNISED
  149. */
  150. template <class R, class S = erased_errored_status_code<typename system_code::value_type>,
  151. class NoValuePolicy = policy::default_status_result_policy<R, S>> //
  152. using status_result = basic_result<R, S, NoValuePolicy>;
  153. /*! AWAITING HUGO JSON CONVERSION TOOL
  154. SIGNATURE NOT RECOGNISED
  155. */
  156. BOOST_OUTCOME_TEMPLATE(class R, class S, class NoValuePolicy)
  157. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_copy_constructible<R>::value && (is_status_code<S>::value || is_errored_status_code<S>::value)))
  158. inline basic_result<R, S, NoValuePolicy> clone(const basic_result<R, S, NoValuePolicy> &v)
  159. {
  160. if(v)
  161. {
  162. return success_type<R>(v.assume_value());
  163. }
  164. return failure_type<S>(v.assume_error().clone(), hooks::spare_storage(&v));
  165. }
  166. /*! AWAITING HUGO JSON CONVERSION TOOL
  167. SIGNATURE NOT RECOGNISED
  168. */
  169. BOOST_OUTCOME_TEMPLATE(class S, class NoValuePolicy)
  170. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(is_status_code<S>::value || is_errored_status_code<S>::value))
  171. inline basic_result<void, S, NoValuePolicy> clone(const basic_result<void, S, NoValuePolicy> &v)
  172. {
  173. if(v)
  174. {
  175. return success_type<void>();
  176. }
  177. return failure_type<S>(v.assume_error().clone(), hooks::spare_storage(&v));
  178. }
  179. } // namespace experimental
  180. BOOST_OUTCOME_V2_NAMESPACE_END
  181. #endif