Clementine
service_registry.hpp
1 //
2 // detail/service_registry.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 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 ASIO_DETAIL_SERVICE_REGISTRY_HPP
12 #define 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 "asio/detail/config.hpp"
19 #include <typeinfo>
20 #include "asio/detail/mutex.hpp"
21 #include "asio/detail/noncopyable.hpp"
22 #include "asio/detail/type_traits.hpp"
23 #include "asio/execution_context.hpp"
24 
25 #include "asio/detail/push_options.hpp"
26 
27 namespace asio {
28 
29 class io_context;
30 
31 namespace detail {
32 
33 template <typename T>
34 class typeid_wrapper {};
35 
37  : private noncopyable
38 {
39 public:
40  // Constructor.
41  ASIO_DECL service_registry(execution_context& owner);
42 
43  // Destructor.
44  ASIO_DECL ~service_registry();
45 
46  // Shutdown all services.
47  ASIO_DECL void shutdown_services();
48 
49  // Destroy all services.
50  ASIO_DECL void destroy_services();
51 
52  // Notify all services of a fork event.
53  ASIO_DECL void notify_fork(execution_context::fork_event fork_ev);
54 
55  // Get the service object corresponding to the specified service type. Will
56  // create a new service object automatically if no such object already
57  // exists. Ownership of the service object is not transferred to the caller.
58  template <typename Service>
59  Service& use_service();
60 
61  // Get the service object corresponding to the specified service type. Will
62  // create a new service object automatically if no such object already
63  // exists. Ownership of the service object is not transferred to the caller.
64  // This overload is used for backwards compatibility with services that
65  // inherit from io_context::service.
66  template <typename Service>
67  Service& use_service(io_context& owner);
68 
69  // Add a service object. Throws on error, in which case ownership of the
70  // object is retained by the caller.
71  template <typename Service>
72  void add_service(Service* new_service);
73 
74  // Check whether a service object of the specified type already exists.
75  template <typename Service>
76  bool has_service() const;
77 
78 private:
79  // Initalise a service's key when the key_type typedef is not available.
80  template <typename Service>
81  static void init_key(execution_context::service::key& key, ...);
82 
83 #if !defined(ASIO_NO_TYPEID)
84  // Initalise a service's key when the key_type typedef is available.
85  template <typename Service>
86  static void init_key(execution_context::service::key& key,
87  typename enable_if<
88  is_base_of<typename Service::key_type, Service>::value>::type*);
89 #endif // !defined(ASIO_NO_TYPEID)
90 
91  // Initialise a service's key based on its id.
92  ASIO_DECL static void init_key_from_id(
93  execution_context::service::key& key,
94  const execution_context::id& id);
95 
96 #if !defined(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(ASIO_NO_TYPEID)
102 
103  // Check if a service matches the given id.
104  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  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  {
124  ~auto_service_ptr() { destroy(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  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  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  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 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 
157 #include "asio/detail/pop_options.hpp"
158 
159 #include "asio/detail/impl/service_registry.hpp"
160 #if defined(ASIO_HEADER_ONLY)
161 # include "asio/detail/impl/service_registry.ipp"
162 #endif // defined(ASIO_HEADER_ONLY)
163 
164 #endif // ASIO_DETAIL_SERVICE_REGISTRY_HPP
Definition: null_mutex.hpp:30
Definition: noncopyable.hpp:25
Class used to uniquely identify a service.
Definition: execution_context.hpp:306
Base class for all io_context services.
Definition: execution_context.hpp:315
Definition: service_registry.hpp:36
A context for function object execution.
Definition: execution_context.hpp:105
Definition: service_registry.hpp:34
Provides core I/O functionality.
Definition: io_context.hpp:211
Definition: chrono.h:284
Definition: type_traits.hpp:97
fork_event
Fork-related event notifications.
Definition: execution_context.hpp:142
Definition: any_io_executor.hpp:28
Definition: execution_context.hpp:379