address_v4_iterator.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // ip/address_v4_iterator.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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) ASIO_NOEXCEPT
  45. : address_(addr)
  46. {
  47. }
  48. /// Copy constructor.
  49. basic_address_iterator(
  50. const basic_address_iterator& other) ASIO_NOEXCEPT
  51. : address_(other.address_)
  52. {
  53. }
  54. #if defined(ASIO_HAS_MOVE)
  55. /// Move constructor.
  56. basic_address_iterator(basic_address_iterator&& other) ASIO_NOEXCEPT
  57. : address_(ASIO_MOVE_CAST(address_v4)(other.address_))
  58. {
  59. }
  60. #endif // defined(ASIO_HAS_MOVE)
  61. /// Assignment operator.
  62. basic_address_iterator& operator=(
  63. const basic_address_iterator& other) ASIO_NOEXCEPT
  64. {
  65. address_ = other.address_;
  66. return *this;
  67. }
  68. #if defined(ASIO_HAS_MOVE)
  69. /// Move assignment operator.
  70. basic_address_iterator& operator=(
  71. basic_address_iterator&& other) ASIO_NOEXCEPT
  72. {
  73. address_ = ASIO_MOVE_CAST(address_v4)(other.address_);
  74. return *this;
  75. }
  76. #endif // defined(ASIO_HAS_MOVE)
  77. /// Dereference the iterator.
  78. const address_v4& operator*() const ASIO_NOEXCEPT
  79. {
  80. return address_;
  81. }
  82. /// Dereference the iterator.
  83. const address_v4* operator->() const ASIO_NOEXCEPT
  84. {
  85. return &address_;
  86. }
  87. /// Pre-increment operator.
  88. basic_address_iterator& operator++() ASIO_NOEXCEPT
  89. {
  90. address_ = address_v4((address_.to_uint() + 1) & 0xFFFFFFFF);
  91. return *this;
  92. }
  93. /// Post-increment operator.
  94. basic_address_iterator operator++(int) ASIO_NOEXCEPT
  95. {
  96. basic_address_iterator tmp(*this);
  97. ++*this;
  98. return tmp;
  99. }
  100. /// Pre-decrement operator.
  101. basic_address_iterator& operator--() ASIO_NOEXCEPT
  102. {
  103. address_ = address_v4((address_.to_uint() - 1) & 0xFFFFFFFF);
  104. return *this;
  105. }
  106. /// Post-decrement operator.
  107. basic_address_iterator operator--(int)
  108. {
  109. basic_address_iterator tmp(*this);
  110. --*this;
  111. return tmp;
  112. }
  113. /// Compare two addresses for equality.
  114. friend bool operator==(const basic_address_iterator& a,
  115. const basic_address_iterator& b)
  116. {
  117. return a.address_ == b.address_;
  118. }
  119. /// Compare two addresses for inequality.
  120. friend bool operator!=(const basic_address_iterator& a,
  121. const basic_address_iterator& b)
  122. {
  123. return a.address_ != b.address_;
  124. }
  125. private:
  126. address_v4 address_;
  127. };
  128. /// An input iterator that can be used for traversing IPv4 addresses.
  129. typedef basic_address_iterator<address_v4> address_v4_iterator;
  130. } // namespace ip
  131. } // namespace asio
  132. #include "asio/detail/pop_options.hpp"
  133. #endif // ASIO_IP_ADDRESS_V4_ITERATOR_HPP