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