Chris@16
|
1 //
|
Chris@16
|
2 // detail/service_registry.hpp
|
Chris@16
|
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
Chris@16
|
4 //
|
Chris@16
|
5 // Copyright (c) 2003-2013 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 #if defined(__GNUC__)
|
Chris@16
|
31 # if (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) || (__GNUC__ > 4)
|
Chris@16
|
32 # pragma GCC visibility push (default)
|
Chris@16
|
33 # endif // (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) || (__GNUC__ > 4)
|
Chris@16
|
34 #endif // defined(__GNUC__)
|
Chris@16
|
35
|
Chris@16
|
36 template <typename T>
|
Chris@16
|
37 class typeid_wrapper {};
|
Chris@16
|
38
|
Chris@16
|
39 #if defined(__GNUC__)
|
Chris@16
|
40 # if (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) || (__GNUC__ > 4)
|
Chris@16
|
41 # pragma GCC visibility pop
|
Chris@16
|
42 # endif // (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) || (__GNUC__ > 4)
|
Chris@16
|
43 #endif // defined(__GNUC__)
|
Chris@16
|
44
|
Chris@16
|
45 class service_registry
|
Chris@16
|
46 : private noncopyable
|
Chris@16
|
47 {
|
Chris@16
|
48 public:
|
Chris@16
|
49 // Constructor. Adds the initial service.
|
Chris@16
|
50 template <typename Service, typename Arg>
|
Chris@16
|
51 service_registry(boost::asio::io_service& o,
|
Chris@16
|
52 Service* initial_service, Arg arg);
|
Chris@16
|
53
|
Chris@16
|
54 // Destructor.
|
Chris@16
|
55 BOOST_ASIO_DECL ~service_registry();
|
Chris@16
|
56
|
Chris@16
|
57 // Notify all services of a fork event.
|
Chris@16
|
58 BOOST_ASIO_DECL void notify_fork(boost::asio::io_service::fork_event fork_ev);
|
Chris@16
|
59
|
Chris@16
|
60 // Get the first service object cast to the specified type. Called during
|
Chris@16
|
61 // io_service construction and so performs no locking or type checking.
|
Chris@16
|
62 template <typename Service>
|
Chris@16
|
63 Service& first_service();
|
Chris@16
|
64
|
Chris@16
|
65 // Get the service object corresponding to the specified service type. Will
|
Chris@16
|
66 // create a new service object automatically if no such object already
|
Chris@16
|
67 // exists. Ownership of the service object is not transferred to the caller.
|
Chris@16
|
68 template <typename Service>
|
Chris@16
|
69 Service& use_service();
|
Chris@16
|
70
|
Chris@16
|
71 // Add a service object. Throws on error, in which case ownership of the
|
Chris@16
|
72 // object is retained by the caller.
|
Chris@16
|
73 template <typename Service>
|
Chris@16
|
74 void add_service(Service* new_service);
|
Chris@16
|
75
|
Chris@16
|
76 // Check whether a service object of the specified type already exists.
|
Chris@16
|
77 template <typename Service>
|
Chris@16
|
78 bool has_service() const;
|
Chris@16
|
79
|
Chris@16
|
80 private:
|
Chris@16
|
81 // Initialise a service's key based on its id.
|
Chris@16
|
82 BOOST_ASIO_DECL static void init_key(
|
Chris@16
|
83 boost::asio::io_service::service::key& key,
|
Chris@16
|
84 const boost::asio::io_service::id& id);
|
Chris@16
|
85
|
Chris@16
|
86 #if !defined(BOOST_ASIO_NO_TYPEID)
|
Chris@16
|
87 // Initialise a service's key based on its id.
|
Chris@16
|
88 template <typename Service>
|
Chris@16
|
89 static void init_key(boost::asio::io_service::service::key& key,
|
Chris@16
|
90 const boost::asio::detail::service_id<Service>& /*id*/);
|
Chris@16
|
91 #endif // !defined(BOOST_ASIO_NO_TYPEID)
|
Chris@16
|
92
|
Chris@16
|
93 // Check if a service matches the given id.
|
Chris@16
|
94 BOOST_ASIO_DECL static bool keys_match(
|
Chris@16
|
95 const boost::asio::io_service::service::key& key1,
|
Chris@16
|
96 const boost::asio::io_service::service::key& key2);
|
Chris@16
|
97
|
Chris@16
|
98 // The type of a factory function used for creating a service instance.
|
Chris@16
|
99 typedef boost::asio::io_service::service*
|
Chris@16
|
100 (*factory_type)(boost::asio::io_service&);
|
Chris@16
|
101
|
Chris@16
|
102 // Factory function for creating a service instance.
|
Chris@16
|
103 template <typename Service>
|
Chris@16
|
104 static boost::asio::io_service::service* create(
|
Chris@16
|
105 boost::asio::io_service& owner);
|
Chris@16
|
106
|
Chris@16
|
107 // Destroy a service instance.
|
Chris@16
|
108 BOOST_ASIO_DECL static void destroy(
|
Chris@16
|
109 boost::asio::io_service::service* service);
|
Chris@16
|
110
|
Chris@16
|
111 // Helper class to manage service pointers.
|
Chris@16
|
112 struct auto_service_ptr;
|
Chris@16
|
113 friend struct auto_service_ptr;
|
Chris@16
|
114 struct auto_service_ptr
|
Chris@16
|
115 {
|
Chris@16
|
116 boost::asio::io_service::service* ptr_;
|
Chris@16
|
117 ~auto_service_ptr() { destroy(ptr_); }
|
Chris@16
|
118 };
|
Chris@16
|
119
|
Chris@16
|
120 // Get the service object corresponding to the specified service key. Will
|
Chris@16
|
121 // create a new service object automatically if no such object already
|
Chris@16
|
122 // exists. Ownership of the service object is not transferred to the caller.
|
Chris@16
|
123 BOOST_ASIO_DECL boost::asio::io_service::service* do_use_service(
|
Chris@16
|
124 const boost::asio::io_service::service::key& key,
|
Chris@16
|
125 factory_type factory);
|
Chris@16
|
126
|
Chris@16
|
127 // Add a service object. Throws on error, in which case ownership of the
|
Chris@16
|
128 // object is retained by the caller.
|
Chris@16
|
129 BOOST_ASIO_DECL void do_add_service(
|
Chris@16
|
130 const boost::asio::io_service::service::key& key,
|
Chris@16
|
131 boost::asio::io_service::service* new_service);
|
Chris@16
|
132
|
Chris@16
|
133 // Check whether a service object with the specified key already exists.
|
Chris@16
|
134 BOOST_ASIO_DECL bool do_has_service(
|
Chris@16
|
135 const boost::asio::io_service::service::key& key) const;
|
Chris@16
|
136
|
Chris@16
|
137 // Mutex to protect access to internal data.
|
Chris@16
|
138 mutable boost::asio::detail::mutex mutex_;
|
Chris@16
|
139
|
Chris@16
|
140 // The owner of this service registry and the services it contains.
|
Chris@16
|
141 boost::asio::io_service& owner_;
|
Chris@16
|
142
|
Chris@16
|
143 // The first service in the list of contained services.
|
Chris@16
|
144 boost::asio::io_service::service* first_service_;
|
Chris@16
|
145 };
|
Chris@16
|
146
|
Chris@16
|
147 } // namespace detail
|
Chris@16
|
148 } // namespace asio
|
Chris@16
|
149 } // namespace boost
|
Chris@16
|
150
|
Chris@16
|
151 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
152
|
Chris@16
|
153 #include <boost/asio/detail/impl/service_registry.hpp>
|
Chris@16
|
154 #if defined(BOOST_ASIO_HEADER_ONLY)
|
Chris@16
|
155 # include <boost/asio/detail/impl/service_registry.ipp>
|
Chris@16
|
156 #endif // defined(BOOST_ASIO_HEADER_ONLY)
|
Chris@16
|
157
|
Chris@16
|
158 #endif // BOOST_ASIO_DETAIL_SERVICE_REGISTRY_HPP
|