basic_resolver_iterator.hpp 5.0 KB

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