address_v6_iterator.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // ip/address_v6_iterator.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Oliver Kowalke (oliver dot kowalke at gmail dot com)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef BOOST_ASIO_IP_ADDRESS_V6_ITERATOR_HPP
  12. #define BOOST_ASIO_IP_ADDRESS_V6_ITERATOR_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <boost/asio/detail/config.hpp>
  17. #include <boost/asio/ip/address_v6.hpp>
  18. #include <boost/asio/detail/push_options.hpp>
  19. namespace boost {
  20. namespace asio {
  21. namespace ip {
  22. template <typename> class basic_address_iterator;
  23. /// An input iterator that can be used for traversing IPv6 addresses.
  24. /**
  25. * In addition to satisfying the input iterator requirements, this iterator
  26. * also supports decrement.
  27. *
  28. * @par Thread Safety
  29. * @e Distinct @e objects: Safe.@n
  30. * @e Shared @e objects: Unsafe.
  31. */
  32. template <> class basic_address_iterator<address_v6>
  33. {
  34. public:
  35. /// The type of the elements pointed to by the iterator.
  36. typedef address_v6 value_type;
  37. /// Distance between two iterators.
  38. typedef std::ptrdiff_t difference_type;
  39. /// The type of a pointer to an element pointed to by the iterator.
  40. typedef const address_v6* pointer;
  41. /// The type of a reference to an element pointed to by the iterator.
  42. typedef const address_v6& reference;
  43. /// Denotes that the iterator satisfies the input iterator requirements.
  44. typedef std::input_iterator_tag iterator_category;
  45. /// Construct an iterator that points to the specified address.
  46. basic_address_iterator(const address_v6& addr) noexcept
  47. : address_(addr)
  48. {
  49. }
  50. /// Copy constructor.
  51. basic_address_iterator(
  52. const basic_address_iterator& other) noexcept
  53. : address_(other.address_)
  54. {
  55. }
  56. /// Move constructor.
  57. basic_address_iterator(basic_address_iterator&& other) noexcept
  58. : address_(static_cast<address_v6&&>(other.address_))
  59. {
  60. }
  61. /// Assignment operator.
  62. basic_address_iterator& operator=(
  63. const basic_address_iterator& other) noexcept
  64. {
  65. address_ = other.address_;
  66. return *this;
  67. }
  68. /// Move assignment operator.
  69. basic_address_iterator& operator=(basic_address_iterator&& other) noexcept
  70. {
  71. address_ = static_cast<address_v6&&>(other.address_);
  72. return *this;
  73. }
  74. /// Dereference the iterator.
  75. const address_v6& operator*() const noexcept
  76. {
  77. return address_;
  78. }
  79. /// Dereference the iterator.
  80. const address_v6* operator->() const noexcept
  81. {
  82. return &address_;
  83. }
  84. /// Pre-increment operator.
  85. basic_address_iterator& operator++() noexcept
  86. {
  87. for (int i = 15; i >= 0; --i)
  88. {
  89. if (address_.addr_.s6_addr[i] < 0xFF)
  90. {
  91. ++address_.addr_.s6_addr[i];
  92. break;
  93. }
  94. address_.addr_.s6_addr[i] = 0;
  95. }
  96. return *this;
  97. }
  98. /// Post-increment operator.
  99. basic_address_iterator operator++(int) noexcept
  100. {
  101. basic_address_iterator tmp(*this);
  102. ++*this;
  103. return tmp;
  104. }
  105. /// Pre-decrement operator.
  106. basic_address_iterator& operator--() noexcept
  107. {
  108. for (int i = 15; i >= 0; --i)
  109. {
  110. if (address_.addr_.s6_addr[i] > 0)
  111. {
  112. --address_.addr_.s6_addr[i];
  113. break;
  114. }
  115. address_.addr_.s6_addr[i] = 0xFF;
  116. }
  117. return *this;
  118. }
  119. /// Post-decrement operator.
  120. basic_address_iterator operator--(int)
  121. {
  122. basic_address_iterator tmp(*this);
  123. --*this;
  124. return tmp;
  125. }
  126. /// Compare two addresses for equality.
  127. friend bool operator==(const basic_address_iterator& a,
  128. const basic_address_iterator& b)
  129. {
  130. return a.address_ == b.address_;
  131. }
  132. /// Compare two addresses for inequality.
  133. friend bool operator!=(const basic_address_iterator& a,
  134. const basic_address_iterator& b)
  135. {
  136. return a.address_ != b.address_;
  137. }
  138. private:
  139. address_v6 address_;
  140. };
  141. /// An input iterator that can be used for traversing IPv6 addresses.
  142. typedef basic_address_iterator<address_v6> address_v6_iterator;
  143. } // namespace ip
  144. } // namespace asio
  145. } // namespace boost
  146. #include <boost/asio/detail/pop_options.hpp>
  147. #endif // BOOST_ASIO_IP_ADDRESS_V6_ITERATOR_HPP