Chris@16: // Chris@16: // basic_io_object.hpp Chris@16: // ~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_BASIC_IO_OBJECT_HPP Chris@16: #define BOOST_ASIO_BASIC_IO_OBJECT_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_MOVE) Chris@16: namespace detail Chris@16: { Chris@16: // Type trait used to determine whether a service supports move. Chris@16: template Chris@16: class service_has_move Chris@16: { Chris@16: private: Chris@16: typedef IoObjectService service_type; Chris@16: typedef typename service_type::implementation_type implementation_type; Chris@16: Chris@16: template Chris@16: static auto eval(T* t, U* u) -> decltype(t->move_construct(*u, *u), char()); Chris@16: static char (&eval(...))[2]; Chris@16: Chris@16: public: Chris@16: static const bool value = Chris@16: sizeof(service_has_move::eval( Chris@16: static_cast(0), Chris@16: static_cast(0))) == 1; Chris@16: }; Chris@16: } Chris@16: #endif // defined(BOOST_ASIO_HAS_MOVE) Chris@16: Chris@16: /// Base class for all I/O objects. Chris@16: /** Chris@16: * @note All I/O objects are non-copyable. However, when using C++0x, certain Chris@16: * I/O objects do support move construction and move assignment. Chris@16: */ Chris@16: #if !defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) Chris@16: template Chris@16: #else Chris@16: template ::value> Chris@16: #endif Chris@16: class basic_io_object Chris@16: { Chris@16: public: Chris@16: /// The type of the service that will be used to provide I/O operations. Chris@16: typedef IoObjectService service_type; Chris@16: Chris@16: /// The underlying implementation type of I/O object. Chris@16: typedef typename service_type::implementation_type implementation_type; Chris@16: Chris@16: /// Get the io_service associated with the object. Chris@16: /** Chris@16: * This function may be used to obtain the io_service object that the I/O Chris@16: * object uses to dispatch handlers for asynchronous operations. Chris@16: * Chris@16: * @return A reference to the io_service object that the I/O object will use Chris@16: * to dispatch handlers. Ownership is not transferred to the caller. Chris@16: */ Chris@16: boost::asio::io_service& get_io_service() Chris@16: { Chris@16: return service.get_io_service(); Chris@16: } Chris@16: Chris@16: protected: Chris@16: /// Construct a basic_io_object. Chris@16: /** Chris@16: * Performs: Chris@16: * @code get_service().construct(get_implementation()); @endcode Chris@16: */ Chris@16: explicit basic_io_object(boost::asio::io_service& io_service) Chris@16: : service(boost::asio::use_service(io_service)) Chris@16: { Chris@16: service.construct(implementation); Chris@16: } Chris@16: Chris@16: #if defined(GENERATING_DOCUMENTATION) Chris@16: /// Move-construct a basic_io_object. Chris@16: /** Chris@16: * Performs: Chris@16: * @code get_service().move_construct( Chris@16: * get_implementation(), other.get_implementation()); @endcode Chris@16: * Chris@16: * @note Available only for services that support movability, Chris@16: */ Chris@16: basic_io_object(basic_io_object&& other); Chris@16: Chris@16: /// Move-assign a basic_io_object. Chris@16: /** Chris@16: * Performs: Chris@16: * @code get_service().move_assign(get_implementation(), Chris@16: * other.get_service(), other.get_implementation()); @endcode Chris@16: * Chris@16: * @note Available only for services that support movability, Chris@16: */ Chris@16: basic_io_object& operator=(basic_io_object&& other); Chris@16: #endif // defined(GENERATING_DOCUMENTATION) Chris@16: Chris@16: /// Protected destructor to prevent deletion through this type. Chris@16: /** Chris@16: * Performs: Chris@16: * @code get_service().destroy(get_implementation()); @endcode Chris@16: */ Chris@16: ~basic_io_object() Chris@16: { Chris@16: service.destroy(implementation); Chris@16: } Chris@16: Chris@16: /// Get the service associated with the I/O object. Chris@16: service_type& get_service() Chris@16: { Chris@16: return service; Chris@16: } Chris@16: Chris@16: /// Get the service associated with the I/O object. Chris@16: const service_type& get_service() const Chris@16: { Chris@16: return service; Chris@16: } Chris@16: Chris@16: /// (Deprecated: Use get_service().) The service associated with the I/O Chris@16: /// object. Chris@16: /** Chris@16: * @note Available only for services that do not support movability. Chris@16: */ Chris@16: service_type& service; Chris@16: Chris@16: /// Get the underlying implementation of the I/O object. Chris@16: implementation_type& get_implementation() Chris@16: { Chris@16: return implementation; Chris@16: } Chris@16: Chris@16: /// Get the underlying implementation of the I/O object. Chris@16: const implementation_type& get_implementation() const Chris@16: { Chris@16: return implementation; Chris@16: } Chris@16: Chris@16: /// (Deprecated: Use get_implementation().) The underlying implementation of Chris@16: /// the I/O object. Chris@16: implementation_type implementation; Chris@16: Chris@16: private: Chris@16: basic_io_object(const basic_io_object&); Chris@16: basic_io_object& operator=(const basic_io_object&); Chris@16: }; Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_MOVE) Chris@16: // Specialisation for movable objects. Chris@16: template Chris@16: class basic_io_object Chris@16: { Chris@16: public: Chris@16: typedef IoObjectService service_type; Chris@16: typedef typename service_type::implementation_type implementation_type; Chris@16: Chris@16: boost::asio::io_service& get_io_service() Chris@16: { Chris@16: return service_->get_io_service(); Chris@16: } Chris@16: Chris@16: protected: Chris@16: explicit basic_io_object(boost::asio::io_service& io_service) Chris@16: : service_(&boost::asio::use_service(io_service)) Chris@16: { Chris@16: service_->construct(implementation); Chris@16: } Chris@16: Chris@16: basic_io_object(basic_io_object&& other) Chris@16: : service_(&other.get_service()) Chris@16: { Chris@16: service_->move_construct(implementation, other.implementation); Chris@16: } Chris@16: Chris@16: ~basic_io_object() Chris@16: { Chris@16: service_->destroy(implementation); Chris@16: } Chris@16: Chris@16: basic_io_object& operator=(basic_io_object&& other) Chris@16: { Chris@16: service_->move_assign(implementation, Chris@16: *other.service_, other.implementation); Chris@16: service_ = other.service_; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: service_type& get_service() Chris@16: { Chris@16: return *service_; Chris@16: } Chris@16: Chris@16: const service_type& get_service() const Chris@16: { Chris@16: return *service_; Chris@16: } Chris@16: Chris@16: implementation_type& get_implementation() Chris@16: { Chris@16: return implementation; Chris@16: } Chris@16: Chris@16: const implementation_type& get_implementation() const Chris@16: { Chris@16: return implementation; Chris@16: } Chris@16: Chris@16: implementation_type implementation; Chris@16: Chris@16: private: Chris@16: basic_io_object(const basic_io_object&); Chris@16: void operator=(const basic_io_object&); Chris@16: Chris@16: IoObjectService* service_; Chris@16: }; Chris@16: #endif // defined(BOOST_ASIO_HAS_MOVE) Chris@16: Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_ASIO_BASIC_IO_OBJECT_HPP