service_registry.hpp 2.3 KB

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