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