getaddrinfo_code.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* Proposed SG14 status_code
  2. (C) 2020-2021 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
  3. File Created: Jan 2020
  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_GETADDRINFO_CODE_HPP
  26. #define BOOST_OUTCOME_SYSTEM_ERROR2_GETADDRINFO_CODE_HPP
  27. #include "quick_status_code_from_enum.hpp"
  28. #ifdef _WIN32
  29. #error Not available for Microsoft Windows
  30. #else
  31. #include <netdb.h>
  32. #include <sys/socket.h>
  33. #include <sys/types.h>
  34. #endif
  35. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
  36. class _getaddrinfo_code_domain;
  37. //! A getaddrinfo error code, those returned by `getaddrinfo()`.
  38. using getaddrinfo_code = status_code<_getaddrinfo_code_domain>;
  39. //! A specialisation of `status_error` for the `getaddrinfo()` error code domain.
  40. using getaddrinfo_error = status_error<_getaddrinfo_code_domain>;
  41. /*! The implementation of the domain for `getaddrinfo()` error codes, those returned by `getaddrinfo()`.
  42. */
  43. class _getaddrinfo_code_domain : public status_code_domain
  44. {
  45. template <class DomainType> friend class status_code;
  46. template <class StatusCode> friend class detail::indirecting_domain;
  47. using _base = status_code_domain;
  48. public:
  49. //! The value type of the `getaddrinfo()` code, which is an `int`
  50. using value_type = int;
  51. using _base::string_ref;
  52. //! Default constructor
  53. constexpr explicit _getaddrinfo_code_domain(typename _base::unique_id_type id = 0x5b24b2de470ff7b6) noexcept
  54. : _base(id)
  55. {
  56. }
  57. _getaddrinfo_code_domain(const _getaddrinfo_code_domain &) = default;
  58. _getaddrinfo_code_domain(_getaddrinfo_code_domain &&) = default;
  59. _getaddrinfo_code_domain &operator=(const _getaddrinfo_code_domain &) = default;
  60. _getaddrinfo_code_domain &operator=(_getaddrinfo_code_domain &&) = default;
  61. ~_getaddrinfo_code_domain() = default;
  62. //! Constexpr singleton getter. Returns constexpr getaddrinfo_code_domain variable.
  63. static inline constexpr const _getaddrinfo_code_domain &get();
  64. virtual string_ref name() const noexcept override { return string_ref("getaddrinfo() domain"); } // NOLINT
  65. protected:
  66. virtual bool _do_failure(const status_code<void> &code) const noexcept override // NOLINT
  67. {
  68. assert(code.domain() == *this); // NOLINT
  69. return static_cast<const getaddrinfo_code &>(code).value() != 0; // NOLINT
  70. }
  71. virtual bool _do_equivalent(const status_code<void> &code1, const status_code<void> &code2) const noexcept override // NOLINT
  72. {
  73. assert(code1.domain() == *this); // NOLINT
  74. const auto &c1 = static_cast<const getaddrinfo_code &>(code1); // NOLINT
  75. if(code2.domain() == *this)
  76. {
  77. const auto &c2 = static_cast<const getaddrinfo_code &>(code2); // NOLINT
  78. return c1.value() == c2.value();
  79. }
  80. return false;
  81. }
  82. virtual generic_code _generic_code(const status_code<void> &code) const noexcept override // NOLINT
  83. {
  84. assert(code.domain() == *this); // NOLINT
  85. const auto &c = static_cast<const getaddrinfo_code &>(code); // NOLINT
  86. switch(c.value())
  87. {
  88. #ifdef EAI_ADDRFAMILY
  89. case EAI_ADDRFAMILY:
  90. return errc::no_such_device_or_address;
  91. #endif
  92. case EAI_FAIL:
  93. return errc::io_error;
  94. case EAI_MEMORY:
  95. return errc::not_enough_memory;
  96. #ifdef EAI_NODATA
  97. case EAI_NODATA:
  98. return errc::no_such_device_or_address;
  99. #endif
  100. case EAI_NONAME:
  101. return errc::no_such_device_or_address;
  102. #ifdef EAI_OVERFLOW
  103. case EAI_OVERFLOW:
  104. return errc::argument_list_too_long;
  105. #endif
  106. case EAI_BADFLAGS: // fallthrough
  107. case EAI_SERVICE:
  108. return errc::invalid_argument;
  109. case EAI_FAMILY: // fallthrough
  110. case EAI_SOCKTYPE:
  111. return errc::operation_not_supported;
  112. case EAI_AGAIN: // fallthrough
  113. case EAI_SYSTEM:
  114. return errc::resource_unavailable_try_again;
  115. default:
  116. return errc::unknown;
  117. }
  118. }
  119. virtual string_ref _do_message(const status_code<void> &code) const noexcept override // NOLINT
  120. {
  121. assert(code.domain() == *this); // NOLINT
  122. const auto &c = static_cast<const getaddrinfo_code &>(code); // NOLINT
  123. return string_ref(gai_strerror(c.value()));
  124. }
  125. #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
  126. BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN virtual void _do_throw_exception(const status_code<void> &code) const override // NOLINT
  127. {
  128. assert(code.domain() == *this); // NOLINT
  129. const auto &c = static_cast<const getaddrinfo_code &>(code); // NOLINT
  130. throw status_error<_getaddrinfo_code_domain>(c);
  131. }
  132. #endif
  133. };
  134. //! A constexpr source variable for the `getaddrinfo()` code domain, which is that of `getaddrinfo()`. Returned by `_getaddrinfo_code_domain::get()`.
  135. constexpr _getaddrinfo_code_domain getaddrinfo_code_domain;
  136. inline constexpr const _getaddrinfo_code_domain &_getaddrinfo_code_domain::get()
  137. {
  138. return getaddrinfo_code_domain;
  139. }
  140. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
  141. #endif