Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

basic_resolver_results.hpp 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. //
  2. // ip/basic_resolver_results.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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_RESULTS_HPP
  11. #define ASIO_IP_BASIC_RESOLVER_RESULTS_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 "asio/detail/socket_ops.hpp"
  19. #include "asio/detail/socket_types.hpp"
  20. #include "asio/ip/basic_resolver_iterator.hpp"
  21. #if defined(ASIO_WINDOWS_RUNTIME)
  22. # include "asio/detail/winrt_utils.hpp"
  23. #endif // defined(ASIO_WINDOWS_RUNTIME)
  24. #include "asio/detail/push_options.hpp"
  25. namespace asio {
  26. namespace ip {
  27. /// A range of entries produced by a resolver.
  28. /**
  29. * The asio::ip::basic_resolver_results class template is used to define
  30. * a range over the results returned by a resolver.
  31. *
  32. * The iterator's value_type, obtained when a results iterator is dereferenced,
  33. * is: @code const basic_resolver_entry<InternetProtocol> @endcode
  34. *
  35. * @note For backward compatibility, basic_resolver_results is derived from
  36. * basic_resolver_iterator. This derivation is deprecated.
  37. *
  38. * @par Thread Safety
  39. * @e Distinct @e objects: Safe.@n
  40. * @e Shared @e objects: Unsafe.
  41. */
  42. template <typename InternetProtocol>
  43. class basic_resolver_results
  44. #if !defined(ASIO_NO_DEPRECATED)
  45. : public basic_resolver_iterator<InternetProtocol>
  46. #else // !defined(ASIO_NO_DEPRECATED)
  47. : private basic_resolver_iterator<InternetProtocol>
  48. #endif // !defined(ASIO_NO_DEPRECATED)
  49. {
  50. public:
  51. /// The protocol type associated with the results.
  52. typedef InternetProtocol protocol_type;
  53. /// The endpoint type associated with the results.
  54. typedef typename protocol_type::endpoint endpoint_type;
  55. /// The type of a value in the results range.
  56. typedef basic_resolver_entry<endpoint_type> value_type;
  57. /// The type of a const reference to a value in the range.
  58. typedef const value_type& const_reference;
  59. /// The type of a non-const reference to a value in the range.
  60. typedef value_type& reference;
  61. /// The type of an iterator into the range.
  62. typedef basic_resolver_iterator<protocol_type> const_iterator;
  63. /// The type of an iterator into the range.
  64. typedef const_iterator iterator;
  65. /// Type used to represent the distance between two iterators in the range.
  66. typedef std::ptrdiff_t difference_type;
  67. /// Type used to represent a count of the elements in the range.
  68. typedef std::size_t size_type;
  69. /// Default constructor creates an empty range.
  70. basic_resolver_results()
  71. {
  72. }
  73. /// Copy constructor.
  74. basic_resolver_results(const basic_resolver_results& other)
  75. : basic_resolver_iterator<InternetProtocol>(other)
  76. {
  77. }
  78. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  79. /// Move constructor.
  80. basic_resolver_results(basic_resolver_results&& other)
  81. : basic_resolver_iterator<InternetProtocol>(
  82. ASIO_MOVE_CAST(basic_resolver_results)(other))
  83. {
  84. }
  85. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  86. /// Assignment operator.
  87. basic_resolver_results& operator=(const basic_resolver_results& other)
  88. {
  89. basic_resolver_iterator<InternetProtocol>::operator=(other);
  90. return *this;
  91. }
  92. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  93. /// Move-assignment operator.
  94. basic_resolver_results& operator=(basic_resolver_results&& other)
  95. {
  96. basic_resolver_iterator<InternetProtocol>::operator=(
  97. ASIO_MOVE_CAST(basic_resolver_results)(other));
  98. return *this;
  99. }
  100. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  101. #if !defined(GENERATING_DOCUMENTATION)
  102. // Create results from an addrinfo list returned by getaddrinfo.
  103. static basic_resolver_results create(
  104. asio::detail::addrinfo_type* address_info,
  105. const std::string& host_name, const std::string& service_name)
  106. {
  107. basic_resolver_results results;
  108. if (!address_info)
  109. return results;
  110. std::string actual_host_name = host_name;
  111. if (address_info->ai_canonname)
  112. actual_host_name = address_info->ai_canonname;
  113. results.values_.reset(new values_type);
  114. while (address_info)
  115. {
  116. if (address_info->ai_family == ASIO_OS_DEF(AF_INET)
  117. || address_info->ai_family == ASIO_OS_DEF(AF_INET6))
  118. {
  119. using namespace std; // For memcpy.
  120. typename InternetProtocol::endpoint endpoint;
  121. endpoint.resize(static_cast<std::size_t>(address_info->ai_addrlen));
  122. memcpy(endpoint.data(), address_info->ai_addr,
  123. address_info->ai_addrlen);
  124. results.values_->push_back(
  125. basic_resolver_entry<InternetProtocol>(endpoint,
  126. actual_host_name, service_name));
  127. }
  128. address_info = address_info->ai_next;
  129. }
  130. return results;
  131. }
  132. // Create results from an endpoint, host name and service name.
  133. static basic_resolver_results create(const endpoint_type& endpoint,
  134. const std::string& host_name, const std::string& service_name)
  135. {
  136. basic_resolver_results results;
  137. results.values_.reset(new values_type);
  138. results.values_->push_back(
  139. basic_resolver_entry<InternetProtocol>(
  140. endpoint, host_name, service_name));
  141. return results;
  142. }
  143. // Create results from a sequence of endpoints, host and service name.
  144. template <typename EndpointIterator>
  145. static basic_resolver_results create(
  146. EndpointIterator begin, EndpointIterator end,
  147. const std::string& host_name, const std::string& service_name)
  148. {
  149. basic_resolver_results results;
  150. if (begin != end)
  151. {
  152. results.values_.reset(new values_type);
  153. for (EndpointIterator ep_iter = begin; ep_iter != end; ++ep_iter)
  154. {
  155. results.values_->push_back(
  156. basic_resolver_entry<InternetProtocol>(
  157. *ep_iter, host_name, service_name));
  158. }
  159. }
  160. return results;
  161. }
  162. # if defined(ASIO_WINDOWS_RUNTIME)
  163. // Create results from a Windows Runtime list of EndpointPair objects.
  164. static basic_resolver_results create(
  165. Windows::Foundation::Collections::IVectorView<
  166. Windows::Networking::EndpointPair^>^ endpoints,
  167. const asio::detail::addrinfo_type& hints,
  168. const std::string& host_name, const std::string& service_name)
  169. {
  170. basic_resolver_results results;
  171. if (endpoints->Size)
  172. {
  173. results.values_.reset(new values_type);
  174. for (unsigned int i = 0; i < endpoints->Size; ++i)
  175. {
  176. auto pair = endpoints->GetAt(i);
  177. if (hints.ai_family == ASIO_OS_DEF(AF_INET)
  178. && pair->RemoteHostName->Type
  179. != Windows::Networking::HostNameType::Ipv4)
  180. continue;
  181. if (hints.ai_family == ASIO_OS_DEF(AF_INET6)
  182. && pair->RemoteHostName->Type
  183. != Windows::Networking::HostNameType::Ipv6)
  184. continue;
  185. results.values_->push_back(
  186. basic_resolver_entry<InternetProtocol>(
  187. typename InternetProtocol::endpoint(
  188. ip::make_address(
  189. asio::detail::winrt_utils::string(
  190. pair->RemoteHostName->CanonicalName)),
  191. asio::detail::winrt_utils::integer(
  192. pair->RemoteServiceName)),
  193. host_name, service_name));
  194. }
  195. }
  196. return results;
  197. }
  198. # endif // defined(ASIO_WINDOWS_RUNTIME)
  199. #endif // !defined(GENERATING_DOCUMENTATION)
  200. /// Get the number of entries in the results range.
  201. size_type size() const ASIO_NOEXCEPT
  202. {
  203. return this->values_->size();
  204. }
  205. /// Get the maximum number of entries permitted in a results range.
  206. size_type max_size() const ASIO_NOEXCEPT
  207. {
  208. return this->values_->max_size();
  209. }
  210. /// Determine whether the results range is empty.
  211. bool empty() const ASIO_NOEXCEPT
  212. {
  213. return this->values_->empty();
  214. }
  215. /// Obtain a begin iterator for the results range.
  216. const_iterator begin() const
  217. {
  218. basic_resolver_results tmp(*this);
  219. tmp.index_ = 0;
  220. return tmp;
  221. }
  222. /// Obtain an end iterator for the results range.
  223. const_iterator end() const
  224. {
  225. return const_iterator();
  226. }
  227. /// Obtain a begin iterator for the results range.
  228. const_iterator cbegin() const
  229. {
  230. return begin();
  231. }
  232. /// Obtain an end iterator for the results range.
  233. const_iterator cend() const
  234. {
  235. return end();
  236. }
  237. /// Swap the results range with another.
  238. void swap(basic_resolver_results& that) ASIO_NOEXCEPT
  239. {
  240. if (this != &that)
  241. {
  242. this->values_.swap(that.values_);
  243. std::size_t index = this->index_;
  244. this->index_ = that.index_;
  245. that.index_ = index;
  246. }
  247. }
  248. /// Test two iterators for equality.
  249. friend bool operator==(const basic_resolver_results& a,
  250. const basic_resolver_results& b)
  251. {
  252. return a.equal(b);
  253. }
  254. /// Test two iterators for inequality.
  255. friend bool operator!=(const basic_resolver_results& a,
  256. const basic_resolver_results& b)
  257. {
  258. return !a.equal(b);
  259. }
  260. private:
  261. typedef std::vector<basic_resolver_entry<InternetProtocol> > values_type;
  262. };
  263. } // namespace ip
  264. } // namespace asio
  265. #include "asio/detail/pop_options.hpp"
  266. #endif // ASIO_IP_BASIC_RESOLVER_RESULTS_HPP