service_registry.hpp 4.5 KB

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