Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:44

0001 //
0002 // detail/service_registry.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_DETAIL_SERVICE_REGISTRY_HPP
0012 #define BOOST_ASIO_DETAIL_SERVICE_REGISTRY_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 <typeinfo>
0020 #include <boost/asio/detail/mutex.hpp>
0021 #include <boost/asio/detail/noncopyable.hpp>
0022 #include <boost/asio/detail/type_traits.hpp>
0023 #include <boost/asio/execution_context.hpp>
0024 
0025 #include <boost/asio/detail/push_options.hpp>
0026 
0027 namespace boost {
0028 namespace asio {
0029 
0030 class io_context;
0031 
0032 namespace detail {
0033 
0034 template <typename T>
0035 class typeid_wrapper {};
0036 
0037 class service_registry
0038   : private noncopyable
0039 {
0040 public:
0041   // Constructor.
0042   BOOST_ASIO_DECL service_registry(execution_context& owner);
0043 
0044   // Destructor.
0045   BOOST_ASIO_DECL ~service_registry();
0046 
0047   // Shutdown all services.
0048   BOOST_ASIO_DECL void shutdown_services();
0049 
0050   // Destroy all services.
0051   BOOST_ASIO_DECL void destroy_services();
0052 
0053   // Notify all services of a fork event.
0054   BOOST_ASIO_DECL void notify_fork(execution_context::fork_event fork_ev);
0055 
0056   // Get the service object corresponding to the specified service type. Will
0057   // create a new service object automatically if no such object already
0058   // exists. Ownership of the service object is not transferred to the caller.
0059   template <typename Service>
0060   Service& use_service();
0061 
0062   // Get the service object corresponding to the specified service type. Will
0063   // create a new service object automatically if no such object already
0064   // exists. Ownership of the service object is not transferred to the caller.
0065   // This overload is used for backwards compatibility with services that
0066   // inherit from io_context::service.
0067   template <typename Service>
0068   Service& use_service(io_context& owner);
0069 
0070   // Add a service object. Throws on error, in which case ownership of the
0071   // object is retained by the caller.
0072   template <typename Service>
0073   void add_service(Service* new_service);
0074 
0075   // Check whether a service object of the specified type already exists.
0076   template <typename Service>
0077   bool has_service() const;
0078 
0079 private:
0080   // Initalise a service's key when the key_type typedef is not available.
0081   template <typename Service>
0082   static void init_key(execution_context::service::key& key, ...);
0083 
0084 #if !defined(BOOST_ASIO_NO_TYPEID)
0085   // Initalise a service's key when the key_type typedef is available.
0086   template <typename Service>
0087   static void init_key(execution_context::service::key& key,
0088       enable_if_t<is_base_of<typename Service::key_type, Service>::value>*);
0089 #endif // !defined(BOOST_ASIO_NO_TYPEID)
0090 
0091   // Initialise a service's key based on its id.
0092   BOOST_ASIO_DECL static void init_key_from_id(
0093       execution_context::service::key& key,
0094       const execution_context::id& id);
0095 
0096 #if !defined(BOOST_ASIO_NO_TYPEID)
0097   // Initialise a service's key based on its id.
0098   template <typename Service>
0099   static void init_key_from_id(execution_context::service::key& key,
0100       const service_id<Service>& /*id*/);
0101 #endif // !defined(BOOST_ASIO_NO_TYPEID)
0102 
0103   // Check if a service matches the given id.
0104   BOOST_ASIO_DECL static bool keys_match(
0105       const execution_context::service::key& key1,
0106       const execution_context::service::key& key2);
0107 
0108   // The type of a factory function used for creating a service instance.
0109   typedef execution_context::service*(*factory_type)(void*);
0110 
0111   // Factory function for creating a service instance.
0112   template <typename Service, typename Owner>
0113   static execution_context::service* create(void* owner);
0114 
0115   // Destroy a service instance.
0116   BOOST_ASIO_DECL static void destroy(execution_context::service* service);
0117 
0118   // Helper class to manage service pointers.
0119   struct auto_service_ptr;
0120   friend struct auto_service_ptr;
0121   struct auto_service_ptr
0122   {
0123     execution_context::service* ptr_;
0124     ~auto_service_ptr() { destroy(ptr_); }
0125   };
0126 
0127   // Get the service object corresponding to the specified service key. Will
0128   // create a new service object automatically if no such object already
0129   // exists. Ownership of the service object is not transferred to the caller.
0130   BOOST_ASIO_DECL execution_context::service* do_use_service(
0131       const execution_context::service::key& key,
0132       factory_type factory, void* owner);
0133 
0134   // Add a service object. Throws on error, in which case ownership of the
0135   // object is retained by the caller.
0136   BOOST_ASIO_DECL void do_add_service(
0137       const execution_context::service::key& key,
0138       execution_context::service* new_service);
0139 
0140   // Check whether a service object with the specified key already exists.
0141   BOOST_ASIO_DECL bool do_has_service(
0142       const execution_context::service::key& key) const;
0143 
0144   // Mutex to protect access to internal data.
0145   mutable boost::asio::detail::mutex mutex_;
0146 
0147   // The owner of this service registry and the services it contains.
0148   execution_context& owner_;
0149 
0150   // The first service in the list of contained services.
0151   execution_context::service* first_service_;
0152 };
0153 
0154 } // namespace detail
0155 } // namespace asio
0156 } // namespace boost
0157 
0158 #include <boost/asio/detail/pop_options.hpp>
0159 
0160 #include <boost/asio/detail/impl/service_registry.hpp>
0161 #if defined(BOOST_ASIO_HEADER_ONLY)
0162 # include <boost/asio/detail/impl/service_registry.ipp>
0163 #endif // defined(BOOST_ASIO_HEADER_ONLY)
0164 
0165 #endif // BOOST_ASIO_DETAIL_SERVICE_REGISTRY_HPP