address_v4_iterator.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // ip/address_v4_iterator.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_ITERATOR_HPP
  11. #define BOOST_ASIO_IP_ADDRESS_V4_ITERATOR_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.hpp>
  17. #include <boost/asio/detail/push_options.hpp>
  18. namespace boost {
  19. namespace asio {
  20. namespace ip {
  21. template <typename> class basic_address_iterator;
  22. /// An input iterator that can be used for traversing IPv4 addresses.
  23. /**
  24. * In addition to satisfying the input iterator requirements, this iterator
  25. * also supports decrement.
  26. *
  27. * @par Thread Safety
  28. * @e Distinct @e objects: Safe.@n
  29. * @e Shared @e objects: Unsafe.
  30. */
  31. template <> class basic_address_iterator<address_v4>
  32. {
  33. public:
  34. /// The type of the elements pointed to by the iterator.
  35. typedef address_v4 value_type;
  36. /// Distance between two iterators.
  37. typedef std::ptrdiff_t difference_type;
  38. /// The type of a pointer to an element pointed to by the iterator.
  39. typedef const address_v4* pointer;
  40. /// The type of a reference to an element pointed to by the iterator.
  41. typedef const address_v4& reference;
  42. /// Denotes that the iterator satisfies the input iterator requirements.
  43. typedef std::input_iterator_tag iterator_category;
  44. /// Construct an iterator that points to the specified address.
  45. basic_address_iterator(const address_v4& addr) noexcept
  46. : address_(addr)
  47. {
  48. }
  49. /// Copy constructor.
  50. basic_address_iterator(const basic_address_iterator& other) noexcept
  51. : address_(other.address_)
  52. {
  53. }
  54. /// Move constructor.
  55. basic_address_iterator(basic_address_iterator&& other) noexcept
  56. : address_(static_cast<address_v4&&>(other.address_))
  57. {
  58. }
  59. /// Assignment operator.
  60. basic_address_iterator& operator=(
  61. const basic_address_iterator& other) noexcept
  62. {
  63. address_ = other.address_;
  64. return *this;
  65. }
  66. /// Move assignment operator.
  67. basic_address_iterator& operator=(basic_address_iterator&& other) noexcept
  68. {
  69. address_ = static_cast<address_v4&&>(other.address_);
  70. return *this;
  71. }
  72. /// Dereference the iterator.
  73. const address_v4& operator*() const noexcept
  74. {
  75. return address_;
  76. }
  77. /// Dereference the iterator.
  78. const address_v4* operator->() const noexcept
  79. {
  80. return &address_;
  81. }
  82. /// Pre-increment operator.
  83. basic_address_iterator& operator++() noexcept
  84. {
  85. address_ = address_v4((address_.to_uint() + 1) & 0xFFFFFFFF);
  86. return *this;
  87. }
  88. /// Post-increment operator.
  89. basic_address_iterator operator++(int) noexcept
  90. {
  91. basic_address_iterator tmp(*this);
  92. ++*this;
  93. return tmp;
  94. }
  95. /// Pre-decrement operator.
  96. basic_address_iterator& operator--() noexcept
  97. {
  98. address_ = address_v4((address_.to_uint() - 1) & 0xFFFFFFFF);
  99. return *this;
  100. }
  101. /// Post-decrement operator.
  102. basic_address_iterator operator--(int)
  103. {
  104. basic_address_iterator tmp(*this);
  105. --*this;
  106. return tmp;
  107. }
  108. /// Compare two addresses for equality.
  109. friend bool operator==(const basic_address_iterator& a,
  110. const basic_address_iterator& b)
  111. {
  112. return a.address_ == b.address_;
  113. }
  114. /// Compare two addresses for inequality.
  115. friend bool operator!=(const basic_address_iterator& a,
  116. const basic_address_iterator& b)
  117. {
  118. return a.address_ != b.address_;
  119. }
  120. private:
  121. address_v4 address_;
  122. };
  123. /// An input iterator that can be used for traversing IPv4 addresses.
  124. typedef basic_address_iterator<address_v4> address_v4_iterator;
  125. } // namespace ip
  126. } // namespace asio
  127. } // namespace boost
  128. #include <boost/asio/detail/pop_options.hpp>
  129. #endif // BOOST_ASIO_IP_ADDRESS_V4_ITERATOR_HPP