Realistic 3D camera system
3D camera system components
basic_resolver_iterator.hpp
Go to the documentation of this file.
1 //
2 // ip/basic_resolver_iterator.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 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_ITERATOR_HPP
12 #define ASIO_IP_BASIC_RESOLVER_ITERATOR_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 <iterator>
22 #include <string>
23 #include <vector>
28 
29 #if defined(ASIO_WINDOWS_RUNTIME)
31 #endif // defined(ASIO_WINDOWS_RUNTIME)
32 
34 
35 namespace asio {
36 namespace ip {
37 
39 
50 template <typename InternetProtocol>
52 {
53 public:
55  typedef std::ptrdiff_t difference_type;
56 
59 
62 
65 
67  typedef std::forward_iterator_tag iterator_category;
68 
71  : index_(0)
72  {
73  }
74 
77  asio::detail::addrinfo_type* address_info,
78  const std::string& host_name, const std::string& service_name)
79  {
81  if (!address_info)
82  return iter;
83 
84  std::string actual_host_name = host_name;
85  if (address_info->ai_canonname)
86  actual_host_name = address_info->ai_canonname;
87 
88  iter.values_.reset(new values_type);
89 
90  while (address_info)
91  {
92  if (address_info->ai_family == ASIO_OS_DEF(AF_INET)
93  || address_info->ai_family == ASIO_OS_DEF(AF_INET6))
94  {
95  using namespace std; // For memcpy.
96  typename InternetProtocol::endpoint endpoint;
97  endpoint.resize(static_cast<std::size_t>(address_info->ai_addrlen));
98  memcpy(endpoint.data(), address_info->ai_addr,
99  address_info->ai_addrlen);
100  iter.values_->push_back(
102  actual_host_name, service_name));
103  }
104  address_info = address_info->ai_next;
105  }
106 
107  return iter;
108  }
109 
112  const typename InternetProtocol::endpoint& endpoint,
113  const std::string& host_name, const std::string& service_name)
114  {
116  iter.values_.reset(new values_type);
117  iter.values_->push_back(
119  endpoint, host_name, service_name));
120  return iter;
121  }
122 
124  template <typename EndpointIterator>
126  EndpointIterator begin, EndpointIterator end,
127  const std::string& host_name, const std::string& service_name)
128  {
130  if (begin != end)
131  {
132  iter.values_.reset(new values_type);
133  for (EndpointIterator ep_iter = begin; ep_iter != end; ++ep_iter)
134  {
135  iter.values_->push_back(
137  *ep_iter, host_name, service_name));
138  }
139  }
140  return iter;
141  }
142 
143 #if defined(ASIO_WINDOWS_RUNTIME)
146  Windows::Foundation::Collections::IVectorView<
147  Windows::Networking::EndpointPair^>^ endpoints,
148  const asio::detail::addrinfo_type& hints,
149  const std::string& host_name, const std::string& service_name)
150  {
152  if (endpoints->Size)
153  {
154  iter.values_.reset(new values_type);
155  for (unsigned int i = 0; i < endpoints->Size; ++i)
156  {
157  auto pair = endpoints->GetAt(i);
158 
159  if (hints.ai_family == ASIO_OS_DEF(AF_INET)
160  && pair->RemoteHostName->Type
161  != Windows::Networking::HostNameType::Ipv4)
162  continue;
163 
164  if (hints.ai_family == ASIO_OS_DEF(AF_INET6)
165  && pair->RemoteHostName->Type
166  != Windows::Networking::HostNameType::Ipv6)
167  continue;
168 
169  iter.values_->push_back(
171  typename InternetProtocol::endpoint(
173  asio::detail::winrt_utils::string(
174  pair->RemoteHostName->CanonicalName)),
175  asio::detail::winrt_utils::integer(
176  pair->RemoteServiceName)),
177  host_name, service_name));
178  }
179  }
180  return iter;
181  }
182 #endif // defined(ASIO_WINDOWS_RUNTIME)
183 
186  {
187  return dereference();
188  }
189 
192  {
193  return &dereference();
194  }
195 
198  {
199  increment();
200  return *this;
201  }
202 
205  {
206  basic_resolver_iterator tmp(*this);
207  ++*this;
208  return tmp;
209  }
210 
212  friend bool operator==(const basic_resolver_iterator& a,
213  const basic_resolver_iterator& b)
214  {
215  return a.equal(b);
216  }
217 
219  friend bool operator!=(const basic_resolver_iterator& a,
220  const basic_resolver_iterator& b)
221  {
222  return !a.equal(b);
223  }
224 
225 private:
226  void increment()
227  {
228  if (++index_ == values_->size())
229  {
230  // Reset state to match a default constructed end iterator.
231  values_.reset();
232  index_ = 0;
233  }
234  }
235 
236  bool equal(const basic_resolver_iterator& other) const
237  {
238  if (!values_ && !other.values_)
239  return true;
240  if (values_ != other.values_)
241  return false;
242  return index_ == other.index_;
243  }
244 
245  const basic_resolver_entry<InternetProtocol>& dereference() const
246  {
247  return (*values_)[index_];
248  }
249 
250  typedef std::vector<basic_resolver_entry<InternetProtocol> > values_type;
251  asio::detail::shared_ptr<values_type> values_;
252  std::size_t index_;
253 };
254 
255 } // namespace ip
256 } // namespace asio
257 
259 
260 #endif // ASIO_IP_BASIC_RESOLVER_ITERATOR_HPP
static basic_resolver_iterator create(asio::detail::addrinfo_type *address_info, const std::string &host_name, const std::string &service_name)
Create an iterator from an addrinfo list returned by getaddrinfo.
friend bool operator!=(const basic_resolver_iterator &a, const basic_resolver_iterator &b)
Test two iterators for inequality.
static basic_resolver_iterator create(EndpointIterator begin, EndpointIterator end, const std::string &host_name, const std::string &service_name)
Create an iterator from a sequence of endpoints, host and service name.
SocketService Iterator begin
Definition: connect.hpp:521
#define ASIO_OS_DEF(c)
STL namespace.
std::forward_iterator_tag iterator_category
The iterator category.
static ASIO_DECL address from_string(const char *str)
Definition: address.ipp:136
asio::basic_streambuf< Allocator > & b
Definition: read.hpp:702
basic_resolver_iterator()
Default constructor creates an end iterator.
addrinfo addrinfo_type
const basic_resolver_entry< InternetProtocol > * pointer
The type of the result of applying operator->() to the iterator.
std::ptrdiff_t difference_type
The type used for the distance between two iterators.
basic_resolver_iterator operator++(int)
Increment operator (postfix).
const basic_resolver_entry< InternetProtocol > & reference
The type of the result of applying operator*() to the iterator.
friend bool operator==(const basic_resolver_iterator &a, const basic_resolver_iterator &b)
Test two iterators for equality.
const basic_resolver_entry< InternetProtocol > & operator*() const
Dereference an iterator.
static basic_resolver_iterator create(const typename InternetProtocol::endpoint &endpoint, const std::string &host_name, const std::string &service_name)
Create an iterator from an endpoint, host name and service name.
An iterator over the entries produced by a resolver.
ASIO_DECL std::string host_name()
Get the current host name.
Definition: host_name.ipp:29
basic_resolver_entry< InternetProtocol > value_type
The type of the value pointed to by the iterator.
SocketService Iterator Iterator end
Definition: connect.hpp:592
An entry produced by a resolver.
basic_resolver_iterator & operator++()
Increment operator (prefix).
const basic_resolver_entry< InternetProtocol > * operator->() const
Dereference an iterator.