| 1 | // |
|---|---|
| 2 | // ip/address_v4_range.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_RANGE_HPP |
| 12 | #define BOOST_ASIO_IP_ADDRESS_V4_RANGE_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_iterator.hpp> |
| 20 | |
| 21 | #include <boost/asio/detail/push_options.hpp> |
| 22 | |
| 23 | namespace boost { |
| 24 | namespace asio { |
| 25 | namespace ip { |
| 26 | |
| 27 | template <typename> class basic_address_range; |
| 28 | |
| 29 | /// Represents a range of IPv4 addresses. |
| 30 | /** |
| 31 | * @par Thread Safety |
| 32 | * @e Distinct @e objects: Safe.@n |
| 33 | * @e Shared @e objects: Unsafe. |
| 34 | */ |
| 35 | template <> class basic_address_range<address_v4> |
| 36 | { |
| 37 | public: |
| 38 | /// The type of an iterator that points into the range. |
| 39 | typedef basic_address_iterator<address_v4> iterator; |
| 40 | |
| 41 | /// Construct an empty range. |
| 42 | basic_address_range() noexcept |
| 43 | : begin_(address_v4()), |
| 44 | end_(address_v4()) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | /// Construct an range that represents the given range of addresses. |
| 49 | explicit basic_address_range(const iterator& first, |
| 50 | const iterator& last) noexcept |
| 51 | : begin_(first), |
| 52 | end_(last) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | /// Copy constructor. |
| 57 | basic_address_range(const basic_address_range& other) noexcept |
| 58 | : begin_(other.begin_), |
| 59 | end_(other.end_) |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | /// Move constructor. |
| 64 | basic_address_range(basic_address_range&& other) noexcept |
| 65 | : begin_(static_cast<iterator&&>(other.begin_)), |
| 66 | end_(static_cast<iterator&&>(other.end_)) |
| 67 | { |
| 68 | } |
| 69 | |
| 70 | /// Assignment operator. |
| 71 | basic_address_range& operator=(const basic_address_range& other) noexcept |
| 72 | { |
| 73 | begin_ = other.begin_; |
| 74 | end_ = other.end_; |
| 75 | return *this; |
| 76 | } |
| 77 | |
| 78 | /// Move assignment operator. |
| 79 | basic_address_range& operator=(basic_address_range&& other) noexcept |
| 80 | { |
| 81 | begin_ = static_cast<iterator&&>(other.begin_); |
| 82 | end_ = static_cast<iterator&&>(other.end_); |
| 83 | return *this; |
| 84 | } |
| 85 | |
| 86 | /// Obtain an iterator that points to the start of the range. |
| 87 | iterator begin() const noexcept |
| 88 | { |
| 89 | return begin_; |
| 90 | } |
| 91 | |
| 92 | /// Obtain an iterator that points to the end of the range. |
| 93 | iterator end() const noexcept |
| 94 | { |
| 95 | return end_; |
| 96 | } |
| 97 | |
| 98 | /// Determine whether the range is empty. |
| 99 | bool empty() const noexcept |
| 100 | { |
| 101 | return size() == 0; |
| 102 | } |
| 103 | |
| 104 | /// Return the size of the range. |
| 105 | std::size_t size() const noexcept |
| 106 | { |
| 107 | return end_->to_uint() - begin_->to_uint(); |
| 108 | } |
| 109 | |
| 110 | /// Find an address in the range. |
| 111 | iterator find(const address_v4& addr) const noexcept |
| 112 | { |
| 113 | return addr >= *begin_ && addr < *end_ ? iterator(addr) : end_; |
| 114 | } |
| 115 | |
| 116 | private: |
| 117 | iterator begin_; |
| 118 | iterator end_; |
| 119 | }; |
| 120 | |
| 121 | /// Represents a range of IPv4 addresses. |
| 122 | typedef basic_address_range<address_v4> address_v4_range; |
| 123 | |
| 124 | } // namespace ip |
| 125 | } // namespace asio |
| 126 | } // namespace boost |
| 127 | |
| 128 | #include <boost/asio/detail/pop_options.hpp> |
| 129 | |
| 130 | #endif // BOOST_ASIO_IP_ADDRESS_V4_RANGE_HPP |
| 131 |
