basic_resolver_iterator.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // ip/basic_resolver_iterator.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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. /// Move constructor.
  69. basic_resolver_iterator(basic_resolver_iterator&& other)
  70. : values_(static_cast<values_ptr_type&&>(other.values_)),
  71. index_(other.index_)
  72. {
  73. other.index_ = 0;
  74. }
  75. /// Assignment operator.
  76. basic_resolver_iterator& operator=(const basic_resolver_iterator& other)
  77. {
  78. values_ = other.values_;
  79. index_ = other.index_;
  80. return *this;
  81. }
  82. /// Move-assignment operator.
  83. basic_resolver_iterator& operator=(basic_resolver_iterator&& other)
  84. {
  85. if (this != &other)
  86. {
  87. values_ = static_cast<values_ptr_type&&>(other.values_);
  88. index_ = other.index_;
  89. other.index_ = 0;
  90. }
  91. return *this;
  92. }
  93. /// Dereference an iterator.
  94. const basic_resolver_entry<InternetProtocol>& operator*() const
  95. {
  96. return dereference();
  97. }
  98. /// Dereference an iterator.
  99. const basic_resolver_entry<InternetProtocol>* operator->() const
  100. {
  101. return &dereference();
  102. }
  103. /// Increment operator (prefix).
  104. basic_resolver_iterator& operator++()
  105. {
  106. increment();
  107. return *this;
  108. }
  109. /// Increment operator (postfix).
  110. basic_resolver_iterator operator++(int)
  111. {
  112. basic_resolver_iterator tmp(*this);
  113. ++*this;
  114. return tmp;
  115. }
  116. /// Test two iterators for equality.
  117. friend bool operator==(const basic_resolver_iterator& a,
  118. const basic_resolver_iterator& b)
  119. {
  120. return a.equal(b);
  121. }
  122. /// Test two iterators for inequality.
  123. friend bool operator!=(const basic_resolver_iterator& a,
  124. const basic_resolver_iterator& b)
  125. {
  126. return !a.equal(b);
  127. }
  128. protected:
  129. void increment()
  130. {
  131. if (++index_ == values_->size())
  132. {
  133. // Reset state to match a default constructed end iterator.
  134. values_.reset();
  135. index_ = 0;
  136. }
  137. }
  138. bool equal(const basic_resolver_iterator& other) const
  139. {
  140. if (!values_ && !other.values_)
  141. return true;
  142. if (values_ != other.values_)
  143. return false;
  144. return index_ == other.index_;
  145. }
  146. const basic_resolver_entry<InternetProtocol>& dereference() const
  147. {
  148. return (*values_)[index_];
  149. }
  150. typedef std::vector<basic_resolver_entry<InternetProtocol>> values_type;
  151. typedef asio::detail::shared_ptr<values_type> values_ptr_type;
  152. values_ptr_type values_;
  153. std::size_t index_;
  154. };
  155. } // namespace ip
  156. } // namespace asio
  157. #include "asio/detail/pop_options.hpp"
  158. #endif // ASIO_IP_BASIC_RESOLVER_ITERATOR_HPP