std_error_code.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* Proposed SG14 status_code
  2. (C) 2018-2019 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
  3. File Created: Aug 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_SYSTEM_ERROR2_STD_ERROR_CODE_HPP
  26. #define BOOST_OUTCOME_SYSTEM_ERROR2_STD_ERROR_CODE_HPP
  27. #include "posix_code.hpp"
  28. #if defined(_WIN32) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
  29. #include "win32_code.hpp"
  30. #endif
  31. #include <system_error>
  32. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
  33. namespace detail
  34. {
  35. struct make_std_categories
  36. {
  37. static const std::error_category &generic_category() { return std::generic_category(); }
  38. static const std::error_category &system_category() { return std::system_category(); }
  39. };
  40. }
  41. template <class error_code_type, class make_category_types> class _error_code_domain;
  42. //! A wrapper of `std::error_code`.
  43. using std_error_code = status_code<_error_code_domain<std::error_code, detail::make_std_categories>>;
  44. /*! The implementation of the domain for `std::error_code` error codes.
  45. */
  46. template <class error_code_type, class make_categories_type> class _error_code_domain : public status_code_domain
  47. {
  48. template <class DomainType> friend class status_code;
  49. template <class StatusCode> friend class detail::indirecting_domain;
  50. using _base = status_code_domain;
  51. using _status_code = status_code<_error_code_domain>;
  52. static _base::string_ref _make_string_ref(error_code_type c) noexcept
  53. {
  54. try
  55. {
  56. std::string msg = c.message();
  57. auto *p = static_cast<char *>(malloc(msg.size() + 1)); // NOLINT
  58. if(p == nullptr)
  59. {
  60. return _base::string_ref("failed to allocate message");
  61. }
  62. memcpy(p, msg.c_str(), msg.size() + 1);
  63. return _base::atomic_refcounted_string_ref(p, msg.size());
  64. }
  65. catch(...)
  66. {
  67. return _base::string_ref("failed to allocate message");
  68. }
  69. }
  70. public:
  71. //! The value type of the `std::error_code` code, which is the `std::error_code`
  72. using value_type = error_code_type;
  73. using _base::string_ref;
  74. //! Default constructor
  75. constexpr explicit _error_code_domain(typename _base::unique_id_type id = 0x223a160d20de97b4) noexcept : _base(id) {}
  76. _error_code_domain(const _error_code_domain &) = default;
  77. _error_code_domain(_error_code_domain &&) = default;
  78. _error_code_domain &operator=(const _error_code_domain &) = default;
  79. _error_code_domain &operator=(_error_code_domain &&) = default;
  80. ~_error_code_domain() = default;
  81. //! Constexpr singleton getter. Returns constexpr error_code_domain variable.
  82. static inline constexpr const _error_code_domain &get();
  83. virtual string_ref name() const noexcept override { return string_ref("error_code compatibility domain"); } // NOLINT
  84. protected:
  85. virtual bool _do_failure(const status_code<void> &code) const noexcept override final // NOLINT
  86. {
  87. assert(code.domain() == *this);
  88. return static_cast<const _status_code &>(code).value().value() != 0; // NOLINT
  89. }
  90. virtual bool _do_equivalent(const status_code<void> &code1, const status_code<void> &code2) const noexcept override final // NOLINT
  91. {
  92. assert(code1.domain() == *this);
  93. const auto &c1 = static_cast<const _status_code &>(code1); // NOLINT
  94. const auto &cat1 = c1.value().category();
  95. // Are we comparing to another wrapped error_code?
  96. if(code2.domain() == *this)
  97. {
  98. const auto &c2 = static_cast<const _status_code &>(code2); // NOLINT
  99. const auto &cat2 = c2.value().category();
  100. // If the error code categories are identical, do literal comparison
  101. if(cat1 == cat2)
  102. {
  103. return c1.value().value() == c2.value().value();
  104. }
  105. // Otherwise fall back onto the _generic_code comparison, which uses default_error_condition()
  106. return false;
  107. }
  108. // Am I an error code with generic category?
  109. const auto &generic_category = make_categories_type::generic_category();
  110. if(cat1 == generic_category)
  111. {
  112. // Convert to generic code, and compare that
  113. generic_code _c1(static_cast<errc>(c1.value().value()));
  114. return _c1 == code2;
  115. }
  116. // Am I an error code with system category?
  117. const auto &system_category = make_categories_type::system_category();
  118. if(cat1 == system_category)
  119. {
  120. // Convert to POSIX or Win32 code, and compare that
  121. #ifdef _WIN32
  122. win32_code _c1((win32::DWORD) c1.value().value());
  123. #else
  124. posix_code _c1(c1.value().value());
  125. #endif
  126. return _c1 == code2;
  127. }
  128. return false;
  129. }
  130. virtual generic_code _generic_code(const status_code<void> &code) const noexcept override final // NOLINT
  131. {
  132. assert(code.domain() == *this);
  133. const auto &c = static_cast<const _status_code &>(code); // NOLINT
  134. // Ask my embedded error code for its mapping to std::errc, which is a subset of our generic_code errc.
  135. return generic_code(static_cast<errc>(c.value().default_error_condition().value()));
  136. }
  137. virtual string_ref _do_message(const status_code<void> &code) const noexcept override // NOLINT
  138. {
  139. assert(code.domain() == *this);
  140. const auto &c = static_cast<const _status_code &>(code); // NOLINT
  141. return _make_string_ref(c.value());
  142. }
  143. #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
  144. BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN virtual void _do_throw_exception(const status_code<void> &code) const override // NOLINT
  145. {
  146. assert(code.domain() == *this);
  147. const auto &c = static_cast<const _status_code &>(code); // NOLINT
  148. throw std::system_error(c.value());
  149. }
  150. #endif
  151. };
  152. //! A constexpr source variable for the `std::error_code` code domain. Returned by `_error_code_domain<error_code_type, detail::make_std_categoriesy>::get()`.
  153. constexpr _error_code_domain<std::error_code, detail::make_std_categories> std_error_code_domain;
  154. template <class error_code_type, class make_categories_type> inline constexpr const _error_code_domain<error_code_type, make_categories_type> &_error_code_domain<error_code_type, make_categories_type>::get()
  155. {
  156. return std_error_code_domain;
  157. }
  158. // Enable implicit construction of `std_error_code` from `std::error_code`.
  159. inline std_error_code make_status_code(std::error_code c) noexcept
  160. {
  161. return std_error_code(in_place, c);
  162. }
  163. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
  164. #endif