trait_std_error_code.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* Traits for Outcome
  2. (C) 2018-2019 Niall Douglas <http://www.nedproductions.biz/> (6 commits)
  3. File Created: March 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_TRAIT_STD_ERROR_CODE_HPP
  26. #define BOOST_OUTCOME_TRAIT_STD_ERROR_CODE_HPP
  27. #include "../config.hpp"
  28. #include <system_error>
  29. BOOST_OUTCOME_V2_NAMESPACE_BEGIN
  30. namespace detail
  31. {
  32. // Customise _set_error_is_errno
  33. template <class State> constexpr inline void _set_error_is_errno(State &state, const std::error_code &error)
  34. {
  35. if(error.category() == std::generic_category()
  36. #ifndef _WIN32
  37. || error.category() == std::system_category()
  38. #endif
  39. )
  40. {
  41. state._status |= status_error_is_errno;
  42. }
  43. }
  44. template <class State> constexpr inline void _set_error_is_errno(State &state, const std::error_condition &error)
  45. {
  46. if(error.category() == std::generic_category()
  47. #ifndef _WIN32
  48. || error.category() == std::system_category()
  49. #endif
  50. )
  51. {
  52. state._status |= status_error_is_errno;
  53. }
  54. }
  55. template <class State> constexpr inline void _set_error_is_errno(State &state, const std::errc & /*unused*/) { state._status |= status_error_is_errno; }
  56. } // namespace detail
  57. namespace policy
  58. {
  59. namespace detail
  60. {
  61. /* Pass through `make_error_code` function for `std::error_code`.
  62. */
  63. inline std::error_code make_error_code(std::error_code v) { return v; }
  64. // Try ADL, if not use fall backs above
  65. template <class T> constexpr inline decltype(auto) error_code(T &&v) { return make_error_code(std::forward<T>(v)); }
  66. struct std_enum_overload_tag
  67. {
  68. };
  69. } // namespace detail
  70. /*! AWAITING HUGO JSON CONVERSION TOOL
  71. SIGNATURE NOT RECOGNISED
  72. */
  73. template <class T> constexpr inline decltype(auto) error_code(T &&v) { return detail::error_code(std::forward<T>(v)); }
  74. /*! AWAITING HUGO JSON CONVERSION TOOL
  75. SIGNATURE NOT RECOGNISED
  76. */
  77. // inline void outcome_throw_as_system_error_with_payload(...) = delete; // To use the error_code_throw_as_system_error policy with a custom Error type, you must define a outcome_throw_as_system_error_with_payload() free function to say how to handle the payload
  78. inline void outcome_throw_as_system_error_with_payload(const std::error_code &error) { BOOST_OUTCOME_THROW_EXCEPTION(std::system_error(error)); } // NOLINT
  79. BOOST_OUTCOME_TEMPLATE(class Error)
  80. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_error_code_enum<std::decay_t<Error>>::value || std::is_error_condition_enum<std::decay_t<Error>>::value))
  81. inline void outcome_throw_as_system_error_with_payload(Error &&error, detail::std_enum_overload_tag /*unused*/ = detail::std_enum_overload_tag()) { BOOST_OUTCOME_THROW_EXCEPTION(std::system_error(make_error_code(error))); } // NOLINT
  82. } // namespace policy
  83. namespace trait
  84. {
  85. namespace detail
  86. {
  87. template <> struct _is_error_code_available<std::error_code>
  88. {
  89. // Shortcut this for lower build impact
  90. static constexpr bool value = true;
  91. using type = std::error_code;
  92. };
  93. } // namespace detail
  94. // std::error_code is an error type
  95. template <> struct is_error_type<std::error_code>
  96. {
  97. static constexpr bool value = true;
  98. };
  99. // For std::error_code, std::is_error_condition_enum<> is the trait we want.
  100. template <class Enum> struct is_error_type_enum<std::error_code, Enum>
  101. {
  102. static constexpr bool value = std::is_error_condition_enum<Enum>::value;
  103. };
  104. } // namespace trait
  105. BOOST_OUTCOME_V2_NAMESPACE_END
  106. #endif