1 //
2 // detail/impl/service_registry.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
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)
9 //
10
11 #ifndef ASIO_DETAIL_IMPL_SERVICE_REGISTRY_HPP
12 #define ASIO_DETAIL_IMPL_SERVICE_REGISTRY_HPP
13
14
15 #include "asio/detail/push_options.hpp"
16
17 namespace asio {
18 namespace detail {
19
20 template <typename Service, typename Arg>
service_registry(asio::io_service & o,Service *,Arg arg)21 service_registry::service_registry(
22 asio::io_service& o, Service*, Arg arg)
23 : owner_(o),
24 first_service_(new Service(o, arg))
25 {
26 asio::io_service::service::key key;
27 init_key(key, Service::id);
28 first_service_->key_ = key;
29 first_service_->next_ = 0;
30 }
31
32 template <typename Service>
first_service()33 Service& service_registry::first_service()
34 {
35 return *static_cast<Service*>(first_service_);
36 }
37
38 template <typename Service>
use_service()39 Service& service_registry::use_service()
40 {
41 asio::io_service::service::key key;
42 init_key(key, Service::id);
43 factory_type factory = &service_registry::create<Service>;
44 return *static_cast<Service*>(do_use_service(key, factory));
45 }
46
47 template <typename Service>
add_service(Service * new_service)48 void service_registry::add_service(Service* new_service)
49 {
50 asio::io_service::service::key key;
51 init_key(key, Service::id);
52 return do_add_service(key, new_service);
53 }
54
55 template <typename Service>
has_service() const56 bool service_registry::has_service() const
57 {
58 asio::io_service::service::key key;
59 init_key(key, Service::id);
60 return do_has_service(key);
61 }
62
63 #if !defined(ASIO_NO_TYPEID)
64 template <typename Service>
init_key(asio::io_service::service::key & key,const asio::detail::service_id<Service> &)65 void service_registry::init_key(asio::io_service::service::key& key,
66 const asio::detail::service_id<Service>& /*id*/)
67 {
68 key.type_info_ = &typeid(typeid_wrapper<Service>);
69 key.id_ = 0;
70 }
71 #endif // !defined(ASIO_NO_TYPEID)
72
73 template <typename Service>
create(asio::io_service & owner)74 asio::io_service::service* service_registry::create(
75 asio::io_service& owner)
76 {
77 return new Service(owner);
78 }
79
80 } // namespace detail
81 } // namespace asio
82
83 #include "asio/detail/pop_options.hpp"
84
85 #endif // ASIO_DETAIL_IMPL_SERVICE_REGISTRY_HPP
86