service_registry.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // detail/service_registry.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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. // Add a service object. Throws on error, in which case ownership of the
  55. // object is retained by the caller.
  56. template <typename Service>
  57. void add_service(Service* new_service);
  58. // Check whether a service object of the specified type already exists.
  59. template <typename Service>
  60. bool has_service() const;
  61. private:
  62. // Initalise a service's key when the key_type typedef is not available.
  63. template <typename Service>
  64. static void init_key(execution_context::service::key& key, ...);
  65. #if !defined(BOOST_ASIO_NO_TYPEID)
  66. // Initalise a service's key when the key_type typedef is available.
  67. template <typename Service>
  68. static void init_key(execution_context::service::key& key,
  69. enable_if_t<is_base_of<typename Service::key_type, Service>::value>*);
  70. #endif // !defined(BOOST_ASIO_NO_TYPEID)
  71. // Initialise a service's key based on its id.
  72. BOOST_ASIO_DECL static void init_key_from_id(
  73. execution_context::service::key& key,
  74. const execution_context::id& id);
  75. #if !defined(BOOST_ASIO_NO_TYPEID)
  76. // Initialise a service's key based on its id.
  77. template <typename Service>
  78. static void init_key_from_id(execution_context::service::key& key,
  79. const service_id<Service>& /*id*/);
  80. #endif // !defined(BOOST_ASIO_NO_TYPEID)
  81. // Check if a service matches the given id.
  82. BOOST_ASIO_DECL static bool keys_match(
  83. const execution_context::service::key& key1,
  84. const execution_context::service::key& key2);
  85. // The type of a factory function used for creating a service instance.
  86. typedef execution_context::service*(*factory_type)(void*);
  87. // Factory function for creating a service instance.
  88. template <typename Service, typename Owner>
  89. static execution_context::service* create(void* owner);
  90. // Destroy a service instance.
  91. BOOST_ASIO_DECL static void destroy(execution_context::service* service);
  92. // Helper class to manage service pointers.
  93. struct auto_service_ptr;
  94. friend struct auto_service_ptr;
  95. struct auto_service_ptr
  96. {
  97. execution_context::service* ptr_;
  98. ~auto_service_ptr() { destroy(ptr_); }
  99. };
  100. // Get the service object corresponding to the specified service key. Will
  101. // create a new service object automatically if no such object already
  102. // exists. Ownership of the service object is not transferred to the caller.
  103. BOOST_ASIO_DECL execution_context::service* do_use_service(
  104. const execution_context::service::key& key,
  105. factory_type factory, void* owner);
  106. // Add a service object. Throws on error, in which case ownership of the
  107. // object is retained by the caller.
  108. BOOST_ASIO_DECL void do_add_service(
  109. const execution_context::service::key& key,
  110. execution_context::service* new_service);
  111. // Check whether a service object with the specified key already exists.
  112. BOOST_ASIO_DECL bool do_has_service(
  113. const execution_context::service::key& key) const;
  114. // Mutex to protect access to internal data.
  115. mutable boost::asio::detail::mutex mutex_;
  116. // The owner of this service registry and the services it contains.
  117. execution_context& owner_;
  118. // The first service in the list of contained services.
  119. execution_context::service* first_service_;
  120. };
  121. } // namespace detail
  122. } // namespace asio
  123. } // namespace boost
  124. #include <boost/asio/detail/pop_options.hpp>
  125. #include <boost/asio/detail/impl/service_registry.hpp>
  126. #if defined(BOOST_ASIO_HEADER_ONLY)
  127. # include <boost/asio/detail/impl/service_registry.ipp>
  128. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  129. #endif // BOOST_ASIO_DETAIL_SERVICE_REGISTRY_HPP