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