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 BOOST_ASIO_DETAIL_IMPL_SERVICE_REGISTRY_HPP
12#define BOOST_ASIO_DETAIL_IMPL_SERVICE_REGISTRY_HPP
13
14#if defined(_MSC_VER) && (_MSC_VER >= 1200)
15# pragma once
16#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18#include <boost/asio/detail/push_options.hpp>
19
20namespace boost {
21namespace asio {
22namespace detail {
23
24template <typename Service, typename Arg>
25service_registry::service_registry(
26 boost::asio::io_service& o, Service*, Arg arg)
27 : owner_(o),
28 first_service_(new Service(o, arg))
29{
30 boost::asio::io_service::service::key key;
31 init_key(key, Service::id);
32 first_service_->key_ = key;
33 first_service_->next_ = 0;
34}
35
36template <typename Service>
37Service& service_registry::first_service()
38{
39 return *static_cast<Service*>(first_service_);
40}
41
42template <typename Service>
43Service& service_registry::use_service()
44{
45 boost::asio::io_service::service::key key;
46 init_key(key, Service::id);
47 factory_type factory = &service_registry::create<Service>;
48 return *static_cast<Service*>(do_use_service(key, factory));
49}
50
51template <typename Service>
52void service_registry::add_service(Service* new_service)
53{
54 boost::asio::io_service::service::key key;
55 init_key(key, Service::id);
56 return do_add_service(key, new_service);
57}
58
59template <typename Service>
60bool service_registry::has_service() const
61{
62 boost::asio::io_service::service::key key;
63 init_key(key, Service::id);
64 return do_has_service(key);
65}
66
67#if !defined(BOOST_ASIO_NO_TYPEID)
68template <typename Service>
69void service_registry::init_key(boost::asio::io_service::service::key& key,
70 const boost::asio::detail::service_id<Service>& /*id*/)
71{
72 key.type_info_ = &typeid(typeid_wrapper<Service>);
73 key.id_ = 0;
74}
75#endif // !defined(BOOST_ASIO_NO_TYPEID)
76
77template <typename Service>
78boost::asio::io_service::service* service_registry::create(
79 boost::asio::io_service& owner)
80{
81 return new Service(owner);
82}
83
84} // namespace detail
85} // namespace asio
86} // namespace boost
87
88#include <boost/asio/detail/pop_options.hpp>
89
90#endif // BOOST_ASIO_DETAIL_IMPL_SERVICE_REGISTRY_HPP
91

source code of boost/boost/asio/detail/impl/service_registry.hpp