service_registry.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // detail/impl/service_registry.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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>
  20. Service& service_registry::use_service()
  21. {
  22. execution_context::service::key key;
  23. init_key<Service>(key, 0);
  24. factory_type factory = &service_registry::create<Service, execution_context>;
  25. return *static_cast<Service*>(do_use_service(key, factory, &owner_));
  26. }
  27. template <typename Service>
  28. Service& service_registry::use_service(io_context& owner)
  29. {
  30. execution_context::service::key key;
  31. init_key<Service>(key, 0);
  32. factory_type factory = &service_registry::create<Service, io_context>;
  33. return *static_cast<Service*>(do_use_service(key, factory, &owner));
  34. }
  35. template <typename Service>
  36. void service_registry::add_service(Service* new_service)
  37. {
  38. execution_context::service::key key;
  39. init_key<Service>(key, 0);
  40. return do_add_service(key, new_service);
  41. }
  42. template <typename Service>
  43. bool service_registry::has_service() const
  44. {
  45. execution_context::service::key key;
  46. init_key<Service>(key, 0);
  47. return do_has_service(key);
  48. }
  49. template <typename Service>
  50. inline void service_registry::init_key(
  51. execution_context::service::key& key, ...)
  52. {
  53. init_key_from_id(key, Service::id);
  54. }
  55. #if !defined(BOOST_ASIO_NO_TYPEID)
  56. template <typename Service>
  57. void service_registry::init_key(execution_context::service::key& key,
  58. typename enable_if<
  59. is_base_of<typename Service::key_type, Service>::value>::type*)
  60. {
  61. key.type_info_ = &typeid(typeid_wrapper<Service>);
  62. key.id_ = 0;
  63. }
  64. template <typename Service>
  65. void service_registry::init_key_from_id(execution_context::service::key& key,
  66. const service_id<Service>& /*id*/)
  67. {
  68. key.type_info_ = &typeid(typeid_wrapper<Service>);
  69. key.id_ = 0;
  70. }
  71. #endif // !defined(BOOST_ASIO_NO_TYPEID)
  72. template <typename Service, typename Owner>
  73. execution_context::service* service_registry::create(void* owner)
  74. {
  75. return new Service(*static_cast<Owner*>(owner));
  76. }
  77. } // namespace detail
  78. } // namespace asio
  79. } // namespace boost
  80. #include <boost/asio/detail/pop_options.hpp>
  81. #endif // BOOST_ASIO_DETAIL_IMPL_SERVICE_REGISTRY_HPP