libs/corosio/include/boost/corosio/resolver_results.hpp

100.0% Lines (47/47) 100.0% Functions (18/18) 91.7% Branches (11/12)
libs/corosio/include/boost/corosio/resolver_results.hpp
Line Branch Hits Source Code
1 //
2 // Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot 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(
50 endpoint ep,
51 std::string_view host,
52 std::string_view service)
53 18 : ep_(ep)
54
1/1
✓ Branch 1 taken 18 times.
36 , host_name_(host)
55
1/1
✓ Branch 1 taken 18 times.
36 , service_name_(service)
56 {
57 18 }
58
59 /** Get the endpoint. */
60 endpoint
61 6 get_endpoint() const noexcept
62 {
63 6 return ep_;
64 }
65
66 /** Implicit conversion to endpoint. */
67 1 operator endpoint() const noexcept
68 {
69 1 return ep_;
70 }
71
72 /** Get the host name from the query. */
73 std::string const&
74 2 host_name() const noexcept
75 {
76 2 return host_name_;
77 }
78
79 /** Get the service name from the query. */
80 std::string const&
81 2 service_name() const noexcept
82 {
83 2 return service_name_;
84 }
85 };
86
87 //------------------------------------------------------------------------------
88
89 /** A range of entries produced by a resolver.
90
91 This class holds the results of a DNS resolution query.
92 It provides a range interface for iterating over the
93 resolved endpoints.
94
95 @par Thread Safety
96 Distinct objects: Safe.@n
97 Shared objects: Safe (immutable after construction).
98 */
99 class resolver_results
100 {
101 public:
102 using value_type = resolver_entry;
103 using const_reference = value_type const&;
104 using reference = const_reference;
105 using const_iterator = std::vector<resolver_entry>::const_iterator;
106 using iterator = const_iterator;
107 using difference_type = std::ptrdiff_t;
108 using size_type = std::size_t;
109
110 private:
111 std::shared_ptr<std::vector<resolver_entry>> entries_;
112
113 public:
114 /** Default constructor creates an empty range. */
115 61 resolver_results() = default;
116
117 /** Construct from a vector of entries.
118
119 @param entries The resolved entries.
120 */
121 explicit
122 15 resolver_results(std::vector<resolver_entry> entries)
123 15 : entries_(std::make_shared<std::vector<resolver_entry>>(
124 15 std::move(entries)))
125 {
126 15 }
127
128 /** Get the number of entries. */
129 size_type
130 11 size() const noexcept
131 {
132
2/2
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 1 time.
11 return entries_ ? entries_->size() : 0;
133 }
134
135 /** Check if the results are empty. */
136 bool
137 11 empty() const noexcept
138 {
139
3/4
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 9 times.
11 return !entries_ || entries_->empty();
140 }
141
142 /** Get an iterator to the first entry. */
143 const_iterator
144 9 begin() const noexcept
145 {
146
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 1 time.
9 if (entries_)
147 8 return entries_->begin();
148 1 return std::vector<resolver_entry>::const_iterator();
149 }
150
151 /** Get an iterator past the last entry. */
152 const_iterator
153 5 end() const noexcept
154 {
155
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 time.
5 if (entries_)
156 4 return entries_->end();
157 1 return std::vector<resolver_entry>::const_iterator();
158 }
159
160 /** Get an iterator to the first entry. */
161 const_iterator
162 1 cbegin() const noexcept
163 {
164 1 return begin();
165 }
166
167 /** Get an iterator past the last entry. */
168 const_iterator
169 2 cend() const noexcept
170 {
171 2 return end();
172 }
173
174 /** Swap with another results object. */
175 void
176 1 swap(resolver_results& other) noexcept
177 {
178 1 entries_.swap(other.entries_);
179 1 }
180
181 /** Test for equality. */
182 friend
183 bool
184 operator==(
185 resolver_results const& a,
186 resolver_results const& b) noexcept
187 {
188 return a.entries_ == b.entries_;
189 }
190
191 /** Test for inequality. */
192 friend
193 bool
194 operator!=(
195 resolver_results const& a,
196 resolver_results const& b) noexcept
197 {
198 return !(a == b);
199 }
200 };
201
202 //------------------------------------------------------------------------------
203
204 /** The result of a reverse DNS resolution.
205
206 This class holds the result of resolving an endpoint
207 into a hostname and service name.
208
209 @par Thread Safety
210 Distinct objects: Safe.@n
211 Shared objects: Safe.
212 */
213 class reverse_resolver_result
214 {
215 corosio::endpoint ep_;
216 std::string host_;
217 std::string service_;
218
219 public:
220 /** Default constructor. */
221 14 reverse_resolver_result() = default;
222
223 /** Construct with endpoint, host name, and service name.
224
225 @param ep The endpoint that was resolved.
226 @param host The resolved host name.
227 @param service The resolved service name.
228 */
229 9 reverse_resolver_result(
230 corosio::endpoint ep,
231 std::string host,
232 std::string service)
233 9 : ep_(ep)
234 9 , host_(std::move(host))
235 9 , service_(std::move(service))
236 {
237 9 }
238
239 /** Get the endpoint that was resolved. */
240 corosio::endpoint
241 endpoint() const noexcept
242 {
243 return ep_;
244 }
245
246 /** Get the resolved host name. */
247 std::string const&
248 4 host_name() const noexcept
249 {
250 4 return host_;
251 }
252
253 /** Get the resolved service name. */
254 std::string const&
255 4 service_name() const noexcept
256 {
257 4 return service_;
258 }
259 };
260
261 } // namespace boost::corosio
262
263 #endif
264