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.

152 lines
4.8KB

  1. //
  2. // detail/service_registry.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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/execution_context.hpp"
  20. #include "asio/detail/push_options.hpp"
  21. namespace asio {
  22. class io_context;
  23. namespace detail {
  24. template <typename T>
  25. class typeid_wrapper {};
  26. class service_registry
  27. : private noncopyable
  28. {
  29. public:
  30. // Constructor.
  31. ASIO_DECL service_registry(execution_context& owner);
  32. // Destructor.
  33. ASIO_DECL ~service_registry();
  34. // Shutdown all services.
  35. ASIO_DECL void shutdown_services();
  36. // Destroy all services.
  37. ASIO_DECL void destroy_services();
  38. // Notify all services of a fork event.
  39. ASIO_DECL void notify_fork(execution_context::fork_event fork_ev);
  40. // Get the service object corresponding to the specified service type. Will
  41. // create a new service object automatically if no such object already
  42. // exists. Ownership of the service object is not transferred to the caller.
  43. template <typename Service>
  44. Service& use_service();
  45. // Get the service object corresponding to the specified service type. Will
  46. // create a new service object automatically if no such object already
  47. // exists. Ownership of the service object is not transferred to the caller.
  48. // This overload is used for backwards compatibility with services that
  49. // inherit from io_context::service.
  50. template <typename Service>
  51. Service& use_service(io_context& owner);
  52. // Add a service object. Throws on error, in which case ownership of the
  53. // object is retained by the caller.
  54. template <typename Service>
  55. void add_service(Service* new_service);
  56. // Check whether a service object of the specified type already exists.
  57. template <typename Service>
  58. bool has_service() const;
  59. private:
  60. // Initialise a service's key based on its id.
  61. ASIO_DECL static void init_key(
  62. execution_context::service::key& key,
  63. const execution_context::id& id);
  64. #if !defined(ASIO_NO_TYPEID)
  65. // Initialise a service's key based on its id.
  66. template <typename Service>
  67. static void init_key(execution_context::service::key& key,
  68. const service_id<Service>& /*id*/);
  69. #endif // !defined(ASIO_NO_TYPEID)
  70. // Check if a service matches the given id.
  71. ASIO_DECL static bool keys_match(
  72. const execution_context::service::key& key1,
  73. const execution_context::service::key& key2);
  74. // The type of a factory function used for creating a service instance.
  75. typedef execution_context::service*(*factory_type)(void*);
  76. // Factory function for creating a service instance.
  77. template <typename Service, typename Owner>
  78. static execution_context::service* create(void* owner);
  79. // Destroy a service instance.
  80. ASIO_DECL static void destroy(execution_context::service* service);
  81. // Helper class to manage service pointers.
  82. struct auto_service_ptr;
  83. friend struct auto_service_ptr;
  84. struct auto_service_ptr
  85. {
  86. execution_context::service* ptr_;
  87. ~auto_service_ptr() { destroy(ptr_); }
  88. };
  89. // Get the service object corresponding to the specified service key. Will
  90. // create a new service object automatically if no such object already
  91. // exists. Ownership of the service object is not transferred to the caller.
  92. ASIO_DECL execution_context::service* do_use_service(
  93. const execution_context::service::key& key,
  94. factory_type factory, void* owner);
  95. // Add a service object. Throws on error, in which case ownership of the
  96. // object is retained by the caller.
  97. ASIO_DECL void do_add_service(
  98. const execution_context::service::key& key,
  99. execution_context::service* new_service);
  100. // Check whether a service object with the specified key already exists.
  101. ASIO_DECL bool do_has_service(
  102. const execution_context::service::key& key) const;
  103. // Mutex to protect access to internal data.
  104. mutable asio::detail::mutex mutex_;
  105. // The owner of this service registry and the services it contains.
  106. execution_context& owner_;
  107. // The first service in the list of contained services.
  108. execution_context::service* first_service_;
  109. };
  110. } // namespace detail
  111. } // namespace asio
  112. #include "asio/detail/pop_options.hpp"
  113. #include "asio/detail/impl/service_registry.hpp"
  114. #if defined(ASIO_HEADER_ONLY)
  115. # include "asio/detail/impl/service_registry.ipp"
  116. #endif // defined(ASIO_HEADER_ONLY)
  117. #endif // ASIO_DETAIL_SERVICE_REGISTRY_HPP