address_v4_range.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #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() 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) noexcept
  42. : begin_(first),
  43. end_(last)
  44. {
  45. }
  46. /// Copy constructor.
  47. basic_address_range(const basic_address_range& other) noexcept
  48. : begin_(other.begin_),
  49. end_(other.end_)
  50. {
  51. }
  52. /// Move constructor.
  53. basic_address_range(basic_address_range&& other) noexcept
  54. : begin_(static_cast<iterator&&>(other.begin_)),
  55. end_(static_cast<iterator&&>(other.end_))
  56. {
  57. }
  58. /// Assignment operator.
  59. basic_address_range& operator=(const basic_address_range& other) noexcept
  60. {
  61. begin_ = other.begin_;
  62. end_ = other.end_;
  63. return *this;
  64. }
  65. /// Move assignment operator.
  66. basic_address_range& operator=(basic_address_range&& other) noexcept
  67. {
  68. begin_ = static_cast<iterator&&>(other.begin_);
  69. end_ = static_cast<iterator&&>(other.end_);
  70. return *this;
  71. }
  72. /// Obtain an iterator that points to the start of the range.
  73. iterator begin() const noexcept
  74. {
  75. return begin_;
  76. }
  77. /// Obtain an iterator that points to the end of the range.
  78. iterator end() const noexcept
  79. {
  80. return end_;
  81. }
  82. /// Determine whether the range is empty.
  83. bool empty() const noexcept
  84. {
  85. return size() == 0;
  86. }
  87. /// Return the size of the range.
  88. std::size_t size() const noexcept
  89. {
  90. return end_->to_uint() - begin_->to_uint();
  91. }
  92. /// Find an address in the range.
  93. iterator find(const address_v4& addr) const noexcept
  94. {
  95. return addr >= *begin_ && addr < *end_ ? iterator(addr) : end_;
  96. }
  97. private:
  98. iterator begin_;
  99. iterator end_;
  100. };
  101. /// Represents a range of IPv4 addresses.
  102. typedef basic_address_range<address_v4> address_v4_range;
  103. } // namespace ip
  104. } // namespace asio
  105. } // namespace boost
  106. #include <boost/asio/detail/pop_options.hpp>
  107. #endif // BOOST_ASIO_IP_ADDRESS_V4_RANGE_HPP