Add as_single completion token.
diff --git a/asio/include/Makefile.am b/asio/include/Makefile.am
index 474b0f8..fa37daf 100644
--- a/asio/include/Makefile.am
+++ b/asio/include/Makefile.am
@@ -294,6 +294,8 @@
asio/execution_context.hpp \
asio/executor.hpp \
asio/executor_work_guard.hpp \
+ asio/experimental/as_single.hpp \
+ asio/experimental/impl/as_single.hpp \
asio/generic/basic_endpoint.hpp \
asio/generic/datagram_protocol.hpp \
asio/generic/detail/endpoint.hpp \
diff --git a/asio/include/asio/experimental/as_single.hpp b/asio/include/asio/experimental/as_single.hpp
new file mode 100644
index 0000000..5f9b62d
--- /dev/null
+++ b/asio/include/asio/experimental/as_single.hpp
@@ -0,0 +1,124 @@
+//
+// experimental/as_single.hpp
+// ~~~~~~~~~~~~~~~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2020 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_EXPERIMENTAL_AS_SINGLE_HPP
+#define ASIO_EXPERIMENTAL_AS_SINGLE_HPP
+
+#if defined(_MSC_VER) && (_MSC_VER >= 1200)
+# pragma once
+#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
+
+#include "asio/detail/config.hpp"
+#include "asio/detail/type_traits.hpp"
+#include "asio/error_code.hpp"
+
+#include "asio/detail/push_options.hpp"
+
+namespace asio {
+namespace experimental {
+
+/// Completion token type used to specify that an error produced by an
+/// asynchronous operation is captured to an error_code variable.
+/**
+ * The as_single_t class is used to indicate that any error_code produced
+ * by an asynchronous operation is captured to a specified variable.
+ */
+template <typename CompletionToken>
+class as_single_t
+{
+public:
+ /// Default constructor.
+ /**
+ * This constructor is only valid if the underlying completion token is
+ * default constructible.
+ */
+ ASIO_CONSTEXPR as_single_t()
+ : token_()
+ {
+ }
+
+ /// Constructor.
+ template <typename T>
+ ASIO_CONSTEXPR explicit as_single_t(
+ ASIO_MOVE_ARG(T) completion_token)
+ : token_(ASIO_MOVE_CAST(T)(completion_token))
+ {
+ }
+
+ /// Adapts an executor to add the @c as_single_t completion token as the
+ /// default.
+ template <typename InnerExecutor>
+ struct executor_with_default : InnerExecutor
+ {
+ /// Specify @c as_single_t as the default completion token type.
+ typedef as_single_t default_completion_token_type;
+
+ /// Construct the adapted executor from the inner executor type.
+ executor_with_default(const InnerExecutor& ex) ASIO_NOEXCEPT
+ : InnerExecutor(ex)
+ {
+ }
+
+ /// Convert the specified executor to the inner executor type, then use
+ /// that to construct the adapted executor.
+ template <typename OtherExecutor>
+ executor_with_default(const OtherExecutor& ex,
+ typename enable_if<
+ is_convertible<OtherExecutor, InnerExecutor>::value
+ >::type* = 0) ASIO_NOEXCEPT
+ : InnerExecutor(ex)
+ {
+ }
+ };
+
+ /// Type alias to adapt an I/O object to use @c as_single_t as its
+ /// default completion token type.
+#if defined(ASIO_HAS_ALIAS_TEMPLATES) \
+ || defined(GENERATING_DOCUMENTATION)
+ template <typename T>
+ using as_default_on_t = typename T::template rebind_executor<
+ executor_with_default<typename T::executor_type> >::other;
+#endif // defined(ASIO_HAS_ALIAS_TEMPLATES)
+ // || defined(GENERATING_DOCUMENTATION)
+
+ /// Function helper to adapt an I/O object to use @c as_single_t as its
+ /// default completion token type.
+ template <typename T>
+ static typename decay<T>::type::template rebind_executor<
+ executor_with_default<typename decay<T>::type::executor_type>
+ >::other
+ as_default_on(ASIO_MOVE_ARG(T) object)
+ {
+ return typename decay<T>::type::template rebind_executor<
+ executor_with_default<typename decay<T>::type::executor_type>
+ >::other(ASIO_MOVE_CAST(T)(object));
+ }
+
+//private:
+ CompletionToken token_;
+};
+
+/// Create a completion token to capture error_code values to a variable.
+template <typename CompletionToken>
+inline ASIO_CONSTEXPR as_single_t<typename decay<CompletionToken>::type>
+as_single(ASIO_MOVE_ARG(CompletionToken) completion_token)
+{
+ return as_single_t<typename decay<CompletionToken>::type>(
+ ASIO_MOVE_CAST(CompletionToken)(completion_token));
+}
+
+} // namespace experimental
+} // namespace asio
+
+#include "asio/detail/pop_options.hpp"
+
+#include "asio/experimental/impl/as_single.hpp"
+
+#endif // ASIO_EXPERIMENTAL_AS_SINGLE_HPP
diff --git a/asio/include/asio/experimental/impl/as_single.hpp b/asio/include/asio/experimental/impl/as_single.hpp
new file mode 100644
index 0000000..fdcc596
--- /dev/null
+++ b/asio/include/asio/experimental/impl/as_single.hpp
@@ -0,0 +1,224 @@
+//
+// experimental/impl/as_single.hpp
+// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2020 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_IMPL_EXPERIMENTAL_AS_SINGLE_HPP
+#define ASIO_IMPL_EXPERIMENTAL_AS_SINGLE_HPP
+
+#if defined(_MSC_VER) && (_MSC_VER >= 1200)
+# pragma once
+#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
+
+#include "asio/detail/config.hpp"
+
+#include <tuple>
+
+#include "asio/associated_executor.hpp"
+#include "asio/associated_allocator.hpp"
+#include "asio/async_result.hpp"
+#include "asio/detail/handler_alloc_helpers.hpp"
+#include "asio/detail/handler_cont_helpers.hpp"
+#include "asio/detail/handler_invoke_helpers.hpp"
+#include "asio/detail/type_traits.hpp"
+#include "asio/detail/variadic_templates.hpp"
+
+#include "asio/detail/push_options.hpp"
+
+namespace asio {
+namespace experimental {
+namespace detail {
+
+// Class to adapt a as_single_t as a completion handler.
+template <typename Handler>
+class as_single_handler
+{
+public:
+ typedef void result_type;
+
+ template <typename CompletionToken>
+ as_single_handler(as_single_t<CompletionToken> e)
+ : handler_(ASIO_MOVE_CAST(CompletionToken)(e.token_))
+ {
+ }
+
+ template <typename RedirectedHandler>
+ as_single_handler(ASIO_MOVE_ARG(RedirectedHandler) h)
+ : handler_(ASIO_MOVE_CAST(RedirectedHandler)(h))
+ {
+ }
+
+ void operator()()
+ {
+ handler_();
+ }
+
+ template <typename Arg>
+ void operator()(ASIO_MOVE_ARG(Arg) arg)
+ {
+ handler_(ASIO_MOVE_CAST(Arg)(arg));
+ }
+
+ template <typename... Args>
+ void operator()(ASIO_MOVE_ARG(Args)... args)
+ {
+ handler_(std::make_tuple(ASIO_MOVE_CAST(Args)(args)...));
+ }
+
+//private:
+ Handler handler_;
+};
+
+template <typename Handler>
+inline void* asio_handler_allocate(std::size_t size,
+ as_single_handler<Handler>* this_handler)
+{
+ return asio_handler_alloc_helpers::allocate(
+ size, this_handler->handler_);
+}
+
+template <typename Handler>
+inline void asio_handler_deallocate(void* pointer, std::size_t size,
+ as_single_handler<Handler>* this_handler)
+{
+ asio_handler_alloc_helpers::deallocate(
+ pointer, size, this_handler->handler_);
+}
+
+template <typename Handler>
+inline bool asio_handler_is_continuation(
+ as_single_handler<Handler>* this_handler)
+{
+ return asio_handler_cont_helpers::is_continuation(
+ this_handler->handler_);
+}
+
+template <typename Function, typename Handler>
+inline void asio_handler_invoke(Function& function,
+ as_single_handler<Handler>* this_handler)
+{
+ asio_handler_invoke_helpers::invoke(
+ function, this_handler->handler_);
+}
+
+template <typename Function, typename Handler>
+inline void asio_handler_invoke(const Function& function,
+ as_single_handler<Handler>* this_handler)
+{
+ asio_handler_invoke_helpers::invoke(
+ function, this_handler->handler_);
+}
+
+template <typename Signature>
+struct as_single_signature
+{
+ typedef Signature type;
+};
+
+template <typename R>
+struct as_single_signature<R()>
+{
+ typedef R type();
+};
+
+template <typename R, typename Arg>
+struct as_single_signature<R(Arg)>
+{
+ typedef R type(Arg);
+};
+
+template <typename R, typename... Args>
+struct as_single_signature<R(Args...)>
+{
+ typedef R type(std::tuple<typename decay<Args>::type...>);
+};
+
+} // namespace detail
+} // namespace experimental
+
+#if !defined(GENERATING_DOCUMENTATION)
+
+template <typename CompletionToken, typename Signature>
+struct async_result<experimental::as_single_t<CompletionToken>, Signature>
+{
+ typedef typename async_result<CompletionToken,
+ typename experimental::detail::as_single_signature<Signature>::type>
+ ::return_type return_type;
+
+ template <typename Initiation>
+ struct init_wrapper
+ {
+ init_wrapper(Initiation init)
+ : initiation_(ASIO_MOVE_CAST(Initiation)(init))
+ {
+ }
+
+ template <typename Handler, typename... Args>
+ void operator()(
+ ASIO_MOVE_ARG(Handler) handler,
+ ASIO_MOVE_ARG(Args)... args)
+ {
+ ASIO_MOVE_CAST(Initiation)(initiation_)(
+ experimental::detail::as_single_handler<
+ typename decay<Handler>::type>(
+ ASIO_MOVE_CAST(Handler)(handler)),
+ ASIO_MOVE_CAST(Args)(args)...);
+ }
+
+ Initiation initiation_;
+ };
+
+ template <typename Initiation, typename RawCompletionToken, typename... Args>
+ static return_type initiate(
+ ASIO_MOVE_ARG(Initiation) initiation,
+ ASIO_MOVE_ARG(RawCompletionToken) token,
+ ASIO_MOVE_ARG(Args)... args)
+ {
+ return async_initiate<CompletionToken,
+ typename experimental::detail::as_single_signature<Signature>::type>(
+ init_wrapper<typename decay<Initiation>::type>(
+ ASIO_MOVE_CAST(Initiation)(initiation)),
+ token.token_, ASIO_MOVE_CAST(Args)(args)...);
+ }
+};
+
+template <typename Handler, typename Executor>
+struct associated_executor<
+ experimental::detail::as_single_handler<Handler>, Executor>
+{
+ typedef typename associated_executor<Handler, Executor>::type type;
+
+ static type get(
+ const experimental::detail::as_single_handler<Handler>& h,
+ const Executor& ex = Executor()) ASIO_NOEXCEPT
+ {
+ return associated_executor<Handler, Executor>::get(h.handler_, ex);
+ }
+};
+
+template <typename Handler, typename Allocator>
+struct associated_allocator<
+ experimental::detail::as_single_handler<Handler>, Allocator>
+{
+ typedef typename associated_allocator<Handler, Allocator>::type type;
+
+ static type get(
+ const experimental::detail::as_single_handler<Handler>& h,
+ const Allocator& a = Allocator()) ASIO_NOEXCEPT
+ {
+ return associated_allocator<Handler, Allocator>::get(h.handler_, a);
+ }
+};
+
+#endif // !defined(GENERATING_DOCUMENTATION)
+
+} // namespace asio
+
+#include "asio/detail/pop_options.hpp"
+
+#endif // ASIO_IMPL_EXPERIMENTAL_AS_SINGLE_HPP
diff --git a/asio/src/examples/cpp17/Makefile.am b/asio/src/examples/cpp17/Makefile.am
index 4c8195c..f7ce0af 100644
--- a/asio/src/examples/cpp17/Makefile.am
+++ b/asio/src/examples/cpp17/Makefile.am
@@ -14,6 +14,7 @@
coroutines_ts/chat_server \
coroutines_ts/echo_server \
coroutines_ts/echo_server_with_default \
+ coroutines_ts/echo_server_with_as_single_default \
coroutines_ts/refactored_echo_server
endif
@@ -23,6 +24,7 @@
coroutines_ts_chat_server_SOURCES = coroutines_ts/chat_server.cpp
coroutines_ts_echo_server_SOURCES = coroutines_ts/echo_server.cpp
coroutines_ts_echo_server_with_default_SOURCES = coroutines_ts/echo_server_with_default.cpp
+coroutines_ts_echo_server_with_as_single_default_SOURCES = coroutines_ts/echo_server_with_as_single_default.cpp
coroutines_ts_refactored_echo_server_SOURCES = coroutines_ts/refactored_echo_server.cpp
endif
diff --git a/asio/src/examples/cpp17/coroutines_ts/.gitignore b/asio/src/examples/cpp17/coroutines_ts/.gitignore
index 229fd66..ba58f2e 100644
--- a/asio/src/examples/cpp17/coroutines_ts/.gitignore
+++ b/asio/src/examples/cpp17/coroutines_ts/.gitignore
@@ -9,3 +9,4 @@
*.tds
*_server
*_server_with_default
+*_server_with_as_single_default
diff --git a/asio/src/examples/cpp17/coroutines_ts/echo_server_with_as_single_default.cpp b/asio/src/examples/cpp17/coroutines_ts/echo_server_with_as_single_default.cpp
new file mode 100644
index 0000000..457af47
--- /dev/null
+++ b/asio/src/examples/cpp17/coroutines_ts/echo_server_with_as_single_default.cpp
@@ -0,0 +1,71 @@
+//
+// echo_server_with_as_single_default.cpp
+// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2020 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)
+//
+
+#include <asio/experimental/as_single.hpp>
+#include <asio/co_spawn.hpp>
+#include <asio/detached.hpp>
+#include <asio/io_context.hpp>
+#include <asio/ip/tcp.hpp>
+#include <asio/signal_set.hpp>
+#include <asio/write.hpp>
+#include <cstdio>
+
+using asio::experimental::as_single_t;
+using asio::ip::tcp;
+using asio::awaitable;
+using asio::co_spawn;
+using asio::detached;
+using asio::use_awaitable_t;
+using default_token = as_single_t<use_awaitable_t<>>;
+using tcp_acceptor = default_token::as_default_on_t<tcp::acceptor>;
+using tcp_socket = default_token::as_default_on_t<tcp::socket>;
+namespace this_coro = asio::this_coro;
+
+awaitable<void> echo(tcp_socket socket)
+{
+ char data[1024];
+ for (;;)
+ {
+ auto [e1, nread] = co_await socket.async_read_some(asio::buffer(data));
+ if (nread == 0) break;
+ auto [e2, nwritten] = co_await async_write(socket, asio::buffer(data, nread));
+ if (nwritten != nread) break;
+ }
+}
+
+awaitable<void> listener()
+{
+ auto executor = co_await this_coro::executor;
+ tcp_acceptor acceptor(executor, {tcp::v4(), 55555});
+ for (;;)
+ {
+ if (auto [e, socket] = co_await acceptor.async_accept(); socket.is_open())
+ co_spawn(executor, echo(std::move(socket)), detached);
+ }
+}
+
+int main()
+{
+ try
+ {
+ asio::io_context io_context(1);
+
+ asio::signal_set signals(io_context, SIGINT, SIGTERM);
+ signals.async_wait([&](auto, auto){ io_context.stop(); });
+
+ co_spawn(io_context, listener(), detached);
+
+ io_context.run();
+ }
+ catch (std::exception& e)
+ {
+ std::printf("Exception: %s\n", e.what());
+ }
+}