address_v4_range.hpp 3.3 KB

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