status_outcome.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* A less simple result type
  2. (C) 2018-2021 Niall Douglas <http://www.nedproductions.biz/> (17 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_OUTCOME_HPP
  26. #define BOOST_OUTCOME_EXPERIMENTAL_STATUS_OUTCOME_HPP
  27. #include "../basic_outcome.hpp"
  28. #include "../detail/trait_std_exception.hpp"
  29. #include "status_result.hpp"
  30. #include "boost/exception_ptr.hpp"
  31. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
  32. template <class DomainType> inline std::exception_ptr basic_outcome_failure_exception_from_error(const status_code<DomainType> &sc)
  33. {
  34. (void) sc;
  35. #ifndef BOOST_NO_EXCEPTIONS
  36. try
  37. {
  38. sc.throw_exception();
  39. }
  40. catch(...)
  41. {
  42. return std::current_exception();
  43. }
  44. #endif
  45. return {};
  46. }
  47. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
  48. BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
  49. namespace experimental
  50. {
  51. namespace policy
  52. {
  53. template <class T, class EC, class E>
  54. using default_status_outcome_policy = std::conditional_t< //
  55. std::is_void<EC>::value && std::is_void<E>::value, //
  56. BOOST_OUTCOME_V2_NAMESPACE::policy::terminate, //
  57. std::conditional_t<(is_status_code<EC>::value || is_errored_status_code<EC>::value) &&
  58. (std::is_void<E>::value || BOOST_OUTCOME_V2_NAMESPACE::trait::is_exception_ptr_available<E>::value), //
  59. status_code_throw<T, EC, E>, //
  60. BOOST_OUTCOME_V2_NAMESPACE::policy::fail_to_compile_observers //
  61. >>;
  62. } // namespace policy
  63. /*! AWAITING HUGO JSON CONVERSION TOOL
  64. SIGNATURE NOT RECOGNISED
  65. */
  66. template <class R, class S = errored_status_code<erased<typename system_code::value_type>>, class P = std::exception_ptr,
  67. class NoValuePolicy = policy::default_status_outcome_policy<R, S, P>> //
  68. using status_outcome = basic_outcome<R, S, P, NoValuePolicy>;
  69. namespace policy
  70. {
  71. template <class T, class DomainType, class E> struct status_code_throw<T, status_code<DomainType>, E> : base
  72. {
  73. using _base = base;
  74. template <class Impl> static constexpr void wide_value_check(Impl &&self)
  75. {
  76. if(!base::_has_value(static_cast<Impl &&>(self)))
  77. {
  78. if(base::_has_exception(static_cast<Impl &&>(self)))
  79. {
  80. BOOST_OUTCOME_V2_NAMESPACE::policy::detail::_rethrow_exception<trait::is_exception_ptr_available<E>::value>(
  81. base::_exception<T, status_code<DomainType>, E, status_code_throw>(static_cast<Impl &&>(self))); // NOLINT
  82. }
  83. if(base::_has_error(static_cast<Impl &&>(self)))
  84. {
  85. #ifndef BOOST_NO_EXCEPTIONS
  86. base::_error(static_cast<Impl &&>(self)).throw_exception();
  87. #else
  88. BOOST_OUTCOME_THROW_EXCEPTION("wide value check failed");
  89. #endif
  90. }
  91. }
  92. }
  93. template <class Impl> static constexpr void wide_error_check(Impl &&self) { _base::narrow_error_check(static_cast<Impl &&>(self)); }
  94. template <class Impl> static constexpr void wide_exception_check(Impl &&self) { _base::narrow_exception_check(static_cast<Impl &&>(self)); }
  95. };
  96. template <class T, class DomainType, class E>
  97. struct status_code_throw<T, errored_status_code<DomainType>, E> : status_code_throw<T, status_code<DomainType>, E>
  98. {
  99. status_code_throw() = default;
  100. using status_code_throw<T, status_code<DomainType>, E>::status_code_throw;
  101. };
  102. } // namespace policy
  103. } // namespace experimental
  104. BOOST_OUTCOME_V2_NAMESPACE_END
  105. #endif