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"
33
class service_registry
38
service_registry(asio::io_service& o)
47
// Shutdown all services. This must be done in a separate loop before the
48
// services are destroyed since the destructors of user-defined handler
49
// objects may try to access other service objects.
50
asio::io_service::service* service = first_service_;
53
service->shutdown_service();
54
service = service->next_;
57
// Destroy all services.
58
while (first_service_)
60
asio::io_service::service* next_service = first_service_->next_;
61
delete first_service_;
62
first_service_ = next_service;
66
// Get the service object corresponding to the specified service type. Will
67
// create a new service object automatically if no such object already
68
// exists. Ownership of the service object is not transferred to the caller.
69
template <typename Service>
70
Service& use_service()
72
asio::detail::mutex::scoped_lock lock(mutex_);
74
// First see if there is an existing service object for the given type.
75
asio::io_service::service* service = first_service_;
78
if (service_id_matches(*service, Service::id))
79
return *static_cast<Service*>(service);
80
service = service->next_;
83
// Create a new service object. The service registry's mutex is not locked
84
// at this time to allow for nested calls into this function from the new
85
// service's constructor.
87
std::auto_ptr<Service> new_service(new Service(owner_));
88
init_service_id(*new_service, Service::id);
89
Service& new_service_ref = *new_service;
92
// Check that nobody else created another service object of the same type
93
// while the lock was released.
94
service = first_service_;
97
if (service_id_matches(*service, Service::id))
98
return *static_cast<Service*>(service);
99
service = service->next_;
102
// Service was successfully initialised, pass ownership to registry.
103
new_service->next_ = first_service_;
104
first_service_ = new_service.release();
106
return new_service_ref;
109
// Add a service object. Returns false on error, in which case ownership of
110
// the object is retained by the caller.
111
template <typename Service>
112
bool add_service(Service* new_service)
114
asio::detail::mutex::scoped_lock lock(mutex_);
116
// Check if there is an existing service object for the given type.
117
asio::io_service::service* service = first_service_;
120
if (service_id_matches(*service, Service::id))
122
service = service->next_;
125
// Take ownership of the service object.
126
init_service_id(*new_service, Service::id);
127
new_service->next_ = first_service_;
128
first_service_ = new_service;
133
// Check whether a service object of the specified type already exists.
134
template <typename Service>
135
bool has_service() const
137
asio::detail::mutex::scoped_lock lock(mutex_);
139
asio::io_service::service* service = first_service_;
142
if (service_id_matches(*service, Service::id))
144
service = service->next_;
151
// Set a service's id.
152
void init_service_id(asio::io_service::service& service,
153
const asio::io_service::id& id)
155
service.type_info_ = 0;
159
// Set a service's id.
160
template <typename Service>
161
void init_service_id(asio::io_service::service& service,
162
const asio::detail::service_id<Service>& /*id*/)
164
service.type_info_ = &typeid(Service);
168
// Check if a service matches the given id.
169
static bool service_id_matches(
170
const asio::io_service::service& service,
171
const asio::io_service::id& id)
173
return service.id_ == &id;
176
// Check if a service matches the given id.
177
template <typename Service>
178
static bool service_id_matches(
179
const asio::io_service::service& service,
180
const asio::detail::service_id<Service>& /*id*/)
182
return service.type_info_ != 0 && *service.type_info_ == typeid(Service);
185
// Mutex to protect access to internal data.
186
mutable asio::detail::mutex mutex_;
188
// The owner of this service registry and the services it contains.
189
asio::io_service& owner_;
191
// The first service in the list of contained services.
192
asio::io_service::service* first_service_;
195
} // namespace detail
198
#include "asio/detail/pop_options.hpp"
200
#endif // ASIO_DETAIL_SERVICE_REGISTRY_HPP