address_v4_iterator.hpp 3.8 KB

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