主要用於service註冊,其中,註冊的key爲execution_context::service::key類型:
friend class boost::asio::detail::service_registry;
struct key
{
key() : type_info_(0), id_(0) {}
const std::type_info* type_info_;
const execution_context::id* id_;
} key_;
完整代碼如下:
class service_registry
: private noncopyable
{
public:
// Constructor.
BOOST_ASIO_DECL service_registry(execution_context& owner);
// Destructor.
BOOST_ASIO_DECL ~service_registry();
// Shutdown all services.
BOOST_ASIO_DECL void shutdown_services();
// Destroy all services.
BOOST_ASIO_DECL void destroy_services();
// Notify all services of a fork event.
BOOST_ASIO_DECL void notify_fork(execution_context::fork_event fork_ev);
// Get the service object corresponding to the specified service type. Will
// create a new service object automatically if no such object already
// exists. Ownership of the service object is not transferred to the caller.
template <typename Service>
Service& use_service();
// Get the service object corresponding to the specified service type. Will
// create a new service object automatically if no such object already
// exists. Ownership of the service object is not transferred to the caller.
// This overload is used for backwards compatibility with services that
// inherit from io_context::service.
template <typename Service>
Service& use_service(io_context& owner);
// Add a service object. Throws on error, in which case ownership of the
// object is retained by the caller.
template <typename Service>
void add_service(Service* new_service);
// Check whether a service object of the specified type already exists.
template <typename Service>
bool has_service() const;
private:
// Initalise a service's key when the key_type typedef is not available.
template <typename Service>
static void init_key(execution_context::service::key& key, ...);
#if !defined(BOOST_ASIO_NO_TYPEID)
// Initalise a service's key when the key_type typedef is available.
template <typename Service>
static void init_key(execution_context::service::key& key,
typename enable_if<
is_base_of<typename Service::key_type, Service>::value>::type*);
#endif // !defined(BOOST_ASIO_NO_TYPEID)
// Initialise a service's key based on its id.
BOOST_ASIO_DECL static void init_key_from_id(
execution_context::service::key& key,
const execution_context::id& id);
#if !defined(BOOST_ASIO_NO_TYPEID)
// Initialise a service's key based on its id.
template <typename Service>
static void init_key_from_id(execution_context::service::key& key,
const service_id<Service>& /*id*/);
#endif // !defined(BOOST_ASIO_NO_TYPEID)
// Check if a service matches the given id.
BOOST_ASIO_DECL static bool keys_match(
const execution_context::service::key& key1,
const execution_context::service::key& key2);
// The type of a factory function used for creating a service instance.
typedef execution_context::service*(*factory_type)(void*);
// Factory function for creating a service instance.
template <typename Service, typename Owner>
static execution_context::service* create(void* owner);
// Destroy a service instance.
BOOST_ASIO_DECL static void destroy(execution_context::service* service);
// Helper class to manage service pointers.
struct auto_service_ptr;
friend struct auto_service_ptr;
struct auto_service_ptr
{
execution_context::service* ptr_;
~auto_service_ptr() { destroy(ptr_); }
};
// Get the service object corresponding to the specified service key. Will
// create a new service object automatically if no such object already
// exists. Ownership of the service object is not transferred to the caller.
BOOST_ASIO_DECL execution_context::service* do_use_service(
const execution_context::service::key& key,
factory_type factory, void* owner);
// Add a service object. Throws on error, in which case ownership of the
// object is retained by the caller.
BOOST_ASIO_DECL void do_add_service(
const execution_context::service::key& key,
execution_context::service* new_service);
// Check whether a service object with the specified key already exists.
BOOST_ASIO_DECL bool do_has_service(
const execution_context::service::key& key) const;
// Mutex to protect access to internal data.
mutable boost::asio::detail::mutex mutex_;
// The owner of this service registry and the services it contains.
execution_context& owner_;
// The first service in the list of contained services.
execution_context::service* first_service_;
};
template <typename Service>
Service& service_registry::use_service()
{
execution_context::service::key key;
init_key<Service>(key, 0);
factory_type factory = &service_registry::create<Service, execution_context>;
return *static_cast<Service*>(do_use_service(key, factory, &owner_));
}
template <typename Service>
Service& service_registry::use_service(io_context& owner)
{
execution_context::service::key key;
init_key<Service>(key, 0);
factory_type factory = &service_registry::create<Service, io_context>;
return *static_cast<Service*>(do_use_service(key, factory, &owner));
}
template <typename Service>
void service_registry::add_service(Service* new_service)
{
execution_context::service::key key;
init_key<Service>(key, 0);
return do_add_service(key, new_service);
}
template <typename Service>
bool service_registry::has_service() const
{
execution_context::service::key key;
init_key<Service>(key, 0);
return do_has_service(key);
}
template <typename Service>
inline void service_registry::init_key(
execution_context::service::key& key, ...)
{
init_key_from_id(key, Service::id);
}
#if !defined(BOOST_ASIO_NO_TYPEID)
template <typename Service>
void service_registry::init_key(execution_context::service::key& key,
typename enable_if<
is_base_of<typename Service::key_type, Service>::value>::type*)
{
key.type_info_ = &typeid(typeid_wrapper<Service>);
key.id_ = 0;
}
template <typename Service>
void service_registry::init_key_from_id(execution_context::service::key& key,
const service_id<Service>& /*id*/)
{
key.type_info_ = &typeid(typeid_wrapper<Service>);
key.id_ = 0;
}
#endif // !defined(BOOST_ASIO_NO_TYPEID)
template <typename Service, typename Owner>
execution_context::service* service_registry::create(void* owner)
{
return new Service(*static_cast<Owner*>(owner));
}
service_registry::service_registry(execution_context& owner)
: owner_(owner),
first_service_(0)
{
}
service_registry::~service_registry()
{
}
// 關閉services
void service_registry::shutdown_services()
{
execution_context::service* service = first_service_;
while (service)
{
service->shutdown();
service = service->next_;
}
}
// 銷燬services
void service_registry::destroy_services()
{
while (first_service_)
{
execution_context::service* next_service = first_service_->next_;
destroy(first_service_);
first_service_ = next_service;
}
}
void service_registry::notify_fork(execution_context::fork_event fork_ev)
{
// 拷貝當前的services
// Make a copy of all of the services while holding the lock. We don't want
// to hold the lock while calling into each service, as it may try to call
// back into this class.
std::vector<execution_context::service*> services;
{
boost::asio::detail::mutex::scoped_lock lock(mutex_);
execution_context::service* service = first_service_;
while (service)
{
services.push_back(service);
service = service->next_;
}
}
// 對於fork_prepare事件,採用正序,否則採用反序
// If processing the fork_prepare event, we want to go in reverse order of
// service registration, which happens to be the existing order of the
// services in the vector. For the other events we want to go in the other
// direction.
std::size_t num_services = services.size();
if (fork_ev == execution_context::fork_prepare)
for (std::size_t i = 0; i < num_services; ++i)
services[i]->notify_fork(fork_ev);
else
for (std::size_t i = num_services; i > 0; --i)
services[i - 1]->notify_fork(fork_ev);
}
// 構造service key
void service_registry::init_key_from_id(execution_context::service::key& key,
const execution_context::id& id)
{
key.type_info_ = 0;
key.id_ = &id;
}
// 比較servic key
bool service_registry::keys_match(
const execution_context::service::key& key1,
const execution_context::service::key& key2)
{
if (key1.id_ && key2.id_)
if (key1.id_ == key2.id_)
return true;
if (key1.type_info_ && key2.type_info_)
if (*key1.type_info_ == *key2.type_info_)
return true;
return false;
}
// 銷燬(釋放)service
void service_registry::destroy(execution_context::service* service)
{
delete service;
}
// 獲取指定key對應的service
execution_context::service* service_registry::do_use_service(
const execution_context::service::key& key,
factory_type factory, void* owner)
{
boost::asio::detail::mutex::scoped_lock lock(mutex_);
// First see if there is an existing service object with the given key.
execution_context::service* service = first_service_;
while (service)
{
if (keys_match(service->key_, key))
return service;
service = service->next_;
}
// Create a new service object. The service registry's mutex is not locked
// at this time to allow for nested calls into this function from the new
// service's constructor.
lock.unlock();
auto_service_ptr new_service = { factory(owner) };
new_service.ptr_->key_ = key;
lock.lock();
// Check that nobody else created another service object of the same type
// while the lock was released.
service = first_service_;
while (service)
{
if (keys_match(service->key_, key))
return service;
service = service->next_;
}
// Service was successfully initialised, pass ownership to registry.
new_service.ptr_->next_ = first_service_;
first_service_ = new_service.ptr_;
new_service.ptr_ = 0;
return first_service_;
}
// 插入key和對應的service
void service_registry::do_add_service(
const execution_context::service::key& key,
execution_context::service* new_service)
{
if (&owner_ != &new_service->context())
boost::asio::detail::throw_exception(invalid_service_owner());
boost::asio::detail::mutex::scoped_lock lock(mutex_);
// Check if there is an existing service object with the given key.
execution_context::service* service = first_service_;
while (service)
{
if (keys_match(service->key_, key))
boost::asio::detail::throw_exception(service_already_exists());
service = service->next_;
}
// Take ownership of the service object.
new_service->key_ = key;
new_service->next_ = first_service_;
first_service_ = new_service;
}
// 判斷是否存在對應key的services
bool service_registry::do_has_service(
const execution_context::service::key& key) const
{
boost::asio::detail::mutex::scoped_lock lock(mutex_);
execution_context::service* service = first_service_;
while (service)
{
if (keys_match(service->key_, key))
return true;
service = service->next_;
}
return false;
}