1 | // |
2 | // detail/service_registry.hpp |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4 | // |
5 | // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
6 | // |
7 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
8 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
9 | // |
10 | |
11 | #ifndef BOOST_ASIO_DETAIL_SERVICE_REGISTRY_HPP |
12 | #define BOOST_ASIO_DETAIL_SERVICE_REGISTRY_HPP |
13 | |
14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) |
15 | # pragma once |
16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) |
17 | |
18 | #include <boost/asio/detail/config.hpp> |
19 | #include <typeinfo> |
20 | #include <boost/asio/detail/mutex.hpp> |
21 | #include <boost/asio/detail/noncopyable.hpp> |
22 | #include <boost/asio/detail/type_traits.hpp> |
23 | #include <boost/asio/execution_context.hpp> |
24 | |
25 | #include <boost/asio/detail/push_options.hpp> |
26 | |
27 | namespace boost { |
28 | namespace asio { |
29 | |
30 | class io_context; |
31 | |
32 | namespace detail { |
33 | |
34 | template <typename T> |
35 | class typeid_wrapper {}; |
36 | |
37 | class service_registry |
38 | : private noncopyable |
39 | { |
40 | public: |
41 | // Constructor. |
42 | BOOST_ASIO_DECL service_registry(execution_context& owner); |
43 | |
44 | // Destructor. |
45 | BOOST_ASIO_DECL ~service_registry(); |
46 | |
47 | // Shutdown all services. |
48 | BOOST_ASIO_DECL void shutdown_services(); |
49 | |
50 | // Destroy all services. |
51 | BOOST_ASIO_DECL void destroy_services(); |
52 | |
53 | // Notify all services of a fork event. |
54 | BOOST_ASIO_DECL void notify_fork(execution_context::fork_event fork_ev); |
55 | |
56 | // Get the service object corresponding to the specified service type. Will |
57 | // create a new service object automatically if no such object already |
58 | // exists. Ownership of the service object is not transferred to the caller. |
59 | template <typename Service> |
60 | Service& use_service(); |
61 | |
62 | // Get the service object corresponding to the specified service type. Will |
63 | // create a new service object automatically if no such object already |
64 | // exists. Ownership of the service object is not transferred to the caller. |
65 | // This overload is used for backwards compatibility with services that |
66 | // inherit from io_context::service. |
67 | template <typename Service> |
68 | Service& use_service(io_context& owner); |
69 | |
70 | // Add a service object. Throws on error, in which case ownership of the |
71 | // object is retained by the caller. |
72 | template <typename Service> |
73 | void add_service(Service* new_service); |
74 | |
75 | // Check whether a service object of the specified type already exists. |
76 | template <typename Service> |
77 | bool has_service() const; |
78 | |
79 | private: |
80 | // Initalise a service's key when the key_type typedef is not available. |
81 | template <typename Service> |
82 | static void init_key(execution_context::service::key& key, ...); |
83 | |
84 | #if !defined(BOOST_ASIO_NO_TYPEID) |
85 | // Initalise a service's key when the key_type typedef is available. |
86 | template <typename Service> |
87 | static void init_key(execution_context::service::key& key, |
88 | enable_if_t<is_base_of<typename Service::key_type, Service>::value>*); |
89 | #endif // !defined(BOOST_ASIO_NO_TYPEID) |
90 | |
91 | // Initialise a service's key based on its id. |
92 | BOOST_ASIO_DECL static void init_key_from_id( |
93 | execution_context::service::key& key, |
94 | const execution_context::id& id); |
95 | |
96 | #if !defined(BOOST_ASIO_NO_TYPEID) |
97 | // Initialise a service's key based on its id. |
98 | template <typename Service> |
99 | static void init_key_from_id(execution_context::service::key& key, |
100 | const service_id<Service>& /*id*/); |
101 | #endif // !defined(BOOST_ASIO_NO_TYPEID) |
102 | |
103 | // Check if a service matches the given id. |
104 | BOOST_ASIO_DECL static bool keys_match( |
105 | const execution_context::service::key& key1, |
106 | const execution_context::service::key& key2); |
107 | |
108 | // The type of a factory function used for creating a service instance. |
109 | typedef execution_context::service*(*factory_type)(void*); |
110 | |
111 | // Factory function for creating a service instance. |
112 | template <typename Service, typename Owner> |
113 | static execution_context::service* create(void* owner); |
114 | |
115 | // Destroy a service instance. |
116 | BOOST_ASIO_DECL static void destroy(execution_context::service* service); |
117 | |
118 | // Helper class to manage service pointers. |
119 | struct auto_service_ptr; |
120 | friend struct auto_service_ptr; |
121 | struct auto_service_ptr |
122 | { |
123 | execution_context::service* ptr_; |
124 | ~auto_service_ptr() { destroy(service: ptr_); } |
125 | }; |
126 | |
127 | // Get the service object corresponding to the specified service key. Will |
128 | // create a new service object automatically if no such object already |
129 | // exists. Ownership of the service object is not transferred to the caller. |
130 | BOOST_ASIO_DECL execution_context::service* do_use_service( |
131 | const execution_context::service::key& key, |
132 | factory_type factory, void* owner); |
133 | |
134 | // Add a service object. Throws on error, in which case ownership of the |
135 | // object is retained by the caller. |
136 | BOOST_ASIO_DECL void do_add_service( |
137 | const execution_context::service::key& key, |
138 | execution_context::service* new_service); |
139 | |
140 | // Check whether a service object with the specified key already exists. |
141 | BOOST_ASIO_DECL bool do_has_service( |
142 | const execution_context::service::key& key) const; |
143 | |
144 | // Mutex to protect access to internal data. |
145 | mutable boost::asio::detail::mutex mutex_; |
146 | |
147 | // The owner of this service registry and the services it contains. |
148 | execution_context& owner_; |
149 | |
150 | // The first service in the list of contained services. |
151 | execution_context::service* first_service_; |
152 | }; |
153 | |
154 | } // namespace detail |
155 | } // namespace asio |
156 | } // namespace boost |
157 | |
158 | #include <boost/asio/detail/pop_options.hpp> |
159 | |
160 | #include <boost/asio/detail/impl/service_registry.hpp> |
161 | #if defined(BOOST_ASIO_HEADER_ONLY) |
162 | # include <boost/asio/detail/impl/service_registry.ipp> |
163 | #endif // defined(BOOST_ASIO_HEADER_ONLY) |
164 | |
165 | #endif // BOOST_ASIO_DETAIL_SERVICE_REGISTRY_HPP |
166 | |