basic_resolver_iterator.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // ip/basic_resolver_iterator.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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 <boost/iterator.hpp>
  17. #include <cstring>
  18. #include <string>
  19. #include <vector>
  20. #include "asio/detail/shared_ptr.hpp"
  21. #include "asio/detail/socket_ops.hpp"
  22. #include "asio/detail/socket_types.hpp"
  23. #include "asio/ip/basic_resolver_entry.hpp"
  24. #include "asio/detail/push_options.hpp"
  25. namespace asio {
  26. namespace ip {
  27. /// An iterator over the entries produced by a resolver.
  28. /**
  29. * The asio::ip::basic_resolver_iterator class template is used to define
  30. * iterators over the results returned by a resolver.
  31. *
  32. * The iterator's value_type, obtained when the iterator is dereferenced, is:
  33. * @code const basic_resolver_entry<InternetProtocol> @endcode
  34. *
  35. * @par Thread Safety
  36. * @e Distinct @e objects: Safe.@n
  37. * @e Shared @e objects: Unsafe.
  38. */
  39. template <typename InternetProtocol>
  40. class basic_resolver_iterator
  41. #if defined(GENERATING_DOCUMENTATION)
  42. : public std::iterator<
  43. #else // defined(GENERATING_DOCUMENTATION)
  44. : public boost::iterator<
  45. #endif // defined(GENERATING_DOCUMENTATION)
  46. std::forward_iterator_tag,
  47. const basic_resolver_entry<InternetProtocol> >
  48. {
  49. public:
  50. /// Default constructor creates an end iterator.
  51. basic_resolver_iterator()
  52. : index_(0)
  53. {
  54. }
  55. /// Create an iterator from an addrinfo list returned by getaddrinfo.
  56. static basic_resolver_iterator create(
  57. asio::detail::addrinfo_type* address_info,
  58. const std::string& host_name, const std::string& service_name)
  59. {
  60. basic_resolver_iterator iter;
  61. if (!address_info)
  62. return iter;
  63. std::string actual_host_name = host_name;
  64. if (address_info->ai_canonname)
  65. actual_host_name = address_info->ai_canonname;
  66. iter.values_.reset(new values_type);
  67. while (address_info)
  68. {
  69. if (address_info->ai_family == PF_INET
  70. || address_info->ai_family == PF_INET6)
  71. {
  72. using namespace std; // For memcpy.
  73. typename InternetProtocol::endpoint endpoint;
  74. endpoint.resize(static_cast<std::size_t>(address_info->ai_addrlen));
  75. memcpy(endpoint.data(), address_info->ai_addr,
  76. address_info->ai_addrlen);
  77. iter.values_->push_back(
  78. basic_resolver_entry<InternetProtocol>(endpoint,
  79. actual_host_name, service_name));
  80. }
  81. address_info = address_info->ai_next;
  82. }
  83. return iter;
  84. }
  85. /// Create an iterator from an endpoint, host name and service name.
  86. static basic_resolver_iterator create(
  87. const typename InternetProtocol::endpoint& endpoint,
  88. const std::string& host_name, const std::string& service_name)
  89. {
  90. basic_resolver_iterator iter;
  91. iter.values_.reset(new values_type);
  92. iter.values_->push_back(
  93. basic_resolver_entry<InternetProtocol>(
  94. endpoint, host_name, service_name));
  95. return iter;
  96. }
  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. private:
  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. asio::detail::shared_ptr<values_type> values_;
  156. std::size_t index_;
  157. };
  158. } // namespace ip
  159. } // namespace asio
  160. #include "asio/detail/pop_options.hpp"
  161. #endif // ASIO_IP_BASIC_RESOLVER_ITERATOR_HPP