service_registry.hpp 5.2 KB

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