basic_resolver_iterator.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // ip/basic_resolver_iterator.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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_BASIC_RESOLVER_ITERATOR_HPP
  11. #define BOOST_ASIO_IP_BASIC_RESOLVER_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 <cstddef>
  17. #include <cstring>
  18. #include <iterator>
  19. #include <string>
  20. #include <vector>
  21. #include <boost/asio/detail/memory.hpp>
  22. #include <boost/asio/detail/socket_ops.hpp>
  23. #include <boost/asio/detail/socket_types.hpp>
  24. #include <boost/asio/ip/basic_resolver_entry.hpp>
  25. #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
  26. # include <boost/asio/detail/winrt_utils.hpp>
  27. #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
  28. #include <boost/asio/detail/push_options.hpp>
  29. namespace boost {
  30. namespace asio {
  31. namespace ip {
  32. /// An iterator over the entries produced by a resolver.
  33. /**
  34. * The boost::asio::ip::basic_resolver_iterator class template is used to define
  35. * iterators over the results returned by a resolver.
  36. *
  37. * The iterator's value_type, obtained when the iterator is dereferenced, is:
  38. * @code const basic_resolver_entry<InternetProtocol> @endcode
  39. *
  40. * @par Thread Safety
  41. * @e Distinct @e objects: Safe.@n
  42. * @e Shared @e objects: Unsafe.
  43. */
  44. template <typename InternetProtocol>
  45. class basic_resolver_iterator
  46. {
  47. public:
  48. /// The type used for the distance between two iterators.
  49. typedef std::ptrdiff_t difference_type;
  50. /// The type of the value pointed to by the iterator.
  51. typedef basic_resolver_entry<InternetProtocol> value_type;
  52. /// The type of the result of applying operator->() to the iterator.
  53. typedef const basic_resolver_entry<InternetProtocol>* pointer;
  54. /// The type of the result of applying operator*() to the iterator.
  55. typedef const basic_resolver_entry<InternetProtocol>& reference;
  56. /// The iterator category.
  57. typedef std::forward_iterator_tag iterator_category;
  58. /// Default constructor creates an end iterator.
  59. basic_resolver_iterator()
  60. : index_(0)
  61. {
  62. }
  63. /// Copy constructor.
  64. basic_resolver_iterator(const basic_resolver_iterator& other)
  65. : values_(other.values_),
  66. index_(other.index_)
  67. {
  68. }
  69. /// Move constructor.
  70. basic_resolver_iterator(basic_resolver_iterator&& other)
  71. : values_(static_cast<values_ptr_type&&>(other.values_)),
  72. index_(other.index_)
  73. {
  74. other.index_ = 0;
  75. }
  76. /// Assignment operator.
  77. basic_resolver_iterator& operator=(const basic_resolver_iterator& other)
  78. {
  79. values_ = other.values_;
  80. index_ = other.index_;
  81. return *this;
  82. }
  83. /// Move-assignment operator.
  84. basic_resolver_iterator& operator=(basic_resolver_iterator&& other)
  85. {
  86. if (this != &other)
  87. {
  88. values_ = static_cast<values_ptr_type&&>(other.values_);
  89. index_ = other.index_;
  90. other.index_ = 0;
  91. }
  92. return *this;
  93. }
  94. /// Dereference an iterator.
  95. const basic_resolver_entry<InternetProtocol>& operator*() const
  96. {
  97. return dereference();
  98. }
  99. /// Dereference an iterator.
  100. const basic_resolver_entry<InternetProtocol>* operator->() const
  101. {
  102. return &dereference();
  103. }
  104. /// Increment operator (prefix).
  105. basic_resolver_iterator& operator++()
  106. {
  107. increment();
  108. return *this;
  109. }
  110. /// Increment operator (postfix).
  111. basic_resolver_iterator operator++(int)
  112. {
  113. basic_resolver_iterator tmp(*this);
  114. ++*this;
  115. return tmp;
  116. }
  117. /// Test two iterators for equality.
  118. friend bool operator==(const basic_resolver_iterator& a,
  119. const basic_resolver_iterator& b)
  120. {
  121. return a.equal(b);
  122. }
  123. /// Test two iterators for inequality.
  124. friend bool operator!=(const basic_resolver_iterator& a,
  125. const basic_resolver_iterator& b)
  126. {
  127. return !a.equal(b);
  128. }
  129. protected:
  130. void increment()
  131. {
  132. if (++index_ == values_->size())
  133. {
  134. // Reset state to match a default constructed end iterator.
  135. values_.reset();
  136. index_ = 0;
  137. }
  138. }
  139. bool equal(const basic_resolver_iterator& other) const
  140. {
  141. if (!values_ && !other.values_)
  142. return true;
  143. if (values_ != other.values_)
  144. return false;
  145. return index_ == other.index_;
  146. }
  147. const basic_resolver_entry<InternetProtocol>& dereference() const
  148. {
  149. return (*values_)[index_];
  150. }
  151. typedef std::vector<basic_resolver_entry<InternetProtocol>> values_type;
  152. typedef boost::asio::detail::shared_ptr<values_type> values_ptr_type;
  153. values_ptr_type values_;
  154. std::size_t index_;
  155. };
  156. } // namespace ip
  157. } // namespace asio
  158. } // namespace boost
  159. #include <boost/asio/detail/pop_options.hpp>
  160. #endif // BOOST_ASIO_IP_BASIC_RESOLVER_ITERATOR_HPP