address_v6_iterator.hpp 4.5 KB

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