Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:00

0001 //
0002 // ip/basic_resolver_results.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_IP_BASIC_RESOLVER_RESULTS_HPP
0012 #define BOOST_ASIO_IP_BASIC_RESOLVER_RESULTS_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 #include <cstddef>
0020 #include <cstring>
0021 #include <boost/asio/detail/socket_ops.hpp>
0022 #include <boost/asio/detail/socket_types.hpp>
0023 #include <boost/asio/ip/basic_resolver_iterator.hpp>
0024 
0025 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
0026 # include <boost/asio/detail/winrt_utils.hpp>
0027 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
0028 
0029 #include <boost/asio/detail/push_options.hpp>
0030 
0031 namespace boost {
0032 namespace asio {
0033 namespace ip {
0034 
0035 /// A range of entries produced by a resolver.
0036 /**
0037  * The boost::asio::ip::basic_resolver_results class template is used to define
0038  * a range over the results returned by a resolver.
0039  *
0040  * The iterator's value_type, obtained when a results iterator is dereferenced,
0041  * is: @code const basic_resolver_entry<InternetProtocol> @endcode
0042  *
0043  * @note For backward compatibility, basic_resolver_results is derived from
0044  * basic_resolver_iterator. This derivation is deprecated.
0045  *
0046  * @par Thread Safety
0047  * @e Distinct @e objects: Safe.@n
0048  * @e Shared @e objects: Unsafe.
0049  */
0050 template <typename InternetProtocol>
0051 class basic_resolver_results
0052 #if !defined(BOOST_ASIO_NO_DEPRECATED)
0053   : public basic_resolver_iterator<InternetProtocol>
0054 #else // !defined(BOOST_ASIO_NO_DEPRECATED)
0055   : private basic_resolver_iterator<InternetProtocol>
0056 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
0057 {
0058 public:
0059   /// The protocol type associated with the results.
0060   typedef InternetProtocol protocol_type;
0061 
0062   /// The endpoint type associated with the results.
0063   typedef typename protocol_type::endpoint endpoint_type;
0064 
0065   /// The type of a value in the results range.
0066   typedef basic_resolver_entry<protocol_type> value_type;
0067 
0068   /// The type of a const reference to a value in the range.
0069   typedef const value_type& const_reference;
0070 
0071   /// The type of a non-const reference to a value in the range.
0072   typedef value_type& reference;
0073 
0074   /// The type of an iterator into the range.
0075   typedef basic_resolver_iterator<protocol_type> const_iterator;
0076 
0077   /// The type of an iterator into the range.
0078   typedef const_iterator iterator;
0079 
0080   /// Type used to represent the distance between two iterators in the range.
0081   typedef std::ptrdiff_t difference_type;
0082 
0083   /// Type used to represent a count of the elements in the range.
0084   typedef std::size_t size_type;
0085 
0086   /// Default constructor creates an empty range.
0087   basic_resolver_results()
0088   {
0089   }
0090 
0091   /// Copy constructor.
0092   basic_resolver_results(const basic_resolver_results& other)
0093     : basic_resolver_iterator<InternetProtocol>(other)
0094   {
0095   }
0096 
0097   /// Move constructor.
0098   basic_resolver_results(basic_resolver_results&& other)
0099     : basic_resolver_iterator<InternetProtocol>(
0100         static_cast<basic_resolver_results&&>(other))
0101   {
0102   }
0103 
0104   /// Assignment operator.
0105   basic_resolver_results& operator=(const basic_resolver_results& other)
0106   {
0107     basic_resolver_iterator<InternetProtocol>::operator=(other);
0108     return *this;
0109   }
0110 
0111   /// Move-assignment operator.
0112   basic_resolver_results& operator=(basic_resolver_results&& other)
0113   {
0114     basic_resolver_iterator<InternetProtocol>::operator=(
0115         static_cast<basic_resolver_results&&>(other));
0116     return *this;
0117   }
0118 
0119 #if !defined(GENERATING_DOCUMENTATION)
0120   // Create results from an addrinfo list returned by getaddrinfo.
0121   static basic_resolver_results create(
0122       boost::asio::detail::addrinfo_type* address_info,
0123       const std::string& host_name, const std::string& service_name)
0124   {
0125     basic_resolver_results results;
0126     if (!address_info)
0127       return results;
0128 
0129     std::string actual_host_name = host_name;
0130     if (address_info->ai_canonname)
0131       actual_host_name = address_info->ai_canonname;
0132 
0133     results.values_.reset(new values_type);
0134 
0135     while (address_info)
0136     {
0137       if (address_info->ai_family == BOOST_ASIO_OS_DEF(AF_INET)
0138           || address_info->ai_family == BOOST_ASIO_OS_DEF(AF_INET6))
0139       {
0140         using namespace std; // For memcpy.
0141         typename InternetProtocol::endpoint endpoint;
0142         endpoint.resize(static_cast<std::size_t>(address_info->ai_addrlen));
0143         memcpy(endpoint.data(), address_info->ai_addr,
0144             address_info->ai_addrlen);
0145         results.values_->push_back(
0146             basic_resolver_entry<InternetProtocol>(endpoint,
0147               actual_host_name, service_name));
0148       }
0149       address_info = address_info->ai_next;
0150     }
0151 
0152     return results;
0153   }
0154 
0155   // Create results from an endpoint, host name and service name.
0156   static basic_resolver_results create(const endpoint_type& endpoint,
0157       const std::string& host_name, const std::string& service_name)
0158   {
0159     basic_resolver_results results;
0160     results.values_.reset(new values_type);
0161     results.values_->push_back(
0162         basic_resolver_entry<InternetProtocol>(
0163           endpoint, host_name, service_name));
0164     return results;
0165   }
0166 
0167   // Create results from a sequence of endpoints, host and service name.
0168   template <typename EndpointIterator>
0169   static basic_resolver_results create(
0170       EndpointIterator begin, EndpointIterator end,
0171       const std::string& host_name, const std::string& service_name)
0172   {
0173     basic_resolver_results results;
0174     if (begin != end)
0175     {
0176       results.values_.reset(new values_type);
0177       for (EndpointIterator ep_iter = begin; ep_iter != end; ++ep_iter)
0178       {
0179         results.values_->push_back(
0180             basic_resolver_entry<InternetProtocol>(
0181               *ep_iter, host_name, service_name));
0182       }
0183     }
0184     return results;
0185   }
0186 
0187 # if defined(BOOST_ASIO_WINDOWS_RUNTIME)
0188   // Create results from a Windows Runtime list of EndpointPair objects.
0189   static basic_resolver_results create(
0190       Windows::Foundation::Collections::IVectorView<
0191         Windows::Networking::EndpointPair^>^ endpoints,
0192       const boost::asio::detail::addrinfo_type& hints,
0193       const std::string& host_name, const std::string& service_name)
0194   {
0195     basic_resolver_results results;
0196     if (endpoints->Size)
0197     {
0198       results.values_.reset(new values_type);
0199       for (unsigned int i = 0; i < endpoints->Size; ++i)
0200       {
0201         auto pair = endpoints->GetAt(i);
0202 
0203         if (hints.ai_family == BOOST_ASIO_OS_DEF(AF_INET)
0204             && pair->RemoteHostName->Type
0205               != Windows::Networking::HostNameType::Ipv4)
0206           continue;
0207 
0208         if (hints.ai_family == BOOST_ASIO_OS_DEF(AF_INET6)
0209             && pair->RemoteHostName->Type
0210               != Windows::Networking::HostNameType::Ipv6)
0211           continue;
0212 
0213         results.values_->push_back(
0214             basic_resolver_entry<InternetProtocol>(
0215               typename InternetProtocol::endpoint(
0216                 ip::make_address(
0217                   boost::asio::detail::winrt_utils::string(
0218                     pair->RemoteHostName->CanonicalName)),
0219                 boost::asio::detail::winrt_utils::integer(
0220                   pair->RemoteServiceName)),
0221               host_name, service_name));
0222       }
0223     }
0224     return results;
0225   }
0226 # endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
0227 #endif // !defined(GENERATING_DOCUMENTATION)
0228 
0229   /// Get the number of entries in the results range.
0230   size_type size() const noexcept
0231   {
0232     return this->values_ ? this->values_->size() : 0;
0233   }
0234 
0235   /// Get the maximum number of entries permitted in a results range.
0236   size_type max_size() const noexcept
0237   {
0238     return this->values_ ? this->values_->max_size() : values_type().max_size();
0239   }
0240 
0241   /// Determine whether the results range is empty.
0242   bool empty() const noexcept
0243   {
0244     return this->values_ ? this->values_->empty() : true;
0245   }
0246 
0247   /// Obtain a begin iterator for the results range.
0248   const_iterator begin() const
0249   {
0250     basic_resolver_results tmp(*this);
0251     tmp.index_ = 0;
0252     return static_cast<basic_resolver_results&&>(tmp);
0253   }
0254 
0255   /// Obtain an end iterator for the results range.
0256   const_iterator end() const
0257   {
0258     return const_iterator();
0259   }
0260 
0261   /// Obtain a begin iterator for the results range.
0262   const_iterator cbegin() const
0263   {
0264     return begin();
0265   }
0266 
0267   /// Obtain an end iterator for the results range.
0268   const_iterator cend() const
0269   {
0270     return end();
0271   }
0272 
0273   /// Swap the results range with another.
0274   void swap(basic_resolver_results& that) noexcept
0275   {
0276     if (this != &that)
0277     {
0278       this->values_.swap(that.values_);
0279       std::size_t index = this->index_;
0280       this->index_ = that.index_;
0281       that.index_ = index;
0282     }
0283   }
0284 
0285   /// Test two iterators for equality.
0286   friend bool operator==(const basic_resolver_results& a,
0287       const basic_resolver_results& b)
0288   {
0289     return a.equal(b);
0290   }
0291 
0292   /// Test two iterators for inequality.
0293   friend bool operator!=(const basic_resolver_results& a,
0294       const basic_resolver_results& b)
0295   {
0296     return !a.equal(b);
0297   }
0298 
0299 private:
0300   typedef std::vector<basic_resolver_entry<InternetProtocol>> values_type;
0301 };
0302 
0303 } // namespace ip
0304 } // namespace asio
0305 } // namespace boost
0306 
0307 #include <boost/asio/detail/pop_options.hpp>
0308 
0309 #endif // BOOST_ASIO_IP_BASIC_RESOLVER_RESULTS_HPP