service_registry.hpp 4.7 KB

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