Clementine
basic_resolver_results.hpp
1 //
2 // ip/basic_resolver_results.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef ASIO_IP_BASIC_RESOLVER_RESULTS_HPP
12 #define ASIO_IP_BASIC_RESOLVER_RESULTS_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include "asio/detail/config.hpp"
19 #include <cstddef>
20 #include <cstring>
21 #include "asio/detail/socket_ops.hpp"
22 #include "asio/detail/socket_types.hpp"
23 #include "asio/ip/basic_resolver_iterator.hpp"
24 
25 #if defined(ASIO_WINDOWS_RUNTIME)
26 # include "asio/detail/winrt_utils.hpp"
27 #endif // defined(ASIO_WINDOWS_RUNTIME)
28 
29 #include "asio/detail/push_options.hpp"
30 
31 namespace asio {
32 namespace ip {
33 
35 
49 template <typename InternetProtocol>
51 #if !defined(ASIO_NO_DEPRECATED)
52  : public basic_resolver_iterator<InternetProtocol>
53 #else // !defined(ASIO_NO_DEPRECATED)
54  : private basic_resolver_iterator<InternetProtocol>
55 #endif // !defined(ASIO_NO_DEPRECATED)
56 {
57 public:
59  typedef InternetProtocol protocol_type;
60 
62  typedef typename protocol_type::endpoint endpoint_type;
63 
66 
68  typedef const value_type& const_reference;
69 
71  typedef value_type& reference;
72 
75 
77  typedef const_iterator iterator;
78 
80  typedef std::ptrdiff_t difference_type;
81 
83  typedef std::size_t size_type;
84 
87  {
88  }
89 
92  : basic_resolver_iterator<InternetProtocol>(other)
93  {
94  }
95 
96 #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
100  ASIO_MOVE_CAST(basic_resolver_results)(other))
101  {
102  }
103 #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
104 
107  {
109  return *this;
110  }
111 
112 #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
115  {
117  ASIO_MOVE_CAST(basic_resolver_results)(other));
118  return *this;
119  }
120 #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
121 
122 #if !defined(GENERATING_DOCUMENTATION)
123  // Create results from an addrinfo list returned by getaddrinfo.
124  static basic_resolver_results create(
125  asio::detail::addrinfo_type* address_info,
126  const std::string& host_name, const std::string& service_name)
127  {
128  basic_resolver_results results;
129  if (!address_info)
130  return results;
131 
132  std::string actual_host_name = host_name;
133  if (address_info->ai_canonname)
134  actual_host_name = address_info->ai_canonname;
135 
136  results.values_.reset(new values_type);
137 
138  while (address_info)
139  {
140  if (address_info->ai_family == ASIO_OS_DEF(AF_INET)
141  || address_info->ai_family == ASIO_OS_DEF(AF_INET6))
142  {
143  using namespace std; // For memcpy.
144  typename InternetProtocol::endpoint endpoint;
145  endpoint.resize(static_cast<std::size_t>(address_info->ai_addrlen));
146  memcpy(endpoint.data(), address_info->ai_addr,
147  address_info->ai_addrlen);
148  results.values_->push_back(
150  actual_host_name, service_name));
151  }
152  address_info = address_info->ai_next;
153  }
154 
155  return results;
156  }
157 
158  // Create results from an endpoint, host name and service name.
159  static basic_resolver_results create(const endpoint_type& endpoint,
160  const std::string& host_name, const std::string& service_name)
161  {
162  basic_resolver_results results;
163  results.values_.reset(new values_type);
164  results.values_->push_back(
166  endpoint, host_name, service_name));
167  return results;
168  }
169 
170  // Create results from a sequence of endpoints, host and service name.
171  template <typename EndpointIterator>
172  static basic_resolver_results create(
173  EndpointIterator begin, EndpointIterator end,
174  const std::string& host_name, const std::string& service_name)
175  {
176  basic_resolver_results results;
177  if (begin != end)
178  {
179  results.values_.reset(new values_type);
180  for (EndpointIterator ep_iter = begin; ep_iter != end; ++ep_iter)
181  {
182  results.values_->push_back(
184  *ep_iter, host_name, service_name));
185  }
186  }
187  return results;
188  }
189 
190 # if defined(ASIO_WINDOWS_RUNTIME)
191  // Create results from a Windows Runtime list of EndpointPair objects.
192  static basic_resolver_results create(
193  Windows::Foundation::Collections::IVectorView<
194  Windows::Networking::EndpointPair^>^ endpoints,
195  const asio::detail::addrinfo_type& hints,
196  const std::string& host_name, const std::string& service_name)
197  {
198  basic_resolver_results results;
199  if (endpoints->Size)
200  {
201  results.values_.reset(new values_type);
202  for (unsigned int i = 0; i < endpoints->Size; ++i)
203  {
204  auto pair = endpoints->GetAt(i);
205 
206  if (hints.ai_family == ASIO_OS_DEF(AF_INET)
207  && pair->RemoteHostName->Type
208  != Windows::Networking::HostNameType::Ipv4)
209  continue;
210 
211  if (hints.ai_family == ASIO_OS_DEF(AF_INET6)
212  && pair->RemoteHostName->Type
213  != Windows::Networking::HostNameType::Ipv6)
214  continue;
215 
216  results.values_->push_back(
218  typename InternetProtocol::endpoint(
219  ip::make_address(
220  asio::detail::winrt_utils::string(
221  pair->RemoteHostName->CanonicalName)),
222  asio::detail::winrt_utils::integer(
223  pair->RemoteServiceName)),
224  host_name, service_name));
225  }
226  }
227  return results;
228  }
229 # endif // defined(ASIO_WINDOWS_RUNTIME)
230 #endif // !defined(GENERATING_DOCUMENTATION)
231 
233  size_type size() const ASIO_NOEXCEPT
234  {
235  return this->values_ ? this->values_->size() : 0;
236  }
237 
239  size_type max_size() const ASIO_NOEXCEPT
240  {
241  return this->values_ ? this->values_->max_size() : values_type().max_size();
242  }
243 
245  bool empty() const ASIO_NOEXCEPT
246  {
247  return this->values_ ? this->values_->empty() : true;
248  }
249 
251  const_iterator begin() const
252  {
253  basic_resolver_results tmp(*this);
254  tmp.index_ = 0;
255  return ASIO_MOVE_CAST(basic_resolver_results)(tmp);
256  }
257 
259  const_iterator end() const
260  {
261  return const_iterator();
262  }
263 
265  const_iterator cbegin() const
266  {
267  return begin();
268  }
269 
271  const_iterator cend() const
272  {
273  return end();
274  }
275 
277  void swap(basic_resolver_results& that) ASIO_NOEXCEPT
278  {
279  if (this != &that)
280  {
281  this->values_.swap(that.values_);
282  std::size_t index = this->index_;
283  this->index_ = that.index_;
284  that.index_ = index;
285  }
286  }
287 
289  friend bool operator==(const basic_resolver_results& a,
290  const basic_resolver_results& b)
291  {
292  return a.equal(b);
293  }
294 
296  friend bool operator!=(const basic_resolver_results& a,
297  const basic_resolver_results& b)
298  {
299  return !a.equal(b);
300  }
301 
302 private:
303  typedef std::vector<basic_resolver_entry<InternetProtocol> > values_type;
304 };
305 
306 } // namespace ip
307 } // namespace asio
308 
309 #include "asio/detail/pop_options.hpp"
310 
311 #endif // ASIO_IP_BASIC_RESOLVER_RESULTS_HPP
const_iterator begin() const
Obtain a begin iterator for the results range.
Definition: basic_resolver_results.hpp:251
const_iterator cbegin() const
Obtain a begin iterator for the results range.
Definition: basic_resolver_results.hpp:265
const_iterator iterator
The type of an iterator into the range.
Definition: basic_resolver_results.hpp:77
const_iterator end() const
Obtain an end iterator for the results range.
Definition: basic_resolver_results.hpp:259
basic_resolver_iterator & operator=(const basic_resolver_iterator &other)
Assignment operator.
Definition: basic_resolver_iterator.hpp:93
void swap(basic_resolver_results &that) ASIO_NOEXCEPT
Swap the results range with another.
Definition: basic_resolver_results.hpp:277
std::size_t size_type
Type used to represent a count of the elements in the range.
Definition: basic_resolver_results.hpp:83
Definition: awaitable.hpp:421
size_type max_size() const ASIO_NOEXCEPT
Get the maximum number of entries permitted in a results range.
Definition: basic_resolver_results.hpp:239
basic_resolver_entry< protocol_type > value_type
The type of a value in the results range.
Definition: basic_resolver_results.hpp:65
size_type size() const ASIO_NOEXCEPT
Get the number of entries in the results range.
Definition: basic_resolver_results.hpp:233
basic_resolver_iterator< protocol_type > const_iterator
The type of an iterator into the range.
Definition: basic_resolver_results.hpp:74
bool empty() const ASIO_NOEXCEPT
Determine whether the results range is empty.
Definition: basic_resolver_results.hpp:245
const value_type & const_reference
The type of a const reference to a value in the range.
Definition: basic_resolver_results.hpp:68
basic_resolver_results(const basic_resolver_results &other)
Copy constructor.
Definition: basic_resolver_results.hpp:91
A range of entries produced by a resolver.
Definition: basic_resolver_results.hpp:50
const_iterator cend() const
Obtain an end iterator for the results range.
Definition: basic_resolver_results.hpp:271
std::ptrdiff_t difference_type
Type used to represent the distance between two iterators in the range.
Definition: basic_resolver_results.hpp:80
friend bool operator!=(const basic_resolver_results &a, const basic_resolver_results &b)
Test two iterators for inequality.
Definition: basic_resolver_results.hpp:296
basic_resolver_results()
Default constructor creates an empty range.
Definition: basic_resolver_results.hpp:86
protocol_type::endpoint endpoint_type
The endpoint type associated with the results.
Definition: basic_resolver_results.hpp:62
An iterator over the entries produced by a resolver.
Definition: basic_resolver_iterator.hpp:51
InternetProtocol protocol_type
The protocol type associated with the results.
Definition: basic_resolver_results.hpp:59
value_type & reference
The type of a non-const reference to a value in the range.
Definition: basic_resolver_results.hpp:71
An entry produced by a resolver.
Definition: basic_resolver_entry.hpp:37
basic_resolver_results & operator=(const basic_resolver_results &other)
Assignment operator.
Definition: basic_resolver_results.hpp:106
friend bool operator==(const basic_resolver_results &a, const basic_resolver_results &b)
Test two iterators for equality.
Definition: basic_resolver_results.hpp:289
Definition: any_io_executor.hpp:28