| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055 |
- //
- // impl/use_future.hpp
- // ~~~~~~~~~~~~~~~~~~~
- //
- // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
- //
- // Distributed under the Boost Software License, Version 1.0. (See accompanying
- // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- //
- #ifndef BOOST_ASIO_IMPL_USE_FUTURE_HPP
- #define BOOST_ASIO_IMPL_USE_FUTURE_HPP
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- # pragma once
- #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
- #include <boost/asio/detail/config.hpp>
- #include <tuple>
- #include <boost/asio/async_result.hpp>
- #include <boost/asio/detail/memory.hpp>
- #include <boost/asio/dispatch.hpp>
- #include <boost/system/error_code.hpp>
- #include <boost/asio/execution.hpp>
- #include <boost/asio/packaged_task.hpp>
- #include <boost/system/system_error.hpp>
- #include <boost/asio/system_executor.hpp>
- #include <boost/asio/detail/push_options.hpp>
- namespace boost {
- namespace asio {
- namespace detail {
- #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
- template <typename T, typename F, typename... Args>
- inline void promise_invoke_and_set(std::promise<T>& p,
- F& f, BOOST_ASIO_MOVE_ARG(Args)... args)
- {
- #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
- try
- #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
- {
- p.set_value(f(BOOST_ASIO_MOVE_CAST(Args)(args)...));
- }
- #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
- catch (...)
- {
- p.set_exception(std::current_exception());
- }
- #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
- }
- template <typename F, typename... Args>
- inline void promise_invoke_and_set(std::promise<void>& p,
- F& f, BOOST_ASIO_MOVE_ARG(Args)... args)
- {
- #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
- try
- #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
- {
- f(BOOST_ASIO_MOVE_CAST(Args)(args)...);
- p.set_value();
- }
- #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
- catch (...)
- {
- p.set_exception(std::current_exception());
- }
- #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
- }
- #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
- template <typename T, typename F>
- inline void promise_invoke_and_set(std::promise<T>& p, F& f)
- {
- #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
- try
- #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
- {
- p.set_value(f());
- }
- #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
- catch (...)
- {
- p.set_exception(std::current_exception());
- }
- #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
- }
- template <typename F, typename Args>
- inline void promise_invoke_and_set(std::promise<void>& p, F& f)
- {
- #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
- try
- #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
- {
- f();
- p.set_value();
- #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
- }
- catch (...)
- {
- p.set_exception(std::current_exception());
- }
- #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
- }
- #if defined(BOOST_ASIO_NO_EXCEPTIONS)
- #define BOOST_ASIO_PRIVATE_PROMISE_INVOKE_DEF(n) \
- template <typename T, typename F, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
- inline void promise_invoke_and_set(std::promise<T>& p, \
- F& f, BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \
- { \
- p.set_value(f(BOOST_ASIO_VARIADIC_MOVE_ARGS(n))); \
- } \
- \
- template <typename F, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
- inline void promise_invoke_and_set(std::promise<void>& p, \
- F& f, BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \
- { \
- f(BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \
- p.set_value(); \
- } \
- /**/
- BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_PROMISE_INVOKE_DEF)
- #undef BOOST_ASIO_PRIVATE_PROMISE_INVOKE_DEF
- #else // defined(BOOST_ASIO_NO_EXCEPTIONS)
- #define BOOST_ASIO_PRIVATE_PROMISE_INVOKE_DEF(n) \
- template <typename T, typename F, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
- inline void promise_invoke_and_set(std::promise<T>& p, \
- F& f, BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \
- { \
- try \
- { \
- p.set_value(f(BOOST_ASIO_VARIADIC_MOVE_ARGS(n))); \
- } \
- catch (...) \
- { \
- p.set_exception(std::current_exception()); \
- } \
- } \
- \
- template <typename F, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
- inline void promise_invoke_and_set(std::promise<void>& p, \
- F& f, BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \
- { \
- try \
- { \
- f(BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \
- p.set_value(); \
- } \
- catch (...) \
- { \
- p.set_exception(std::current_exception()); \
- } \
- } \
- /**/
- BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_PROMISE_INVOKE_DEF)
- #undef BOOST_ASIO_PRIVATE_PROMISE_INVOKE_DEF
- #endif // defined(BOOST_ASIO_NO_EXCEPTIONS)
- #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
- // A function object adapter to invoke a nullary function object and capture
- // any exception thrown into a promise.
- template <typename T, typename F>
- class promise_invoker
- {
- public:
- promise_invoker(const shared_ptr<std::promise<T> >& p,
- BOOST_ASIO_MOVE_ARG(F) f)
- : p_(p), f_(BOOST_ASIO_MOVE_CAST(F)(f))
- {
- }
- void operator()()
- {
- #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
- try
- #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
- {
- f_();
- }
- #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
- catch (...)
- {
- p_->set_exception(std::current_exception());
- }
- #endif // !defined(BOOST_ASIO_NO_EXCEPTIONS)
- }
- private:
- shared_ptr<std::promise<T> > p_;
- typename decay<F>::type f_;
- };
- // An executor that adapts the system_executor to capture any exeption thrown
- // by a submitted function object and save it into a promise.
- template <typename T, typename Blocking = execution::blocking_t::possibly_t>
- class promise_executor
- {
- public:
- explicit promise_executor(const shared_ptr<std::promise<T> >& p)
- : p_(p)
- {
- }
- execution_context& query(execution::context_t) const BOOST_ASIO_NOEXCEPT
- {
- return boost::asio::query(system_executor(), execution::context);
- }
- static BOOST_ASIO_CONSTEXPR Blocking query(execution::blocking_t)
- {
- return Blocking();
- }
- promise_executor<T, execution::blocking_t::possibly_t>
- require(execution::blocking_t::possibly_t) const
- {
- return promise_executor<T, execution::blocking_t::possibly_t>(p_);
- }
- promise_executor<T, execution::blocking_t::never_t>
- require(execution::blocking_t::never_t) const
- {
- return promise_executor<T, execution::blocking_t::never_t>(p_);
- }
- template <typename F>
- void execute(BOOST_ASIO_MOVE_ARG(F) f) const
- {
- #if defined(BOOST_ASIO_NO_DEPRECATED)
- boost::asio::require(system_executor(), Blocking()).execute(
- promise_invoker<T, F>(p_, BOOST_ASIO_MOVE_CAST(F)(f)));
- #else // defined(BOOST_ASIO_NO_DEPRECATED)
- execution::execute(
- boost::asio::require(system_executor(), Blocking()),
- promise_invoker<T, F>(p_, BOOST_ASIO_MOVE_CAST(F)(f)));
- #endif // defined(BOOST_ASIO_NO_DEPRECATED)
- }
- #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
- execution_context& context() const BOOST_ASIO_NOEXCEPT
- {
- return system_executor().context();
- }
- void on_work_started() const BOOST_ASIO_NOEXCEPT {}
- void on_work_finished() const BOOST_ASIO_NOEXCEPT {}
- template <typename F, typename A>
- void dispatch(BOOST_ASIO_MOVE_ARG(F) f, const A&) const
- {
- promise_invoker<T, F>(p_, BOOST_ASIO_MOVE_CAST(F)(f))();
- }
- template <typename F, typename A>
- void post(BOOST_ASIO_MOVE_ARG(F) f, const A& a) const
- {
- system_executor().post(
- promise_invoker<T, F>(p_, BOOST_ASIO_MOVE_CAST(F)(f)), a);
- }
- template <typename F, typename A>
- void defer(BOOST_ASIO_MOVE_ARG(F) f, const A& a) const
- {
- system_executor().defer(
- promise_invoker<T, F>(p_, BOOST_ASIO_MOVE_CAST(F)(f)), a);
- }
- #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
- friend bool operator==(const promise_executor& a,
- const promise_executor& b) BOOST_ASIO_NOEXCEPT
- {
- return a.p_ == b.p_;
- }
- friend bool operator!=(const promise_executor& a,
- const promise_executor& b) BOOST_ASIO_NOEXCEPT
- {
- return a.p_ != b.p_;
- }
- private:
- shared_ptr<std::promise<T> > p_;
- };
- // The base class for all completion handlers that create promises.
- template <typename T>
- class promise_creator
- {
- public:
- typedef promise_executor<T> executor_type;
- executor_type get_executor() const BOOST_ASIO_NOEXCEPT
- {
- return executor_type(p_);
- }
- typedef std::future<T> future_type;
- future_type get_future()
- {
- return p_->get_future();
- }
- protected:
- template <typename Allocator>
- void create_promise(const Allocator& a)
- {
- BOOST_ASIO_REBIND_ALLOC(Allocator, char) b(a);
- p_ = std::allocate_shared<std::promise<T>>(b, std::allocator_arg, b);
- }
- shared_ptr<std::promise<T> > p_;
- };
- // For completion signature void().
- class promise_handler_0
- : public promise_creator<void>
- {
- public:
- void operator()()
- {
- this->p_->set_value();
- }
- };
- // For completion signature void(error_code).
- class promise_handler_ec_0
- : public promise_creator<void>
- {
- public:
- void operator()(const boost::system::error_code& ec)
- {
- if (ec)
- {
- this->p_->set_exception(
- std::make_exception_ptr(
- boost::system::system_error(ec)));
- }
- else
- {
- this->p_->set_value();
- }
- }
- };
- // For completion signature void(exception_ptr).
- class promise_handler_ex_0
- : public promise_creator<void>
- {
- public:
- void operator()(const std::exception_ptr& ex)
- {
- if (ex)
- {
- this->p_->set_exception(ex);
- }
- else
- {
- this->p_->set_value();
- }
- }
- };
- // For completion signature void(T).
- template <typename T>
- class promise_handler_1
- : public promise_creator<T>
- {
- public:
- template <typename Arg>
- void operator()(BOOST_ASIO_MOVE_ARG(Arg) arg)
- {
- this->p_->set_value(BOOST_ASIO_MOVE_CAST(Arg)(arg));
- }
- };
- // For completion signature void(error_code, T).
- template <typename T>
- class promise_handler_ec_1
- : public promise_creator<T>
- {
- public:
- template <typename Arg>
- void operator()(const boost::system::error_code& ec,
- BOOST_ASIO_MOVE_ARG(Arg) arg)
- {
- if (ec)
- {
- this->p_->set_exception(
- std::make_exception_ptr(
- boost::system::system_error(ec)));
- }
- else
- this->p_->set_value(BOOST_ASIO_MOVE_CAST(Arg)(arg));
- }
- };
- // For completion signature void(exception_ptr, T).
- template <typename T>
- class promise_handler_ex_1
- : public promise_creator<T>
- {
- public:
- template <typename Arg>
- void operator()(const std::exception_ptr& ex,
- BOOST_ASIO_MOVE_ARG(Arg) arg)
- {
- if (ex)
- this->p_->set_exception(ex);
- else
- this->p_->set_value(BOOST_ASIO_MOVE_CAST(Arg)(arg));
- }
- };
- // For completion signature void(T1, ..., Tn);
- template <typename T>
- class promise_handler_n
- : public promise_creator<T>
- {
- public:
- #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
- template <typename... Args>
- void operator()(BOOST_ASIO_MOVE_ARG(Args)... args)
- {
- this->p_->set_value(
- std::forward_as_tuple(
- BOOST_ASIO_MOVE_CAST(Args)(args)...));
- }
- #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
- #define BOOST_ASIO_PRIVATE_CALL_OP_DEF(n) \
- template <BOOST_ASIO_VARIADIC_TPARAMS(n)> \
- void operator()(BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \
- {\
- this->p_->set_value( \
- std::forward_as_tuple( \
- BOOST_ASIO_VARIADIC_MOVE_ARGS(n))); \
- } \
- /**/
- BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_CALL_OP_DEF)
- #undef BOOST_ASIO_PRIVATE_CALL_OP_DEF
- #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
- };
- // For completion signature void(error_code, T1, ..., Tn);
- template <typename T>
- class promise_handler_ec_n
- : public promise_creator<T>
- {
- public:
- #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
- template <typename... Args>
- void operator()(const boost::system::error_code& ec,
- BOOST_ASIO_MOVE_ARG(Args)... args)
- {
- if (ec)
- {
- this->p_->set_exception(
- std::make_exception_ptr(
- boost::system::system_error(ec)));
- }
- else
- {
- this->p_->set_value(
- std::forward_as_tuple(
- BOOST_ASIO_MOVE_CAST(Args)(args)...));
- }
- }
- #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
- #define BOOST_ASIO_PRIVATE_CALL_OP_DEF(n) \
- template <BOOST_ASIO_VARIADIC_TPARAMS(n)> \
- void operator()(const boost::system::error_code& ec, \
- BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \
- {\
- if (ec) \
- { \
- this->p_->set_exception( \
- std::make_exception_ptr( \
- boost::system::system_error(ec))); \
- } \
- else \
- { \
- this->p_->set_value( \
- std::forward_as_tuple( \
- BOOST_ASIO_VARIADIC_MOVE_ARGS(n))); \
- } \
- } \
- /**/
- BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_CALL_OP_DEF)
- #undef BOOST_ASIO_PRIVATE_CALL_OP_DEF
- #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
- };
- // For completion signature void(exception_ptr, T1, ..., Tn);
- template <typename T>
- class promise_handler_ex_n
- : public promise_creator<T>
- {
- public:
- #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
- template <typename... Args>
- void operator()(const std::exception_ptr& ex,
- BOOST_ASIO_MOVE_ARG(Args)... args)
- {
- if (ex)
- this->p_->set_exception(ex);
- else
- {
- this->p_->set_value(
- std::forward_as_tuple(
- BOOST_ASIO_MOVE_CAST(Args)(args)...));
- }
- }
- #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
- #define BOOST_ASIO_PRIVATE_CALL_OP_DEF(n) \
- template <BOOST_ASIO_VARIADIC_TPARAMS(n)> \
- void operator()(const std::exception_ptr& ex, \
- BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \
- {\
- if (ex) \
- this->p_->set_exception(ex); \
- else \
- { \
- this->p_->set_value( \
- std::forward_as_tuple( \
- BOOST_ASIO_VARIADIC_MOVE_ARGS(n))); \
- } \
- } \
- /**/
- BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_CALL_OP_DEF)
- #undef BOOST_ASIO_PRIVATE_CALL_OP_DEF
- #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
- };
- // Helper template to choose the appropriate concrete promise handler
- // implementation based on the supplied completion signature.
- template <typename> class promise_handler_selector;
- template <>
- class promise_handler_selector<void()>
- : public promise_handler_0 {};
- template <>
- class promise_handler_selector<void(boost::system::error_code)>
- : public promise_handler_ec_0 {};
- template <>
- class promise_handler_selector<void(std::exception_ptr)>
- : public promise_handler_ex_0 {};
- template <typename Arg>
- class promise_handler_selector<void(Arg)>
- : public promise_handler_1<Arg> {};
- template <typename Arg>
- class promise_handler_selector<void(boost::system::error_code, Arg)>
- : public promise_handler_ec_1<Arg> {};
- template <typename Arg>
- class promise_handler_selector<void(std::exception_ptr, Arg)>
- : public promise_handler_ex_1<Arg> {};
- #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
- template <typename... Arg>
- class promise_handler_selector<void(Arg...)>
- : public promise_handler_n<std::tuple<Arg...> > {};
- template <typename... Arg>
- class promise_handler_selector<void(boost::system::error_code, Arg...)>
- : public promise_handler_ec_n<std::tuple<Arg...> > {};
- template <typename... Arg>
- class promise_handler_selector<void(std::exception_ptr, Arg...)>
- : public promise_handler_ex_n<std::tuple<Arg...> > {};
- #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
- #define BOOST_ASIO_PRIVATE_PROMISE_SELECTOR_DEF(n) \
- template <typename Arg, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
- class promise_handler_selector< \
- void(Arg, BOOST_ASIO_VARIADIC_TARGS(n))> \
- : public promise_handler_n< \
- std::tuple<Arg, BOOST_ASIO_VARIADIC_TARGS(n)> > {}; \
- \
- template <typename Arg, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
- class promise_handler_selector< \
- void(boost::system::error_code, Arg, BOOST_ASIO_VARIADIC_TARGS(n))> \
- : public promise_handler_ec_n< \
- std::tuple<Arg, BOOST_ASIO_VARIADIC_TARGS(n)> > {}; \
- \
- template <typename Arg, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
- class promise_handler_selector< \
- void(std::exception_ptr, Arg, BOOST_ASIO_VARIADIC_TARGS(n))> \
- : public promise_handler_ex_n< \
- std::tuple<Arg, BOOST_ASIO_VARIADIC_TARGS(n)> > {}; \
- /**/
- BOOST_ASIO_VARIADIC_GENERATE_5(BOOST_ASIO_PRIVATE_PROMISE_SELECTOR_DEF)
- #undef BOOST_ASIO_PRIVATE_PROMISE_SELECTOR_DEF
- #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
- // Completion handlers produced from the use_future completion token, when not
- // using use_future::operator().
- template <typename Signature, typename Allocator>
- class promise_handler
- : public promise_handler_selector<Signature>
- {
- public:
- typedef Allocator allocator_type;
- typedef void result_type;
- promise_handler(use_future_t<Allocator> u)
- : allocator_(u.get_allocator())
- {
- this->create_promise(allocator_);
- }
- allocator_type get_allocator() const BOOST_ASIO_NOEXCEPT
- {
- return allocator_;
- }
- private:
- Allocator allocator_;
- };
- template <typename Function>
- struct promise_function_wrapper
- {
- explicit promise_function_wrapper(Function& f)
- : function_(BOOST_ASIO_MOVE_CAST(Function)(f))
- {
- }
- explicit promise_function_wrapper(const Function& f)
- : function_(f)
- {
- }
- void operator()()
- {
- function_();
- }
- Function function_;
- };
- #if !defined(BOOST_ASIO_NO_DEPRECATED)
- template <typename Function, typename Signature, typename Allocator>
- inline void asio_handler_invoke(Function& f,
- promise_handler<Signature, Allocator>* h)
- {
- typename promise_handler<Signature, Allocator>::executor_type
- ex(h->get_executor());
- boost::asio::dispatch(ex, promise_function_wrapper<Function>(f));
- }
- template <typename Function, typename Signature, typename Allocator>
- inline void asio_handler_invoke(const Function& f,
- promise_handler<Signature, Allocator>* h)
- {
- typename promise_handler<Signature, Allocator>::executor_type
- ex(h->get_executor());
- boost::asio::dispatch(ex, promise_function_wrapper<Function>(f));
- }
- #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
- // Helper base class for async_result specialisation.
- template <typename Signature, typename Allocator>
- class promise_async_result
- {
- public:
- typedef promise_handler<Signature, Allocator> completion_handler_type;
- typedef typename completion_handler_type::future_type return_type;
- explicit promise_async_result(completion_handler_type& h)
- : future_(h.get_future())
- {
- }
- return_type get()
- {
- return BOOST_ASIO_MOVE_CAST(return_type)(future_);
- }
- private:
- return_type future_;
- };
- // Return value from use_future::operator().
- template <typename Function, typename Allocator>
- class packaged_token
- {
- public:
- packaged_token(Function f, const Allocator& a)
- : function_(BOOST_ASIO_MOVE_CAST(Function)(f)),
- allocator_(a)
- {
- }
- //private:
- Function function_;
- Allocator allocator_;
- };
- // Completion handlers produced from the use_future completion token, when
- // using use_future::operator().
- template <typename Function, typename Allocator, typename Result>
- class packaged_handler
- : public promise_creator<Result>
- {
- public:
- typedef Allocator allocator_type;
- typedef void result_type;
- packaged_handler(packaged_token<Function, Allocator> t)
- : function_(BOOST_ASIO_MOVE_CAST(Function)(t.function_)),
- allocator_(t.allocator_)
- {
- this->create_promise(allocator_);
- }
- allocator_type get_allocator() const BOOST_ASIO_NOEXCEPT
- {
- return allocator_;
- }
- #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
- template <typename... Args>
- void operator()(BOOST_ASIO_MOVE_ARG(Args)... args)
- {
- (promise_invoke_and_set)(*this->p_,
- function_, BOOST_ASIO_MOVE_CAST(Args)(args)...);
- }
- #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
- void operator()()
- {
- (promise_invoke_and_set)(*this->p_, function_);
- }
- #define BOOST_ASIO_PRIVATE_CALL_OP_DEF(n) \
- template <BOOST_ASIO_VARIADIC_TPARAMS(n)> \
- void operator()(BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \
- {\
- (promise_invoke_and_set)(*this->p_, \
- function_, BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \
- } \
- /**/
- BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_CALL_OP_DEF)
- #undef BOOST_ASIO_PRIVATE_CALL_OP_DEF
- #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
- private:
- Function function_;
- Allocator allocator_;
- };
- #if !defined(BOOST_ASIO_NO_DEPRECATED)
- template <typename Function,
- typename Function1, typename Allocator, typename Result>
- inline void asio_handler_invoke(Function& f,
- packaged_handler<Function1, Allocator, Result>* h)
- {
- typename packaged_handler<Function1, Allocator, Result>::executor_type
- ex(h->get_executor());
- boost::asio::dispatch(ex, promise_function_wrapper<Function>(f));
- }
- template <typename Function,
- typename Function1, typename Allocator, typename Result>
- inline void asio_handler_invoke(const Function& f,
- packaged_handler<Function1, Allocator, Result>* h)
- {
- typename packaged_handler<Function1, Allocator, Result>::executor_type
- ex(h->get_executor());
- boost::asio::dispatch(ex, promise_function_wrapper<Function>(f));
- }
- #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
- // Helper base class for async_result specialisation.
- template <typename Function, typename Allocator, typename Result>
- class packaged_async_result
- {
- public:
- typedef packaged_handler<Function, Allocator, Result> completion_handler_type;
- typedef typename completion_handler_type::future_type return_type;
- explicit packaged_async_result(completion_handler_type& h)
- : future_(h.get_future())
- {
- }
- return_type get()
- {
- return BOOST_ASIO_MOVE_CAST(return_type)(future_);
- }
- private:
- return_type future_;
- };
- } // namespace detail
- template <typename Allocator> template <typename Function>
- inline detail::packaged_token<typename decay<Function>::type, Allocator>
- use_future_t<Allocator>::operator()(BOOST_ASIO_MOVE_ARG(Function) f) const
- {
- return detail::packaged_token<typename decay<Function>::type, Allocator>(
- BOOST_ASIO_MOVE_CAST(Function)(f), allocator_);
- }
- #if !defined(GENERATING_DOCUMENTATION)
- #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
- template <typename Allocator, typename Result, typename... Args>
- class async_result<use_future_t<Allocator>, Result(Args...)>
- : public detail::promise_async_result<
- void(typename decay<Args>::type...), Allocator>
- {
- public:
- explicit async_result(
- typename detail::promise_async_result<void(typename decay<Args>::type...),
- Allocator>::completion_handler_type& h)
- : detail::promise_async_result<
- void(typename decay<Args>::type...), Allocator>(h)
- {
- }
- };
- template <typename Function, typename Allocator,
- typename Result, typename... Args>
- class async_result<detail::packaged_token<Function, Allocator>, Result(Args...)>
- : public detail::packaged_async_result<Function, Allocator,
- typename result_of<Function(Args...)>::type>
- {
- public:
- explicit async_result(
- typename detail::packaged_async_result<Function, Allocator,
- typename result_of<Function(Args...)>::type>::completion_handler_type& h)
- : detail::packaged_async_result<Function, Allocator,
- typename result_of<Function(Args...)>::type>(h)
- {
- }
- };
- #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
- template <typename Allocator, typename Result>
- class async_result<use_future_t<Allocator>, Result()>
- : public detail::promise_async_result<void(), Allocator>
- {
- public:
- explicit async_result(
- typename detail::promise_async_result<
- void(), Allocator>::completion_handler_type& h)
- : detail::promise_async_result<void(), Allocator>(h)
- {
- }
- };
- template <typename Function, typename Allocator, typename Result>
- class async_result<detail::packaged_token<Function, Allocator>, Result()>
- : public detail::packaged_async_result<Function, Allocator,
- typename result_of<Function()>::type>
- {
- public:
- explicit async_result(
- typename detail::packaged_async_result<Function, Allocator,
- typename result_of<Function()>::type>::completion_handler_type& h)
- : detail::packaged_async_result<Function, Allocator,
- typename result_of<Function()>::type>(h)
- {
- }
- };
- #define BOOST_ASIO_PRIVATE_ASYNC_RESULT_DEF(n) \
- template <typename Allocator, \
- typename Result, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
- class async_result<use_future_t<Allocator>, \
- Result(BOOST_ASIO_VARIADIC_TARGS(n))> \
- : public detail::promise_async_result< \
- void(BOOST_ASIO_VARIADIC_DECAY(n)), Allocator> \
- { \
- public: \
- explicit async_result( \
- typename detail::promise_async_result< \
- void(BOOST_ASIO_VARIADIC_DECAY(n)), \
- Allocator>::completion_handler_type& h) \
- : detail::promise_async_result< \
- void(BOOST_ASIO_VARIADIC_DECAY(n)), Allocator>(h) \
- { \
- } \
- }; \
- \
- template <typename Function, typename Allocator, \
- typename Result, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
- class async_result<detail::packaged_token<Function, Allocator>, \
- Result(BOOST_ASIO_VARIADIC_TARGS(n))> \
- : public detail::packaged_async_result<Function, Allocator, \
- typename result_of<Function(BOOST_ASIO_VARIADIC_TARGS(n))>::type> \
- { \
- public: \
- explicit async_result( \
- typename detail::packaged_async_result<Function, Allocator, \
- typename result_of<Function(BOOST_ASIO_VARIADIC_TARGS(n))>::type \
- >::completion_handler_type& h) \
- : detail::packaged_async_result<Function, Allocator, \
- typename result_of<Function(BOOST_ASIO_VARIADIC_TARGS(n))>::type>(h) \
- { \
- } \
- }; \
- /**/
- BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_ASYNC_RESULT_DEF)
- #undef BOOST_ASIO_PRIVATE_ASYNC_RESULT_DEF
- #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
- namespace traits {
- #if !defined(BOOST_ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT)
- template <typename T, typename Blocking>
- struct equality_comparable<
- boost::asio::detail::promise_executor<T, Blocking> >
- {
- BOOST_ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
- BOOST_ASIO_STATIC_CONSTEXPR(bool, is_noexcept = true);
- };
- #endif // !defined(BOOST_ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT)
- #if !defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT)
- template <typename T, typename Blocking, typename Function>
- struct execute_member<
- boost::asio::detail::promise_executor<T, Blocking>, Function>
- {
- BOOST_ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
- BOOST_ASIO_STATIC_CONSTEXPR(bool, is_noexcept = false);
- typedef void result_type;
- };
- #endif // !defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT)
- #if !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
- template <typename T, typename Blocking, typename Property>
- struct query_static_constexpr_member<
- boost::asio::detail::promise_executor<T, Blocking>,
- Property,
- typename boost::asio::enable_if<
- boost::asio::is_convertible<
- Property,
- boost::asio::execution::blocking_t
- >::value
- >::type
- >
- {
- BOOST_ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
- BOOST_ASIO_STATIC_CONSTEXPR(bool, is_noexcept = true);
- typedef Blocking result_type;
- static BOOST_ASIO_CONSTEXPR result_type value() BOOST_ASIO_NOEXCEPT
- {
- return Blocking();
- }
- };
- #endif // !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
- #if !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_MEMBER_TRAIT)
- template <typename T, typename Blocking>
- struct query_member<
- boost::asio::detail::promise_executor<T, Blocking>,
- execution::context_t
- >
- {
- BOOST_ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
- BOOST_ASIO_STATIC_CONSTEXPR(bool, is_noexcept = true);
- typedef boost::asio::system_context& result_type;
- };
- #endif // !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_MEMBER_TRAIT)
- #if !defined(BOOST_ASIO_HAS_DEDUCED_REQUIRE_MEMBER_TRAIT)
- template <typename T, typename Blocking>
- struct require_member<
- boost::asio::detail::promise_executor<T, Blocking>,
- execution::blocking_t::possibly_t
- >
- {
- BOOST_ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
- BOOST_ASIO_STATIC_CONSTEXPR(bool, is_noexcept = true);
- typedef boost::asio::detail::promise_executor<T,
- execution::blocking_t::possibly_t> result_type;
- };
- template <typename T, typename Blocking>
- struct require_member<
- boost::asio::detail::promise_executor<T, Blocking>,
- execution::blocking_t::never_t
- >
- {
- BOOST_ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
- BOOST_ASIO_STATIC_CONSTEXPR(bool, is_noexcept = true);
- typedef boost::asio::detail::promise_executor<T,
- execution::blocking_t::never_t> result_type;
- };
- #endif // !defined(BOOST_ASIO_HAS_DEDUCED_REQUIRE_MEMBER_TRAIT)
- } // namespace traits
- #endif // !defined(GENERATING_DOCUMENTATION)
- } // namespace asio
- } // namespace boost
- #include <boost/asio/detail/pop_options.hpp>
- #endif // BOOST_ASIO_IMPL_USE_FUTURE_HPP
|