include/boost/corosio/resolver_results.hpp

100.0% Lines (47/47) 100.0% Functions (18/18)
include/boost/corosio/resolver_results.hpp
Line Hits Source Code
1 //
2 // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/cppalliance/corosio
8 //
9
10 #ifndef BOOST_COROSIO_RESOLVER_RESULTS_HPP
11 #define BOOST_COROSIO_RESOLVER_RESULTS_HPP
12
13 #include <boost/corosio/detail/config.hpp>
14 #include <boost/corosio/endpoint.hpp>
15
16 #include <cstddef>
17 #include <memory>
18 #include <string>
19 #include <string_view>
20 #include <vector>
21
22 namespace boost::corosio {
23
24 /** A single entry produced by a resolver.
25
26 This class represents one resolved endpoint along with
27 the host and service names used in the query.
28
29 @par Thread Safety
30 Distinct objects: Safe.@n
31 Shared objects: Safe.
32 */
33 class resolver_entry
34 {
35 endpoint ep_;
36 std::string host_name_;
37 std::string service_name_;
38
39 public:
40 /** Default constructor. */
41 resolver_entry() = default;
42
43 /** Construct with endpoint, host name, and service name.
44
45 @param ep The resolved endpoint.
46 @param host The host name from the query.
47 @param service The service name from the query.
48 */
49 18 resolver_entry(endpoint ep, std::string_view host, std::string_view service)
50 18 : ep_(ep)
51 36 , host_name_(host)
52 36 , service_name_(service)
53 {
54 18 }
55
56 /** Get the endpoint. */
57 6 endpoint get_endpoint() const noexcept
58 {
59 6 return ep_;
60 }
61
62 /** Implicit conversion to endpoint. */
63 1 operator endpoint() const noexcept
64 {
65 1 return ep_;
66 }
67
68 /** Get the host name from the query. */
69 2 std::string const& host_name() const noexcept
70 {
71 2 return host_name_;
72 }
73
74 /** Get the service name from the query. */
75 2 std::string const& service_name() const noexcept
76 {
77 2 return service_name_;
78 }
79 };
80
81
82 /** A range of entries produced by a resolver.
83
84 This class holds the results of a DNS resolution query.
85 It provides a range interface for iterating over the
86 resolved endpoints.
87
88 @par Thread Safety
89 Distinct objects: Safe.@n
90 Shared objects: Safe (immutable after construction).
91 */
92 class resolver_results
93 {
94 public:
95 using value_type = resolver_entry;
96 using const_reference = value_type const&;
97 using reference = const_reference;
98 using const_iterator = std::vector<resolver_entry>::const_iterator;
99 using iterator = const_iterator;
100 using difference_type = std::ptrdiff_t;
101 using size_type = std::size_t;
102
103 private:
104 std::shared_ptr<std::vector<resolver_entry>> entries_;
105
106 public:
107 /** Default constructor creates an empty range. */
108 61 resolver_results() = default;
109
110 /** Construct from a vector of entries.
111
112 @param entries The resolved entries.
113 */
114 15 explicit resolver_results(std::vector<resolver_entry> entries)
115 15 : entries_(
116 15 std::make_shared<std::vector<resolver_entry>>(std::move(entries)))
117 {
118 15 }
119
120 /** Get the number of entries. */
121 11 size_type size() const noexcept
122 {
123 11 return entries_ ? entries_->size() : 0;
124 }
125
126 /** Check if the results are empty. */
127 11 bool empty() const noexcept
128 {
129 11 return !entries_ || entries_->empty();
130 }
131
132 /** Get an iterator to the first entry. */
133 9 const_iterator begin() const noexcept
134 {
135 9 if (entries_)
136 8 return entries_->begin();
137 1 return std::vector<resolver_entry>::const_iterator();
138 }
139
140 /** Get an iterator past the last entry. */
141 5 const_iterator end() const noexcept
142 {
143 5 if (entries_)
144 4 return entries_->end();
145 1 return std::vector<resolver_entry>::const_iterator();
146 }
147
148 /** Get an iterator to the first entry. */
149 1 const_iterator cbegin() const noexcept
150 {
151 1 return begin();
152 }
153
154 /** Get an iterator past the last entry. */
155 2 const_iterator cend() const noexcept
156 {
157 2 return end();
158 }
159
160 /** Swap with another results object. */
161 1 void swap(resolver_results& other) noexcept
162 {
163 1 entries_.swap(other.entries_);
164 1 }
165
166 /** Test for equality. */
167 friend bool
168 operator==(resolver_results const& a, resolver_results const& b) noexcept
169 {
170 return a.entries_ == b.entries_;
171 }
172
173 /** Test for inequality. */
174 friend bool
175 operator!=(resolver_results const& a, resolver_results const& b) noexcept
176 {
177 return !(a == b);
178 }
179 };
180
181
182 /** The result of a reverse DNS resolution.
183
184 This class holds the result of resolving an endpoint
185 into a hostname and service name.
186
187 @par Thread Safety
188 Distinct objects: Safe.@n
189 Shared objects: Safe.
190 */
191 class reverse_resolver_result
192 {
193 corosio::endpoint ep_;
194 std::string host_;
195 std::string service_;
196
197 public:
198 /** Default constructor. */
199 14 reverse_resolver_result() = default;
200
201 /** Construct with endpoint, host name, and service name.
202
203 @param ep The endpoint that was resolved.
204 @param host The resolved host name.
205 @param service The resolved service name.
206 */
207 9 reverse_resolver_result(
208 corosio::endpoint ep, std::string host, std::string service)
209 9 : ep_(ep)
210 9 , host_(std::move(host))
211 9 , service_(std::move(service))
212 {
213 9 }
214
215 /** Get the endpoint that was resolved. */
216 corosio::endpoint endpoint() const noexcept
217 {
218 return ep_;
219 }
220
221 /** Get the resolved host name. */
222 4 std::string const& host_name() const noexcept
223 {
224 4 return host_;
225 }
226
227 /** Get the resolved service name. */
228 4 std::string const& service_name() const noexcept
229 {
230 4 return service_;
231 }
232 };
233
234 } // namespace boost::corosio
235
236 #endif
237