Clementine
basic_resolver_iterator.hpp
1 //
2 // ip/basic_resolver_iterator.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_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>
24 #include "asio/detail/memory.hpp"
25 #include "asio/detail/socket_ops.hpp"
26 #include "asio/detail/socket_types.hpp"
27 #include "asio/ip/basic_resolver_entry.hpp"
28 
29 #if defined(ASIO_WINDOWS_RUNTIME)
30 # include "asio/detail/winrt_utils.hpp"
31 #endif // defined(ASIO_WINDOWS_RUNTIME)
32 
33 #include "asio/detail/push_options.hpp"
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  : values_(other.values_),
78  index_(other.index_)
79  {
80  }
81 
82 #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
85  : values_(ASIO_MOVE_CAST(values_ptr_type)(other.values_)),
86  index_(other.index_)
87  {
88  other.index_ = 0;
89  }
90 #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
91 
94  {
95  values_ = other.values_;
96  index_ = other.index_;
97  return *this;
98  }
99 
100 #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
103  {
104  if (this != &other)
105  {
106  values_ = ASIO_MOVE_CAST(values_ptr_type)(other.values_);
107  index_ = other.index_;
108  other.index_ = 0;
109  }
110 
111  return *this;
112  }
113 #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
114 
117  {
118  return dereference();
119  }
120 
123  {
124  return &dereference();
125  }
126 
129  {
130  increment();
131  return *this;
132  }
133 
136  {
137  basic_resolver_iterator tmp(*this);
138  ++*this;
139  return tmp;
140  }
141 
143  friend bool operator==(const basic_resolver_iterator& a,
144  const basic_resolver_iterator& b)
145  {
146  return a.equal(b);
147  }
148 
150  friend bool operator!=(const basic_resolver_iterator& a,
151  const basic_resolver_iterator& b)
152  {
153  return !a.equal(b);
154  }
155 
156 protected:
157  void increment()
158  {
159  if (++index_ == values_->size())
160  {
161  // Reset state to match a default constructed end iterator.
162  values_.reset();
163  index_ = 0;
164  }
165  }
166 
167  bool equal(const basic_resolver_iterator& other) const
168  {
169  if (!values_ && !other.values_)
170  return true;
171  if (values_ != other.values_)
172  return false;
173  return index_ == other.index_;
174  }
175 
176  const basic_resolver_entry<InternetProtocol>& dereference() const
177  {
178  return (*values_)[index_];
179  }
180 
181  typedef std::vector<basic_resolver_entry<InternetProtocol> > values_type;
182  typedef asio::detail::shared_ptr<values_type> values_ptr_type;
183  values_ptr_type values_;
184  std::size_t index_;
185 };
186 
187 } // namespace ip
188 } // namespace asio
189 
190 #include "asio/detail/pop_options.hpp"
191 
192 #endif // ASIO_IP_BASIC_RESOLVER_ITERATOR_HPP
friend bool operator!=(const basic_resolver_iterator &a, const basic_resolver_iterator &b)
Test two iterators for inequality.
Definition: basic_resolver_iterator.hpp:150
basic_resolver_iterator & operator=(const basic_resolver_iterator &other)
Assignment operator.
Definition: basic_resolver_iterator.hpp:93
std::forward_iterator_tag iterator_category
The iterator category.
Definition: basic_resolver_iterator.hpp:67
basic_resolver_iterator()
Default constructor creates an end iterator.
Definition: basic_resolver_iterator.hpp:70
basic_resolver_iterator(const basic_resolver_iterator &other)
Copy constructor.
Definition: basic_resolver_iterator.hpp:76
const basic_resolver_entry< InternetProtocol > * pointer
The type of the result of applying operator->() to the iterator.
Definition: basic_resolver_iterator.hpp:61
std::ptrdiff_t difference_type
The type used for the distance between two iterators.
Definition: basic_resolver_iterator.hpp:55
basic_resolver_iterator operator++(int)
Increment operator (postfix).
Definition: basic_resolver_iterator.hpp:135
const basic_resolver_entry< InternetProtocol > & reference
The type of the result of applying operator*() to the iterator.
Definition: basic_resolver_iterator.hpp:64
friend bool operator==(const basic_resolver_iterator &a, const basic_resolver_iterator &b)
Test two iterators for equality.
Definition: basic_resolver_iterator.hpp:143
const basic_resolver_entry< InternetProtocol > * operator->() const
Dereference an iterator.
Definition: basic_resolver_iterator.hpp:122
An iterator over the entries produced by a resolver.
Definition: basic_resolver_iterator.hpp:51
const basic_resolver_entry< InternetProtocol > & operator*() const
Dereference an iterator.
Definition: basic_resolver_iterator.hpp:116
basic_resolver_entry< InternetProtocol > value_type
The type of the value pointed to by the iterator.
Definition: basic_resolver_iterator.hpp:58
An entry produced by a resolver.
Definition: basic_resolver_entry.hpp:37
basic_resolver_iterator & operator++()
Increment operator (prefix).
Definition: basic_resolver_iterator.hpp:128
Definition: any_io_executor.hpp:28