diff --git a/CHANGELOG.md b/CHANGELOG.md index fd938636..35bf6257 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Version 198: * static_buffer improvements * flat_static_buffer_improvements * saved_handler maintains a work_guard (websocket) +* Add buffer_traits.hpp, buffers_type API Changes: diff --git a/doc/qbk/03_core/3_buffers.qbk b/doc/qbk/03_core/3_buffers.qbk index 82b00c46..e1397d4e 100644 --- a/doc/qbk/03_core/3_buffers.qbk +++ b/doc/qbk/03_core/3_buffers.qbk @@ -53,7 +53,7 @@ transferred. ][ This class represents the buffer sequence formed from a prefix of an existing buffer sequence. This is the type of buffer returned by - [link beast.ref.boost__beast__buffers_prefix.overload3 `buffers_prefix`]. + [link beast.ref.boost__beast__buffers_prefix `buffers_prefix`]. ]] [[ [link beast.ref.boost__beast__buffers_range `buffers_range`] diff --git a/doc/qbk/quickref.xml b/doc/qbk/quickref.xml index 162c9dbc..20ee7b2c 100644 --- a/doc/qbk/quickref.xml +++ b/doc/qbk/quickref.xml @@ -235,6 +235,7 @@ Type Traits + buffers_type get_lowest_layer has_get_executor is_async_read_stream diff --git a/include/boost/beast/core.hpp b/include/boost/beast/core.hpp index 6e189def..ad492e2e 100644 --- a/include/boost/beast/core.hpp +++ b/include/boost/beast/core.hpp @@ -13,6 +13,7 @@ #include #include +#include #include #include #include diff --git a/include/boost/beast/core/buffer_traits.hpp b/include/boost/beast/core/buffer_traits.hpp new file mode 100644 index 00000000..0141e333 --- /dev/null +++ b/include/boost/beast/core/buffer_traits.hpp @@ -0,0 +1,73 @@ +// +// Copyright (c) 2018 Vinnie Falco (vinnie dot falco at gmail 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) +// +// Official repository: https://github.com/boostorg/beast +// + +#ifndef BOOST_BEAST_BUFFER_TRAITS_HPP +#define BOOST_BEAST_BUFFER_TRAITS_HPP + +#include +#include +#include + +namespace boost { +namespace beast { + +/** Type alias used to obtain the underlying buffer type of a buffer sequence. + + This metafunction is used to determine the underlying buffer type for + a given buffer sequence. The equivalent type of the alias will vary + depending on the template type argument: + + @li If the template argument is a MutableBufferSequence, + the resulting type alias will be `net::mutable_buffer`, else + + @li If the template argument is a ConstBufferSequence, + the resulting type alias will be `net::const_buffer`, otherwise + + @li The resulting type alias will be `void`. + + @par Example + The following code returns the first buffer in a buffer sequence, + or generates a compilation error if the argument is not a buffer + sequence: + @code + template + buffers_type + buffers_front (BufferSequence const& buffers) + { + static_assert( + net::is_const_buffer_sequence::value, + "BufferSequence requirements not met"); + auto const first = net::buffer_sequence_begin (buffers); + if (first == net::buffer_sequence_end (buffers)) + return {}; + return *first; + } + @endcode + + @param T The buffer sequence type to use. +*/ +template +#if BOOST_BEAST_DOXYGEN +struct buffers_type : __see_below__ {}; +//using buffers_type = __see_below__; +#else +using buffers_type = typename + std::conditional< + net::is_mutable_buffer_sequence::value, + net::mutable_buffer, + typename std::conditional< + net::is_const_buffer_sequence::value, + net::const_buffer, + void>::type>::type; +#endif + +} // beast +} // boost + +#endif diff --git a/test/beast/core/CMakeLists.txt b/test/beast/core/CMakeLists.txt index 2125f4d5..456cbb99 100644 --- a/test/beast/core/CMakeLists.txt +++ b/test/beast/core/CMakeLists.txt @@ -20,6 +20,7 @@ add_executable (tests-beast-core file_test.hpp bind_handler.cpp buffer.cpp + buffer_traits.cpp buffered_read_stream.cpp buffers_adapter.cpp buffers_cat.cpp diff --git a/test/beast/core/Jamfile b/test/beast/core/Jamfile index 5dbbf695..b3e1eb2c 100644 --- a/test/beast/core/Jamfile +++ b/test/beast/core/Jamfile @@ -10,6 +10,7 @@ local SOURCES = bind_handler.cpp buffer.cpp + buffer_traits.cpp buffered_read_stream.cpp buffers_adapter.cpp buffers_cat.cpp diff --git a/test/beast/core/buffer_traits.cpp b/test/beast/core/buffer_traits.cpp new file mode 100644 index 00000000..846c7bbe --- /dev/null +++ b/test/beast/core/buffer_traits.cpp @@ -0,0 +1,39 @@ +// +// Copyright (c) 2018 Vinnie Falco (vinnie dot falco at gmail 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) +// +// Official repository: https://github.com/boostorg/beast +// + +// Test that header file is self-contained. +#include + +#include + +namespace boost { +namespace beast { + +BOOST_STATIC_ASSERT( + std::is_same>::value); + +BOOST_STATIC_ASSERT( + std::is_same>::value); + +BOOST_STATIC_ASSERT( + std::is_same>>::value); + +BOOST_STATIC_ASSERT( + std::is_same>>::value); + +BOOST_STATIC_ASSERT( + std::is_same>>::value); + +} // beast +} // boost