2
// service_registry.hpp
3
// ~~~~~~~~~~~~~~~~~~~~
5
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11
#ifndef ASIO_DETAIL_SERVICE_REGISTRY_HPP
12
#define ASIO_DETAIL_SERVICE_REGISTRY_HPP
14
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
16
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18
#include "asio/detail/push_options.hpp"
20
#include "asio/detail/push_options.hpp"
23
#include "asio/detail/pop_options.hpp"
25
#include "asio/io_service.hpp"
26
#include "asio/detail/mutex.hpp"
27
#include "asio/detail/noncopyable.hpp"
28
#include "asio/detail/service_id.hpp"
30
#if defined(BOOST_NO_TYPEID)
31
# if !defined(ASIO_NO_TYPEID)
32
# define ASIO_NO_TYPEID
33
# endif // !defined(ASIO_NO_TYPEID)
34
#endif // defined(BOOST_NO_TYPEID)
39
class service_registry
44
service_registry(asio::io_service& o)
53
// Shutdown all services. This must be done in a separate loop before the
54
// services are destroyed since the destructors of user-defined handler
55
// objects may try to access other service objects.
56
asio::io_service::service* service = first_service_;
59
service->shutdown_service();
60
service = service->next_;
63
// Destroy all services.
64
while (first_service_)
66
asio::io_service::service* next_service = first_service_->next_;
67
delete first_service_;
68
first_service_ = next_service;
72
// Get the service object corresponding to the specified service type. Will
73
// create a new service object automatically if no such object already
74
// exists. Ownership of the service object is not transferred to the caller.
75
template <typename Service>
76
Service& use_service()
78
asio::detail::mutex::scoped_lock lock(mutex_);
80
// First see if there is an existing service object for the given type.
81
asio::io_service::service* service = first_service_;
84
if (service_id_matches(*service, Service::id))
85
return *static_cast<Service*>(service);
86
service = service->next_;
89
// Create a new service object. The service registry's mutex is not locked
90
// at this time to allow for nested calls into this function from the new
91
// service's constructor.
93
std::auto_ptr<Service> new_service(new Service(owner_));
94
init_service_id(*new_service, Service::id);
95
Service& new_service_ref = *new_service;
98
// Check that nobody else created another service object of the same type
99
// while the lock was released.
100
service = first_service_;
103
if (service_id_matches(*service, Service::id))
104
return *static_cast<Service*>(service);
105
service = service->next_;
108
// Service was successfully initialised, pass ownership to registry.
109
new_service->next_ = first_service_;
110
first_service_ = new_service.release();
112
return new_service_ref;
115
// Add a service object. Returns false on error, in which case ownership of
116
// the object is retained by the caller.
117
template <typename Service>
118
bool add_service(Service* new_service)
120
asio::detail::mutex::scoped_lock lock(mutex_);
122
// Check if there is an existing service object for the given type.
123
asio::io_service::service* service = first_service_;
126
if (service_id_matches(*service, Service::id))
128
service = service->next_;
131
// Take ownership of the service object.
132
init_service_id(*new_service, Service::id);
133
new_service->next_ = first_service_;
134
first_service_ = new_service;
139
// Check whether a service object of the specified type already exists.
140
template <typename Service>
141
bool has_service() const
143
asio::detail::mutex::scoped_lock lock(mutex_);
145
asio::io_service::service* service = first_service_;
148
if (service_id_matches(*service, Service::id))
150
service = service->next_;
157
// Set a service's id.
158
void init_service_id(asio::io_service::service& service,
159
const asio::io_service::id& id)
161
service.type_info_ = 0;
165
#if !defined(ASIO_NO_TYPEID)
166
// Set a service's id.
167
template <typename Service>
168
void init_service_id(asio::io_service::service& service,
169
const asio::detail::service_id<Service>& /*id*/)
171
service.type_info_ = &typeid(Service);
174
#endif // !defined(ASIO_NO_TYPEID)
176
// Check if a service matches the given id.
177
static bool service_id_matches(
178
const asio::io_service::service& service,
179
const asio::io_service::id& id)
181
return service.id_ == &id;
184
#if !defined(ASIO_NO_TYPEID)
185
// Check if a service matches the given id.
186
template <typename Service>
187
static bool service_id_matches(
188
const asio::io_service::service& service,
189
const asio::detail::service_id<Service>& /*id*/)
191
return service.type_info_ != 0 && *service.type_info_ == typeid(Service);
193
#endif // !defined(ASIO_NO_TYPEID)
195
// Mutex to protect access to internal data.
196
mutable asio::detail::mutex mutex_;
198
// The owner of this service registry and the services it contains.
199
asio::io_service& owner_;
201
// The first service in the list of contained services.
202
asio::io_service::service* first_service_;
205
} // namespace detail
208
#include "asio/detail/pop_options.hpp"
210
#endif // ASIO_DETAIL_SERVICE_REGISTRY_HPP