Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // ip/basic_resolver_iterator.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_ITERATOR_HPP
0012 #define BOOST_ASIO_IP_BASIC_RESOLVER_ITERATOR_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 <iterator>
0022 #include <string>
0023 #include <vector>
0024 #include <boost/asio/detail/memory.hpp>
0025 #include <boost/asio/detail/socket_ops.hpp>
0026 #include <boost/asio/detail/socket_types.hpp>
0027 #include <boost/asio/ip/basic_resolver_entry.hpp>
0028 
0029 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
0030 # include <boost/asio/detail/winrt_utils.hpp>
0031 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
0032 
0033 #include <boost/asio/detail/push_options.hpp>
0034 
0035 namespace boost {
0036 namespace asio {
0037 namespace ip {
0038 
0039 /// An iterator over the entries produced by a resolver.
0040 /**
0041  * The boost::asio::ip::basic_resolver_iterator class template is used to define
0042  * iterators over the results returned by a resolver.
0043  *
0044  * The iterator's value_type, obtained when the iterator is dereferenced, is:
0045  * @code const basic_resolver_entry<InternetProtocol> @endcode
0046  *
0047  * @par Thread Safety
0048  * @e Distinct @e objects: Safe.@n
0049  * @e Shared @e objects: Unsafe.
0050  */
0051 template <typename InternetProtocol>
0052 class basic_resolver_iterator
0053 {
0054 public:
0055   /// The type used for the distance between two iterators.
0056   typedef std::ptrdiff_t difference_type;
0057 
0058   /// The type of the value pointed to by the iterator.
0059   typedef basic_resolver_entry<InternetProtocol> value_type;
0060 
0061   /// The type of the result of applying operator->() to the iterator.
0062   typedef const basic_resolver_entry<InternetProtocol>* pointer;
0063 
0064   /// The type of the result of applying operator*() to the iterator.
0065   typedef const basic_resolver_entry<InternetProtocol>& reference;
0066 
0067   /// The iterator category.
0068   typedef std::forward_iterator_tag iterator_category;
0069 
0070   /// Default constructor creates an end iterator.
0071   basic_resolver_iterator()
0072     : index_(0)
0073   {
0074   }
0075 
0076   /// Copy constructor.
0077   basic_resolver_iterator(const basic_resolver_iterator& other)
0078     : values_(other.values_),
0079       index_(other.index_)
0080   {
0081   }
0082 
0083   /// Move constructor.
0084   basic_resolver_iterator(basic_resolver_iterator&& other)
0085     : values_(static_cast<values_ptr_type&&>(other.values_)),
0086       index_(other.index_)
0087   {
0088     other.index_ = 0;
0089   }
0090 
0091   /// Assignment operator.
0092   basic_resolver_iterator& operator=(const basic_resolver_iterator& other)
0093   {
0094     values_ = other.values_;
0095     index_ = other.index_;
0096     return *this;
0097   }
0098 
0099   /// Move-assignment operator.
0100   basic_resolver_iterator& operator=(basic_resolver_iterator&& other)
0101   {
0102     if (this != &other)
0103     {
0104       values_ = static_cast<values_ptr_type&&>(other.values_);
0105       index_ = other.index_;
0106       other.index_ = 0;
0107     }
0108 
0109     return *this;
0110   }
0111 
0112   /// Dereference an iterator.
0113   const basic_resolver_entry<InternetProtocol>& operator*() const
0114   {
0115     return dereference();
0116   }
0117 
0118   /// Dereference an iterator.
0119   const basic_resolver_entry<InternetProtocol>* operator->() const
0120   {
0121     return &dereference();
0122   }
0123 
0124   /// Increment operator (prefix).
0125   basic_resolver_iterator& operator++()
0126   {
0127     increment();
0128     return *this;
0129   }
0130 
0131   /// Increment operator (postfix).
0132   basic_resolver_iterator operator++(int)
0133   {
0134     basic_resolver_iterator tmp(*this);
0135     ++*this;
0136     return tmp;
0137   }
0138 
0139   /// Test two iterators for equality.
0140   friend bool operator==(const basic_resolver_iterator& a,
0141       const basic_resolver_iterator& b)
0142   {
0143     return a.equal(b);
0144   }
0145 
0146   /// Test two iterators for inequality.
0147   friend bool operator!=(const basic_resolver_iterator& a,
0148       const basic_resolver_iterator& b)
0149   {
0150     return !a.equal(b);
0151   }
0152 
0153 protected:
0154   void increment()
0155   {
0156     if (++index_ == values_->size())
0157     {
0158       // Reset state to match a default constructed end iterator.
0159       values_.reset();
0160       index_ = 0;
0161     }
0162   }
0163 
0164   bool equal(const basic_resolver_iterator& other) const
0165   {
0166     if (!values_ && !other.values_)
0167       return true;
0168     if (values_ != other.values_)
0169       return false;
0170     return index_ == other.index_;
0171   }
0172 
0173   const basic_resolver_entry<InternetProtocol>& dereference() const
0174   {
0175     return (*values_)[index_];
0176   }
0177 
0178   typedef std::vector<basic_resolver_entry<InternetProtocol>> values_type;
0179   typedef boost::asio::detail::shared_ptr<values_type> values_ptr_type;
0180   values_ptr_type values_;
0181   std::size_t index_;
0182 };
0183 
0184 } // namespace ip
0185 } // namespace asio
0186 } // namespace boost
0187 
0188 #include <boost/asio/detail/pop_options.hpp>
0189 
0190 #endif // BOOST_ASIO_IP_BASIC_RESOLVER_ITERATOR_HPP