1//
2// ip/address_v4_iterator.hpp
3// ~~~~~~~~~~~~~~~~~~~~~~~~~~
4//
5// Copyright (c) 2003-2024 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 BOOST_ASIO_IP_ADDRESS_V4_ITERATOR_HPP
12#define BOOST_ASIO_IP_ADDRESS_V4_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 <boost/asio/detail/config.hpp>
19#include <boost/asio/ip/address_v4.hpp>
20
21#include <boost/asio/detail/push_options.hpp>
22
23namespace boost {
24namespace asio {
25namespace ip {
26
27template <typename> class basic_address_iterator;
28
29/// An input iterator that can be used for traversing IPv4 addresses.
30/**
31 * In addition to satisfying the input iterator requirements, this iterator
32 * also supports decrement.
33 *
34 * @par Thread Safety
35 * @e Distinct @e objects: Safe.@n
36 * @e Shared @e objects: Unsafe.
37 */
38template <> class basic_address_iterator<address_v4>
39{
40public:
41 /// The type of the elements pointed to by the iterator.
42 typedef address_v4 value_type;
43
44 /// Distance between two iterators.
45 typedef std::ptrdiff_t difference_type;
46
47 /// The type of a pointer to an element pointed to by the iterator.
48 typedef const address_v4* pointer;
49
50 /// The type of a reference to an element pointed to by the iterator.
51 typedef const address_v4& reference;
52
53 /// Denotes that the iterator satisfies the input iterator requirements.
54 typedef std::input_iterator_tag iterator_category;
55
56 /// Construct an iterator that points to the specified address.
57 basic_address_iterator(const address_v4& addr) noexcept
58 : address_(addr)
59 {
60 }
61
62 /// Copy constructor.
63 basic_address_iterator(const basic_address_iterator& other) noexcept
64 : address_(other.address_)
65 {
66 }
67
68 /// Move constructor.
69 basic_address_iterator(basic_address_iterator&& other) noexcept
70 : address_(static_cast<address_v4&&>(other.address_))
71 {
72 }
73
74 /// Assignment operator.
75 basic_address_iterator& operator=(
76 const basic_address_iterator& other) noexcept
77 {
78 address_ = other.address_;
79 return *this;
80 }
81
82 /// Move assignment operator.
83 basic_address_iterator& operator=(basic_address_iterator&& other) noexcept
84 {
85 address_ = static_cast<address_v4&&>(other.address_);
86 return *this;
87 }
88
89 /// Dereference the iterator.
90 const address_v4& operator*() const noexcept
91 {
92 return address_;
93 }
94
95 /// Dereference the iterator.
96 const address_v4* operator->() const noexcept
97 {
98 return &address_;
99 }
100
101 /// Pre-increment operator.
102 basic_address_iterator& operator++() noexcept
103 {
104 address_ = address_v4((address_.to_uint() + 1) & 0xFFFFFFFF);
105 return *this;
106 }
107
108 /// Post-increment operator.
109 basic_address_iterator operator++(int) noexcept
110 {
111 basic_address_iterator tmp(*this);
112 ++*this;
113 return tmp;
114 }
115
116 /// Pre-decrement operator.
117 basic_address_iterator& operator--() noexcept
118 {
119 address_ = address_v4((address_.to_uint() - 1) & 0xFFFFFFFF);
120 return *this;
121 }
122
123 /// Post-decrement operator.
124 basic_address_iterator operator--(int)
125 {
126 basic_address_iterator tmp(*this);
127 --*this;
128 return tmp;
129 }
130
131 /// Compare two addresses for equality.
132 friend bool operator==(const basic_address_iterator& a,
133 const basic_address_iterator& b)
134 {
135 return a.address_ == b.address_;
136 }
137
138 /// Compare two addresses for inequality.
139 friend bool operator!=(const basic_address_iterator& a,
140 const basic_address_iterator& b)
141 {
142 return a.address_ != b.address_;
143 }
144
145private:
146 address_v4 address_;
147};
148
149/// An input iterator that can be used for traversing IPv4 addresses.
150typedef basic_address_iterator<address_v4> address_v4_iterator;
151
152} // namespace ip
153} // namespace asio
154} // namespace boost
155
156#include <boost/asio/detail/pop_options.hpp>
157
158#endif // BOOST_ASIO_IP_ADDRESS_V4_ITERATOR_HPP
159

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