Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
5.3KB

  1. //
  2. // detail/service_registry.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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_SERVICE_REGISTRY_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #include <typeinfo>
  17. #include "asio/detail/mutex.hpp"
  18. #include "asio/detail/noncopyable.hpp"
  19. #include "asio/detail/type_traits.hpp"
  20. #include "asio/execution_context.hpp"
  21. #include "asio/detail/push_options.hpp"
  22. namespace asio {
  23. class io_context;
  24. namespace detail {
  25. template <typename T>
  26. class typeid_wrapper {};
  27. class service_registry
  28. : private noncopyable
  29. {
  30. public:
  31. // Constructor.
  32. ASIO_DECL service_registry(execution_context& owner);
  33. // Destructor.
  34. ASIO_DECL ~service_registry();
  35. // Shutdown all services.
  36. ASIO_DECL void shutdown_services();
  37. // Destroy all services.
  38. ASIO_DECL void destroy_services();
  39. // Notify all services of a fork event.
  40. ASIO_DECL void notify_fork(execution_context::fork_event fork_ev);
  41. // Get the service object corresponding to the specified service type. Will
  42. // create a new service object automatically if no such object already
  43. // exists. Ownership of the service object is not transferred to the caller.
  44. template <typename Service>
  45. Service& use_service();
  46. // Get the service object corresponding to the specified service type. Will
  47. // create a new service object automatically if no such object already
  48. // exists. Ownership of the service object is not transferred to the caller.
  49. // This overload is used for backwards compatibility with services that
  50. // inherit from io_context::service.
  51. template <typename Service>
  52. Service& use_service(io_context& owner);
  53. // Add a service object. Throws on error, in which case ownership of the
  54. // object is retained by the caller.
  55. template <typename Service>
  56. void add_service(Service* new_service);
  57. // Check whether a service object of the specified type already exists.
  58. template <typename Service>
  59. bool has_service() const;
  60. private:
  61. // Initalise a service's key when the key_type typedef is not available.
  62. template <typename Service>
  63. static void init_key(execution_context::service::key& key, ...);
  64. #if !defined(ASIO_NO_TYPEID)
  65. // Initalise a service's key when the key_type typedef is available.
  66. template <typename Service>
  67. static void init_key(execution_context::service::key& key,
  68. typename enable_if<
  69. is_base_of<typename Service::key_type, Service>::value>::type*);
  70. #endif // !defined(ASIO_NO_TYPEID)
  71. // Initialise a service's key based on its id.
  72. ASIO_DECL static void init_key_from_id(
  73. execution_context::service::key& key,
  74. const execution_context::id& id);
  75. #if !defined(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(ASIO_NO_TYPEID)
  81. // Check if a service matches the given id.
  82. 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. 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. 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. 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. ASIO_DECL bool do_has_service(
  113. const execution_context::service::key& key) const;
  114. // Mutex to protect access to internal data.
  115. mutable 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. #include "asio/detail/pop_options.hpp"
  124. #include "asio/detail/impl/service_registry.hpp"
  125. #if defined(ASIO_HEADER_ONLY)
  126. # include "asio/detail/impl/service_registry.ipp"
  127. #endif // defined(ASIO_HEADER_ONLY)
  128. #endif // ASIO_DETAIL_SERVICE_REGISTRY_HPP