1//
2// ip/address_v6_iterator.hpp
3// ~~~~~~~~~~~~~~~~~~~~~~~~~~
4//
5// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6// Oliver Kowalke (oliver dot kowalke at gmail dot com)
7//
8// Distributed under the Boost Software License, Version 1.0. (See accompanying
9// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10//
11
12#ifndef BOOST_ASIO_IP_ADDRESS_V6_ITERATOR_HPP
13#define BOOST_ASIO_IP_ADDRESS_V6_ITERATOR_HPP
14
15#if defined(_MSC_VER) && (_MSC_VER >= 1200)
16# pragma once
17#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18
19#include <boost/asio/detail/config.hpp>
20#include <boost/asio/ip/address_v6.hpp>
21
22#include <boost/asio/detail/push_options.hpp>
23
24namespace boost {
25namespace asio {
26namespace ip {
27
28template <typename> class basic_address_iterator;
29
30/// An input iterator that can be used for traversing IPv6 addresses.
31/**
32 * In addition to satisfying the input iterator requirements, this iterator
33 * also supports decrement.
34 *
35 * @par Thread Safety
36 * @e Distinct @e objects: Safe.@n
37 * @e Shared @e objects: Unsafe.
38 */
39template <> class basic_address_iterator<address_v6>
40{
41public:
42 /// The type of the elements pointed to by the iterator.
43 typedef address_v6 value_type;
44
45 /// Distance between two iterators.
46 typedef std::ptrdiff_t difference_type;
47
48 /// The type of a pointer to an element pointed to by the iterator.
49 typedef const address_v6* pointer;
50
51 /// The type of a reference to an element pointed to by the iterator.
52 typedef const address_v6& reference;
53
54 /// Denotes that the iterator satisfies the input iterator requirements.
55 typedef std::input_iterator_tag iterator_category;
56
57 /// Construct an iterator that points to the specified address.
58 basic_address_iterator(const address_v6& addr) noexcept
59 : address_(addr)
60 {
61 }
62
63 /// Copy constructor.
64 basic_address_iterator(
65 const basic_address_iterator& other) noexcept
66 : address_(other.address_)
67 {
68 }
69
70 /// Move constructor.
71 basic_address_iterator(basic_address_iterator&& other) noexcept
72 : address_(static_cast<address_v6&&>(other.address_))
73 {
74 }
75
76 /// Assignment operator.
77 basic_address_iterator& operator=(
78 const basic_address_iterator& other) noexcept
79 {
80 address_ = other.address_;
81 return *this;
82 }
83
84 /// Move assignment operator.
85 basic_address_iterator& operator=(basic_address_iterator&& other) noexcept
86 {
87 address_ = static_cast<address_v6&&>(other.address_);
88 return *this;
89 }
90
91 /// Dereference the iterator.
92 const address_v6& operator*() const noexcept
93 {
94 return address_;
95 }
96
97 /// Dereference the iterator.
98 const address_v6* operator->() const noexcept
99 {
100 return &address_;
101 }
102
103 /// Pre-increment operator.
104 basic_address_iterator& operator++() noexcept
105 {
106 for (int i = 15; i >= 0; --i)
107 {
108 if (address_.addr_.s6_addr[i] < 0xFF)
109 {
110 ++address_.addr_.s6_addr[i];
111 break;
112 }
113
114 address_.addr_.s6_addr[i] = 0;
115 }
116
117 return *this;
118 }
119
120 /// Post-increment operator.
121 basic_address_iterator operator++(int) noexcept
122 {
123 basic_address_iterator tmp(*this);
124 ++*this;
125 return tmp;
126 }
127
128 /// Pre-decrement operator.
129 basic_address_iterator& operator--() noexcept
130 {
131 for (int i = 15; i >= 0; --i)
132 {
133 if (address_.addr_.s6_addr[i] > 0)
134 {
135 --address_.addr_.s6_addr[i];
136 break;
137 }
138
139 address_.addr_.s6_addr[i] = 0xFF;
140 }
141
142 return *this;
143 }
144
145 /// Post-decrement operator.
146 basic_address_iterator operator--(int)
147 {
148 basic_address_iterator tmp(*this);
149 --*this;
150 return tmp;
151 }
152
153 /// Compare two addresses for equality.
154 friend bool operator==(const basic_address_iterator& a,
155 const basic_address_iterator& b)
156 {
157 return a.address_ == b.address_;
158 }
159
160 /// Compare two addresses for inequality.
161 friend bool operator!=(const basic_address_iterator& a,
162 const basic_address_iterator& b)
163 {
164 return a.address_ != b.address_;
165 }
166
167private:
168 address_v6 address_;
169};
170
171/// An input iterator that can be used for traversing IPv6 addresses.
172typedef basic_address_iterator<address_v6> address_v6_iterator;
173
174} // namespace ip
175} // namespace asio
176} // namespace boost
177
178#include <boost/asio/detail/pop_options.hpp>
179
180#endif // BOOST_ASIO_IP_ADDRESS_V6_ITERATOR_HPP
181

source code of boost/libs/asio/include/boost/asio/ip/address_v6_iterator.hpp