posix_code.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* Proposed SG14 status_code
  2. (C) 2018-2024 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
  3. File Created: Feb 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_POSIX_CODE_HPP
  26. #define BOOST_OUTCOME_SYSTEM_ERROR2_POSIX_CODE_HPP
  27. #ifdef BOOST_OUTCOME_SYSTEM_ERROR2_NOT_POSIX
  28. #error <posix_code.hpp> is not includable when BOOST_OUTCOME_SYSTEM_ERROR2_NOT_POSIX is defined!
  29. #endif
  30. #include "quick_status_code_from_enum.hpp"
  31. #include <cstring> // for strchr and strerror_r
  32. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
  33. // Fix for issue #48 Issue compiling on arm-none-eabi (newlib) with GNU extensions off
  34. #if !defined(_MSC_VER) && !defined(__APPLE__)
  35. namespace detail
  36. {
  37. namespace avoid_string_include
  38. {
  39. #if defined(__GLIBC__) && !defined(__UCLIBC__)
  40. // This returns int for non-glibc strerror_r, but glibc's is particularly weird so we retain it
  41. extern "C" char *strerror_r(int errnum, char *buf, size_t buflen);
  42. #else
  43. extern "C" int strerror_r(int errnum, char *buf, size_t buflen);
  44. #endif
  45. } // namespace avoid_string_include
  46. } // namespace detail
  47. #endif
  48. class _posix_code_domain;
  49. //! A POSIX error code, those returned by `errno`.
  50. using posix_code = status_code<_posix_code_domain>;
  51. //! A specialisation of `status_error` for the POSIX error code domain.
  52. using posix_error = status_error<_posix_code_domain>;
  53. namespace mixins
  54. {
  55. template <class Base> struct mixin<Base, _posix_code_domain> : public Base
  56. {
  57. using Base::Base;
  58. //! Returns a `posix_code` for the current value of `errno`.
  59. static posix_code current() noexcept;
  60. };
  61. } // namespace mixins
  62. /*! The implementation of the domain for POSIX error codes, those returned by `errno`.
  63. */
  64. class _posix_code_domain : public status_code_domain
  65. {
  66. template <class DomainType> friend class status_code;
  67. using _base = status_code_domain;
  68. static _base::string_ref _make_string_ref(int c) noexcept
  69. {
  70. char buffer[1024] = "";
  71. #ifdef _WIN32
  72. strerror_s(buffer, sizeof(buffer), c);
  73. #elif defined(__GLIBC__) && !defined(__UCLIBC__) // handle glibc's weird strerror_r()
  74. char *s = detail::avoid_string_include::strerror_r(c, buffer, sizeof(buffer)); // NOLINT
  75. if(s != nullptr)
  76. {
  77. strncpy(buffer, s, sizeof(buffer) - 1); // NOLINT
  78. buffer[1023] = 0;
  79. }
  80. #elif !defined(__APPLE__)
  81. detail::avoid_string_include::strerror_r(c, buffer, sizeof(buffer));
  82. #else
  83. strerror_r(c, buffer, sizeof(buffer));
  84. #endif
  85. size_t length = strlen(buffer); // NOLINT
  86. auto *p = static_cast<char *>(malloc(length + 1)); // NOLINT
  87. if(p == nullptr)
  88. {
  89. return _base::string_ref("failed to get message from system");
  90. }
  91. memcpy(p, buffer, length + 1); // NOLINT
  92. return _base::atomic_refcounted_string_ref(p, length);
  93. }
  94. public:
  95. //! The value type of the POSIX code, which is an `int`
  96. using value_type = int;
  97. using _base::string_ref;
  98. //! Default constructor
  99. constexpr explicit _posix_code_domain(typename _base::unique_id_type id = 0xa59a56fe5f310933) noexcept
  100. : _base(id)
  101. {
  102. }
  103. _posix_code_domain(const _posix_code_domain &) = default;
  104. _posix_code_domain(_posix_code_domain &&) = default;
  105. _posix_code_domain &operator=(const _posix_code_domain &) = default;
  106. _posix_code_domain &operator=(_posix_code_domain &&) = default;
  107. ~_posix_code_domain() = default;
  108. //! Constexpr singleton getter. Returns constexpr posix_code_domain variable.
  109. static inline constexpr const _posix_code_domain &get();
  110. virtual string_ref name() const noexcept override { return string_ref("posix domain"); } // NOLINT
  111. virtual payload_info_t payload_info() const noexcept override
  112. {
  113. return {sizeof(value_type), sizeof(status_code_domain *) + sizeof(value_type),
  114. (alignof(value_type) > alignof(status_code_domain *)) ? alignof(value_type) : alignof(status_code_domain *)};
  115. }
  116. protected:
  117. virtual bool _do_failure(const status_code<void> &code) const noexcept override // NOLINT
  118. {
  119. assert(code.domain() == *this); // NOLINT
  120. return static_cast<const posix_code &>(code).value() != 0; // NOLINT
  121. }
  122. virtual bool _do_equivalent(const status_code<void> &code1, const status_code<void> &code2) const noexcept override // NOLINT
  123. {
  124. assert(code1.domain() == *this); // NOLINT
  125. const auto &c1 = static_cast<const posix_code &>(code1); // NOLINT
  126. if(code2.domain() == *this)
  127. {
  128. const auto &c2 = static_cast<const posix_code &>(code2); // NOLINT
  129. return c1.value() == c2.value();
  130. }
  131. if(code2.domain() == generic_code_domain)
  132. {
  133. const auto &c2 = static_cast<const generic_code &>(code2); // NOLINT
  134. if(static_cast<int>(c2.value()) == c1.value())
  135. {
  136. return true;
  137. }
  138. }
  139. return false;
  140. }
  141. virtual generic_code _generic_code(const status_code<void> &code) const noexcept override // NOLINT
  142. {
  143. assert(code.domain() == *this); // NOLINT
  144. const auto &c = static_cast<const posix_code &>(code); // NOLINT
  145. return generic_code(static_cast<errc>(c.value()));
  146. }
  147. virtual string_ref _do_message(const status_code<void> &code) const noexcept override // NOLINT
  148. {
  149. assert(code.domain() == *this); // NOLINT
  150. const auto &c = static_cast<const posix_code &>(code); // NOLINT
  151. return _make_string_ref(c.value());
  152. }
  153. #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
  154. BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN virtual void _do_throw_exception(const status_code<void> &code) const override // NOLINT
  155. {
  156. assert(code.domain() == *this); // NOLINT
  157. const auto &c = static_cast<const posix_code &>(code); // NOLINT
  158. throw status_error<_posix_code_domain>(c);
  159. }
  160. #endif
  161. };
  162. //! A constexpr source variable for the POSIX code domain, which is that of `errno`. Returned by `_posix_code_domain::get()`.
  163. constexpr _posix_code_domain posix_code_domain;
  164. inline constexpr const _posix_code_domain &_posix_code_domain::get()
  165. {
  166. return posix_code_domain;
  167. }
  168. namespace mixins
  169. {
  170. template <class Base> inline posix_code mixin<Base, _posix_code_domain>::current() noexcept
  171. {
  172. return posix_code(errno);
  173. }
  174. } // namespace mixins
  175. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
  176. #endif