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