boost_result.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* A very simple result type
  2. (C) 2017-2024 Niall Douglas <http://www.nedproductions.biz/> (10 commits)
  3. File Created: June 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_BOOST_RESULT_HPP
  26. #define BOOST_OUTCOME_BOOST_RESULT_HPP
  27. #include "config.hpp"
  28. #include "boost/system/system_error.hpp"
  29. #include "boost/exception_ptr.hpp"
  30. #include "boost/version.hpp"
  31. BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
  32. /*! AWAITING HUGO JSON CONVERSION TOOL
  33. SIGNATURE NOT RECOGNISED
  34. */
  35. namespace policy
  36. {
  37. namespace detail
  38. {
  39. /* Pass through `make_error_code` function for `boost::system::error_code`.
  40. */
  41. inline boost::system::error_code make_error_code(boost::system::error_code v) { return v; }
  42. /* Pass through `make_exception_ptr` function for `boost::exception_ptr`.
  43. */
  44. inline boost::exception_ptr make_exception_ptr(boost::exception_ptr v) { return v; }
  45. } // namespace detail
  46. } // namespace policy
  47. BOOST_OUTCOME_V2_NAMESPACE_END
  48. #include "std_result.hpp"
  49. // ADL injection of outcome_throw_as_system_error_with_payload
  50. namespace boost
  51. {
  52. namespace system
  53. {
  54. inline void outcome_throw_as_system_error_with_payload(const error_code &error) { BOOST_OUTCOME_THROW_EXCEPTION(system_error(error)); }
  55. namespace errc
  56. {
  57. BOOST_OUTCOME_TEMPLATE(class Error)
  58. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(is_error_code_enum<std::decay_t<Error>>::value || is_error_condition_enum<std::decay_t<Error>>::value))
  59. inline void outcome_throw_as_system_error_with_payload(Error &&error) { BOOST_OUTCOME_THROW_EXCEPTION(system_error(make_error_code(error))); }
  60. } // namespace errc
  61. } // namespace system
  62. } // namespace boost
  63. BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
  64. namespace detail
  65. {
  66. // Customise _set_error_is_errno
  67. template <class State> constexpr inline void _set_error_is_errno(State &state, const boost::system::error_code &error)
  68. {
  69. if(error.category() == boost::system::generic_category()
  70. #ifndef _WIN32
  71. || error.category() == boost::system::system_category()
  72. #endif
  73. )
  74. {
  75. state._status.set_have_error_is_errno(true);
  76. }
  77. }
  78. template <class State> constexpr inline void _set_error_is_errno(State &state, const boost::system::error_condition &error)
  79. {
  80. if(error.category() == boost::system::generic_category()
  81. #ifndef _WIN32
  82. || error.category() == boost::system::system_category()
  83. #endif
  84. )
  85. {
  86. state._status.set_have_error_is_errno(true);
  87. }
  88. }
  89. template <class State> constexpr inline void _set_error_is_errno(State &state, const boost::system::errc::errc_t & /*unused*/)
  90. {
  91. state._status.set_have_error_is_errno(true);
  92. }
  93. } // namespace detail
  94. /*! AWAITING HUGO JSON CONVERSION TOOL
  95. SIGNATURE NOT RECOGNISED
  96. */
  97. namespace trait
  98. {
  99. namespace detail
  100. {
  101. // Shortcut these for lower build impact
  102. template <> struct _is_error_code_available<boost::system::error_code>
  103. {
  104. static constexpr bool value = true;
  105. using type = boost::system::error_code;
  106. };
  107. template <> struct _is_exception_ptr_available<boost::exception_ptr>
  108. {
  109. static constexpr bool value = true;
  110. using type = boost::exception_ptr;
  111. };
  112. } // namespace detail
  113. // boost::system::error_code is an error type
  114. template <> struct is_error_type<boost::system::error_code>
  115. {
  116. static constexpr bool value = true;
  117. };
  118. // boost::system::error_code::errc_t is an error type
  119. template <> struct is_error_type<boost::system::errc::errc_t>
  120. {
  121. static constexpr bool value = true;
  122. };
  123. // boost::exception_ptr is an error types
  124. template <> struct is_error_type<boost::exception_ptr>
  125. {
  126. static constexpr bool value = true;
  127. };
  128. // For boost::system::error_code, boost::system::is_error_condition_enum<> is the trait we want.
  129. template <class Enum> struct is_error_type_enum<boost::system::error_code, Enum>
  130. {
  131. static constexpr bool value = boost::system::is_error_condition_enum<Enum>::value;
  132. };
  133. } // namespace trait
  134. /*! AWAITING HUGO JSON CONVERSION TOOL
  135. SIGNATURE NOT RECOGNISED
  136. */
  137. template <class R, class S = boost::system::error_code, class NoValuePolicy = policy::default_policy<R, S, void>> //
  138. using boost_result = basic_result<R, S, NoValuePolicy>;
  139. /*! AWAITING HUGO JSON CONVERSION TOOL
  140. type alias template <class R, class S = boost::system::error_code> boost_unchecked. Potential doc page: `boost_unchecked<T, E = boost::system::error_code>`
  141. */
  142. template <class R, class S = boost::system::error_code> using boost_unchecked = boost_result<R, S, policy::all_narrow>;
  143. /*! AWAITING HUGO JSON CONVERSION TOOL
  144. type alias template <class R, class S = boost::system::error_code> boost_checked. Potential doc page: `boost_checked<T, E = boost::system::error_code>`
  145. */
  146. template <class R, class S = boost::system::error_code> using boost_checked = boost_result<R, S, policy::throw_bad_result_access<S, void>>;
  147. BOOST_OUTCOME_V2_NAMESPACE_END
  148. #endif