From 9f468ec1c15cb67405ddcce8e38d61c0cc961c4b Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Thu, 31 Oct 2019 05:10:22 -0700 Subject: [PATCH] Use automatically deduced return types for all async operations: C++14 or later is required to support completion tokens that use per-operation return type deduction. For C++11, a completion token's async_result specialisation must still provide the nested typedef `return_type`. --- CHANGELOG.md | 1 + include/boost/beast/core/basic_stream.hpp | 8 ++++-- include/boost/beast/core/detail/config.hpp | 4 +-- include/boost/beast/http/impl/write.hpp | 16 +++++------ include/boost/beast/http/write.hpp | 27 +++++++++---------- include/boost/beast/websocket/impl/accept.hpp | 19 +++++++------ include/boost/beast/websocket/stream.hpp | 25 ++++++++--------- test/beast/zlib/deflate_stream.cpp | 9 ++++--- 8 files changed, 60 insertions(+), 49 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3760a4e0..8b292ef2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ Version 275: * Async init-fns use the executor's default token * Add basic_stream::rebind_executor +* Use automatically deduced return types for all async operations -------------------------------------------------------------------------------- diff --git a/include/boost/beast/core/basic_stream.hpp b/include/boost/beast/core/basic_stream.hpp index 9b4ae5cd..4b376d5e 100644 --- a/include/boost/beast/core/basic_stream.hpp +++ b/include/boost/beast/core/basic_stream.hpp @@ -1125,7 +1125,9 @@ public: class Iterator, class IteratorConnectHandler = net::default_completion_token_t> - BOOST_ASIO_INITFN_RESULT_TYPE(IteratorConnectHandler,void (error_code, Iterator)) + BOOST_ASIO_INITFN_RESULT_TYPE( + IteratorConnectHandler, + void(error_code, Iterator)) async_connect( Iterator begin, Iterator end, IteratorConnectHandler&& handler = @@ -1182,7 +1184,9 @@ public: class ConnectCondition, class IteratorConnectHandler = net::default_completion_token_t> - BOOST_ASIO_INITFN_RESULT_TYPE(IteratorConnectHandler,void (error_code, Iterator)) + BOOST_ASIO_INITFN_RESULT_TYPE( + IteratorConnectHandler, + void(error_code, Iterator)) async_connect( Iterator begin, Iterator end, ConnectCondition connect_condition, diff --git a/include/boost/beast/core/detail/config.hpp b/include/boost/beast/core/detail/config.hpp index bebe8c79..5dd637b1 100644 --- a/include/boost/beast/core/detail/config.hpp +++ b/include/boost/beast/core/detail/config.hpp @@ -85,12 +85,12 @@ namespace net = boost::asio; #ifndef BOOST_BEAST_ASYNC_RESULT1 #define BOOST_BEAST_ASYNC_RESULT1(type) \ - BOOST_ASIO_INITFN_RESULT_TYPE(type, void(::boost::beast::error_code)) + BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(type, void(::boost::beast::error_code)) #endif #ifndef BOOST_BEAST_ASYNC_RESULT2 #define BOOST_BEAST_ASYNC_RESULT2(type) \ - BOOST_ASIO_INITFN_RESULT_TYPE(type, void(::boost::beast::error_code, std::size_t)) + BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(type, void(::boost::beast::error_code, std::size_t)) #endif #endif diff --git a/include/boost/beast/http/impl/write.hpp b/include/boost/beast/http/impl/write.hpp index 79b0c83d..ebf74b2d 100644 --- a/include/boost/beast/http/impl/write.hpp +++ b/include/boost/beast/http/impl/write.hpp @@ -802,13 +802,13 @@ template< class AsyncWriteStream, bool isRequest, class Body, class Fields, class WriteHandler> -typename std::enable_if< - is_mutable_body_writer::value, - BOOST_BEAST_ASYNC_RESULT2(WriteHandler)>::type +BOOST_BEAST_ASYNC_RESULT2(WriteHandler) async_write( AsyncWriteStream& stream, message& msg, - WriteHandler&& handler) + WriteHandler&& handler, + typename std::enable_if< + is_mutable_body_writer::value>::type*) { static_assert( is_async_write_stream::value, @@ -831,13 +831,13 @@ template< class AsyncWriteStream, bool isRequest, class Body, class Fields, class WriteHandler> -typename std::enable_if< - ! is_mutable_body_writer::value, - BOOST_BEAST_ASYNC_RESULT2(WriteHandler)>::type +BOOST_BEAST_ASYNC_RESULT2(WriteHandler) async_write( AsyncWriteStream& stream, message const& msg, - WriteHandler&& handler) + WriteHandler&& handler, + typename std::enable_if< + ! is_mutable_body_writer::value>::type*) { static_assert( is_async_write_stream::value, diff --git a/include/boost/beast/http/write.hpp b/include/boost/beast/http/write.hpp index 00a05370..546479c5 100644 --- a/include/boost/beast/http/write.hpp +++ b/include/boost/beast/http/write.hpp @@ -645,19 +645,18 @@ template< class WriteHandler = net::default_completion_token_t< executor_type>> -#if BOOST_BEAST_DOXYGEN BOOST_BEAST_ASYNC_RESULT2(WriteHandler) -#else -typename std::enable_if< - is_mutable_body_writer::value, - BOOST_BEAST_ASYNC_RESULT2(WriteHandler)>::type -#endif async_write( AsyncWriteStream& stream, message& msg, WriteHandler&& handler = net::default_completion_token_t< - executor_type>); + executor_type>{} +#ifndef BOOST_BEAST_DOXYGEN + , typename std::enable_if< + is_mutable_body_writer::value>::type* = 0 +#endif + ); /** Write a complete message to a stream asynchronously. @@ -708,19 +707,19 @@ template< class WriteHandler = net::default_completion_token_t< executor_type>> -#if BOOST_BEAST_DOXYGEN BOOST_BEAST_ASYNC_RESULT2(WriteHandler) -#else -typename std::enable_if< - ! is_mutable_body_writer::value, - BOOST_BEAST_ASYNC_RESULT2(WriteHandler)>::type -#endif async_write( AsyncWriteStream& stream, message const& msg, WriteHandler&& handler = net::default_completion_token_t< - executor_type>{}); + executor_type>{} +#ifndef BOOST_BEAST_DOXYGEN + , typename std::enable_if< + ! is_mutable_body_writer::value>::type* = 0 +#endif + ); + //------------------------------------------------------------------------------ diff --git a/include/boost/beast/websocket/impl/accept.hpp b/include/boost/beast/websocket/impl/accept.hpp index 1e33d8b4..538819b2 100644 --- a/include/boost/beast/websocket/impl/accept.hpp +++ b/include/boost/beast/websocket/impl/accept.hpp @@ -591,13 +591,15 @@ template template< class ConstBufferSequence, class AcceptHandler> -typename std::enable_if< - ! http::detail::is_header::value, - BOOST_BEAST_ASYNC_RESULT1(AcceptHandler)>::type +BOOST_BEAST_ASYNC_RESULT1(AcceptHandler) stream:: async_accept( ConstBufferSequence const& buffers, - AcceptHandler&& handler) + AcceptHandler&& handler, + typename std::enable_if< + ! http::detail::is_header< + ConstBufferSequence>::value>::type* +) { static_assert(is_async_stream::value, "AsyncStream type requirements not met"); @@ -620,14 +622,15 @@ template< class ConstBufferSequence, class ResponseDecorator, class AcceptHandler> -typename std::enable_if< - ! http::detail::is_header::value, - BOOST_BEAST_ASYNC_RESULT1(AcceptHandler)>::type +BOOST_BEAST_ASYNC_RESULT1(AcceptHandler) stream:: async_accept_ex( ConstBufferSequence const& buffers, ResponseDecorator const& decorator, - AcceptHandler&& handler) + AcceptHandler&& handler, + typename std::enable_if< + ! http::detail::is_header< + ConstBufferSequence>::value>::type*) { static_assert(is_async_stream::value, "AsyncStream type requirements not met"); diff --git a/include/boost/beast/websocket/stream.hpp b/include/boost/beast/websocket/stream.hpp index 9608b205..f23290d8 100644 --- a/include/boost/beast/websocket/stream.hpp +++ b/include/boost/beast/websocket/stream.hpp @@ -1357,18 +1357,18 @@ public: class AcceptHandler = net::default_completion_token_t > -#if BOOST_BEAST_DOXYGEN - void_or_deduced -#else - typename std::enable_if< - ! http::detail::is_header::value, - BOOST_BEAST_ASYNC_RESULT1(AcceptHandler)>::type -#endif + BOOST_BEAST_ASYNC_RESULT1(AcceptHandler) async_accept( ConstBufferSequence const& buffers, AcceptHandler&& handler = net::default_completion_token_t< - executor_type>{}); + executor_type>{} +#ifndef BOOST_BEAST_DOXYGEN + , typename std::enable_if< + ! http::detail::is_header< + ConstBufferSequence>::value>::type* = 0 +#endif + ); /** Perform the WebSocket handshake asynchronously in the server role. @@ -2644,13 +2644,14 @@ public: class ConstBufferSequence, class ResponseDecorator, class AcceptHandler> - typename std::enable_if< - ! http::detail::is_header::value, - BOOST_BEAST_ASYNC_RESULT1(AcceptHandler)>::type + BOOST_BEAST_ASYNC_RESULT1(AcceptHandler) async_accept_ex( ConstBufferSequence const& buffers, ResponseDecorator const& decorator, - AcceptHandler&& handler); + AcceptHandler&& handler, + typename std::enable_if< + ! http::detail::is_header< + ConstBufferSequence>::value>::type* = 0); template< class Body, class Allocator, diff --git a/test/beast/zlib/deflate_stream.cpp b/test/beast/zlib/deflate_stream.cpp index 4f449eea..ce738d03 100644 --- a/test/beast/zlib/deflate_stream.cpp +++ b/test/beast/zlib/deflate_stream.cpp @@ -403,7 +403,8 @@ public: explicit fixture(std::size_t n, Strategy s) { ds.reset(8, 15, 1, s); - std::iota(in.begin(), in.end(), 0); + std::iota(in.begin(), in.end(), + static_cast(0)); out.resize(n); zp.next_in = in.data(); zp.avail_in = in.size(); @@ -479,8 +480,10 @@ public: // 125 will mostly fill the lit buffer, so emitting a distance code // results in a flush. auto constexpr n = 125; - std::iota(in.begin(), in.begin() + n, 0); - std::iota(in.begin() + n, in.end(), 0); + std::iota(in.begin(), in.begin() + n, + static_cast(0)); + std::iota(in.begin() + n, in.end(), + static_cast(0)); ds.reset(8, 15, 1, Strategy::normal); std::string out;