Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-21 08:40:09

0001 //
0002 // basic_io_object.hpp
0003 // ~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2025 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_BASIC_IO_OBJECT_HPP
0012 #define BOOST_ASIO_BASIC_IO_OBJECT_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 
0020 #if !defined(BOOST_ASIO_NO_DEPRECATED) \
0021   || defined(GENERATING_DOCUMENTATION)
0022 
0023 #include <boost/asio/io_context.hpp>
0024 
0025 #include <boost/asio/detail/push_options.hpp>
0026 
0027 namespace boost {
0028 namespace asio {
0029 
0030 namespace detail
0031 {
0032   // Type trait used to determine whether a service supports move.
0033   template <typename IoObjectService>
0034   class service_has_move
0035   {
0036   private:
0037     typedef IoObjectService service_type;
0038     typedef typename service_type::implementation_type implementation_type;
0039 
0040     template <typename T, typename U>
0041     static auto asio_service_has_move_eval(T* t, U* u)
0042       -> decltype(t->move_construct(*u, *u), char());
0043     static char (&asio_service_has_move_eval(...))[2];
0044 
0045   public:
0046     static const bool value =
0047       sizeof(asio_service_has_move_eval(
0048         static_cast<service_type*>(0),
0049         static_cast<implementation_type*>(0))) == 1;
0050   };
0051 }
0052 
0053 /// (Deprecated) Base class for all I/O objects.
0054 /**
0055  * @note All I/O objects are non-copyable. However, when using C++0x, certain
0056  * I/O objects do support move construction and move assignment.
0057  */
0058 #if defined(GENERATING_DOCUMENTATION)
0059 template <typename IoObjectService>
0060 #else
0061 template <typename IoObjectService,
0062     bool Movable = detail::service_has_move<IoObjectService>::value>
0063 #endif
0064 class BOOST_ASIO_DEPRECATED_MSG("Deprecated without replacement")
0065   basic_io_object
0066 {
0067 public:
0068   /// The type of the service that will be used to provide I/O operations.
0069   typedef IoObjectService service_type;
0070 
0071   /// The underlying implementation type of I/O object.
0072   typedef typename service_type::implementation_type implementation_type;
0073 
0074   /// Get the io_context associated with the object.
0075   /**
0076    * This function may be used to obtain the io_context object that the I/O
0077    * object uses to dispatch handlers for asynchronous operations.
0078    *
0079    * @return A reference to the io_context object that the I/O object will use
0080    * to dispatch handlers. Ownership is not transferred to the caller.
0081    */
0082   boost::asio::io_context& get_io_context()
0083   {
0084     return service_.get_io_context();
0085   }
0086 
0087   /// Get the io_context associated with the object.
0088   /**
0089    * This function may be used to obtain the io_context object that the I/O
0090    * object uses to dispatch handlers for asynchronous operations.
0091    *
0092    * @return A reference to the io_context object that the I/O object will use
0093    * to dispatch handlers. Ownership is not transferred to the caller.
0094    */
0095   boost::asio::io_context& get_io_service()
0096   {
0097     return service_.get_io_context();
0098   }
0099 
0100   /// The type of the executor associated with the object.
0101   typedef boost::asio::io_context::executor_type executor_type;
0102 
0103   /// Get the executor associated with the object.
0104   executor_type get_executor() noexcept
0105   {
0106     return service_.get_io_context().get_executor();
0107   }
0108 
0109 protected:
0110   /// Construct a basic_io_object.
0111   /**
0112    * Performs:
0113    * @code get_service().construct(get_implementation()); @endcode
0114    */
0115   explicit basic_io_object(boost::asio::io_context& io_context)
0116     : service_(boost::asio::use_service<IoObjectService>(io_context))
0117   {
0118     service_.construct(implementation_);
0119   }
0120 
0121 #if defined(GENERATING_DOCUMENTATION)
0122   /// Move-construct a basic_io_object.
0123   /**
0124    * Performs:
0125    * @code get_service().move_construct(
0126    *     get_implementation(), other.get_implementation()); @endcode
0127    *
0128    * @note Available only for services that support movability,
0129    */
0130   basic_io_object(basic_io_object&& other);
0131 
0132   /// Move-assign a basic_io_object.
0133   /**
0134    * Performs:
0135    * @code get_service().move_assign(get_implementation(),
0136    *     other.get_service(), other.get_implementation()); @endcode
0137    *
0138    * @note Available only for services that support movability,
0139    */
0140   basic_io_object& operator=(basic_io_object&& other);
0141 
0142   /// Perform a converting move-construction of a basic_io_object.
0143   template <typename IoObjectService1>
0144   basic_io_object(IoObjectService1& other_service,
0145       typename IoObjectService1::implementation_type& other_implementation);
0146 #endif // defined(GENERATING_DOCUMENTATION)
0147 
0148   /// Protected destructor to prevent deletion through this type.
0149   /**
0150    * Performs:
0151    * @code get_service().destroy(get_implementation()); @endcode
0152    */
0153   ~basic_io_object()
0154   {
0155     service_.destroy(implementation_);
0156   }
0157 
0158   /// Get the service associated with the I/O object.
0159   service_type& get_service()
0160   {
0161     return service_;
0162   }
0163 
0164   /// Get the service associated with the I/O object.
0165   const service_type& get_service() const
0166   {
0167     return service_;
0168   }
0169 
0170   /// Get the underlying implementation of the I/O object.
0171   implementation_type& get_implementation()
0172   {
0173     return implementation_;
0174   }
0175 
0176   /// Get the underlying implementation of the I/O object.
0177   const implementation_type& get_implementation() const
0178   {
0179     return implementation_;
0180   }
0181 
0182 private:
0183   basic_io_object(const basic_io_object&);
0184   basic_io_object& operator=(const basic_io_object&);
0185 
0186   // The service associated with the I/O object.
0187   service_type& service_;
0188 
0189   /// The underlying implementation of the I/O object.
0190   implementation_type implementation_;
0191 };
0192 
0193 // Specialisation for movable objects.
0194 template <typename IoObjectService>
0195 class BOOST_ASIO_DEPRECATED_MSG("Deprecated without replacement")
0196   basic_io_object<IoObjectService, true>
0197 {
0198 public:
0199   typedef IoObjectService service_type;
0200   typedef typename service_type::implementation_type implementation_type;
0201 
0202   boost::asio::io_context& get_io_context()
0203   {
0204     return service_->get_io_context();
0205   }
0206 
0207   boost::asio::io_context& get_io_service()
0208   {
0209     return service_->get_io_context();
0210   }
0211 
0212   typedef boost::asio::io_context::executor_type executor_type;
0213 
0214   executor_type get_executor() noexcept
0215   {
0216     return service_->get_io_context().get_executor();
0217   }
0218 
0219 protected:
0220   explicit basic_io_object(boost::asio::io_context& io_context)
0221     : service_(&boost::asio::use_service<IoObjectService>(io_context))
0222   {
0223     service_->construct(implementation_);
0224   }
0225 
0226   basic_io_object(basic_io_object&& other)
0227     : service_(&other.get_service())
0228   {
0229     service_->move_construct(implementation_, other.implementation_);
0230   }
0231 
0232   template <typename IoObjectService1>
0233   basic_io_object(IoObjectService1& other_service,
0234       typename IoObjectService1::implementation_type& other_implementation)
0235     : service_(&boost::asio::use_service<IoObjectService>(
0236           other_service.get_io_context()))
0237   {
0238     service_->converting_move_construct(implementation_,
0239         other_service, other_implementation);
0240   }
0241 
0242   ~basic_io_object()
0243   {
0244     service_->destroy(implementation_);
0245   }
0246 
0247   basic_io_object& operator=(basic_io_object&& other)
0248   {
0249     service_->move_assign(implementation_,
0250         *other.service_, other.implementation_);
0251     service_ = other.service_;
0252     return *this;
0253   }
0254 
0255   service_type& get_service()
0256   {
0257     return *service_;
0258   }
0259 
0260   const service_type& get_service() const
0261   {
0262     return *service_;
0263   }
0264 
0265   implementation_type& get_implementation()
0266   {
0267     return implementation_;
0268   }
0269 
0270   const implementation_type& get_implementation() const
0271   {
0272     return implementation_;
0273   }
0274 
0275 private:
0276   basic_io_object(const basic_io_object&);
0277   void operator=(const basic_io_object&);
0278 
0279   IoObjectService* service_;
0280   implementation_type implementation_;
0281 };
0282 
0283 } // namespace asio
0284 } // namespace boost
0285 
0286 #include <boost/asio/detail/pop_options.hpp>
0287 
0288 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
0289        //   || defined(GENERATING_DOCUMENTATION)
0290 
0291 #endif // BOOST_ASIO_BASIC_IO_OBJECT_HPP