Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // basic_io_object.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_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 #include <boost/asio/io_context.hpp>
0020 
0021 #include <boost/asio/detail/push_options.hpp>
0022 
0023 namespace boost {
0024 namespace asio {
0025 
0026 namespace detail
0027 {
0028   // Type trait used to determine whether a service supports move.
0029   template <typename IoObjectService>
0030   class service_has_move
0031   {
0032   private:
0033     typedef IoObjectService service_type;
0034     typedef typename service_type::implementation_type implementation_type;
0035 
0036     template <typename T, typename U>
0037     static auto asio_service_has_move_eval(T* t, U* u)
0038       -> decltype(t->move_construct(*u, *u), char());
0039     static char (&asio_service_has_move_eval(...))[2];
0040 
0041   public:
0042     static const bool value =
0043       sizeof(asio_service_has_move_eval(
0044         static_cast<service_type*>(0),
0045         static_cast<implementation_type*>(0))) == 1;
0046   };
0047 }
0048 
0049 /// Base class for all I/O objects.
0050 /**
0051  * @note All I/O objects are non-copyable. However, when using C++0x, certain
0052  * I/O objects do support move construction and move assignment.
0053  */
0054 #if defined(GENERATING_DOCUMENTATION)
0055 template <typename IoObjectService>
0056 #else
0057 template <typename IoObjectService,
0058     bool Movable = detail::service_has_move<IoObjectService>::value>
0059 #endif
0060 class basic_io_object
0061 {
0062 public:
0063   /// The type of the service that will be used to provide I/O operations.
0064   typedef IoObjectService service_type;
0065 
0066   /// The underlying implementation type of I/O object.
0067   typedef typename service_type::implementation_type implementation_type;
0068 
0069 #if !defined(BOOST_ASIO_NO_DEPRECATED)
0070   /// (Deprecated: Use get_executor().) Get the io_context associated with the
0071   /// object.
0072   /**
0073    * This function may be used to obtain the io_context object that the I/O
0074    * object uses to dispatch handlers for asynchronous operations.
0075    *
0076    * @return A reference to the io_context object that the I/O object will use
0077    * to dispatch handlers. Ownership is not transferred to the caller.
0078    */
0079   boost::asio::io_context& get_io_context()
0080   {
0081     return service_.get_io_context();
0082   }
0083 
0084   /// (Deprecated: Use get_executor().) Get the io_context associated with the
0085   /// object.
0086   /**
0087    * This function may be used to obtain the io_context object that the I/O
0088    * object uses to dispatch handlers for asynchronous operations.
0089    *
0090    * @return A reference to the io_context object that the I/O object will use
0091    * to dispatch handlers. Ownership is not transferred to the caller.
0092    */
0093   boost::asio::io_context& get_io_service()
0094   {
0095     return service_.get_io_context();
0096   }
0097 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
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 #if !defined(BOOST_ASIO_NO_DEPRECATED)
0201   boost::asio::io_context& get_io_context()
0202   {
0203     return service_->get_io_context();
0204   }
0205 
0206   boost::asio::io_context& get_io_service()
0207   {
0208     return service_->get_io_context();
0209   }
0210 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
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 // BOOST_ASIO_BASIC_IO_OBJECT_HPP