address_v4_range.hpp 2.9 KB

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