address_v6_iterator.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // ip/address_v6_iterator.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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 ASIO_IP_ADDRESS_V6_ITERATOR_HPP
  12. #define 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 "asio/detail/config.hpp"
  17. #include "asio/ip/address_v6.hpp"
  18. #include "asio/detail/push_options.hpp"
  19. namespace asio {
  20. namespace ip {
  21. template <typename> class basic_address_iterator;
  22. /// An input iterator that can be used for traversing IPv6 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_v6>
  32. {
  33. public:
  34. /// The type of the elements pointed to by the iterator.
  35. typedef address_v6 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_v6* pointer;
  40. /// The type of a reference to an element pointed to by the iterator.
  41. typedef const address_v6& 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_v6& addr) ASIO_NOEXCEPT
  46. : address_(addr)
  47. {
  48. }
  49. /// Copy constructor.
  50. basic_address_iterator(
  51. const basic_address_iterator& other) ASIO_NOEXCEPT
  52. : address_(other.address_)
  53. {
  54. }
  55. #if defined(ASIO_HAS_MOVE)
  56. /// Move constructor.
  57. basic_address_iterator(basic_address_iterator&& other) ASIO_NOEXCEPT
  58. : address_(ASIO_MOVE_CAST(address_v6)(other.address_))
  59. {
  60. }
  61. #endif // defined(ASIO_HAS_MOVE)
  62. /// Assignment operator.
  63. basic_address_iterator& operator=(
  64. const basic_address_iterator& other) ASIO_NOEXCEPT
  65. {
  66. address_ = other.address_;
  67. return *this;
  68. }
  69. #if defined(ASIO_HAS_MOVE)
  70. /// Move assignment operator.
  71. basic_address_iterator& operator=(
  72. basic_address_iterator&& other) ASIO_NOEXCEPT
  73. {
  74. address_ = ASIO_MOVE_CAST(address_v6)(other.address_);
  75. return *this;
  76. }
  77. #endif // defined(ASIO_HAS_MOVE)
  78. /// Dereference the iterator.
  79. const address_v6& operator*() const ASIO_NOEXCEPT
  80. {
  81. return address_;
  82. }
  83. /// Dereference the iterator.
  84. const address_v6* operator->() const ASIO_NOEXCEPT
  85. {
  86. return &address_;
  87. }
  88. /// Pre-increment operator.
  89. basic_address_iterator& operator++() ASIO_NOEXCEPT
  90. {
  91. for (int i = 15; i >= 0; --i)
  92. {
  93. if (address_.addr_.s6_addr[i] < 0xFF)
  94. {
  95. ++address_.addr_.s6_addr[i];
  96. break;
  97. }
  98. address_.addr_.s6_addr[i] = 0;
  99. }
  100. return *this;
  101. }
  102. /// Post-increment operator.
  103. basic_address_iterator operator++(int) ASIO_NOEXCEPT
  104. {
  105. basic_address_iterator tmp(*this);
  106. ++*this;
  107. return tmp;
  108. }
  109. /// Pre-decrement operator.
  110. basic_address_iterator& operator--() ASIO_NOEXCEPT
  111. {
  112. for (int i = 15; i >= 0; --i)
  113. {
  114. if (address_.addr_.s6_addr[i] > 0)
  115. {
  116. --address_.addr_.s6_addr[i];
  117. break;
  118. }
  119. address_.addr_.s6_addr[i] = 0xFF;
  120. }
  121. return *this;
  122. }
  123. /// Post-decrement operator.
  124. basic_address_iterator operator--(int)
  125. {
  126. basic_address_iterator tmp(*this);
  127. --*this;
  128. return tmp;
  129. }
  130. /// Compare two addresses for equality.
  131. friend bool operator==(const basic_address_iterator& a,
  132. const basic_address_iterator& b)
  133. {
  134. return a.address_ == b.address_;
  135. }
  136. /// Compare two addresses for inequality.
  137. friend bool operator!=(const basic_address_iterator& a,
  138. const basic_address_iterator& b)
  139. {
  140. return a.address_ != b.address_;
  141. }
  142. private:
  143. address_v6 address_;
  144. };
  145. /// An input iterator that can be used for traversing IPv6 addresses.
  146. typedef basic_address_iterator<address_v6> address_v6_iterator;
  147. } // namespace ip
  148. } // namespace asio
  149. #include "asio/detail/pop_options.hpp"
  150. #endif // ASIO_IP_ADDRESS_V6_ITERATOR_HPP