2 // detail/impl/service_registry.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 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 BOOST_ASIO_DETAIL_IMPL_SERVICE_REGISTRY_HPP
12 #define BOOST_ASIO_DETAIL_IMPL_SERVICE_REGISTRY_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/push_options.hpp>
24 template <typename Service>
25 Service& service_registry::use_service()
27 execution_context::service::key key;
28 init_key<Service>(key, 0);
29 factory_type factory = &service_registry::create<Service, execution_context>;
30 return *static_cast<Service*>(do_use_service(key, factory, &owner_));
33 template <typename Service>
34 Service& service_registry::use_service(io_context& owner)
36 execution_context::service::key key;
37 init_key<Service>(key, 0);
38 factory_type factory = &service_registry::create<Service, io_context>;
39 return *static_cast<Service*>(do_use_service(key, factory, &owner));
42 template <typename Service>
43 void service_registry::add_service(Service* new_service)
45 execution_context::service::key key;
46 init_key<Service>(key, 0);
47 return do_add_service(key, new_service);
50 template <typename Service>
51 bool service_registry::has_service() const
53 execution_context::service::key key;
54 init_key<Service>(key, 0);
55 return do_has_service(key);
58 template <typename Service>
59 inline void service_registry::init_key(
60 execution_context::service::key& key, ...)
62 init_key_from_id(key, Service::id);
65 #if !defined(BOOST_ASIO_NO_TYPEID)
66 template <typename Service>
67 void service_registry::init_key(execution_context::service::key& key,
69 is_base_of<typename Service::key_type, Service>::value>::type*)
71 key.type_info_ = &typeid(typeid_wrapper<Service>);
75 template <typename Service>
76 void service_registry::init_key_from_id(execution_context::service::key& key,
77 const service_id<Service>& /*id*/)
79 key.type_info_ = &typeid(typeid_wrapper<Service>);
82 #endif // !defined(BOOST_ASIO_NO_TYPEID)
84 template <typename Service, typename Owner>
85 execution_context::service* service_registry::create(void* owner)
87 return new Service(*static_cast<Owner*>(owner));
94 #include <boost/asio/detail/pop_options.hpp>
96 #endif // BOOST_ASIO_DETAIL_IMPL_SERVICE_REGISTRY_HPP