service_registry.hpp 2.5 KB

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