address_v4_iterator.hpp 4.2 KB

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