basic_resolver_iterator.hpp 4.9 KB

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