1 //
2 // ip/address_v4_range.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2019 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.
basic_address_range()42   basic_address_range() BOOST_ASIO_NOEXCEPT
43     : begin_(address_v4()),
44       end_(address_v4())
45   {
46   }
47 
48   /// Construct an range that represents the given range of addresses.
basic_address_range(const iterator & first,const iterator & last)49   explicit basic_address_range(const iterator& first,
50       const iterator& last) BOOST_ASIO_NOEXCEPT
51     : begin_(first),
52       end_(last)
53   {
54   }
55 
56   /// Copy constructor.
basic_address_range(const basic_address_range & other)57   basic_address_range(const basic_address_range& other) BOOST_ASIO_NOEXCEPT
58     : begin_(other.begin_),
59       end_(other.end_)
60   {
61   }
62 
63 #if defined(BOOST_ASIO_HAS_MOVE)
64   /// Move constructor.
basic_address_range(basic_address_range && other)65   basic_address_range(basic_address_range&& other) BOOST_ASIO_NOEXCEPT
66     : begin_(BOOST_ASIO_MOVE_CAST(iterator)(other.begin_)),
67       end_(BOOST_ASIO_MOVE_CAST(iterator)(other.end_))
68   {
69   }
70 #endif // defined(BOOST_ASIO_HAS_MOVE)
71 
72   /// Assignment operator.
operator =(const basic_address_range & other)73   basic_address_range& operator=(
74       const basic_address_range& other) BOOST_ASIO_NOEXCEPT
75   {
76     begin_ = other.begin_;
77     end_ = other.end_;
78     return *this;
79   }
80 
81 #if defined(BOOST_ASIO_HAS_MOVE)
82   /// Move assignment operator.
operator =(basic_address_range && other)83   basic_address_range& operator=(
84       basic_address_range&& other) BOOST_ASIO_NOEXCEPT
85   {
86     begin_ = BOOST_ASIO_MOVE_CAST(iterator)(other.begin_);
87     end_ = BOOST_ASIO_MOVE_CAST(iterator)(other.end_);
88     return *this;
89   }
90 #endif // defined(BOOST_ASIO_HAS_MOVE)
91 
92   /// Obtain an iterator that points to the start of the range.
begin() const93   iterator begin() const BOOST_ASIO_NOEXCEPT
94   {
95     return begin_;
96   }
97 
98   /// Obtain an iterator that points to the end of the range.
end() const99   iterator end() const BOOST_ASIO_NOEXCEPT
100   {
101     return end_;
102   }
103 
104   /// Determine whether the range is empty.
empty() const105   bool empty() const BOOST_ASIO_NOEXCEPT
106   {
107     return size() == 0;
108   }
109 
110   /// Return the size of the range.
size() const111   std::size_t size() const BOOST_ASIO_NOEXCEPT
112   {
113     return end_->to_uint() - begin_->to_uint();
114   }
115 
116   /// Find an address in the range.
find(const address_v4 & addr) const117   iterator find(const address_v4& addr) const BOOST_ASIO_NOEXCEPT
118   {
119     return addr >= *begin_ && addr < *end_ ? iterator(addr) : end_;
120   }
121 
122 private:
123   iterator begin_;
124   iterator end_;
125 };
126 
127 /// Represents a range of IPv4 addresses.
128 typedef basic_address_range<address_v4> address_v4_range;
129 
130 } // namespace ip
131 } // namespace asio
132 } // namespace boost
133 
134 #include <boost/asio/detail/pop_options.hpp>
135 
136 #endif // BOOST_ASIO_IP_ADDRESS_V4_RANGE_HPP
137