service_registry.hpp 5.5 KB

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