boost_result.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* A very simple result type
  2. (C) 2017-2019 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/exception_ptr.hpp"
  29. #include "boost/system/system_error.hpp"
  30. BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
  31. /*! AWAITING HUGO JSON CONVERSION TOOL
  32. SIGNATURE NOT RECOGNISED
  33. */
  34. namespace policy
  35. {
  36. namespace detail
  37. {
  38. /* Pass through `make_error_code` function for `boost::system::error_code`.
  39. */
  40. inline boost::system::error_code make_error_code(boost::system::error_code v) { return v; }
  41. /* Pass through `make_exception_ptr` function for `boost::exception_ptr`.
  42. The reason this needs to be here, declared before the rest of Outcome,
  43. is that there is no boost::make_exception_ptr as Boost still uses the old
  44. naming boost::copy_exception. Therefore the ADL discovered make_exception_ptr
  45. doesn't work, hence this hacky pre-declaration here.
  46. I was tempted to just inject a boost::make_exception_ptr, but I can see
  47. Boost doing that itself at some point. This hack should keep working after.
  48. */
  49. inline boost::exception_ptr make_exception_ptr(boost::exception_ptr v) { return v; }
  50. } // namespace detail
  51. } // namespace policy
  52. BOOST_OUTCOME_V2_NAMESPACE_END
  53. #include "std_result.hpp"
  54. // ADL injection of outcome_throw_as_system_error_with_payload
  55. namespace boost
  56. {
  57. namespace system
  58. {
  59. inline void outcome_throw_as_system_error_with_payload(const error_code &error) { BOOST_OUTCOME_THROW_EXCEPTION(system_error(error)); }
  60. namespace errc
  61. {
  62. BOOST_OUTCOME_TEMPLATE(class Error)
  63. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(is_error_code_enum<std::decay_t<Error>>::value || is_error_condition_enum<std::decay_t<Error>>::value))
  64. inline void outcome_throw_as_system_error_with_payload(Error &&error) { BOOST_OUTCOME_THROW_EXCEPTION(system_error(make_error_code(error))); }
  65. } // namespace errc
  66. } // namespace system
  67. } // namespace boost
  68. BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
  69. namespace detail
  70. {
  71. // Customise _set_error_is_errno
  72. template <class State> constexpr inline void _set_error_is_errno(State &state, const boost::system::error_code &error)
  73. {
  74. if(error.category() == boost::system::generic_category()
  75. #ifndef _WIN32
  76. || error.category() == boost::system::system_category()
  77. #endif
  78. )
  79. {
  80. state._status |= status_error_is_errno;
  81. }
  82. }
  83. template <class State> constexpr inline void _set_error_is_errno(State &state, const boost::system::error_condition &error)
  84. {
  85. if(error.category() == boost::system::generic_category()
  86. #ifndef _WIN32
  87. || error.category() == boost::system::system_category()
  88. #endif
  89. )
  90. {
  91. state._status |= status_error_is_errno;
  92. }
  93. }
  94. template <class State> constexpr inline void _set_error_is_errno(State &state, const boost::system::errc::errc_t & /*unused*/) { state._status |= status_error_is_errno; }
  95. } // namespace detail
  96. /*! AWAITING HUGO JSON CONVERSION TOOL
  97. SIGNATURE NOT RECOGNISED
  98. */
  99. namespace trait
  100. {
  101. namespace detail
  102. {
  103. // Shortcut these for lower build impact
  104. template <> struct _is_error_code_available<boost::system::error_code>
  105. {
  106. static constexpr bool value = true;
  107. using type = boost::system::error_code;
  108. };
  109. template <> struct _is_exception_ptr_available<boost::exception_ptr>
  110. {
  111. static constexpr bool value = true;
  112. using type = boost::exception_ptr;
  113. };
  114. } // namespace detail
  115. // boost::system::error_code is an error type
  116. template <> struct is_error_type<boost::system::error_code>
  117. {
  118. static constexpr bool value = true;
  119. };
  120. // boost::system::error_code::errc_t is an error type
  121. template <> struct is_error_type<boost::system::errc::errc_t>
  122. {
  123. static constexpr bool value = true;
  124. };
  125. // boost::exception_ptr is an error types
  126. template <> struct is_error_type<boost::exception_ptr>
  127. {
  128. static constexpr bool value = true;
  129. };
  130. // For boost::system::error_code, boost::system::is_error_condition_enum<> is the trait we want.
  131. template <class Enum> struct is_error_type_enum<boost::system::error_code, Enum>
  132. {
  133. static constexpr bool value = boost::system::is_error_condition_enum<Enum>::value;
  134. };
  135. } // namespace trait
  136. /*! AWAITING HUGO JSON CONVERSION TOOL
  137. SIGNATURE NOT RECOGNISED
  138. */
  139. template <class R, class S = boost::system::error_code, class NoValuePolicy = policy::default_policy<R, S, void>> //
  140. using boost_result = basic_result<R, S, NoValuePolicy>;
  141. /*! AWAITING HUGO JSON CONVERSION TOOL
  142. type alias template <class R, class S = boost::system::error_code> boost_unchecked. Potential doc page: `boost_unchecked<T, E = boost::system::error_code>`
  143. */
  144. template <class R, class S = boost::system::error_code> using boost_unchecked = boost_result<R, S, policy::all_narrow>;
  145. /*! AWAITING HUGO JSON CONVERSION TOOL
  146. type alias template <class R, class S = boost::system::error_code> boost_checked. Potential doc page: `boost_checked<T, E = boost::system::error_code>`
  147. */
  148. template <class R, class S = boost::system::error_code> using boost_checked = boost_result<R, S, policy::throw_bad_result_access<S, void>>;
  149. BOOST_OUTCOME_V2_NAMESPACE_END
  150. #endif