/Src/Dependencies/Boost/boost/asio/detail/service_registry.hpp

http://hadesmem.googlecode.com/ · C++ Header · 157 lines · 91 code · 32 blank · 34 comment · 10 complexity · f03630c75c08d427dd80ea3397492f36 MD5 · raw file

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