]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* Proposed SG14 status_code |
2 | (C) 2020 Niall Douglas <http://www.nedproductions.biz/> (5 commits) | |
3 | File Created: Jan 2020 | |
4 | ||
5 | ||
6 | Boost Software License - Version 1.0 - August 17th, 2003 | |
7 | ||
8 | Permission is hereby granted, free of charge, to any person or organization | |
9 | obtaining a copy of the software and accompanying documentation covered by | |
10 | this license (the "Software") to use, reproduce, display, distribute, | |
11 | execute, and transmit the Software, and to prepare derivative works of the | |
12 | Software, and to permit third-parties to whom the Software is furnished to | |
13 | do so, all subject to the following: | |
14 | ||
15 | The copyright notices in the Software and this entire statement, including | |
16 | the above license grant, this restriction and the following disclaimer, | |
17 | must be included in all copies of the Software, in whole or in part, and | |
18 | all derivative works of the Software, unless such copies or derivative | |
19 | works are solely in the form of machine-executable object code generated by | |
20 | a source language processor. | |
21 | ||
22 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
23 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
24 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT | |
25 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE | |
26 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, | |
27 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
28 | DEALINGS IN THE SOFTWARE. | |
29 | */ | |
30 | ||
31 | #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_GETADDRINFO_CODE_HPP | |
32 | #define BOOST_OUTCOME_SYSTEM_ERROR2_GETADDRINFO_CODE_HPP | |
33 | ||
20effc67 | 34 | #include "quick_status_code_from_enum.hpp" |
f67539c2 TL |
35 | |
36 | #ifdef _WIN32 | |
37 | #error Not available for Microsoft Windows | |
38 | #else | |
39 | #include <netdb.h> | |
40 | #include <sys/socket.h> | |
41 | #include <sys/types.h> | |
42 | #endif | |
43 | ||
44 | BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN | |
45 | ||
46 | class _getaddrinfo_code_domain; | |
47 | //! A getaddrinfo error code, those returned by `getaddrinfo()`. | |
48 | using getaddrinfo_code = status_code<_getaddrinfo_code_domain>; | |
49 | //! A specialisation of `status_error` for the `getaddrinfo()` error code domain. | |
50 | using getaddrinfo_error = status_error<_getaddrinfo_code_domain>; | |
51 | ||
52 | /*! The implementation of the domain for `getaddrinfo()` error codes, those returned by `getaddrinfo()`. | |
53 | */ | |
54 | class _getaddrinfo_code_domain : public status_code_domain | |
55 | { | |
56 | template <class DomainType> friend class status_code; | |
57 | template <class StatusCode> friend class detail::indirecting_domain; | |
58 | using _base = status_code_domain; | |
59 | ||
60 | public: | |
61 | //! The value type of the `getaddrinfo()` code, which is an `int` | |
62 | using value_type = int; | |
63 | using _base::string_ref; | |
64 | ||
65 | //! Default constructor | |
66 | constexpr explicit _getaddrinfo_code_domain(typename _base::unique_id_type id = 0x5b24b2de470ff7b6) noexcept | |
67 | : _base(id) | |
68 | { | |
69 | } | |
70 | _getaddrinfo_code_domain(const _getaddrinfo_code_domain &) = default; | |
71 | _getaddrinfo_code_domain(_getaddrinfo_code_domain &&) = default; | |
72 | _getaddrinfo_code_domain &operator=(const _getaddrinfo_code_domain &) = default; | |
73 | _getaddrinfo_code_domain &operator=(_getaddrinfo_code_domain &&) = default; | |
74 | ~_getaddrinfo_code_domain() = default; | |
75 | ||
76 | //! Constexpr singleton getter. Returns constexpr getaddrinfo_code_domain variable. | |
77 | static inline constexpr const _getaddrinfo_code_domain &get(); | |
78 | ||
79 | virtual string_ref name() const noexcept override { return string_ref("getaddrinfo() domain"); } // NOLINT | |
80 | protected: | |
81 | virtual bool _do_failure(const status_code<void> &code) const noexcept override // NOLINT | |
82 | { | |
83 | assert(code.domain() == *this); // NOLINT | |
84 | return static_cast<const getaddrinfo_code &>(code).value() != 0; // NOLINT | |
85 | } | |
86 | virtual bool _do_equivalent(const status_code<void> &code1, const status_code<void> &code2) const noexcept override // NOLINT | |
87 | { | |
88 | assert(code1.domain() == *this); // NOLINT | |
89 | const auto &c1 = static_cast<const getaddrinfo_code &>(code1); // NOLINT | |
90 | if(code2.domain() == *this) | |
91 | { | |
92 | const auto &c2 = static_cast<const getaddrinfo_code &>(code2); // NOLINT | |
93 | return c1.value() == c2.value(); | |
94 | } | |
95 | return false; | |
96 | } | |
97 | virtual generic_code _generic_code(const status_code<void> &code) const noexcept override // NOLINT | |
98 | { | |
99 | assert(code.domain() == *this); // NOLINT | |
100 | const auto &c = static_cast<const getaddrinfo_code &>(code); // NOLINT | |
101 | switch(c.value()) | |
102 | { | |
103 | #ifdef EAI_ADDRFAMILY | |
104 | case EAI_ADDRFAMILY: | |
105 | return errc::no_such_device_or_address; | |
106 | #endif | |
107 | case EAI_FAIL: | |
108 | return errc::io_error; | |
109 | case EAI_MEMORY: | |
110 | return errc::not_enough_memory; | |
111 | #ifdef EAI_NODATA | |
112 | case EAI_NODATA: | |
113 | return errc::no_such_device_or_address; | |
114 | #endif | |
115 | case EAI_NONAME: | |
116 | return errc::no_such_device_or_address; | |
117 | #ifdef EAI_OVERFLOW | |
118 | case EAI_OVERFLOW: | |
119 | return errc::argument_list_too_long; | |
120 | #endif | |
121 | case EAI_BADFLAGS: // fallthrough | |
122 | case EAI_SERVICE: | |
123 | return errc::invalid_argument; | |
124 | case EAI_FAMILY: // fallthrough | |
125 | case EAI_SOCKTYPE: | |
126 | return errc::operation_not_supported; | |
127 | case EAI_AGAIN: // fallthrough | |
128 | case EAI_SYSTEM: | |
129 | return errc::resource_unavailable_try_again; | |
130 | default: | |
131 | return errc::unknown; | |
132 | } | |
133 | } | |
134 | virtual string_ref _do_message(const status_code<void> &code) const noexcept override // NOLINT | |
135 | { | |
136 | assert(code.domain() == *this); // NOLINT | |
137 | const auto &c = static_cast<const getaddrinfo_code &>(code); // NOLINT | |
138 | return string_ref(gai_strerror(c.value())); | |
139 | } | |
140 | #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE) | |
141 | BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN virtual void _do_throw_exception(const status_code<void> &code) const override // NOLINT | |
142 | { | |
143 | assert(code.domain() == *this); // NOLINT | |
144 | const auto &c = static_cast<const getaddrinfo_code &>(code); // NOLINT | |
145 | throw status_error<_getaddrinfo_code_domain>(c); | |
146 | } | |
147 | #endif | |
148 | }; | |
149 | //! A constexpr source variable for the `getaddrinfo()` code domain, which is that of `getaddrinfo()`. Returned by `_getaddrinfo_code_domain::get()`. | |
150 | constexpr _getaddrinfo_code_domain getaddrinfo_code_domain; | |
151 | inline constexpr const _getaddrinfo_code_domain &_getaddrinfo_code_domain::get() | |
152 | { | |
153 | return getaddrinfo_code_domain; | |
154 | } | |
155 | ||
156 | BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END | |
157 | ||
158 | #endif |