service_registry.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // detail/impl/service_registry.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 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, typename... Args>
  36. Service& service_registry::make_service(Args&&... args)
  37. {
  38. auto_service_ptr new_service =
  39. { create<Service, execution_context>(owner_,
  40. &owner_, static_cast<Args&&>(args)...) };
  41. add_service(static_cast<Service*>(new_service.ptr_));
  42. Service& result = *static_cast<Service*>(new_service.ptr_);
  43. new_service.ptr_ = 0;
  44. return result;
  45. }
  46. template <typename Service>
  47. void service_registry::add_service(Service* new_service)
  48. {
  49. execution_context::service::key key;
  50. init_key<Service>(key, 0);
  51. return do_add_service(key, new_service);
  52. }
  53. template <typename Service>
  54. bool service_registry::has_service() const
  55. {
  56. execution_context::service::key key;
  57. init_key<Service>(key, 0);
  58. return do_has_service(key);
  59. }
  60. template <typename Service>
  61. inline void service_registry::init_key(
  62. execution_context::service::key& key, ...)
  63. {
  64. init_key_from_id(key, Service::id);
  65. }
  66. #if !defined(BOOST_ASIO_NO_TYPEID)
  67. template <typename Service>
  68. void service_registry::init_key(execution_context::service::key& key,
  69. enable_if_t<is_base_of<typename Service::key_type, Service>::value>*)
  70. {
  71. key.type_info_ = &typeid(typeid_wrapper<Service>);
  72. key.id_ = 0;
  73. }
  74. template <typename Service>
  75. void service_registry::init_key_from_id(execution_context::service::key& key,
  76. const service_id<Service>& /*id*/)
  77. {
  78. key.type_info_ = &typeid(typeid_wrapper<Service>);
  79. key.id_ = 0;
  80. }
  81. #endif // !defined(BOOST_ASIO_NO_TYPEID)
  82. template <typename Service, typename Owner, typename... Args>
  83. execution_context::service* service_registry::create(
  84. execution_context& context, void* owner, Args&&... args)
  85. {
  86. Service* svc = allocate_object<Service>(
  87. execution_context::allocator<void>(context),
  88. *static_cast<Owner*>(owner), static_cast<Args&&>(args)...);
  89. svc->destroy_ = &service_registry::destroy_allocated<Service>;
  90. return svc;
  91. }
  92. template <typename Service>
  93. void service_registry::destroy_allocated(execution_context::service* service)
  94. {
  95. deallocate_object(execution_context::allocator<void>(service->owner_),
  96. static_cast<Service*>(service));
  97. }
  98. } // namespace detail
  99. } // namespace asio
  100. } // namespace boost
  101. #include <boost/asio/detail/pop_options.hpp>
  102. #endif // BOOST_ASIO_DETAIL_IMPL_SERVICE_REGISTRY_HPP