address_v6_iterator.hpp 4.1 KB

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