Update use_future interface and implementation.
This change makes use_future consistent with the networking TS. In
particular, the package() function has been removed; this functionality
is now accessed via use_future::operator().
diff --git a/asio/include/Makefile.am b/asio/include/Makefile.am
index aeb53b5..02cf03e 100644
--- a/asio/include/Makefile.am
+++ b/asio/include/Makefile.am
@@ -376,7 +376,7 @@
asio/local/detail/endpoint.hpp \
asio/local/detail/impl/endpoint.ipp \
asio/local/stream_protocol.hpp \
- asio/package.hpp \
+ asio/packaged_task.hpp \
asio/placeholders.hpp \
asio/posix/basic_descriptor.hpp \
asio/posix/basic_stream_descriptor.hpp \
diff --git a/asio/include/asio.hpp b/asio/include/asio.hpp
index 7cbf667..bdbaeeb 100644
--- a/asio/include/asio.hpp
+++ b/asio/include/asio.hpp
@@ -96,6 +96,7 @@
#include "asio/local/connect_pair.hpp"
#include "asio/local/datagram_protocol.hpp"
#include "asio/local/stream_protocol.hpp"
+#include "asio/packaged_task.hpp"
#include "asio/placeholders.hpp"
#include "asio/posix/basic_descriptor.hpp"
#include "asio/posix/basic_stream_descriptor.hpp"
@@ -124,6 +125,7 @@
#include "asio/thread.hpp"
#include "asio/thread_pool.hpp"
#include "asio/time_traits.hpp"
+#include "asio/use_future.hpp"
#include "asio/uses_executor.hpp"
#include "asio/version.hpp"
#include "asio/wait_traits.hpp"
diff --git a/asio/include/asio/detail/config.hpp b/asio/include/asio/detail/config.hpp
index 45a16b7..13c6cee 100644
--- a/asio/include/asio/detail/config.hpp
+++ b/asio/include/asio/detail/config.hpp
@@ -675,6 +675,33 @@
# endif // !defined(ASIO_DISABLE_STD_CALL_ONCE)
#endif // !defined(ASIO_HAS_STD_CALL_ONCE)
+// Standard library support for futures.
+#if !defined(ASIO_HAS_STD_FUTURE)
+# if !defined(ASIO_DISABLE_STD_FUTURE)
+# if defined(__clang__)
+# if defined(ASIO_HAS_CLANG_LIBCXX)
+# define ASIO_HAS_STD_FUTURE 1
+# elif (__cplusplus >= 201103)
+# if __has_include(<future>)
+# define ASIO_HAS_STD_FUTURE 1
+# endif // __has_include(<mutex>)
+# endif // (__cplusplus >= 201103)
+# endif // defined(__clang__)
+# if defined(__GNUC__)
+# if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 7)) || (__GNUC__ > 4)
+# if defined(__GXX_EXPERIMENTAL_CXX0X__)
+# define ASIO_HAS_STD_FUTURE 1
+# endif // defined(__GXX_EXPERIMENTAL_CXX0X__)
+# endif // ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 7)) || (__GNUC__ > 4)
+# endif // defined(__GNUC__)
+# if defined(ASIO_MSVC)
+# if (_MSC_VER >= 1700)
+# define ASIO_HAS_STD_FUTURE 1
+# endif // (_MSC_VER >= 1700)
+# endif // defined(ASIO_MSVC)
+# endif // !defined(ASIO_DISABLE_STD_FUTURE)
+#endif // !defined(ASIO_HAS_STD_FUTURE)
+
// Windows App target. Windows but with a limited API.
#if !defined(ASIO_WINDOWS_APP)
# if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0603)
diff --git a/asio/include/asio/detail/variadic_templates.hpp b/asio/include/asio/detail/variadic_templates.hpp
index 696c884..0b877fe 100644
--- a/asio/include/asio/detail/variadic_templates.hpp
+++ b/asio/include/asio/detail/variadic_templates.hpp
@@ -94,6 +94,24 @@
ASIO_MOVE_CAST(T3)(x3), ASIO_MOVE_CAST(T4)(x4), \
ASIO_MOVE_CAST(T5)(x5)
+# define ASIO_VARIADIC_DECAY(n) \
+ ASIO_VARIADIC_DECAY_##n
+
+# define ASIO_VARIADIC_DECAY_1 \
+ typename decay<T1>::type
+# define ASIO_VARIADIC_DECAY_2 \
+ typename decay<T1>::type, typename decay<T2>::type
+# define ASIO_VARIADIC_DECAY_3 \
+ typename decay<T1>::type, typename decay<T2>::type, \
+ typename decay<T3>::type
+# define ASIO_VARIADIC_DECAY_4 \
+ typename decay<T1>::type, typename decay<T2>::type, \
+ typename decay<T3>::type, typename decay<T4>::type
+# define ASIO_VARIADIC_DECAY_5 \
+ typename decay<T1>::type, typename decay<T2>::type, \
+ typename decay<T3>::type, typename decay<T4>::type, \
+ typename decay<T5>::type
+
# define ASIO_VARIADIC_GENERATE(m) m(1) m(2) m(3) m(4) m(5)
#endif // !defined(ASIO_HAS_VARIADIC_TEMPLATES)
diff --git a/asio/include/asio/impl/use_future.hpp b/asio/include/asio/impl/use_future.hpp
index f3f7d59..1410358 100644
--- a/asio/include/asio/impl/use_future.hpp
+++ b/asio/include/asio/impl/use_future.hpp
@@ -17,151 +17,847 @@
#include "asio/detail/config.hpp"
#include <future>
+#include <tuple>
#include "asio/async_result.hpp"
+#include "asio/detail/memory.hpp"
#include "asio/error_code.hpp"
+#include "asio/packaged_task.hpp"
#include "asio/system_error.hpp"
+#include "asio/system_executor.hpp"
#include "asio/detail/push_options.hpp"
namespace asio {
namespace detail {
- // Completion handler to adapt a promise as a completion handler.
- template <typename T>
- class promise_handler
+#if defined(ASIO_HAS_VARIADIC_TEMPLATES)
+
+template <typename T, typename F, typename... Args>
+inline void promise_invoke_and_set(std::promise<T>& p,
+ F& f, ASIO_MOVE_ARG(Args)... args)
+{
+ try
{
- public:
- // Construct from use_future special value.
- template <typename Allocator>
- promise_handler(use_future_t<Allocator> uf)
- : promise_(std::allocate_shared<std::promise<T> >(
- uf.get_allocator(), std::allocator_arg, uf.get_allocator()))
- {
- }
-
- void operator()(T t)
- {
- promise_->set_value(t);
- }
-
- void operator()(const asio::error_code& ec, T t)
- {
- if (ec)
- promise_->set_exception(
- std::make_exception_ptr(
- asio::system_error(ec)));
- else
- promise_->set_value(t);
- }
-
- //private:
- std::shared_ptr<std::promise<T> > promise_;
- };
-
- // Completion handler to adapt a void promise as a completion handler.
- template <>
- class promise_handler<void>
+ p.set_value(f(ASIO_MOVE_CAST(Args)(args)...));
+ }
+ catch (...)
{
- public:
- // Construct from use_future special value. Used during rebinding.
- template <typename Allocator>
- promise_handler(use_future_t<Allocator> uf)
- : promise_(std::allocate_shared<std::promise<void> >(
- uf.get_allocator(), std::allocator_arg, uf.get_allocator()))
- {
- }
+ p.set_exception(std::current_exception());
+ }
+}
- void operator()()
- {
- promise_->set_value();
- }
-
- void operator()(const asio::error_code& ec)
- {
- if (ec)
- promise_->set_exception(
- std::make_exception_ptr(
- asio::system_error(ec)));
- else
- promise_->set_value();
- }
-
- //private:
- std::shared_ptr<std::promise<void> > promise_;
- };
-
- // Ensure any exceptions thrown from the handler are propagated back to the
- // caller via the future.
- template <typename Function, typename T>
- void asio_handler_invoke(Function f, promise_handler<T>* h)
+template <typename F, typename... Args>
+inline void promise_invoke_and_set(std::promise<void>& p,
+ F& f, ASIO_MOVE_ARG(Args)... args)
+{
+ try
{
- std::shared_ptr<std::promise<T> > p(h->promise_);
+ f(ASIO_MOVE_CAST(Args)(args)...);
+ p.set_value();
+ }
+ catch (...)
+ {
+ p.set_exception(std::current_exception());
+ }
+}
+
+#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
+
+template <typename T, typename F>
+inline void promise_invoke_and_set(std::promise<T>& p, F& f)
+{
+ try
+ {
+ p.set_value(f());
+ }
+ catch (...)
+ {
+ p.set_exception(std::current_exception());
+ }
+}
+
+template <typename F, typename Args>
+inline void promise_invoke_and_set(std::promise<void>& p, F& f)
+{
+ try
+ {
+ f();
+ p.set_value();
+ }
+ catch (...)
+ {
+ p.set_exception(std::current_exception());
+ }
+}
+
+#define ASIO_PRIVATE_PROMISE_INVOKE_DEF(n) \
+ template <typename T, typename F, ASIO_VARIADIC_TPARAMS(n)> \
+ inline void promise_invoke_and_set(std::promise<T>& p, \
+ F& f, ASIO_VARIADIC_MOVE_PARAMS(n)) \
+ { \
+ try \
+ { \
+ p.set_value(f(ASIO_VARIADIC_MOVE_ARGS(n))); \
+ } \
+ catch (...) \
+ { \
+ p.set_exception(std::current_exception()); \
+ } \
+ } \
+ \
+ template <typename F, ASIO_VARIADIC_TPARAMS(n)> \
+ inline void promise_invoke_and_set(std::promise<void>& p, \
+ F& f, ASIO_VARIADIC_MOVE_PARAMS(n)) \
+ { \
+ try \
+ { \
+ f(ASIO_VARIADIC_MOVE_ARGS(n)); \
+ p.set_value(); \
+ } \
+ catch (...) \
+ { \
+ p.set_exception(std::current_exception()); \
+ } \
+ } \
+ /**/
+ ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_PROMISE_INVOKE_DEF)
+#undef ASIO_PRIVATE_PROMISE_INVOKE_DEF
+
+#endif // defined(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,
+ ASIO_MOVE_ARG(F) f)
+ : p_(p), f_(f)
+ {
+ }
+
+ void operator()()
+ {
try
{
- f();
+ f_();
}
catch (...)
{
- p->set_exception(std::current_exception());
+ p_->set_exception(std::current_exception());
}
}
+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>
+class promise_executor
+{
+public:
+ explicit promise_executor(const shared_ptr<std::promise<T> >& p)
+ : p_(p)
+ {
+ }
+
+ execution_context& context() const ASIO_NOEXCEPT
+ {
+ return system_executor().context();
+ }
+
+ void on_work_started() const ASIO_NOEXCEPT {}
+ void on_work_finished() const ASIO_NOEXCEPT {}
+
+ template <typename F, typename A>
+ void dispatch(ASIO_MOVE_ARG(F) f, const A&) const
+ {
+ promise_invoker<T, F>(p_, ASIO_MOVE_CAST(F)(f))();
+ }
+
+ template <typename F, typename A>
+ void post(ASIO_MOVE_ARG(F) f, const A& a) const
+ {
+ system_executor().post(
+ promise_invoker<T, F>(p_, ASIO_MOVE_CAST(F)(f)), a);
+ }
+
+ template <typename F, typename A>
+ void defer(ASIO_MOVE_ARG(F) f, const A& a) const
+ {
+ system_executor().defer(
+ promise_invoker<T, F>(p_, ASIO_MOVE_CAST(F)(f)), a);
+ }
+
+ friend bool operator==(const promise_executor& a,
+ const promise_executor& b) ASIO_NOEXCEPT
+ {
+ return a.p_ == b.p_;
+ }
+
+ friend bool operator!=(const promise_executor& a,
+ const promise_executor& b) 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 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)
+ {
+ p_ = std::allocate_shared<std::promise<T>>(
+ typename Allocator::template rebind<char>::other(a),
+ std::allocator_arg,
+ typename Allocator::template rebind<char>::other(a));
+ }
+
+ 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 asio::error_code& ec)
+ {
+ if (ec)
+ {
+ this->p_->set_exception(
+ std::make_exception_ptr(
+ asio::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()(ASIO_MOVE_ARG(Arg) arg)
+ {
+ this->p_->set_value(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 asio::error_code& ec,
+ ASIO_MOVE_ARG(Arg) arg)
+ {
+ if (ec)
+ {
+ this->p_->set_exception(
+ std::make_exception_ptr(
+ asio::system_error(ec)));
+ }
+ else
+ this->p_->set_value(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,
+ ASIO_MOVE_ARG(Arg) arg)
+ {
+ if (ex)
+ this->p_->set_exception(ex);
+ else
+ this->p_->set_value(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(ASIO_HAS_VARIADIC_TEMPLATES)
+
+ template <typename... Args>
+ void operator()(ASIO_MOVE_ARG(Args)... args)
+ {
+ this->p_->set_value(
+ std::forward_as_tuple(
+ ASIO_MOVE_CAST(Args)(args)...));
+ }
+
+#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
+
+#define ASIO_PRIVATE_CALL_OP_DEF(n) \
+ template <ASIO_VARIADIC_TPARAMS(n)> \
+ void operator()(ASIO_VARIADIC_MOVE_PARAMS(n)) \
+ {\
+ this->p_->set_value( \
+ std::forward_as_tuple( \
+ ASIO_VARIADIC_MOVE_ARGS(n))); \
+ } \
+ /**/
+ ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_CALL_OP_DEF)
+#undef ASIO_PRIVATE_CALL_OP_DEF
+
+#endif // defined(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(ASIO_HAS_VARIADIC_TEMPLATES)
+
+ template <typename... Args>
+ void operator()(const asio::error_code& ec,
+ ASIO_MOVE_ARG(Args)... args)
+ {
+ if (ec)
+ {
+ this->p_->set_exception(
+ std::make_exception_ptr(
+ asio::system_error(ec)));
+ }
+ else
+ {
+ this->p_->set_value(
+ std::forward_as_tuple(
+ ASIO_MOVE_CAST(Args)(args)...));
+ }
+ }
+
+#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
+
+#define ASIO_PRIVATE_CALL_OP_DEF(n) \
+ template <ASIO_VARIADIC_TPARAMS(n)> \
+ void operator()(const asio::error_code& ec, \
+ ASIO_VARIADIC_MOVE_PARAMS(n)) \
+ {\
+ if (ec) \
+ { \
+ this->p_->set_exception( \
+ std::make_exception_ptr( \
+ asio::system_error(ec))); \
+ } \
+ else \
+ { \
+ this->p_->set_value( \
+ std::forward_as_tuple( \
+ ASIO_VARIADIC_MOVE_ARGS(n))); \
+ } \
+ } \
+ /**/
+ ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_CALL_OP_DEF)
+#undef ASIO_PRIVATE_CALL_OP_DEF
+
+#endif // defined(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(ASIO_HAS_VARIADIC_TEMPLATES)
+
+ template <typename... Args>
+ void operator()(const std::exception_ptr& ex,
+ ASIO_MOVE_ARG(Args)... args)
+ {
+ if (ex)
+ this->p_->set_exception(ex);
+ else
+ {
+ this->p_->set_value(
+ std::forward_as_tuple(
+ ASIO_MOVE_CAST(Args)(args)...));
+ }
+ }
+
+#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
+
+#define ASIO_PRIVATE_CALL_OP_DEF(n) \
+ template <ASIO_VARIADIC_TPARAMS(n)> \
+ void operator()(const std::exception_ptr& ex, \
+ ASIO_VARIADIC_MOVE_PARAMS(n)) \
+ {\
+ if (ex) \
+ this->p_->set_exception(ex); \
+ else \
+ { \
+ this->p_->set_value( \
+ std::forward_as_tuple( \
+ ASIO_VARIADIC_MOVE_ARGS(n))); \
+ } \
+ } \
+ /**/
+ ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_CALL_OP_DEF)
+#undef ASIO_PRIVATE_CALL_OP_DEF
+
+#endif // defined(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(asio::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(asio::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> {};
+
+template <typename... Arg>
+class promise_handler_selector<void(Arg...)>
+ : public promise_handler_n<std::tuple<Arg...> > {};
+
+template <typename... Arg>
+class promise_handler_selector<void(asio::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...> > {};
+
+// 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 ASIO_NOEXCEPT
+ {
+ return allocator_;
+ }
+
+private:
+ Allocator allocator_;
+};
+
+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());
+ ex.dispatch(ASIO_MOVE_CAST(Function)(f), std::allocator<void>());
+}
+
+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());
+ ex.dispatch(f, std::allocator<void>());
+}
+
+// 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 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_(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_(ASIO_MOVE_CAST(Function)(t.function_)),
+ allocator_(t.allocator_)
+ {
+ this->create_promise(allocator_);
+ }
+
+ allocator_type get_allocator() const ASIO_NOEXCEPT
+ {
+ return allocator_;
+ }
+
+#if defined(ASIO_HAS_VARIADIC_TEMPLATES)
+
+ template <typename... Args>
+ void operator()(ASIO_MOVE_ARG(Args)... args)
+ {
+ (promise_invoke_and_set)(*this->p_,
+ function_, ASIO_MOVE_CAST(Args)(args)...);
+ }
+
+#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
+
+ void operator()()
+ {
+ (promise_invoke_and_set)(*this->p_, function_);
+ }
+
+#define ASIO_PRIVATE_CALL_OP_DEF(n) \
+ template <ASIO_VARIADIC_TPARAMS(n)> \
+ void operator()(ASIO_VARIADIC_MOVE_PARAMS(n)) \
+ {\
+ (promise_invoke_and_set)(*this->p_, \
+ function_, ASIO_VARIADIC_MOVE_ARGS(n)); \
+ } \
+ /**/
+ ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_CALL_OP_DEF)
+#undef ASIO_PRIVATE_CALL_OP_DEF
+
+#endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
+
+private:
+ Function function_;
+ Allocator allocator_;
+};
+
+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());
+ ex.dispatch(ASIO_MOVE_CAST(Function)(f), std::allocator<void>());
+}
+
+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());
+ ex.dispatch(f, std::allocator<void>());
+}
+
+// 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 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()(ASIO_MOVE_ARG(Function) f) const
+{
+ return detail::packaged_token<typename decay<Function>::type, Allocator>(
+ ASIO_MOVE_CAST(Function)(f), allocator_);
+}
+
#if !defined(GENERATING_DOCUMENTATION)
-// Handler traits specialisation for promise_handler.
-template <typename T>
-class async_result<detail::promise_handler<T> >
+#if defined(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:
- // The initiating function will return a future.
- typedef std::future<T> type;
-
- // Constructor creates a new promise for the async operation, and obtains the
- // corresponding future.
- explicit async_result(detail::promise_handler<T>& h)
+ 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)
{
- value_ = h.promise_->get_future();
}
-
- // Obtain the future to be returned from the initiating function.
- type get() { return std::move(value_); }
-
-private:
- type value_;
};
-// Handler type specialisation for use_future.
-template <typename Allocator, typename ReturnType>
-struct handler_type<use_future_t<Allocator>, ReturnType()>
+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>
{
- typedef detail::promise_handler<void> 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)
+ {
+ }
};
-// Handler type specialisation for use_future.
-template <typename Allocator, typename ReturnType, typename Arg1>
-struct handler_type<use_future_t<Allocator>, ReturnType(Arg1)>
+#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
+
+template <typename Allocator, typename Result>
+class async_result<use_future_t<Allocator>, Result()>
+ : public detail::promise_async_result<void(), Allocator>
{
- typedef detail::promise_handler<Arg1> type;
+public:
+ explicit async_result(
+ typename detail::promise_async_result<
+ void(), Allocator>::completion_handler_type& h)
+ : detail::promise_async_result<void(), Allocator>(h)
+ {
+ }
};
-// Handler type specialisation for use_future.
-template <typename Allocator, typename ReturnType>
-struct handler_type<use_future_t<Allocator>,
- ReturnType(asio::error_code)>
+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>
{
- typedef detail::promise_handler<void> 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)
+ {
+ }
};
-// Handler type specialisation for use_future.
-template <typename Allocator, typename ReturnType, typename Arg2>
-struct handler_type<use_future_t<Allocator>,
- ReturnType(asio::error_code, Arg2)>
+#define ASIO_PRIVATE_ASYNC_RESULT_DEF(n) \
+ template <typename Allocator, \
+ typename Result, ASIO_VARIADIC_TPARAMS(n)> \
+ class async_result<use_future_t<Allocator>, \
+ Result(ASIO_VARIADIC_TARGS(n))> \
+ : public detail::promise_async_result< \
+ void(ASIO_VARIADIC_DECAY(n)), Allocator> \
+ { \
+ public: \
+ explicit async_result( \
+ typename detail::promise_async_result< \
+ void(ASIO_VARIADIC_DECAY(n)), \
+ Allocator>::completion_handler_type& h) \
+ : detail::promise_async_result< \
+ void(ASIO_VARIADIC_DECAY(n)), Allocator>(h) \
+ { \
+ } \
+ }; \
+ \
+ template <typename Function, typename Allocator, \
+ typename Result, ASIO_VARIADIC_TPARAMS(n)> \
+ class async_result<detail::packaged_token<Function, Allocator>, \
+ Result(ASIO_VARIADIC_TARGS(n))> \
+ : public detail::packaged_async_result<Function, Allocator, \
+ typename result_of<Function(ASIO_VARIADIC_TARGS(n))>::type> \
+ { \
+ public: \
+ explicit async_result( \
+ typename detail::packaged_async_result<Function, Allocator, \
+ typename result_of<Function(ASIO_VARIADIC_TARGS(n))>::type \
+ >::completion_handler_type& h) \
+ : detail::packaged_async_result<Function, Allocator, \
+ typename result_of<Function(ASIO_VARIADIC_TARGS(n))>::type>(h) \
+ { \
+ } \
+ }; \
+ /**/
+ ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_ASYNC_RESULT_DEF)
+#undef ASIO_PRIVATE_ASYNC_RESULT_DEF
+
+#endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
+
+#if !defined(ASIO_NO_DEPRECATED)
+
+template <typename Allocator, typename Signature>
+struct handler_type<use_future_t<Allocator>, Signature>
{
- typedef detail::promise_handler<Arg2> type;
+ typedef typename async_result<use_future_t<Allocator>,
+ Signature>::completion_handler_type type;
};
+template <typename Signature, typename Allocator>
+class async_result<detail::promise_handler<Signature, Allocator> >
+ : public detail::promise_async_result<Signature, Allocator>
+{
+public:
+ typedef typename detail::promise_async_result<
+ Signature, Allocator>::return_type type;
+
+ explicit async_result(
+ typename detail::promise_async_result<
+ Signature, Allocator>::completion_handler_type& h)
+ : detail::promise_async_result<Signature, Allocator>(h)
+ {
+ }
+};
+
+template <typename Function, typename Allocator, typename Signature>
+struct handler_type<detail::packaged_token<Function, Allocator>, Signature>
+{
+ typedef typename async_result<detail::packaged_token<Function, Allocator>,
+ Signature>::completion_handler_type type;
+};
+
+template <typename Function, typename Allocator, typename Result>
+class async_result<detail::packaged_handler<Function, Allocator, Result> >
+ : public detail::packaged_async_result<Function, Allocator, Result>
+{
+public:
+ typedef typename detail::packaged_async_result<
+ Function, Allocator, Result>::return_type type;
+
+ explicit async_result(
+ typename detail::packaged_async_result<
+ Function, Allocator, Result>::completion_handler_type& h)
+ : detail::packaged_async_result<Function, Allocator, Result>(h)
+ {
+ }
+};
+
+#endif // !defined(ASIO_NO_DEPRECATED)
+
#endif // !defined(GENERATING_DOCUMENTATION)
} // namespace asio
diff --git a/asio/include/asio/package.hpp b/asio/include/asio/package.hpp
deleted file mode 100644
index 4e5e1c5..0000000
--- a/asio/include/asio/package.hpp
+++ /dev/null
@@ -1,293 +0,0 @@
-//
-// package.hpp
-// ~~~~~~~~~~~
-//
-// Copyright (c) 2003-2015 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 ASIO_USE_PACKAGE_HPP
-#define ASIO_USE_PACKAGE_HPP
-
-#if defined(_MSC_VER) && (_MSC_VER >= 1200)
-# pragma once
-#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
-
-#include "asio/detail/config.hpp"
-#include <future>
-#include <memory>
-#include "asio/async_result.hpp"
-#include "asio/detail/type_traits.hpp"
-#include "asio/detail/variadic_templates.hpp"
-
-#include "asio/detail/push_options.hpp"
-
-namespace asio {
-
-/// Class to enable lazy construction of a packaged_task from a completion
-/// token.
-/**
- * The packaged_token class is used to adapt a function object as a packaged
- * task. When this adapter is passed as a completion token to an asynchronous
- * operation, the result of the function object is retuned via a std::future.
- *
- * Use the @ref package function rather than using this class directly.
- */
-template <typename Function, typename Allocator = std::allocator<void> >
-class packaged_token
-{
-public:
- /// The allocator type. The allocator is used when constructing the
- /// @c std::promise object for a given asynchronous operation.
- typedef Allocator allocator_type;
-
- /// Construct using specified allocator.
- explicit packaged_token(Function f)
- : func_(std::move(f))
- {
- }
-
- /// Construct using specified allocator.
- packaged_token(Function f, const Allocator& allocator)
- : func_(std::move(f)),
- allocator_(allocator)
- {
- }
-
- /// Obtain allocator.
- allocator_type get_allocator() const ASIO_NOEXCEPT
- {
- return allocator_;
- }
-
-private:
- template <class, class> friend class packaged_handler;
- Function func_;
- Allocator allocator_;
-};
-
-/// A packaged_task with an associated allocator.
-template <typename Signature, typename Allocator>
-class packaged_handler : public std::packaged_task<Signature>
-{
-public:
- /// The allocator type. The allocator is used when constructing the
- /// @c std::promise object for a given asynchronous operation.
- typedef Allocator allocator_type;
-
- /// Construct from a packaged token.
- template <typename Function>
- packaged_handler(
- packaged_token<Function, Allocator>&& token)
-#if defined(_MSC_VER)
- : std::packaged_task<Signature>(std::move(token.func_)),
-#elif defined(ASIO_HAS_CLANG_LIBCXX)
- : std::packaged_task<Signature>(std::allocator_arg,
- typename std::allocator_traits<
- Allocator>::template rebind_alloc<char>(token.allocator_),
- std::move(token.func_)),
-#else
- : std::packaged_task<Signature>(std::allocator_arg,
- token.allocator_, std::move(token.func_)),
-#endif
- allocator_(token.allocator_)
- {
- }
-
- /// Move construct from another packaged handler.
- packaged_handler(packaged_handler&& other)
- : std::packaged_task<Signature>(
- static_cast<std::packaged_task<Signature>&&>(other)),
- allocator_(other.allocator_)
- {
- }
-
- /// Obtain allocator.
- allocator_type get_allocator() const ASIO_NOEXCEPT
- {
- return allocator_;
- }
-
-private:
- Allocator allocator_;
-};
-
-/// Wrap a function object in a packaged task.
-/**
- * The @c package function is used to adapt a function object as a packaged
- * task. When this adapter is passed as a completion token to an asynchronous
- * operation, the result of the function object is retuned via a std::future.
- *
- * @par Example
- *
- * @code std::future<std::size_t> fut =
- * my_socket.async_read_some(buffer,
- * package([](asio::error_code ec, std::size_t n)
- * {
- * return ec ? 0 : n;
- * }));
- * ...
- * std::size_t n = fut.get(); @endcode
- */
-template <typename Function>
-inline packaged_token<typename decay<Function>::type, std::allocator<void> >
-package(ASIO_MOVE_ARG(Function) function)
-{
- return packaged_token<typename decay<Function>::type, std::allocator<void> >(
- ASIO_MOVE_CAST(Function)(function), std::allocator<void>());
-}
-
-/// Wrap a function object in a packaged task.
-/**
- * The @c package function is used to adapt a function object as a packaged
- * task. When this adapter is passed as a completion token to an asynchronous
- * operation, the result of the function object is retuned via a std::future.
- *
- * @par Example
- *
- * @code std::future<std::size_t> fut =
- * my_socket.async_read_some(buffer,
- * package([](asio::error_code ec, std::size_t n)
- * {
- * return ec ? 0 : n;
- * }));
- * ...
- * std::size_t n = fut.get(); @endcode
- */
-template <typename Function, typename Allocator>
-inline packaged_token<typename decay<Function>::type, Allocator> package(
- ASIO_MOVE_ARG(Function) function, const Allocator& a)
-{
- return packaged_token<typename decay<Function>::type, Allocator>(
- ASIO_MOVE_CAST(Function)(function), a);
-}
-
-#if !defined(GENERATING_DOCUMENTATION)
-
-#if defined(ASIO_HAS_VARIADIC_TEMPLATES)
-
-template <typename Function, typename Allocator, typename R, typename... Args>
-struct handler_type<packaged_token<Function, Allocator>, R(Args...)>
-{
- typedef packaged_handler<
- typename result_of<Function(Args...)>::type(Args...),
- Allocator> type;
-};
-
-#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
-
-template <typename Function, typename Allocator, typename R>
-struct handler_type<packaged_token<Function, Allocator>, R()>
-{
- typedef packaged_handler<
- typename result_of<Function()>::type(),
- Allocator> type;
-};
-
-#define ASIO_PRIVATE_HANDLER_TYPE_DEF(n) \
- template <typename Function, typename Allocator, \
- typename R, ASIO_VARIADIC_TPARAMS(n)> \
- struct handler_type< \
- packaged_token<Function, Allocator>, R(ASIO_VARIADIC_TARGS(n))> \
- { \
- typedef packaged_handler< \
- typename result_of< \
- Function(ASIO_VARIADIC_TARGS(n))>::type( \
- ASIO_VARIADIC_TARGS(n)), \
- Allocator> type; \
- }; \
- /**/
- ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_HANDLER_TYPE_DEF)
-#undef ASIO_PRIVATE_HANDLER_TYPE_DEF
-
-#endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
-
-#if defined(ASIO_HAS_VARIADIC_TEMPLATES)
-
-template <typename R, typename... Args>
-class async_result<std::packaged_task<R(Args...)> >
-{
-public:
- typedef std::future<R> type;
-
- explicit async_result(std::packaged_task<R(Args...)>& h)
- : future_(h.get_future())
- {
- }
-
- type get()
- {
- return std::move(future_);
- }
-
-private:
- type future_;
-};
-
-#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
-
-template <typename R>
-class async_result<std::packaged_task<R()> >
-{
-public:
- typedef std::future<R> type;
-
- explicit async_result(std::packaged_task<R()>& h)
- : future_(h.get_future())
- {
- }
-
- type get()
- {
- return std::move(future_);
- }
-
-private:
- type future_;
-};
-
-#define ASIO_PRIVATE_ASYNC_RESULT_DEF(n) \
- template <typename R, ASIO_VARIADIC_TPARAMS(n)> \
- class async_result<std::packaged_task<R(ASIO_VARIADIC_TARGS(n))> > \
- { \
- public: \
- typedef std::future<R> type; \
- \
- explicit async_result( \
- std::packaged_task<R(ASIO_VARIADIC_TARGS(n))>& h) \
- : future_(h.get_future()) \
- { \
- } \
- \
- type get() \
- { \
- return std::move(future_); \
- } \
- \
- private: \
- type future_; \
- }; \
- /**/
- ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_ASYNC_RESULT_DEF)
-#undef ASIO_PRIVATE_ASYNC_RESULT_DEF
-
-#endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
-
-template <typename Signature, typename Allocator>
-class async_result<packaged_handler<Signature, Allocator>>
- : public async_result<std::packaged_task<Signature>>
-{
-public:
- explicit async_result(packaged_handler<Signature, Allocator>& h)
- : async_result<std::packaged_task<Signature>>(h) {}
-};
-
-#endif // !defined(GENERATING_DOCUMENTATION)
-
-} // namespace asio
-
-#include "asio/detail/pop_options.hpp"
-
-#endif // ASIO_USE_PACKAGE_HPP
diff --git a/asio/include/asio/packaged_task.hpp b/asio/include/asio/packaged_task.hpp
new file mode 100644
index 0000000..507df18
--- /dev/null
+++ b/asio/include/asio/packaged_task.hpp
@@ -0,0 +1,126 @@
+//
+// packaged_task.hpp
+// ~~~~~~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2015 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 ASIO_PACKAGED_TASK_HPP
+#define ASIO_PACKAGED_TASK_HPP
+
+#if defined(_MSC_VER) && (_MSC_VER >= 1200)
+# pragma once
+#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
+
+#include "asio/detail/config.hpp"
+
+#if defined(ASIO_HAS_STD_FUTURE) \
+ || defined(GENERATING_DOCUMENTATION)
+
+#include <future>
+#include "asio/async_result.hpp"
+#include "asio/detail/type_traits.hpp"
+#include "asio/detail/variadic_templates.hpp"
+
+#include "asio/detail/push_options.hpp"
+
+namespace asio {
+
+#if defined(ASIO_HAS_VARIADIC_TEMPLATES) \
+ || defined(GENERATING_DOCUMENTATION)
+
+/// Partial specialisation of @c async_result for @c std::packaged_task.
+template <typename Result, typename... Args, typename Signature>
+class async_result<std::packaged_task<Result(Args...)>, Signature>
+{
+public:
+ /// The packaged task is the concrete completion handler type.
+ typedef std::packaged_task<Result(Args...)> completion_handler_type;
+
+ /// The return type of the initiating function is the future obtained from
+ /// the packaged task.
+ typedef std::future<Result> return_type;
+
+ /// The constructor extracts the future from the packaged task.
+ explicit async_result(completion_handler_type& h)
+ : future_(h.get_future())
+ {
+ }
+
+ /// Returns the packaged task's future.
+ return_type get()
+ {
+ return std::move(future_);
+ }
+
+private:
+ return_type future_;
+};
+
+#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
+ // || defined(GENERATING_DOCUMENTATION)
+
+template <typename Result, typename Signature>
+struct async_result<std::packaged_task<Result()>, Signature>
+{
+ typedef std::packaged_task<Result()> completion_handler_type;
+ typedef std::future<Result> return_type;
+
+ explicit async_result(completion_handler_type& h)
+ : future_(h.get_future())
+ {
+ }
+
+ return_type get()
+ {
+ return std::move(future_);
+ }
+
+private:
+ return_type future_;
+};
+
+#define ASIO_PRIVATE_ASYNC_RESULT_DEF(n) \
+ template <typename Result, \
+ ASIO_VARIADIC_TPARAMS(n), typename Signature> \
+ class async_result< \
+ std::packaged_task<Result(ASIO_VARIADIC_TARGS(n))>, Signature> \
+ { \
+ public: \
+ typedef std::packaged_task< \
+ Result(ASIO_VARIADIC_TARGS(n))> \
+ completion_handler_type; \
+ \
+ typedef std::future<Result> return_type; \
+ \
+ explicit async_result(completion_handler_type& h) \
+ : future_(h.get_future()) \
+ { \
+ } \
+ \
+ return_type get() \
+ { \
+ return std::move(future_); \
+ } \
+ \
+ private: \
+ return_type future_; \
+ }; \
+ /**/
+ ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_ASYNC_RESULT_DEF)
+#undef ASIO_PRIVATE_ASYNC_RESULT_DEF
+
+#endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
+ // || defined(GENERATING_DOCUMENTATION)
+
+} // namespace asio
+
+#include "asio/detail/pop_options.hpp"
+
+#endif // defined(ASIO_HAS_STD_FUTURE)
+ // || defined(GENERATING_DOCUMENTATION)
+
+#endif // ASIO_PACKAGED_TASK_HPP
diff --git a/asio/include/asio/ts/executor.hpp b/asio/include/asio/ts/executor.hpp
index 8ba3871..2fd3e55 100644
--- a/asio/include/asio/ts/executor.hpp
+++ b/asio/include/asio/ts/executor.hpp
@@ -29,7 +29,7 @@
#include "asio/post.hpp"
#include "asio/defer.hpp"
#include "asio/strand.hpp"
-#include "asio/package.hpp"
+#include "asio/packaged_task.hpp"
#include "asio/use_future.hpp"
#endif // ASIO_TS_EXECUTOR_HPP
diff --git a/asio/include/asio/use_future.hpp b/asio/include/asio/use_future.hpp
index 15136a8..88a9965 100644
--- a/asio/include/asio/use_future.hpp
+++ b/asio/include/asio/use_future.hpp
@@ -16,11 +16,25 @@
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include "asio/detail/config.hpp"
+
+#if defined(ASIO_HAS_STD_FUTURE) \
+ || defined(GENERATING_DOCUMENTATION)
+
#include <memory>
+#include "asio/detail/type_traits.hpp"
#include "asio/detail/push_options.hpp"
namespace asio {
+namespace detail {
+
+template <typename Function, typename Allocator>
+class packaged_token;
+
+template <typename Function, typename Allocator, typename Result>
+class packaged_handler;
+
+} // namespace detail
/// Class used to specify that an asynchronous operation should return a future.
/**
@@ -78,6 +92,31 @@
return allocator_;
}
+ /// Wrap a function object in a packaged task.
+ /**
+ * The @c package function is used to adapt a function object as a packaged
+ * task. When this adapter is passed as a completion token to an asynchronous
+ * operation, the result of the function object is retuned via a std::future.
+ *
+ * @par Example
+ *
+ * @code std::future<std::size_t> fut =
+ * my_socket.async_read_some(buffer,
+ * use_future([](asio::error_code ec, std::size_t n)
+ * {
+ * return ec ? 0 : n;
+ * }));
+ * ...
+ * std::size_t n = fut.get(); @endcode
+ */
+ template <typename Function>
+#if defined(GENERATING_DOCUMENTATION)
+ unspecified
+#else // defined(GENERATING_DOCUMENTATION)
+ detail::packaged_token<typename decay<Function>::type, Allocator>
+#endif // defined(GENERATING_DOCUMENTATION)
+ operator()(ASIO_MOVE_ARG(Function) f) const;
+
private:
Allocator allocator_;
};
@@ -98,4 +137,7 @@
#include "asio/impl/use_future.hpp"
+#endif // defined(ASIO_HAS_STD_FUTURE)
+ // || defined(GENERATING_DOCUMENTATION)
+
#endif // ASIO_USE_FUTURE_HPP
diff --git a/asio/src/examples/cpp11/executors/bank_account_2.cpp b/asio/src/examples/cpp11/executors/bank_account_2.cpp
index 4b8dcd5..b646cb2 100644
--- a/asio/src/examples/cpp11/executors/bank_account_2.cpp
+++ b/asio/src/examples/cpp11/executors/bank_account_2.cpp
@@ -1,11 +1,11 @@
-#include <asio/package.hpp>
#include <asio/post.hpp>
#include <asio/thread_pool.hpp>
+#include <asio/use_future.hpp>
#include <iostream>
-using asio::package;
using asio::post;
using asio::thread_pool;
+using asio::use_future;
// Traditional active object pattern.
// Member functions block until operation is finished.
@@ -19,7 +19,7 @@
void deposit(int amount)
{
post(pool_,
- package([=]
+ use_future([=]
{
balance_ += amount;
})).get();
@@ -28,7 +28,7 @@
void withdraw(int amount)
{
post(pool_,
- package([=]
+ use_future([=]
{
if (balance_ >= amount)
balance_ -= amount;
@@ -38,7 +38,7 @@
int balance() const
{
return post(pool_,
- package([=]
+ use_future([=]
{
return balance_;
})).get();
diff --git a/asio/src/examples/cpp11/executors/pipeline.cpp b/asio/src/examples/cpp11/executors/pipeline.cpp
index a851e98..b5d76cc 100644
--- a/asio/src/examples/cpp11/executors/pipeline.cpp
+++ b/asio/src/examples/cpp11/executors/pipeline.cpp
@@ -1,9 +1,9 @@
#include <asio/associated_executor.hpp>
#include <asio/bind_executor.hpp>
#include <asio/execution_context.hpp>
-#include <asio/package.hpp>
#include <asio/post.hpp>
#include <asio/system_executor.hpp>
+#include <asio/use_future.hpp>
#include <condition_variable>
#include <future>
#include <memory>
@@ -15,9 +15,9 @@
using asio::execution_context;
using asio::executor_binder;
using asio::get_associated_executor;
-using asio::package;
using asio::post;
using asio::system_executor;
+using asio::use_future;
using asio::use_service;
// An executor that launches a new thread for each function submitted to it.
@@ -190,7 +190,7 @@
// Run the function, and as we're the last stage return a future so that the
// caller can wait for the pipeline to finish.
- return post(ex, package([in, f]() mutable { f(in); }));
+ return post(ex, use_future([in, f]() mutable { f(in); }));
}
// Launch an intermediate stage in a pipeline.
diff --git a/asio/src/examples/cpp14/executors/bank_account_2.cpp b/asio/src/examples/cpp14/executors/bank_account_2.cpp
index 28644dd..5cde164 100644
--- a/asio/src/examples/cpp14/executors/bank_account_2.cpp
+++ b/asio/src/examples/cpp14/executors/bank_account_2.cpp
@@ -2,9 +2,9 @@
#include <asio/ts/thread_pool.hpp>
#include <iostream>
-using asio::package;
using asio::post;
using asio::thread_pool;
+using asio::use_future;
// Traditional active object pattern.
// Member functions block until operation is finished.
@@ -18,7 +18,7 @@
void deposit(int amount)
{
post(pool_,
- package([=]
+ use_future([=]
{
balance_ += amount;
})).get();
@@ -27,7 +27,7 @@
void withdraw(int amount)
{
post(pool_,
- package([=]
+ use_future([=]
{
if (balance_ >= amount)
balance_ -= amount;
@@ -37,7 +37,7 @@
int balance() const
{
return post(pool_,
- package([=]
+ use_future([=]
{
return balance_;
})).get();
diff --git a/asio/src/examples/cpp14/executors/pipeline.cpp b/asio/src/examples/cpp14/executors/pipeline.cpp
index 1bee173..ccdb2f4 100644
--- a/asio/src/examples/cpp14/executors/pipeline.cpp
+++ b/asio/src/examples/cpp14/executors/pipeline.cpp
@@ -10,9 +10,9 @@
using asio::execution_context;
using asio::executor_binder;
using asio::get_associated_executor;
-using asio::package;
using asio::post;
using asio::system_executor;
+using asio::use_future;
using asio::use_service;
// An executor that launches a new thread for each function submitted to it.
@@ -185,7 +185,7 @@
// Run the function, and as we're the last stage return a future so that the
// caller can wait for the pipeline to finish.
- return post(ex, package([in, f = std::move(f)]() mutable { f(in); }));
+ return post(ex, use_future([in, f = std::move(f)]() mutable { f(in); }));
}
// Launch an intermediate stage in a pipeline.
diff --git a/asio/src/tests/Makefile.am b/asio/src/tests/Makefile.am
index b7ac755..778c413 100644
--- a/asio/src/tests/Makefile.am
+++ b/asio/src/tests/Makefile.am
@@ -94,6 +94,7 @@
unit/system_timer \
unit/thread \
unit/time_traits \
+ unit/use_future \
unit/wait_traits \
unit/waitable_timer_service \
unit/windows/basic_handle \
@@ -209,6 +210,7 @@
unit/system_timer \
unit/thread \
unit/time_traits \
+ unit/use_future \
unit/wait_traits \
unit/waitable_timer_service \
unit/windows/basic_handle \
@@ -337,6 +339,7 @@
unit_system_timer_SOURCES = unit/system_timer.cpp
unit_thread_SOURCES = unit/thread.cpp
unit_time_traits_SOURCES = unit/time_traits.cpp
+unit_use_future_SOURCES = unit/use_future.cpp
unit_wait_traits_SOURCES = unit/wait_traits.cpp
unit_waitable_timer_service_SOURCES = unit/waitable_timer_service.cpp
unit_windows_basic_handle_SOURCES = unit/windows/basic_handle.cpp
@@ -365,8 +368,10 @@
EXTRA_DIST = \
latency/allocator.hpp \
performance/handler_allocator.hpp \
+ unit/archetypes/async_ops.hpp \
unit/archetypes/async_result.hpp \
unit/archetypes/deprecated_async_result.hpp \
+ unit/archetypes/deprecated_async_ops.hpp \
unit/archetypes/gettable_socket_option.hpp \
unit/archetypes/io_control_command.hpp \
unit/archetypes/settable_socket_option.hpp
diff --git a/asio/src/tests/unit/.gitignore b/asio/src/tests/unit/.gitignore
index e0e342a..0b5bf5f 100644
--- a/asio/src/tests/unit/.gitignore
+++ b/asio/src/tests/unit/.gitignore
@@ -54,6 +54,7 @@
system_timer
thread
time_traits
+use_future
wait_traits
waitable_timer_service
write
diff --git a/asio/src/tests/unit/archetypes/async_ops.hpp b/asio/src/tests/unit/archetypes/async_ops.hpp
new file mode 100644
index 0000000..5e5cdec
--- /dev/null
+++ b/asio/src/tests/unit/archetypes/async_ops.hpp
@@ -0,0 +1,415 @@
+//
+// async_ops.hpp
+// ~~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2015 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 ARCHETYPES_ASYNC_OPS_HPP
+#define ARCHETYPES_ASYNC_OPS_HPP
+
+#include <asio/associated_allocator.hpp>
+#include <asio/associated_executor.hpp>
+#include <asio/async_result.hpp>
+#include <asio/error.hpp>
+
+#if defined(ASIO_HAS_BOOST_BIND)
+# include <boost/bind.hpp>
+#else // defined(ASIO_HAS_BOOST_BIND)
+# include <functional>
+#endif // defined(ASIO_HAS_BOOST_BIND)
+
+namespace archetypes {
+
+#if defined(ASIO_HAS_BOOST_BIND)
+namespace bindns = boost;
+#else // defined(ASIO_HAS_BOOST_BIND)
+namespace bindns = std;
+#endif // defined(ASIO_HAS_BOOST_BIND)
+
+template <typename CompletionToken>
+ASIO_INITFN_RESULT_TYPE(CompletionToken, void())
+async_op_0(ASIO_MOVE_ARG(CompletionToken) token)
+{
+ typedef typename asio::async_completion<CompletionToken,
+ void()>::completion_handler_type handler_type;
+
+ asio::async_completion<CompletionToken,
+ void()> completion(token);
+
+ typename asio::associated_allocator<handler_type>::type a
+ = asio::get_associated_allocator(completion.completion_handler);
+
+ typename asio::associated_executor<handler_type>::type ex
+ = asio::get_associated_executor(completion.completion_handler);
+
+ ex.post(ASIO_MOVE_CAST(handler_type)(completion.completion_handler), a);
+
+ return completion.result.get();
+}
+
+template <typename CompletionToken>
+ASIO_INITFN_RESULT_TYPE(CompletionToken, void(asio::error_code))
+async_op_ec_0(bool ok, ASIO_MOVE_ARG(CompletionToken) token)
+{
+ typedef typename asio::async_completion<CompletionToken,
+ void(asio::error_code)>::completion_handler_type handler_type;
+
+ asio::async_completion<CompletionToken,
+ void(asio::error_code)> completion(token);
+
+ typename asio::associated_allocator<handler_type>::type a
+ = asio::get_associated_allocator(completion.completion_handler);
+
+ typename asio::associated_executor<handler_type>::type ex
+ = asio::get_associated_executor(completion.completion_handler);
+
+ if (ok)
+ {
+ ex.post(
+ bindns::bind(
+ ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
+ asio::error_code()), a);
+ }
+ else
+ {
+ ex.post(
+ bindns::bind(
+ ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
+ asio::error_code(asio::error::operation_aborted)), a);
+ }
+
+ return completion.result.get();
+}
+
+template <typename CompletionToken>
+ASIO_INITFN_RESULT_TYPE(CompletionToken, void(std::exception_ptr))
+async_op_ex_0(bool ok, ASIO_MOVE_ARG(CompletionToken) token)
+{
+ typedef typename asio::async_completion<CompletionToken,
+ void(std::exception_ptr)>::completion_handler_type handler_type;
+
+ asio::async_completion<CompletionToken,
+ void(std::exception_ptr)> completion(token);
+
+ typename asio::associated_allocator<handler_type>::type a
+ = asio::get_associated_allocator(completion.completion_handler);
+
+ typename asio::associated_executor<handler_type>::type ex
+ = asio::get_associated_executor(completion.completion_handler);
+
+ if (ok)
+ {
+ ex.post(
+ bindns::bind(
+ ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
+ std::exception_ptr()), a);
+ }
+ else
+ {
+ ex.post(
+ bindns::bind(
+ ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
+ std::make_exception_ptr(std::runtime_error("blah"))), a);
+ }
+
+ return completion.result.get();
+}
+
+template <typename CompletionToken>
+ASIO_INITFN_RESULT_TYPE(CompletionToken, void(int))
+async_op_1(ASIO_MOVE_ARG(CompletionToken) token)
+{
+ typedef typename asio::async_completion<CompletionToken,
+ void(int)>::completion_handler_type handler_type;
+
+ asio::async_completion<CompletionToken,
+ void(int)> completion(token);
+
+ typename asio::associated_allocator<handler_type>::type a
+ = asio::get_associated_allocator(completion.completion_handler);
+
+ typename asio::associated_executor<handler_type>::type ex
+ = asio::get_associated_executor(completion.completion_handler);
+
+ ex.post(
+ bindns::bind(
+ ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
+ 42), a);
+
+ return completion.result.get();
+}
+
+template <typename CompletionToken>
+ASIO_INITFN_RESULT_TYPE(CompletionToken,
+ void(asio::error_code, int))
+async_op_ec_1(bool ok, ASIO_MOVE_ARG(CompletionToken) token)
+{
+ typedef typename asio::async_completion<CompletionToken,
+ void(asio::error_code, int)>::completion_handler_type
+ handler_type;
+
+ asio::async_completion<CompletionToken,
+ void(asio::error_code, int)> completion(token);
+
+ typename asio::associated_allocator<handler_type>::type a
+ = asio::get_associated_allocator(completion.completion_handler);
+
+ typename asio::associated_executor<handler_type>::type ex
+ = asio::get_associated_executor(completion.completion_handler);
+
+ if (ok)
+ {
+ ex.post(
+ bindns::bind(
+ ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
+ asio::error_code(), 42), a);
+ }
+ else
+ {
+ ex.post(
+ bindns::bind(
+ ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
+ asio::error_code(asio::error::operation_aborted),
+ 0), a);
+ }
+
+ return completion.result.get();
+}
+
+template <typename CompletionToken>
+ASIO_INITFN_RESULT_TYPE(CompletionToken, void(std::exception_ptr, int))
+async_op_ex_1(bool ok, ASIO_MOVE_ARG(CompletionToken) token)
+{
+ typedef typename asio::async_completion<CompletionToken,
+ void(std::exception_ptr, int)>::completion_handler_type
+ handler_type;
+
+ asio::async_completion<CompletionToken,
+ void(std::exception_ptr, int)> completion(token);
+
+ typename asio::associated_allocator<handler_type>::type a
+ = asio::get_associated_allocator(completion.completion_handler);
+
+ typename asio::associated_executor<handler_type>::type ex
+ = asio::get_associated_executor(completion.completion_handler);
+
+ if (ok)
+ {
+ ex.post(
+ bindns::bind(
+ ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
+ std::exception_ptr(), 42), a);
+ }
+ else
+ {
+ ex.post(
+ bindns::bind(
+ ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
+ std::make_exception_ptr(std::runtime_error("blah")), 0), a);
+ }
+
+ return completion.result.get();
+}
+
+template <typename CompletionToken>
+ASIO_INITFN_RESULT_TYPE(CompletionToken, void(int, double))
+async_op_2(ASIO_MOVE_ARG(CompletionToken) token)
+{
+ typedef typename asio::async_completion<CompletionToken,
+ void(int, double)>::completion_handler_type handler_type;
+
+ asio::async_completion<CompletionToken,
+ void(int, double)> completion(token);
+
+ typename asio::associated_allocator<handler_type>::type a
+ = asio::get_associated_allocator(completion.completion_handler);
+
+ typename asio::associated_executor<handler_type>::type ex
+ = asio::get_associated_executor(completion.completion_handler);
+
+ ex.post(
+ bindns::bind(
+ ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
+ 42, 2.0), a);
+
+ return completion.result.get();
+}
+
+template <typename CompletionToken>
+ASIO_INITFN_RESULT_TYPE(CompletionToken,
+ void(asio::error_code, int, double))
+async_op_ec_2(bool ok, ASIO_MOVE_ARG(CompletionToken) token)
+{
+ typedef typename asio::async_completion<CompletionToken,
+ void(asio::error_code, int, double)>::completion_handler_type
+ handler_type;
+
+ asio::async_completion<CompletionToken,
+ void(asio::error_code, int, double)> completion(token);
+
+ typename asio::associated_allocator<handler_type>::type a
+ = asio::get_associated_allocator(completion.completion_handler);
+
+ typename asio::associated_executor<handler_type>::type ex
+ = asio::get_associated_executor(completion.completion_handler);
+
+ if (ok)
+ {
+ ex.post(
+ bindns::bind(
+ ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
+ asio::error_code(), 42, 2.0), a);
+ }
+ else
+ {
+ ex.post(
+ bindns::bind(
+ ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
+ asio::error_code(asio::error::operation_aborted),
+ 0, 0.0), a);
+ }
+
+ return completion.result.get();
+}
+
+template <typename CompletionToken>
+ASIO_INITFN_RESULT_TYPE(CompletionToken,
+ void(std::exception_ptr, int, double))
+async_op_ex_2(bool ok, ASIO_MOVE_ARG(CompletionToken) token)
+{
+ typedef typename asio::async_completion<CompletionToken,
+ void(std::exception_ptr, int, double)>::completion_handler_type
+ handler_type;
+
+ asio::async_completion<CompletionToken,
+ void(std::exception_ptr, int, double)> completion(token);
+
+ typename asio::associated_allocator<handler_type>::type a
+ = asio::get_associated_allocator(completion.completion_handler);
+
+ typename asio::associated_executor<handler_type>::type ex
+ = asio::get_associated_executor(completion.completion_handler);
+
+ if (ok)
+ {
+ ex.post(
+ bindns::bind(
+ ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
+ std::exception_ptr(), 42, 2.0), a);
+ }
+ else
+ {
+ ex.post(
+ bindns::bind(
+ ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
+ std::make_exception_ptr(std::runtime_error("blah")), 0, 0.0), a);
+ }
+
+ return completion.result.get();
+}
+
+template <typename CompletionToken>
+ASIO_INITFN_RESULT_TYPE(CompletionToken, void(int, double, char))
+async_op_3(ASIO_MOVE_ARG(CompletionToken) token)
+{
+ typedef typename asio::async_completion<CompletionToken,
+ void(int, double, char)>::completion_handler_type handler_type;
+
+ asio::async_completion<CompletionToken,
+ void(int, double, char)> completion(token);
+
+ typename asio::associated_allocator<handler_type>::type a
+ = asio::get_associated_allocator(completion.completion_handler);
+
+ typename asio::associated_executor<handler_type>::type ex
+ = asio::get_associated_executor(completion.completion_handler);
+
+ ex.post(
+ bindns::bind(
+ ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
+ 42, 2.0, 'a'), a);
+
+ return completion.result.get();
+}
+
+template <typename CompletionToken>
+ASIO_INITFN_RESULT_TYPE(CompletionToken,
+ void(asio::error_code, int, double, char))
+async_op_ec_3(bool ok, ASIO_MOVE_ARG(CompletionToken) token)
+{
+ typedef typename asio::async_completion<CompletionToken,
+ void(asio::error_code, int, double, char)>::completion_handler_type
+ handler_type;
+
+ asio::async_completion<CompletionToken,
+ void(asio::error_code, int, double, char)> completion(token);
+
+ typename asio::associated_allocator<handler_type>::type a
+ = asio::get_associated_allocator(completion.completion_handler);
+
+ typename asio::associated_executor<handler_type>::type ex
+ = asio::get_associated_executor(completion.completion_handler);
+
+ if (ok)
+ {
+ ex.post(
+ bindns::bind(
+ ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
+ asio::error_code(), 42, 2.0, 'a'), a);
+ }
+ else
+ {
+ ex.post(
+ bindns::bind(
+ ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
+ asio::error_code(asio::error::operation_aborted),
+ 0, 0.0, 'z'), a);
+ }
+
+ return completion.result.get();
+}
+
+template <typename CompletionToken>
+ASIO_INITFN_RESULT_TYPE(CompletionToken,
+ void(std::exception_ptr, int, double, char))
+async_op_ex_3(bool ok, ASIO_MOVE_ARG(CompletionToken) token)
+{
+ typedef typename asio::async_completion<CompletionToken,
+ void(std::exception_ptr, int, double, char)>::completion_handler_type
+ handler_type;
+
+ asio::async_completion<CompletionToken,
+ void(std::exception_ptr, int, double, char)> completion(token);
+
+ typename asio::associated_allocator<handler_type>::type a
+ = asio::get_associated_allocator(completion.completion_handler);
+
+ typename asio::associated_executor<handler_type>::type ex
+ = asio::get_associated_executor(completion.completion_handler);
+
+ if (ok)
+ {
+ ex.post(
+ bindns::bind(
+ ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
+ std::exception_ptr(), 42, 2.0, 'a'), a);
+ }
+ else
+ {
+ ex.post(
+ bindns::bind(
+ ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
+ std::make_exception_ptr(std::runtime_error("blah")),
+ 0, 0.0, 'z'), a);
+ }
+
+ return completion.result.get();
+}
+
+} // namespace archetypes
+
+#endif // ARCHETYPES_ASYNC_OPS_HPP
diff --git a/asio/src/tests/unit/archetypes/deprecated_async_ops.hpp b/asio/src/tests/unit/archetypes/deprecated_async_ops.hpp
new file mode 100644
index 0000000..dd224a7
--- /dev/null
+++ b/asio/src/tests/unit/archetypes/deprecated_async_ops.hpp
@@ -0,0 +1,345 @@
+//
+// deprecated_async_ops.hpp
+// ~~~~~~~~~~~~~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2015 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 ARCHETYPES_DEPRECATED_ASYNC_OPS_HPP
+#define ARCHETYPES_DEPRECATED_ASYNC_OPS_HPP
+
+#include <asio/async_result.hpp>
+
+#if !defined(ASIO_NO_DEPRECATED)
+
+#include <asio/handler_type.hpp>
+#include <asio/error.hpp>
+#include <asio/io_context.hpp>
+
+#if defined(ASIO_HAS_BOOST_BIND)
+# include <boost/bind.hpp>
+#else // defined(ASIO_HAS_BOOST_BIND)
+# include <functional>
+#endif // defined(ASIO_HAS_BOOST_BIND)
+
+namespace archetypes {
+
+#if defined(ASIO_HAS_BOOST_BIND)
+namespace bindns = boost;
+#else // defined(ASIO_HAS_BOOST_BIND)
+namespace bindns = std;
+#endif // defined(ASIO_HAS_BOOST_BIND)
+
+template <typename CompletionToken>
+typename asio::async_result<
+ typename asio::handler_type<CompletionToken,
+ void()>::type>::type
+deprecated_async_op_0(asio::io_context& ctx,
+ ASIO_MOVE_ARG(CompletionToken) token)
+{
+ typedef typename asio::handler_type<CompletionToken,
+ void()>::type handler_type;
+
+ handler_type handler(ASIO_MOVE_CAST(CompletionToken)(token));
+
+ asio::async_result<handler_type> result(handler);
+
+ ctx.post(bindns::bind(ASIO_MOVE_CAST(handler_type)(handler)));
+
+ return result.get();
+}
+
+template <typename CompletionToken>
+typename asio::async_result<
+ typename asio::handler_type<CompletionToken,
+ void(asio::error_code)>::type>::type
+deprecated_async_op_ec_0(asio::io_context& ctx,
+ bool ok, ASIO_MOVE_ARG(CompletionToken) token)
+{
+ typedef typename asio::handler_type<CompletionToken,
+ void(asio::error_code)>::type handler_type;
+
+ handler_type handler(ASIO_MOVE_CAST(CompletionToken)(token));
+
+ asio::async_result<handler_type> result(handler);
+
+ if (ok)
+ {
+ ctx.post(bindns::bind(ASIO_MOVE_CAST(handler_type)(handler),
+ asio::error_code()));
+ }
+ else
+ {
+ ctx.post(bindns::bind(ASIO_MOVE_CAST(handler_type)(handler),
+ asio::error_code(asio::error::operation_aborted)));
+ }
+
+ return result.get();
+}
+
+template <typename CompletionToken>
+typename asio::async_result<
+ typename asio::handler_type<CompletionToken,
+ void(std::exception_ptr)>::type>::type
+deprecated_async_op_ex_0(asio::io_context& ctx,
+ bool ok, ASIO_MOVE_ARG(CompletionToken) token)
+{
+ typedef typename asio::handler_type<CompletionToken,
+ void(std::exception_ptr)>::type handler_type;
+
+ handler_type handler(ASIO_MOVE_CAST(CompletionToken)(token));
+
+ asio::async_result<handler_type> result(handler);
+
+ if (ok)
+ {
+ ctx.post(bindns::bind(ASIO_MOVE_CAST(handler_type)(handler),
+ std::exception_ptr()));
+ }
+ else
+ {
+ ctx.post(bindns::bind(ASIO_MOVE_CAST(handler_type)(handler),
+ std::make_exception_ptr(std::runtime_error("blah"))));
+ }
+
+ return result.get();
+}
+
+template <typename CompletionToken>
+typename asio::async_result<
+ typename asio::handler_type<CompletionToken,
+ void(int)>::type>::type
+deprecated_async_op_1(asio::io_context& ctx,
+ ASIO_MOVE_ARG(CompletionToken) token)
+{
+ typedef typename asio::handler_type<CompletionToken,
+ void(int)>::type handler_type;
+
+ handler_type handler(ASIO_MOVE_CAST(CompletionToken)(token));
+
+ asio::async_result<handler_type> result(handler);
+
+ ctx.post(bindns::bind(ASIO_MOVE_CAST(handler_type)(handler), 42));
+
+ return result.get();
+}
+
+template <typename CompletionToken>
+typename asio::async_result<
+ typename asio::handler_type<CompletionToken,
+ void(asio::error_code, int)>::type>::type
+deprecated_async_op_ec_1(asio::io_context& ctx,
+ bool ok, ASIO_MOVE_ARG(CompletionToken) token)
+{
+ typedef typename asio::handler_type<CompletionToken,
+ void(asio::error_code, int)>::type handler_type;
+
+ handler_type handler(ASIO_MOVE_CAST(CompletionToken)(token));
+
+ asio::async_result<handler_type> result(handler);
+
+ if (ok)
+ {
+ ctx.post(bindns::bind(ASIO_MOVE_CAST(handler_type)(handler),
+ asio::error_code(), 42));
+ }
+ else
+ {
+ ctx.post(bindns::bind(ASIO_MOVE_CAST(handler_type)(handler),
+ asio::error_code(asio::error::operation_aborted), 0));
+ }
+
+ return result.get();
+}
+
+template <typename CompletionToken>
+typename asio::async_result<
+ typename asio::handler_type<CompletionToken,
+ void(std::exception_ptr, int)>::type>::type
+deprecated_async_op_ex_1(asio::io_context& ctx,
+ bool ok, ASIO_MOVE_ARG(CompletionToken) token)
+{
+ typedef typename asio::handler_type<CompletionToken,
+ void(std::exception_ptr, int)>::type handler_type;
+
+ handler_type handler(ASIO_MOVE_CAST(CompletionToken)(token));
+
+ asio::async_result<handler_type> result(handler);
+
+ if (ok)
+ {
+ ctx.post(bindns::bind(ASIO_MOVE_CAST(handler_type)(handler),
+ std::exception_ptr(), 42));
+ }
+ else
+ {
+ ctx.post(bindns::bind(ASIO_MOVE_CAST(handler_type)(handler),
+ std::make_exception_ptr(std::runtime_error("blah")), 0));
+ }
+
+ return result.get();
+}
+
+template <typename CompletionToken>
+typename asio::async_result<
+ typename asio::handler_type<CompletionToken,
+ void(int, double)>::type>::type
+deprecated_async_op_2(asio::io_context& ctx,
+ ASIO_MOVE_ARG(CompletionToken) token)
+{
+ typedef typename asio::handler_type<CompletionToken,
+ void(int, double)>::type handler_type;
+
+ handler_type handler(ASIO_MOVE_CAST(CompletionToken)(token));
+
+ asio::async_result<handler_type> result(handler);
+
+ ctx.post(bindns::bind(ASIO_MOVE_CAST(handler_type)(handler),
+ 42, 2.0));
+
+ return result.get();
+}
+
+template <typename CompletionToken>
+typename asio::async_result<
+ typename asio::handler_type<CompletionToken,
+ void(asio::error_code, int, double)>::type>::type
+deprecated_async_op_ec_2(asio::io_context& ctx,
+ bool ok, ASIO_MOVE_ARG(CompletionToken) token)
+{
+ typedef typename asio::handler_type<CompletionToken,
+ void(asio::error_code, int, double)>::type handler_type;
+
+ handler_type handler(ASIO_MOVE_CAST(CompletionToken)(token));
+
+ asio::async_result<handler_type> result(handler);
+
+ if (ok)
+ {
+ ctx.post(bindns::bind(ASIO_MOVE_CAST(handler_type)(handler),
+ asio::error_code(), 42, 2.0));
+ }
+ else
+ {
+ ctx.post(bindns::bind(ASIO_MOVE_CAST(handler_type)(handler),
+ asio::error_code(asio::error::operation_aborted),
+ 0, 0.0));
+ }
+
+ return result.get();
+}
+
+template <typename CompletionToken>
+typename asio::async_result<
+ typename asio::handler_type<CompletionToken,
+ void(std::exception_ptr, int, double)>::type>::type
+deprecated_async_op_ex_2(asio::io_context& ctx,
+ bool ok, ASIO_MOVE_ARG(CompletionToken) token)
+{
+ typedef typename asio::handler_type<CompletionToken,
+ void(std::exception_ptr, int, double)>::type handler_type;
+
+ handler_type handler(ASIO_MOVE_CAST(CompletionToken)(token));
+
+ asio::async_result<handler_type> result(handler);
+
+ if (ok)
+ {
+ ctx.post(bindns::bind(ASIO_MOVE_CAST(handler_type)(handler),
+ std::exception_ptr(), 42, 2.0));
+ }
+ else
+ {
+ ctx.post(bindns::bind(ASIO_MOVE_CAST(handler_type)(handler),
+ std::make_exception_ptr(std::runtime_error("blah")), 0, 0.0));
+ }
+
+ return result.get();
+}
+
+template <typename CompletionToken>
+typename asio::async_result<
+ typename asio::handler_type<CompletionToken,
+ void(int, double, char)>::type>::type
+deprecated_async_op_3(asio::io_context& ctx,
+ ASIO_MOVE_ARG(CompletionToken) token)
+{
+ typedef typename asio::handler_type<CompletionToken,
+ void(int, double, char)>::type handler_type;
+
+ handler_type handler(ASIO_MOVE_CAST(CompletionToken)(token));
+
+ asio::async_result<handler_type> result(handler);
+
+ ctx.post(bindns::bind(ASIO_MOVE_CAST(handler_type)(handler),
+ 42, 2.0, 'a'));
+
+ return result.get();
+}
+
+template <typename CompletionToken>
+typename asio::async_result<
+ typename asio::handler_type<CompletionToken,
+ void(asio::error_code, int, double, char)>::type>::type
+deprecated_async_op_ec_3(asio::io_context& ctx,
+ bool ok, ASIO_MOVE_ARG(CompletionToken) token)
+{
+ typedef typename asio::handler_type<CompletionToken,
+ void(asio::error_code, int, double, char)>::type handler_type;
+
+ handler_type handler(ASIO_MOVE_CAST(CompletionToken)(token));
+
+ asio::async_result<handler_type> result(handler);
+
+ if (ok)
+ {
+ ctx.post(bindns::bind(ASIO_MOVE_CAST(handler_type)(handler),
+ asio::error_code(), 42, 2.0, 'a'));
+ }
+ else
+ {
+ ctx.post(bindns::bind(ASIO_MOVE_CAST(handler_type)(handler),
+ asio::error_code(asio::error::operation_aborted),
+ 0, 0.0, 'z'));
+ }
+
+ return result.get();
+}
+
+template <typename CompletionToken>
+typename asio::async_result<
+ typename asio::handler_type<CompletionToken,
+ void(std::exception_ptr, int, double, char)>::type>::type
+deprecated_async_op_ex_3(asio::io_context& ctx,
+ bool ok, ASIO_MOVE_ARG(CompletionToken) token)
+{
+ typedef typename asio::handler_type<CompletionToken,
+ void(std::exception_ptr, int, double, char)>::type handler_type;
+
+ handler_type handler(ASIO_MOVE_CAST(CompletionToken)(token));
+
+ asio::async_result<handler_type> result(handler);
+
+ if (ok)
+ {
+ ctx.post(bindns::bind(ASIO_MOVE_CAST(handler_type)(handler),
+ std::exception_ptr(), 42, 2.0, 'a'));
+ }
+ else
+ {
+ ctx.post(bindns::bind(ASIO_MOVE_CAST(handler_type)(handler),
+ std::make_exception_ptr(std::runtime_error("blah")),
+ 0, 0.0, 'z'));
+ }
+
+ return result.get();
+}
+
+} // namespace archetypes
+
+#endif // !defined(ASIO_NO_DEPRECATED)
+
+#endif // ARCHETYPES_DEPRECATED_ASYNC_OPS_HPP
diff --git a/asio/src/tests/unit/use_future.cpp b/asio/src/tests/unit/use_future.cpp
new file mode 100644
index 0000000..354e37d
--- /dev/null
+++ b/asio/src/tests/unit/use_future.cpp
@@ -0,0 +1,1346 @@
+//
+// use_future.cpp
+// ~~~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2015 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)
+//
+
+// Disable autolinking for unit tests.
+#if !defined(BOOST_ALL_NO_LIB)
+#define BOOST_ALL_NO_LIB 1
+#endif // !defined(BOOST_ALL_NO_LIB)
+
+// Test that header file is self-contained.
+#include "asio/use_future.hpp"
+
+#include <string>
+#include "unit_test.hpp"
+
+#if defined(ASIO_HAS_STD_FUTURE)
+
+#include "archetypes/async_ops.hpp"
+#include "archetypes/deprecated_async_ops.hpp"
+
+void use_future_0_test()
+{
+ using asio::use_future;
+ using namespace archetypes;
+
+ std::future<void> f;
+
+ f = async_op_0(use_future);
+ try
+ {
+ f.get();
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ec_0(true, use_future);
+ try
+ {
+ f.get();
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ec_0(false, use_future);
+ try
+ {
+ f.get();
+ ASIO_CHECK(false);
+ }
+ catch (asio::system_error& e)
+ {
+ ASIO_CHECK(e.code() == asio::error::operation_aborted);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ex_0(true, use_future);
+ try
+ {
+ f.get();
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ex_0(false, use_future);
+ try
+ {
+ f.get();
+ ASIO_CHECK(false);
+ }
+ catch (std::exception& e)
+ {
+ ASIO_CHECK(e.what() == std::string("blah"));
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+}
+
+void use_future_1_test()
+{
+ using asio::use_future;
+ using namespace archetypes;
+
+ std::future<int> f;
+
+ f = async_op_1(use_future);
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ec_1(true, use_future);
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ec_1(false, use_future);
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(false);
+ (void)i;
+ }
+ catch (asio::system_error& e)
+ {
+ ASIO_CHECK(e.code() == asio::error::operation_aborted);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ex_1(true, use_future);
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ex_1(false, use_future);
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(false);
+ (void)i;
+ }
+ catch (std::exception& e)
+ {
+ ASIO_CHECK(e.what() == std::string("blah"));
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+}
+
+void use_future_2_test()
+{
+ using asio::use_future;
+ using namespace archetypes;
+
+ std::future<std::tuple<int, double>> f;
+
+ f = async_op_2(use_future);
+ try
+ {
+ int i;
+ double d;
+ std::tie(i, d) = f.get();
+ ASIO_CHECK(i == 42);
+ ASIO_CHECK(d == 2.0);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ec_2(true, use_future);
+ try
+ {
+ int i;
+ double d;
+ std::tie(i, d) = f.get();
+ ASIO_CHECK(i == 42);
+ ASIO_CHECK(d == 2.0);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ec_2(false, use_future);
+ try
+ {
+ std::tuple<int, double> t = f.get();
+ ASIO_CHECK(false);
+ (void)t;
+ }
+ catch (asio::system_error& e)
+ {
+ ASIO_CHECK(e.code() == asio::error::operation_aborted);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ex_2(true, use_future);
+ try
+ {
+ int i;
+ double d;
+ std::tie(i, d) = f.get();
+ ASIO_CHECK(i == 42);
+ ASIO_CHECK(d == 2.0);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ex_2(false, use_future);
+ try
+ {
+ std::tuple<int, double> t = f.get();
+ ASIO_CHECK(false);
+ (void)t;
+ }
+ catch (std::exception& e)
+ {
+ ASIO_CHECK(e.what() == std::string("blah"));
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+}
+
+void use_future_3_test()
+{
+ using asio::use_future;
+ using namespace archetypes;
+
+ std::future<std::tuple<int, double, char>> f;
+
+ f = async_op_3(use_future);
+ try
+ {
+ int i;
+ double d;
+ char c;
+ std::tie(i, d, c) = f.get();
+ ASIO_CHECK(i == 42);
+ ASIO_CHECK(d == 2.0);
+ ASIO_CHECK(c == 'a');
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ec_3(true, use_future);
+ try
+ {
+ int i;
+ double d;
+ char c;
+ std::tie(i, d, c) = f.get();
+ ASIO_CHECK(i == 42);
+ ASIO_CHECK(d == 2.0);
+ ASIO_CHECK(c == 'a');
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ec_3(false, use_future);
+ try
+ {
+ std::tuple<int, double, char> t = f.get();
+ ASIO_CHECK(false);
+ (void)t;
+ }
+ catch (asio::system_error& e)
+ {
+ ASIO_CHECK(e.code() == asio::error::operation_aborted);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ex_3(true, use_future);
+ try
+ {
+ int i;
+ double d;
+ char c;
+ std::tie(i, d, c) = f.get();
+ ASIO_CHECK(i == 42);
+ ASIO_CHECK(d == 2.0);
+ ASIO_CHECK(c == 'a');
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ex_3(false, use_future);
+ try
+ {
+ std::tuple<int, double, char> t = f.get();
+ ASIO_CHECK(false);
+ (void)t;
+ }
+ catch (std::exception& e)
+ {
+ ASIO_CHECK(e.what() == std::string("blah"));
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+}
+
+int package_0()
+{
+ return 42;
+}
+
+int package_ec_0(asio::error_code ec)
+{
+ return ec ? 0 : 42;
+}
+
+int package_ex_0(std::exception_ptr ex)
+{
+ return ex ? 0 : 42;
+}
+
+void use_future_package_0_test()
+{
+ using asio::use_future;
+ using namespace archetypes;
+
+ std::future<int> f;
+
+ f = async_op_0(use_future(package_0));
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ec_0(true, use_future(&package_ec_0));
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ec_0(false, use_future(package_ec_0));
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 0);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ex_0(true, use_future(package_ex_0));
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ex_0(false, use_future(package_ex_0));
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 0);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+}
+
+int package_1(int i)
+{
+ return i;
+}
+
+int package_ec_1(asio::error_code ec, int i)
+{
+ return ec ? 0 : i;
+}
+
+int package_ex_1(std::exception_ptr ex, int i)
+{
+ return ex ? 0 : i;
+}
+
+void use_future_package_1_test()
+{
+ using asio::use_future;
+ using namespace archetypes;
+
+ std::future<int> f;
+
+ f = async_op_1(use_future(package_1));
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ec_1(true, use_future(package_ec_1));
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ec_1(false, use_future(package_ec_1));
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 0);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ex_1(true, use_future(package_ex_1));
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ex_1(false, use_future(package_ex_1));
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 0);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+}
+
+int package_2(int i, double)
+{
+ return i;
+}
+
+int package_ec_2(asio::error_code ec, int i, double)
+{
+ return ec ? 0 : i;
+}
+
+int package_ex_2(std::exception_ptr ex, int i, double)
+{
+ return ex ? 0 : i;
+}
+
+void use_future_package_2_test()
+{
+ using asio::use_future;
+ using namespace archetypes;
+
+ std::future<int> f;
+
+ f = async_op_2(use_future(package_2));
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ec_2(true, use_future(package_ec_2));
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ec_2(false, use_future(package_ec_2));
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 0);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ex_2(true, use_future(package_ex_2));
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ex_2(false, use_future(package_ex_2));
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 0);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+}
+
+int package_3(int i, double, char)
+{
+ return i;
+}
+
+int package_ec_3(asio::error_code ec, int i, double, char)
+{
+ return ec ? 0 : i;
+}
+
+int package_ex_3(std::exception_ptr ex, int i, double, char)
+{
+ return ex ? 0 : i;
+}
+
+void use_future_package_3_test()
+{
+ using asio::use_future;
+ using namespace archetypes;
+
+ std::future<int> f;
+
+ f = async_op_3(use_future(package_3));
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ec_3(true, use_future(package_ec_3));
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ec_3(false, use_future(package_ec_3));
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 0);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ex_3(true, use_future(package_ex_3));
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ex_3(false, use_future(package_ex_3));
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 0);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+}
+
+void deprecated_use_future_0_test()
+{
+#if !defined(ASIO_NO_DEPRECATED)
+ using asio::use_future;
+ using namespace archetypes;
+
+ std::future<void> f;
+ asio::io_context ctx;
+
+ f = deprecated_async_op_0(ctx, use_future);
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ f.get();
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ec_0(ctx, true, use_future);
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ f.get();
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ec_0(ctx, false, use_future);
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ f.get();
+ ASIO_CHECK(false);
+ }
+ catch (asio::system_error& e)
+ {
+ ASIO_CHECK(e.code() == asio::error::operation_aborted);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ex_0(true, use_future);
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ f.get();
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = async_op_ex_0(false, use_future);
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ f.get();
+ ASIO_CHECK(false);
+ }
+ catch (std::exception& e)
+ {
+ ASIO_CHECK(e.what() == std::string("blah"));
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+#endif // !defined(ASIO_NO_DEPRECATED)
+}
+
+void deprecated_use_future_1_test()
+{
+#if !defined(ASIO_NO_DEPRECATED)
+ using asio::use_future;
+ using namespace archetypes;
+
+ std::future<int> f;
+ asio::io_context ctx;
+
+ f = deprecated_async_op_1(ctx, use_future);
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ec_1(ctx, true, use_future);
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ec_1(ctx, false, use_future);
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(false);
+ (void)i;
+ }
+ catch (asio::system_error& e)
+ {
+ ASIO_CHECK(e.code() == asio::error::operation_aborted);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ex_1(ctx, true, use_future);
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ex_1(ctx, false, use_future);
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(false);
+ (void)i;
+ }
+ catch (std::exception& e)
+ {
+ ASIO_CHECK(e.what() == std::string("blah"));
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+#endif // !defined(ASIO_NO_DEPRECATED)
+}
+
+void deprecated_use_future_2_test()
+{
+#if !defined(ASIO_NO_DEPRECATED)
+ using asio::use_future;
+ using namespace archetypes;
+
+ std::future<std::tuple<int, double>> f;
+ asio::io_context ctx;
+
+ f = deprecated_async_op_2(ctx, use_future);
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i;
+ double d;
+ std::tie(i, d) = f.get();
+ ASIO_CHECK(i == 42);
+ ASIO_CHECK(d == 2.0);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ec_2(ctx, true, use_future);
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i;
+ double d;
+ std::tie(i, d) = f.get();
+ ASIO_CHECK(i == 42);
+ ASIO_CHECK(d == 2.0);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ec_2(ctx, false, use_future);
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ std::tuple<int, double> t = f.get();
+ ASIO_CHECK(false);
+ (void)t;
+ }
+ catch (asio::system_error& e)
+ {
+ ASIO_CHECK(e.code() == asio::error::operation_aborted);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ex_2(ctx, true, use_future);
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i;
+ double d;
+ std::tie(i, d) = f.get();
+ ASIO_CHECK(i == 42);
+ ASIO_CHECK(d == 2.0);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ex_2(ctx, false, use_future);
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ std::tuple<int, double> t = f.get();
+ ASIO_CHECK(false);
+ (void)t;
+ }
+ catch (std::exception& e)
+ {
+ ASIO_CHECK(e.what() == std::string("blah"));
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+#endif // !defined(ASIO_NO_DEPRECATED)
+}
+
+void deprecated_use_future_3_test()
+{
+#if !defined(ASIO_NO_DEPRECATED)
+ using asio::use_future;
+ using namespace archetypes;
+
+ std::future<std::tuple<int, double, char>> f;
+ asio::io_context ctx;
+
+ f = deprecated_async_op_3(ctx, use_future);
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i;
+ double d;
+ char c;
+ std::tie(i, d, c) = f.get();
+ ASIO_CHECK(i == 42);
+ ASIO_CHECK(d == 2.0);
+ ASIO_CHECK(c == 'a');
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ec_3(ctx, true, use_future);
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i;
+ double d;
+ char c;
+ std::tie(i, d, c) = f.get();
+ ASIO_CHECK(i == 42);
+ ASIO_CHECK(d == 2.0);
+ ASIO_CHECK(c == 'a');
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ec_3(ctx, false, use_future);
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ std::tuple<int, double, char> t = f.get();
+ ASIO_CHECK(false);
+ (void)t;
+ }
+ catch (asio::system_error& e)
+ {
+ ASIO_CHECK(e.code() == asio::error::operation_aborted);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ex_3(ctx, true, use_future);
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i;
+ double d;
+ char c;
+ std::tie(i, d, c) = f.get();
+ ASIO_CHECK(i == 42);
+ ASIO_CHECK(d == 2.0);
+ ASIO_CHECK(c == 'a');
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ex_3(ctx, false, use_future);
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ std::tuple<int, double, char> t = f.get();
+ ASIO_CHECK(false);
+ (void)t;
+ }
+ catch (std::exception& e)
+ {
+ ASIO_CHECK(e.what() == std::string("blah"));
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+#endif // !defined(ASIO_NO_DEPRECATED)
+}
+
+void deprecated_use_future_package_0_test()
+{
+#if !defined(ASIO_NO_DEPRECATED)
+ using asio::use_future;
+ using namespace archetypes;
+
+ std::future<int> f;
+ asio::io_context ctx;
+
+ f = deprecated_async_op_0(ctx, use_future(package_0));
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ec_0(ctx, true, use_future(&package_ec_0));
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ec_0(ctx, false, use_future(package_ec_0));
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 0);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ex_0(ctx, true, use_future(package_ex_0));
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ex_0(ctx, false, use_future(package_ex_0));
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 0);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+#endif // !defined(ASIO_NO_DEPRECATED)
+}
+
+void deprecated_use_future_package_1_test()
+{
+#if !defined(ASIO_NO_DEPRECATED)
+ using asio::use_future;
+ using namespace archetypes;
+
+ std::future<int> f;
+ asio::io_context ctx;
+
+ f = deprecated_async_op_1(ctx, use_future(package_1));
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ec_1(ctx, true, use_future(package_ec_1));
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ec_1(ctx, false, use_future(package_ec_1));
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 0);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ex_1(ctx, true, use_future(package_ex_1));
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ex_1(ctx, false, use_future(package_ex_1));
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 0);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+#endif // !defined(ASIO_NO_DEPRECATED)
+}
+
+void deprecated_use_future_package_2_test()
+{
+#if !defined(ASIO_NO_DEPRECATED)
+ using asio::use_future;
+ using namespace archetypes;
+
+ std::future<int> f;
+ asio::io_context ctx;
+
+ f = deprecated_async_op_2(ctx, use_future(package_2));
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ec_2(ctx, true, use_future(package_ec_2));
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ec_2(ctx, false, use_future(package_ec_2));
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 0);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ex_2(ctx, true, use_future(package_ex_2));
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ex_2(ctx, false, use_future(package_ex_2));
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 0);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+#endif // !defined(ASIO_NO_DEPRECATED)
+}
+
+void deprecated_use_future_package_3_test()
+{
+#if !defined(ASIO_NO_DEPRECATED)
+ using asio::use_future;
+ using namespace archetypes;
+
+ std::future<int> f;
+ asio::io_context ctx;
+
+ f = deprecated_async_op_3(ctx, use_future(package_3));
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ec_3(ctx, true, use_future(package_ec_3));
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ec_3(ctx, false, use_future(package_ec_3));
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 0);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ex_3(ctx, true, use_future(package_ex_3));
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 42);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+
+ f = deprecated_async_op_ex_3(ctx, false, use_future(package_ex_3));
+ ctx.restart();
+ ctx.run();
+ try
+ {
+ int i = f.get();
+ ASIO_CHECK(i == 0);
+ }
+ catch (...)
+ {
+ ASIO_CHECK(false);
+ }
+#endif // !defined(ASIO_NO_DEPRECATED)
+}
+
+ASIO_TEST_SUITE
+(
+ "use_future",
+ ASIO_TEST_CASE(use_future_0_test)
+ ASIO_TEST_CASE(use_future_1_test)
+ ASIO_TEST_CASE(use_future_2_test)
+ ASIO_TEST_CASE(use_future_3_test)
+ ASIO_TEST_CASE(use_future_package_0_test)
+ ASIO_TEST_CASE(use_future_package_1_test)
+ ASIO_TEST_CASE(use_future_package_2_test)
+ ASIO_TEST_CASE(use_future_package_3_test)
+ ASIO_TEST_CASE(deprecated_use_future_0_test)
+ ASIO_TEST_CASE(deprecated_use_future_1_test)
+ ASIO_TEST_CASE(deprecated_use_future_2_test)
+ ASIO_TEST_CASE(deprecated_use_future_3_test)
+ ASIO_TEST_CASE(deprecated_use_future_package_0_test)
+ ASIO_TEST_CASE(deprecated_use_future_package_1_test)
+ ASIO_TEST_CASE(deprecated_use_future_package_2_test)
+ ASIO_TEST_CASE(deprecated_use_future_package_3_test)
+)
+
+#else // defined(ASIO_HAS_STD_FUTURE)
+
+ASIO_TEST_SUITE
+(
+ "use_future",
+ ASIO_TEST_CASE(null_test)
+)
+
+#endif // defined(ASIO_HAS_STD_FUTURE)