Chris@16: // Chris@16: // detail/service_registry.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_DETAIL_SERVICE_REGISTRY_HPP Chris@16: #define BOOST_ASIO_DETAIL_SERVICE_REGISTRY_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: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: namespace detail { Chris@16: Chris@16: template Chris@16: class typeid_wrapper {}; Chris@16: Chris@16: class service_registry Chris@16: : private noncopyable Chris@16: { Chris@16: public: Chris@16: // Constructor. Adds the initial service. Chris@16: template Chris@16: service_registry(boost::asio::io_service& o, Chris@16: Service* initial_service, Arg arg); Chris@16: Chris@16: // Destructor. Chris@16: BOOST_ASIO_DECL ~service_registry(); Chris@16: Chris@16: // Notify all services of a fork event. Chris@16: BOOST_ASIO_DECL void notify_fork(boost::asio::io_service::fork_event fork_ev); Chris@16: Chris@16: // Get the first service object cast to the specified type. Called during Chris@16: // io_service construction and so performs no locking or type checking. Chris@16: template Chris@16: Service& first_service(); Chris@16: Chris@16: // Get the service object corresponding to the specified service type. Will Chris@16: // create a new service object automatically if no such object already Chris@16: // exists. Ownership of the service object is not transferred to the caller. Chris@16: template Chris@16: Service& use_service(); Chris@16: Chris@16: // Add a service object. Throws on error, in which case ownership of the Chris@16: // object is retained by the caller. Chris@16: template Chris@16: void add_service(Service* new_service); Chris@16: Chris@16: // Check whether a service object of the specified type already exists. Chris@16: template Chris@16: bool has_service() const; Chris@16: Chris@16: private: Chris@16: // Initialise a service's key based on its id. Chris@16: BOOST_ASIO_DECL static void init_key( Chris@16: boost::asio::io_service::service::key& key, Chris@16: const boost::asio::io_service::id& id); Chris@16: Chris@16: #if !defined(BOOST_ASIO_NO_TYPEID) Chris@16: // Initialise a service's key based on its id. Chris@16: template Chris@16: static void init_key(boost::asio::io_service::service::key& key, Chris@16: const boost::asio::detail::service_id& /*id*/); Chris@16: #endif // !defined(BOOST_ASIO_NO_TYPEID) Chris@16: Chris@16: // Check if a service matches the given id. Chris@16: BOOST_ASIO_DECL static bool keys_match( Chris@16: const boost::asio::io_service::service::key& key1, Chris@16: const boost::asio::io_service::service::key& key2); Chris@16: Chris@16: // The type of a factory function used for creating a service instance. Chris@16: typedef boost::asio::io_service::service* Chris@16: (*factory_type)(boost::asio::io_service&); Chris@16: Chris@16: // Factory function for creating a service instance. Chris@16: template Chris@16: static boost::asio::io_service::service* create( Chris@16: boost::asio::io_service& owner); Chris@16: Chris@16: // Destroy a service instance. Chris@16: BOOST_ASIO_DECL static void destroy( Chris@16: boost::asio::io_service::service* service); Chris@16: Chris@16: // Helper class to manage service pointers. Chris@16: struct auto_service_ptr; Chris@16: friend struct auto_service_ptr; Chris@16: struct auto_service_ptr Chris@16: { Chris@16: boost::asio::io_service::service* ptr_; Chris@16: ~auto_service_ptr() { destroy(ptr_); } Chris@16: }; Chris@16: Chris@16: // Get the service object corresponding to the specified service key. Will Chris@16: // create a new service object automatically if no such object already Chris@16: // exists. Ownership of the service object is not transferred to the caller. Chris@16: BOOST_ASIO_DECL boost::asio::io_service::service* do_use_service( Chris@16: const boost::asio::io_service::service::key& key, Chris@16: factory_type factory); Chris@16: Chris@16: // Add a service object. Throws on error, in which case ownership of the Chris@16: // object is retained by the caller. Chris@16: BOOST_ASIO_DECL void do_add_service( Chris@16: const boost::asio::io_service::service::key& key, Chris@16: boost::asio::io_service::service* new_service); Chris@16: Chris@16: // Check whether a service object with the specified key already exists. Chris@16: BOOST_ASIO_DECL bool do_has_service( Chris@16: const boost::asio::io_service::service::key& key) const; Chris@16: Chris@16: // Mutex to protect access to internal data. Chris@16: mutable boost::asio::detail::mutex mutex_; Chris@16: Chris@16: // The owner of this service registry and the services it contains. Chris@16: boost::asio::io_service& owner_; Chris@16: Chris@16: // The first service in the list of contained services. Chris@16: boost::asio::io_service::service* first_service_; Chris@16: }; Chris@16: Chris@16: } // namespace detail Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #include Chris@16: #if defined(BOOST_ASIO_HEADER_ONLY) Chris@16: # include Chris@16: #endif // defined(BOOST_ASIO_HEADER_ONLY) Chris@16: Chris@16: #endif // BOOST_ASIO_DETAIL_SERVICE_REGISTRY_HPP