execution_context.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // impl/execution_context.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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_IMPL_EXECUTION_CONTEXT_HPP
  11. #define BOOST_ASIO_IMPL_EXECUTION_CONTEXT_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/handler_type_requirements.hpp>
  16. #include <boost/asio/detail/scoped_ptr.hpp>
  17. #include <boost/asio/detail/service_registry.hpp>
  18. #include <boost/asio/detail/push_options.hpp>
  19. namespace boost {
  20. namespace asio {
  21. #if !defined(GENERATING_DOCUMENTATION)
  22. template <typename Service>
  23. inline Service& use_service(execution_context& e)
  24. {
  25. // Check that Service meets the necessary type requirements.
  26. (void)static_cast<execution_context::service*>(static_cast<Service*>(0));
  27. return e.service_registry_->template use_service<Service>();
  28. }
  29. #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  30. template <typename Service, typename... Args>
  31. Service& make_service(execution_context& e, BOOST_ASIO_MOVE_ARG(Args)... args)
  32. {
  33. detail::scoped_ptr<Service> svc(
  34. new Service(e, BOOST_ASIO_MOVE_CAST(Args)(args)...));
  35. e.service_registry_->template add_service<Service>(svc.get());
  36. Service& result = *svc;
  37. svc.release();
  38. return result;
  39. }
  40. #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  41. template <typename Service>
  42. Service& make_service(execution_context& e)
  43. {
  44. detail::scoped_ptr<Service> svc(new Service(e));
  45. e.service_registry_->template add_service<Service>(svc.get());
  46. Service& result = *svc;
  47. svc.release();
  48. return result;
  49. }
  50. #define BOOST_ASIO_PRIVATE_MAKE_SERVICE_DEF(n) \
  51. template <typename Service, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
  52. Service& make_service(execution_context& e, \
  53. BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \
  54. { \
  55. detail::scoped_ptr<Service> svc( \
  56. new Service(e, BOOST_ASIO_VARIADIC_MOVE_ARGS(n))); \
  57. e.service_registry_->template add_service<Service>(svc.get()); \
  58. Service& result = *svc; \
  59. svc.release(); \
  60. return result; \
  61. } \
  62. /**/
  63. BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_MAKE_SERVICE_DEF)
  64. #undef BOOST_ASIO_PRIVATE_MAKE_SERVICE_DEF
  65. #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  66. template <typename Service>
  67. inline void add_service(execution_context& e, Service* svc)
  68. {
  69. // Check that Service meets the necessary type requirements.
  70. (void)static_cast<execution_context::service*>(static_cast<Service*>(0));
  71. e.service_registry_->template add_service<Service>(svc);
  72. }
  73. template <typename Service>
  74. inline bool has_service(execution_context& e)
  75. {
  76. // Check that Service meets the necessary type requirements.
  77. (void)static_cast<execution_context::service*>(static_cast<Service*>(0));
  78. return e.service_registry_->template has_service<Service>();
  79. }
  80. #endif // !defined(GENERATING_DOCUMENTATION)
  81. inline execution_context& execution_context::service::context()
  82. {
  83. return owner_;
  84. }
  85. } // namespace asio
  86. } // namespace boost
  87. #include <boost/asio/detail/pop_options.hpp>
  88. #endif // BOOST_ASIO_IMPL_EXECUTION_CONTEXT_HPP