service_registry.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //
  2. // service_registry.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 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/push_options.hpp"
  16. #include "asio/detail/push_options.hpp"
  17. #include <typeinfo>
  18. #include "asio/detail/pop_options.hpp"
  19. #include "asio/io_service.hpp"
  20. #include "asio/detail/mutex.hpp"
  21. #include "asio/detail/noncopyable.hpp"
  22. #include "asio/detail/service_id.hpp"
  23. #if defined(BOOST_NO_TYPEID)
  24. # if !defined(ASIO_NO_TYPEID)
  25. # define ASIO_NO_TYPEID
  26. # endif // !defined(ASIO_NO_TYPEID)
  27. #endif // defined(BOOST_NO_TYPEID)
  28. namespace asio {
  29. namespace detail {
  30. #if defined(__GNUC__)
  31. # if (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) || (__GNUC__ > 4)
  32. # pragma GCC visibility push (default)
  33. # endif // (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) || (__GNUC__ > 4)
  34. #endif // defined(__GNUC__)
  35. template <typename T>
  36. class typeid_wrapper {};
  37. #if defined(__GNUC__)
  38. # if (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) || (__GNUC__ > 4)
  39. # pragma GCC visibility pop
  40. # endif // (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) || (__GNUC__ > 4)
  41. #endif // defined(__GNUC__)
  42. class service_registry
  43. : private noncopyable
  44. {
  45. public:
  46. // Constructor.
  47. service_registry(asio::io_service& o)
  48. : owner_(o),
  49. first_service_(0)
  50. {
  51. }
  52. // Destructor.
  53. ~service_registry()
  54. {
  55. // Shutdown all services. This must be done in a separate loop before the
  56. // services are destroyed since the destructors of user-defined handler
  57. // objects may try to access other service objects.
  58. asio::io_service::service* service = first_service_;
  59. while (service)
  60. {
  61. service->shutdown_service();
  62. service = service->next_;
  63. }
  64. // Destroy all services.
  65. while (first_service_)
  66. {
  67. asio::io_service::service* next_service = first_service_->next_;
  68. destroy(first_service_);
  69. first_service_ = next_service;
  70. }
  71. }
  72. // Get the service object corresponding to the specified service type. Will
  73. // create a new service object automatically if no such object already
  74. // exists. Ownership of the service object is not transferred to the caller.
  75. template <typename Service>
  76. Service& use_service()
  77. {
  78. asio::io_service::service::key key;
  79. init_key(key, Service::id);
  80. factory_type factory = &service_registry::create<Service>;
  81. return *static_cast<Service*>(do_use_service(key, factory));
  82. }
  83. // Add a service object. Returns false on error, in which case ownership of
  84. // the object is retained by the caller.
  85. template <typename Service>
  86. bool add_service(Service* new_service)
  87. {
  88. asio::io_service::service::key key;
  89. init_key(key, Service::id);
  90. return do_add_service(key, new_service);
  91. }
  92. // Check whether a service object of the specified type already exists.
  93. template <typename Service>
  94. bool has_service() const
  95. {
  96. asio::io_service::service::key key;
  97. init_key(key, Service::id);
  98. return do_has_service(key);
  99. }
  100. private:
  101. // Initialise a service's key based on its id.
  102. void init_key(asio::io_service::service::key& key,
  103. const asio::io_service::id& id)
  104. {
  105. key.type_info_ = 0;
  106. key.id_ = &id;
  107. }
  108. #if !defined(ASIO_NO_TYPEID)
  109. // Initialise a service's key based on its id.
  110. template <typename Service>
  111. void init_key(asio::io_service::service::key& key,
  112. const asio::detail::service_id<Service>& /*id*/)
  113. {
  114. key.type_info_ = &typeid(typeid_wrapper<Service>);
  115. key.id_ = 0;
  116. }
  117. #endif // !defined(ASIO_NO_TYPEID)
  118. // Check if a service matches the given id.
  119. static bool keys_match(
  120. const asio::io_service::service::key& key1,
  121. const asio::io_service::service::key& key2)
  122. {
  123. if (key1.id_ && key2.id_)
  124. if (key1.id_ == key2.id_)
  125. return true;
  126. if (key1.type_info_ && key2.type_info_)
  127. if (*key1.type_info_ == *key2.type_info_)
  128. return true;
  129. return false;
  130. }
  131. // The type of a factory function used for creating a service instance.
  132. typedef asio::io_service::service*
  133. (*factory_type)(asio::io_service&);
  134. // Factory function for creating a service instance.
  135. template <typename Service>
  136. static asio::io_service::service* create(
  137. asio::io_service& owner)
  138. {
  139. return new Service(owner);
  140. }
  141. // Destroy a service instance.
  142. static void destroy(asio::io_service::service* service)
  143. {
  144. delete service;
  145. }
  146. // Helper class to manage service pointers.
  147. struct auto_service_ptr
  148. {
  149. asio::io_service::service* ptr_;
  150. ~auto_service_ptr() { destroy(ptr_); }
  151. };
  152. // Get the service object corresponding to the specified service key. Will
  153. // create a new service object automatically if no such object already
  154. // exists. Ownership of the service object is not transferred to the caller.
  155. asio::io_service::service* do_use_service(
  156. const asio::io_service::service::key& key,
  157. factory_type factory)
  158. {
  159. asio::detail::mutex::scoped_lock lock(mutex_);
  160. // First see if there is an existing service object with the given key.
  161. asio::io_service::service* service = first_service_;
  162. while (service)
  163. {
  164. if (keys_match(service->key_, key))
  165. return service;
  166. service = service->next_;
  167. }
  168. // Create a new service object. The service registry's mutex is not locked
  169. // at this time to allow for nested calls into this function from the new
  170. // service's constructor.
  171. lock.unlock();
  172. auto_service_ptr new_service = { factory(owner_) };
  173. new_service.ptr_->key_ = key;
  174. lock.lock();
  175. // Check that nobody else created another service object of the same type
  176. // while the lock was released.
  177. service = first_service_;
  178. while (service)
  179. {
  180. if (keys_match(service->key_, key))
  181. return service;
  182. service = service->next_;
  183. }
  184. // Service was successfully initialised, pass ownership to registry.
  185. new_service.ptr_->next_ = first_service_;
  186. first_service_ = new_service.ptr_;
  187. new_service.ptr_ = 0;
  188. return first_service_;
  189. }
  190. // Add a service object. Returns false on error, in which case ownership of
  191. // the object is retained by the caller.
  192. bool do_add_service(
  193. const asio::io_service::service::key& key,
  194. asio::io_service::service* new_service)
  195. {
  196. asio::detail::mutex::scoped_lock lock(mutex_);
  197. // Check if there is an existing service object with the given key.
  198. asio::io_service::service* service = first_service_;
  199. while (service)
  200. {
  201. if (keys_match(service->key_, key))
  202. return false;
  203. service = service->next_;
  204. }
  205. // Take ownership of the service object.
  206. new_service->key_ = key;
  207. new_service->next_ = first_service_;
  208. first_service_ = new_service;
  209. return true;
  210. }
  211. // Check whether a service object with the specified key already exists.
  212. bool do_has_service(const asio::io_service::service::key& key) const
  213. {
  214. asio::detail::mutex::scoped_lock lock(mutex_);
  215. asio::io_service::service* service = first_service_;
  216. while (service)
  217. {
  218. if (keys_match(service->key_, key))
  219. return true;
  220. service = service->next_;
  221. }
  222. return false;
  223. }
  224. // Mutex to protect access to internal data.
  225. mutable asio::detail::mutex mutex_;
  226. // The owner of this service registry and the services it contains.
  227. asio::io_service& owner_;
  228. // The first service in the list of contained services.
  229. asio::io_service::service* first_service_;
  230. };
  231. } // namespace detail
  232. } // namespace asio
  233. #include "asio/detail/pop_options.hpp"
  234. #endif // ASIO_DETAIL_SERVICE_REGISTRY_HPP