service_registry.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // detail/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_SERVICE_REGISTRY_HPP
  11. #define BOOST_ASIO_DETAIL_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/config.hpp>
  16. #include <typeinfo>
  17. #include <boost/asio/detail/mutex.hpp>
  18. #include <boost/asio/detail/noncopyable.hpp>
  19. #include <boost/asio/detail/type_traits.hpp>
  20. #include <boost/asio/execution_context.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. class io_context;
  25. namespace detail {
  26. template <typename T>
  27. class typeid_wrapper {};
  28. class service_registry
  29. : private noncopyable
  30. {
  31. public:
  32. // Constructor.
  33. BOOST_ASIO_DECL service_registry(execution_context& owner);
  34. // Destructor.
  35. BOOST_ASIO_DECL ~service_registry();
  36. // Shutdown all services.
  37. BOOST_ASIO_DECL void shutdown_services();
  38. // Destroy all services.
  39. BOOST_ASIO_DECL void destroy_services();
  40. // Notify all services of a fork event.
  41. BOOST_ASIO_DECL void notify_fork(execution_context::fork_event fork_ev);
  42. // Get the service object corresponding to the specified service type. Will
  43. // create a new service object automatically if no such object already
  44. // exists. Ownership of the service object is not transferred to the caller.
  45. template <typename Service>
  46. Service& use_service();
  47. // Get the service object corresponding to the specified service type. Will
  48. // create a new service object automatically if no such object already
  49. // exists. Ownership of the service object is not transferred to the caller.
  50. // This overload is used for backwards compatibility with services that
  51. // inherit from io_context::service.
  52. template <typename Service>
  53. Service& use_service(io_context& owner);
  54. // Create and add a service object.
  55. template <typename Service, typename... Args>
  56. Service& make_service(Args&&... args);
  57. // Add a service object. Throws on error, in which case ownership of the
  58. // object is retained by the caller.
  59. template <typename Service>
  60. void add_service(Service* new_service);
  61. // Check whether a service object of the specified type already exists.
  62. template <typename Service>
  63. bool has_service() const;
  64. private:
  65. // Initialise a service's key when the key_type typedef is not available.
  66. template <typename Service>
  67. static void init_key(execution_context::service::key& key, ...);
  68. #if !defined(BOOST_ASIO_NO_TYPEID)
  69. // Initialise a service's key when the key_type typedef is available.
  70. template <typename Service>
  71. static void init_key(execution_context::service::key& key,
  72. enable_if_t<is_base_of<typename Service::key_type, Service>::value>*);
  73. #endif // !defined(BOOST_ASIO_NO_TYPEID)
  74. // Initialise a service's key based on its id.
  75. BOOST_ASIO_DECL static void init_key_from_id(
  76. execution_context::service::key& key,
  77. const execution_context::id& id);
  78. #if !defined(BOOST_ASIO_NO_TYPEID)
  79. // Initialise a service's key based on its id.
  80. template <typename Service>
  81. static void init_key_from_id(execution_context::service::key& key,
  82. const service_id<Service>& /*id*/);
  83. #endif // !defined(BOOST_ASIO_NO_TYPEID)
  84. // Check if a service matches the given id.
  85. BOOST_ASIO_DECL static bool keys_match(
  86. const execution_context::service::key& key1,
  87. const execution_context::service::key& key2);
  88. // The type of a factory function used for creating a service instance.
  89. typedef execution_context::service*(*factory_type)(execution_context&, void*);
  90. // Factory function for creating a service instance.
  91. template <typename Service, typename Owner, typename... Args>
  92. static execution_context::service* create(
  93. execution_context& context, void* owner, Args&&... args);
  94. // Helper function to destroy an allocated service instance.
  95. template <typename Service>
  96. static void destroy_allocated(execution_context::service* service);
  97. // Helper function to destroy an added service instance.
  98. BOOST_ASIO_DECL static void destroy_added(
  99. execution_context::service* service);
  100. // Helper class to manage service pointers.
  101. struct auto_service_ptr;
  102. friend struct auto_service_ptr;
  103. struct auto_service_ptr
  104. {
  105. execution_context::service* ptr_;
  106. BOOST_ASIO_DECL ~auto_service_ptr();
  107. };
  108. // Get the service object corresponding to the specified service key. Will
  109. // create a new service object automatically if no such object already
  110. // exists. Ownership of the service object is not transferred to the caller.
  111. BOOST_ASIO_DECL execution_context::service* do_use_service(
  112. const execution_context::service::key& key,
  113. factory_type factory, void* owner);
  114. // Add a service object. Throws on error, in which case ownership of the
  115. // object is retained by the caller.
  116. BOOST_ASIO_DECL void do_add_service(
  117. const execution_context::service::key& key,
  118. execution_context::service* new_service);
  119. // Check whether a service object with the specified key already exists.
  120. BOOST_ASIO_DECL bool do_has_service(
  121. const execution_context::service::key& key) const;
  122. // Mutex to protect access to internal data.
  123. mutable boost::asio::detail::mutex mutex_;
  124. // The owner of this service registry and the services it contains.
  125. execution_context& owner_;
  126. // The first service in the list of contained services.
  127. execution_context::service* first_service_;
  128. };
  129. } // namespace detail
  130. } // namespace asio
  131. } // namespace boost
  132. #include <boost/asio/detail/pop_options.hpp>
  133. #include <boost/asio/detail/impl/service_registry.hpp>
  134. #if defined(BOOST_ASIO_HEADER_ONLY)
  135. # include <boost/asio/detail/impl/service_registry.ipp>
  136. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  137. #endif // BOOST_ASIO_DETAIL_SERVICE_REGISTRY_HPP