execution_context.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // impl/execution_context.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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_IMPL_EXECUTION_CONTEXT_HPP
  11. #define 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 "asio/detail/handler_type_requirements.hpp"
  16. #include "asio/detail/scoped_ptr.hpp"
  17. #include "asio/detail/service_registry.hpp"
  18. #include "asio/detail/push_options.hpp"
  19. namespace asio {
  20. #if !defined(GENERATING_DOCUMENTATION)
  21. template <typename Service>
  22. inline Service& use_service(execution_context& e)
  23. {
  24. // Check that Service meets the necessary type requirements.
  25. (void)static_cast<execution_context::service*>(static_cast<Service*>(0));
  26. return e.service_registry_->template use_service<Service>();
  27. }
  28. template <typename Service, typename... Args>
  29. Service& make_service(execution_context& e, Args&&... args)
  30. {
  31. detail::scoped_ptr<Service> svc(
  32. new Service(e, static_cast<Args&&>(args)...));
  33. e.service_registry_->template add_service<Service>(svc.get());
  34. Service& result = *svc;
  35. svc.release();
  36. return result;
  37. }
  38. template <typename Service>
  39. inline void add_service(execution_context& e, Service* svc)
  40. {
  41. // Check that Service meets the necessary type requirements.
  42. (void)static_cast<execution_context::service*>(static_cast<Service*>(0));
  43. e.service_registry_->template add_service<Service>(svc);
  44. }
  45. template <typename Service>
  46. inline bool has_service(execution_context& e)
  47. {
  48. // Check that Service meets the necessary type requirements.
  49. (void)static_cast<execution_context::service*>(static_cast<Service*>(0));
  50. return e.service_registry_->template has_service<Service>();
  51. }
  52. #endif // !defined(GENERATING_DOCUMENTATION)
  53. inline execution_context& execution_context::service::context()
  54. {
  55. return owner_;
  56. }
  57. } // namespace asio
  58. #include "asio/detail/pop_options.hpp"
  59. #endif // ASIO_IMPL_EXECUTION_CONTEXT_HPP