Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 08:35:32

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 basic_io_object
0065 {
0066 public:
0067   /// The type of the service that will be used to provide I/O operations.
0068   typedef IoObjectService service_type;
0069 
0070   /// The underlying implementation type of I/O object.
0071   typedef typename service_type::implementation_type implementation_type;
0072 
0073   /// Get the io_context associated with the object.
0074   /**
0075    * This function may be used to obtain the io_context object that the I/O
0076    * object uses to dispatch handlers for asynchronous operations.
0077    *
0078    * @return A reference to the io_context object that the I/O object will use
0079    * to dispatch handlers. Ownership is not transferred to the caller.
0080    */
0081   boost::asio::io_context& get_io_context()
0082   {
0083     return service_.get_io_context();
0084   }
0085 
0086   /// Get the io_context associated with the object.
0087   /**
0088    * This function may be used to obtain the io_context object that the I/O
0089    * object uses to dispatch handlers for asynchronous operations.
0090    *
0091    * @return A reference to the io_context object that the I/O object will use
0092    * to dispatch handlers. Ownership is not transferred to the caller.
0093    */
0094   boost::asio::io_context& get_io_service()
0095   {
0096     return service_.get_io_context();
0097   }
0098 
0099   /// The type of the executor associated with the object.
0100   typedef boost::asio::io_context::executor_type executor_type;
0101 
0102   /// Get the executor associated with the object.
0103   executor_type get_executor() noexcept
0104   {
0105     return service_.get_io_context().get_executor();
0106   }
0107 
0108 protected:
0109   /// Construct a basic_io_object.
0110   /**
0111    * Performs:
0112    * @code get_service().construct(get_implementation()); @endcode
0113    */
0114   explicit basic_io_object(boost::asio::io_context& io_context)
0115     : service_(boost::asio::use_service<IoObjectService>(io_context))
0116   {
0117     service_.construct(implementation_);
0118   }
0119 
0120 #if defined(GENERATING_DOCUMENTATION)
0121   /// Move-construct a basic_io_object.
0122   /**
0123    * Performs:
0124    * @code get_service().move_construct(
0125    *     get_implementation(), other.get_implementation()); @endcode
0126    *
0127    * @note Available only for services that support movability,
0128    */
0129   basic_io_object(basic_io_object&& other);
0130 
0131   /// Move-assign a basic_io_object.
0132   /**
0133    * Performs:
0134    * @code get_service().move_assign(get_implementation(),
0135    *     other.get_service(), other.get_implementation()); @endcode
0136    *
0137    * @note Available only for services that support movability,
0138    */
0139   basic_io_object& operator=(basic_io_object&& other);
0140 
0141   /// Perform a converting move-construction of a basic_io_object.
0142   template <typename IoObjectService1>
0143   basic_io_object(IoObjectService1& other_service,
0144       typename IoObjectService1::implementation_type& other_implementation);
0145 #endif // defined(GENERATING_DOCUMENTATION)
0146 
0147   /// Protected destructor to prevent deletion through this type.
0148   /**
0149    * Performs:
0150    * @code get_service().destroy(get_implementation()); @endcode
0151    */
0152   ~basic_io_object()
0153   {
0154     service_.destroy(implementation_);
0155   }
0156 
0157   /// Get the service associated with the I/O object.
0158   service_type& get_service()
0159   {
0160     return service_;
0161   }
0162 
0163   /// Get the service associated with the I/O object.
0164   const service_type& get_service() const
0165   {
0166     return service_;
0167   }
0168 
0169   /// Get the underlying implementation of the I/O object.
0170   implementation_type& get_implementation()
0171   {
0172     return implementation_;
0173   }
0174 
0175   /// Get the underlying implementation of the I/O object.
0176   const implementation_type& get_implementation() const
0177   {
0178     return implementation_;
0179   }
0180 
0181 private:
0182   basic_io_object(const basic_io_object&);
0183   basic_io_object& operator=(const basic_io_object&);
0184 
0185   // The service associated with the I/O object.
0186   service_type& service_;
0187 
0188   /// The underlying implementation of the I/O object.
0189   implementation_type implementation_;
0190 };
0191 
0192 // Specialisation for movable objects.
0193 template <typename IoObjectService>
0194 class basic_io_object<IoObjectService, true>
0195 {
0196 public:
0197   typedef IoObjectService service_type;
0198   typedef typename service_type::implementation_type implementation_type;
0199 
0200   boost::asio::io_context& get_io_context()
0201   {
0202     return service_->get_io_context();
0203   }
0204 
0205   boost::asio::io_context& get_io_service()
0206   {
0207     return service_->get_io_context();
0208   }
0209 
0210   typedef boost::asio::io_context::executor_type executor_type;
0211 
0212   executor_type get_executor() noexcept
0213   {
0214     return service_->get_io_context().get_executor();
0215   }
0216 
0217 protected:
0218   explicit basic_io_object(boost::asio::io_context& io_context)
0219     : service_(&boost::asio::use_service<IoObjectService>(io_context))
0220   {
0221     service_->construct(implementation_);
0222   }
0223 
0224   basic_io_object(basic_io_object&& other)
0225     : service_(&other.get_service())
0226   {
0227     service_->move_construct(implementation_, other.implementation_);
0228   }
0229 
0230   template <typename IoObjectService1>
0231   basic_io_object(IoObjectService1& other_service,
0232       typename IoObjectService1::implementation_type& other_implementation)
0233     : service_(&boost::asio::use_service<IoObjectService>(
0234           other_service.get_io_context()))
0235   {
0236     service_->converting_move_construct(implementation_,
0237         other_service, other_implementation);
0238   }
0239 
0240   ~basic_io_object()
0241   {
0242     service_->destroy(implementation_);
0243   }
0244 
0245   basic_io_object& operator=(basic_io_object&& other)
0246   {
0247     service_->move_assign(implementation_,
0248         *other.service_, other.implementation_);
0249     service_ = other.service_;
0250     return *this;
0251   }
0252 
0253   service_type& get_service()
0254   {
0255     return *service_;
0256   }
0257 
0258   const service_type& get_service() const
0259   {
0260     return *service_;
0261   }
0262 
0263   implementation_type& get_implementation()
0264   {
0265     return implementation_;
0266   }
0267 
0268   const implementation_type& get_implementation() const
0269   {
0270     return implementation_;
0271   }
0272 
0273 private:
0274   basic_io_object(const basic_io_object&);
0275   void operator=(const basic_io_object&);
0276 
0277   IoObjectService* service_;
0278   implementation_type implementation_;
0279 };
0280 
0281 } // namespace asio
0282 } // namespace boost
0283 
0284 #include <boost/asio/detail/pop_options.hpp>
0285 
0286 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
0287        //   || defined(GENERATING_DOCUMENTATION)
0288 
0289 #endif // BOOST_ASIO_BASIC_IO_OBJECT_HPP