TLA Line data Source code
1 : //
2 : // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3 : // Copyright (c) 2026 Michael Vandeberg
4 : //
5 : // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 : // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 : //
8 : // Official repository: https://github.com/cppalliance/corosio
9 : //
10 :
11 : #ifndef BOOST_COROSIO_RESOLVER_RESULTS_HPP
12 : #define BOOST_COROSIO_RESOLVER_RESULTS_HPP
13 :
14 : #include <boost/corosio/detail/config.hpp>
15 : #include <boost/corosio/endpoint.hpp>
16 :
17 : #include <string>
18 : #include <string_view>
19 : #include <vector>
20 :
21 : namespace boost::corosio {
22 :
23 : /** A single entry produced by a resolver.
24 :
25 : This class represents one resolved endpoint along with
26 : the host and service names used in the query.
27 :
28 : @par Thread Safety
29 : Distinct objects: Safe.@n
30 : Shared objects: Safe.
31 : */
32 : class resolver_entry
33 : {
34 : endpoint ep_;
35 : std::string host_name_;
36 : std::string service_name_;
37 :
38 : public:
39 : /// Construct a default empty entry.
40 : resolver_entry() = default;
41 :
42 : /** Construct with endpoint, host name, and service name.
43 :
44 : @param ep The resolved endpoint.
45 : @param host The host name from the query.
46 : @param service The service name from the query.
47 : */
48 HIT 21 : resolver_entry(endpoint ep, std::string_view host, std::string_view service)
49 21 : : ep_(ep)
50 42 : , host_name_(host)
51 42 : , service_name_(service)
52 : {
53 21 : }
54 :
55 : /// Return the resolved endpoint.
56 6 : endpoint get_endpoint() const noexcept
57 : {
58 6 : return ep_;
59 : }
60 :
61 : /// Convert to endpoint.
62 1 : operator endpoint() const noexcept
63 : {
64 1 : return ep_;
65 : }
66 :
67 : /// Return the host name from the query.
68 2 : std::string const& host_name() const noexcept
69 : {
70 2 : return host_name_;
71 : }
72 :
73 : /// Return the service name from the query.
74 2 : std::string const& service_name() const noexcept
75 : {
76 2 : return service_name_;
77 : }
78 : };
79 :
80 : /** A range of entries produced by a resolver.
81 :
82 : This is an alias for `std::vector<resolver_entry>`: a contiguous,
83 : owning range of the endpoints resolved by a query. It supports the
84 : full `std::vector` interface (iteration, `size()`, `empty()`, etc.).
85 :
86 : @note Copying a `resolver_results` deep-copies every entry, and each
87 : entry owns two `std::string`s (the host and service names). When you
88 : want to hand a result to a sink that takes the range by value — such
89 : as `corosio::connect` — pass an rvalue (`std::move(results)`) or use
90 : the iterator-based `connect` overloads to avoid the copy.
91 :
92 : @par Thread Safety
93 : Distinct objects: Safe.@n
94 : Shared objects: Unsafe.
95 : */
96 : using resolver_results = std::vector<resolver_entry>;
97 :
98 : /** The result of a reverse DNS resolution.
99 :
100 : This class holds the result of resolving an endpoint
101 : into a hostname and service name.
102 :
103 : @par Thread Safety
104 : Distinct objects: Safe.@n
105 : Shared objects: Safe.
106 : */
107 : class reverse_resolver_result
108 : {
109 : corosio::endpoint ep_;
110 : std::string host_;
111 : std::string service_;
112 :
113 : public:
114 : /// Construct a default empty result.
115 19 : reverse_resolver_result() = default;
116 :
117 : /** Construct with endpoint, host name, and service name.
118 :
119 : @param ep The endpoint that was resolved.
120 : @param host The resolved host name.
121 : @param service The resolved service name.
122 : */
123 10 : reverse_resolver_result(
124 : corosio::endpoint ep, std::string host, std::string service)
125 10 : : ep_(ep)
126 10 : , host_(std::move(host))
127 10 : , service_(std::move(service))
128 : {
129 10 : }
130 :
131 : /// Return the endpoint that was resolved.
132 : corosio::endpoint endpoint() const noexcept
133 : {
134 : return ep_;
135 : }
136 :
137 : /// Return the resolved host name.
138 5 : std::string const& host_name() const noexcept
139 : {
140 5 : return host_;
141 : }
142 :
143 : /// Return the resolved service name.
144 4 : std::string const& service_name() const noexcept
145 : {
146 4 : return service_;
147 : }
148 : };
149 :
150 : } // namespace boost::corosio
151 :
152 : #endif
|