New buffers() replaces to_string() (API Change):

A new function, buffers(), returns an implementation defined object
which wraps a ConstBufferSequence and supports formatting to a
std::ostream.

The function to_string is removed, as the new implementation allows
conversion to string using boost::lexical_cast on the return value
of the call to buffers(). Streaming to an output stream is more
efficient: no dynamic allocations are performed.

Example:

    streambuf sb;
    std::cout << buffers(sb.data()) << std::endl;
This commit is contained in:
Vinnie Falco
2017-05-04 05:01:50 -07:00
parent 3add5f293c
commit 38b473902a
23 changed files with 156 additions and 156 deletions

View File

@@ -9,6 +9,7 @@ API Changes:
* Refactor http::header contents
* New ostream() returns dynamic buffer output stream
* New buffers() replaces to_string()
--------------------------------------------------------------------------------

View File

@@ -176,11 +176,11 @@
<simplelist type="vert" columns="1">
<member><link linkend="beast.ref.bind_handler">bind_handler</link></member>
<member><link linkend="beast.ref.buffer_cat">buffer_cat</link></member>
<member><link linkend="beast.ref.buffers">buffers</link></member>
<member><link linkend="beast.ref.ostream">ostream</link></member>
<member><link linkend="beast.ref.prepare_buffer">prepare_buffer</link></member>
<member><link linkend="beast.ref.prepare_buffers">prepare_buffers</link></member>
<member><link linkend="beast.ref.system_category">system_category</link></member>
<member><link linkend="beast.ref.to_string">to_string</link></member>
</simplelist>
</entry>
<entry valign="top">

View File

@@ -5,7 +5,7 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <beast/core/to_string.hpp>
#include <beast/core/ostream.hpp>
#include <beast/websocket.hpp>
#include <beast/websocket/ssl.hpp>
#include <boost/asio.hpp>
@@ -45,5 +45,5 @@ int main()
beast::websocket::opcode op;
ws.read(op, sb);
ws.close(beast::websocket::close_code::normal);
std::cout << to_string(sb.data()) << "\n";
std::cout << beast::buffers(sb.data()) << "\n";
}

View File

@@ -5,7 +5,7 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <beast/core/to_string.hpp>
#include <beast/core/ostream.hpp>
#include <beast/websocket.hpp>
#include <boost/asio.hpp>
#include <iostream>
@@ -31,5 +31,5 @@ int main()
beast::websocket::opcode op;
ws.read(op, sb);
ws.close(beast::websocket::close_code::normal);
std::cout << beast::to_string(sb.data()) << "\n";
std::cout << beast::buffers(sb.data()) << "\n";
}

View File

@@ -17,19 +17,18 @@
#include <beast/core/buffers_adapter.hpp>
#include <beast/core/consuming_buffers.hpp>
#include <beast/core/dynabuf_readstream.hpp>
#include <beast/core/ostream.hpp>
#include <beast/core/error.hpp>
#include <beast/core/flat_streambuf.hpp>
#include <beast/core/handler_alloc.hpp>
#include <beast/core/handler_concepts.hpp>
#include <beast/core/handler_helpers.hpp>
#include <beast/core/handler_ptr.hpp>
#include <beast/core/ostream.hpp>
#include <beast/core/placeholders.hpp>
#include <beast/core/prepare_buffers.hpp>
#include <beast/core/static_streambuf.hpp>
#include <beast/core/static_string.hpp>
#include <beast/core/stream_concepts.hpp>
#include <beast/core/streambuf.hpp>
#include <beast/core/to_string.hpp>
#endif

View File

@@ -19,6 +19,40 @@
namespace beast {
namespace detail {
template<class Buffers>
class buffers_helper
{
Buffers b_;
public:
explicit
buffers_helper(Buffers const& b)
: b_(b)
{
}
template<class B>
friend
std::ostream&
operator<<(std::ostream& os,
buffers_helper<B> const& v);
};
template<class Buffers>
std::ostream&
operator<<(std::ostream& os,
buffers_helper<Buffers> const& v)
{
using boost::asio::buffer_cast;
using boost::asio::buffer_size;
for(auto const& b : v.b_)
os.write(buffer_cast<char const*>(b),
buffer_size(b));
return os;
}
//------------------------------------------------------------------------------
template<
class DynamicBuffer,
class CharT,

View File

@@ -17,6 +17,40 @@
namespace beast {
/** Return an object representing a @b ConstBufferSequence.
This function wraps a reference to a buffer sequence and permits
the following operation:
@li `operator<<` to `std::ostream`. No character translation is
performed; unprintable and null characters will be transferred
as-is to the output stream.
@par Example
@code
streambuf sb;
...
std::cout << buffers(sb.data()) << std::endl;
@endcode
@param b An object meeting the requirements of @b ConstBufferSequence
to be streamed. The implementation will make a copy of this object.
*/
template<class ConstBufferSequence>
#if BEAST_DOXYGEN
implementation_defined
#else
detail::buffers_helper<ConstBufferSequence>
#endif
buffers(ConstBufferSequence const& b)
{
static_assert(is_ConstBufferSequence<
ConstBufferSequence>::value,
"ConstBufferSequence not met");
return detail::buffers_helper<
ConstBufferSequence>{b};
}
/** Return an output stream that formats values into a @b DynamicBuffer.
This function wraps the caller provided @b DynamicBuffer into

View File

@@ -1,53 +0,0 @@
//
// Copyright (c) 2013-2017 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)
//
#ifndef BEAST_TO_STRING_HPP
#define BEAST_TO_STRING_HPP
#include <beast/config.hpp>
#include <beast/core/buffer_concepts.hpp>
#include <boost/asio/buffer.hpp>
#include <string>
namespace beast {
/** Convert a @b `ConstBufferSequence` to a `std::string`.
This function will convert the octets in a buffer sequence to a string.
All octets will be inserted into the resulting string, including null
or unprintable characters.
@param buffers The buffer sequence to convert.
@return A string representing the contents of the input area.
@note This function participates in overload resolution only if
the buffers parameter meets the requirements of @b `ConstBufferSequence`.
*/
template<class ConstBufferSequence>
#if BEAST_DOXYGEN
std::string
#else
typename std::enable_if<
is_ConstBufferSequence<ConstBufferSequence>::value,
std::string>::type
#endif
to_string(ConstBufferSequence const& buffers)
{
using boost::asio::buffer_cast;
using boost::asio::buffer_size;
std::string s;
s.reserve(buffer_size(buffers));
for(auto const& buffer : buffers)
s.append(buffer_cast<char const*>(buffer),
buffer_size(buffer));
return s;
}
} // beast
#endif

View File

@@ -1,39 +0,0 @@
# Remember that this blacklist file is GLOBAL to all sanitizers
# Be therefore extremely careful when considering to add a sanitizer
# filter here instead of using a runtime suppression
#
# Remember also that filters here quite literally completely
# remove instrumentation altogether, so filtering here means
# that sanitizers such as tsan will false positive on problems
# introduced by code filtered here.
#
# The main use for this file is ubsan, as it's the only sanitizer
# without a runtime suppression facility.
#
# Be ESPECIALLY careful when filtering out entire source files!
# Try if at all possible to filter only functions using fun:regex
# Remember you must use mangled symbol names with fun:regex
#### Compile time filters for ubsan ####
## The well known ubsan failure in libstdc++ extant for years :)
# Line 96:24: runtime error: load of value 4294967221, which is not a valid value for type 'std::_Ios_Fmtflags'
fun:*_Ios_Fmtflags*
# boost/any.hpp:259:16: runtime error: downcast of address 0x000004392e70 which does not point to an object of type 'any::holder<int>'
fun:*any_cast*
# boost/lexical_cast.hpp:1625:43: runtime error: downcast of address 0x7fbb4fffbce8 which does not point to an object of type 'buffer_t' (aka 'parser_buf<std::basic_streambuf<char, char_traits<char> >, char>')
fun:*shl_input_streamable*
#### Compile time filters for asan ####
#### Compile time filters for msan ####
#### Compile time filters for tsan ####

View File

@@ -37,7 +37,6 @@ unit-test core-tests :
core/static_string.cpp
core/stream_concepts.cpp
core/streambuf.cpp
core/to_string.cpp
core/base64.cpp
core/empty_base_optimization.cpp
core/get_lowest_layer.cpp

View File

@@ -30,7 +30,6 @@ add_executable (core-tests
static_string.cpp
stream_concepts.cpp
streambuf.cpp
to_string.cpp
base64.cpp
empty_base_optimization.cpp
get_lowest_layer.cpp

View File

@@ -8,10 +8,12 @@
// Test that header file is self-contained.
#include <beast/core/buffers_adapter.hpp>
#include <beast/core/ostream.hpp>
#include <beast/core/streambuf.hpp>
#include <beast/unit_test/suite.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/streambuf.hpp>
#include <boost/lexical_cast.hpp>
#include <iterator>
namespace beast {
@@ -24,14 +26,8 @@ public:
std::string
to_string(ConstBufferSequence const& bs)
{
using boost::asio::buffer_cast;
using boost::asio::buffer_size;
std::string s;
s.reserve(buffer_size(bs));
for(auto const& b : bs)
s.append(buffer_cast<char const*>(b),
buffer_size(b));
return s;
return boost::lexical_cast<
std::string>(buffers(bs));
}
void testBuffersAdapter()

View File

@@ -9,7 +9,7 @@
#include <beast/core/consuming_buffers.hpp>
#include "buffer_test.hpp"
#include <beast/core/to_string.hpp>
#include <beast/core/ostream.hpp>
#include <beast/unit_test/suite.hpp>
#include <boost/asio/buffer.hpp>
#include <string>
@@ -19,6 +19,15 @@ namespace beast {
class consuming_buffers_test : public beast::unit_test::suite
{
public:
template<class ConstBufferSequence>
static
std::string
to_string(ConstBufferSequence const& bs)
{
return boost::lexical_cast<
std::string>(buffers(bs));
}
template<class BufferSequence>
static
consuming_buffers<BufferSequence>

View File

@@ -9,9 +9,10 @@
#include <beast/core/flat_streambuf.hpp>
#include "buffer_test.hpp"
#include <beast/core/to_string.hpp>
#include <beast/core/ostream.hpp>
#include <beast/test/test_allocator.hpp>
#include <beast/unit_test/suite.hpp>
#include <boost/lexical_cast.hpp>
#include <algorithm>
namespace beast {
@@ -22,6 +23,15 @@ static_assert(is_DynamicBuffer<flat_streambuf>::value,
class flat_streambuf_test : public beast::unit_test::suite
{
public:
template<class ConstBufferSequence>
static
std::string
to_string(ConstBufferSequence const& bs)
{
return boost::lexical_cast<
std::string>(buffers(bs));
}
template<class Alloc1, class Alloc2>
static
bool

View File

@@ -10,10 +10,9 @@
#include <beast/core/streambuf.hpp>
#include <beast/unit_test/suite.hpp>
#include <boost/lexical_cast.hpp>
#include <ostream>
#include <beast/core/to_string.hpp>
namespace beast {
class ostream_test : public beast::unit_test::suite
@@ -23,7 +22,8 @@ public:
{
streambuf sb;
ostream(sb) << "Hello, world!\n";
BEAST_EXPECT(to_string(sb.data()) == "Hello, world!\n");
BEAST_EXPECT(boost::lexical_cast<std::string>(
buffers(sb.data())) == "Hello, world!\n");
}
};

View File

@@ -10,7 +10,7 @@
#include "buffer_test.hpp"
#include <beast/core/buffer_concepts.hpp>
#include <beast/core/to_string.hpp>
#include <beast/core/ostream.hpp>
#include <beast/test/test_allocator.hpp>
#include <beast/unit_test/suite.hpp>
#include <boost/asio/buffer.hpp>
@@ -26,6 +26,15 @@ static_assert(is_DynamicBuffer<streambuf>::value, "");
class basic_streambuf_test : public beast::unit_test::suite
{
public:
template<class ConstBufferSequence>
static
std::string
to_string(ConstBufferSequence const& bs)
{
return boost::lexical_cast<
std::string>(buffers(bs));
}
template<class Alloc1, class Alloc2>
static
bool

View File

@@ -1,28 +0,0 @@
//
// Copyright (c) 2013-2017 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)
//
// Test that header file is self-contained.
#include <beast/core/to_string.hpp>
#include <beast/unit_test/suite.hpp>
#include <boost/asio/buffer.hpp>
namespace beast {
class to_string_test : public beast::unit_test::suite
{
public:
void run()
{
BEAST_EXPECT(to_string(boost::asio::const_buffers_1("x", 1)) == "x");
}
};
BEAST_DEFINE_TESTSUITE(to_string,core,beast);
} // beast

View File

@@ -8,8 +8,9 @@
// Test that header file is self-contained.
#include <beast/http/chunk_encode.hpp>
#include <beast/core/to_string.hpp>
#include <beast/core/ostream.hpp>
#include <beast/unit_test/suite.hpp>
#include <boost/lexical_cast.hpp>
namespace beast {
namespace http {
@@ -30,6 +31,15 @@ public:
}
};
template<class ConstBufferSequence>
static
std::string
to_string(ConstBufferSequence const& bs)
{
return boost::lexical_cast<
std::string>(buffers(bs));
}
static
void
encode1(std::string& s, final_chunk const& fc)

View File

@@ -8,7 +8,7 @@
// Test that header file is self-contained.
#include <beast/http/dynamic_body.hpp>
#include <beast/core/to_string.hpp>
#include <beast/core/ostream.hpp>
#include <beast/http/fields.hpp>
#include <beast/http/message_parser.hpp>
#include <beast/http/read.hpp>
@@ -25,7 +25,8 @@ class dynamic_body_test : public beast::unit_test::suite
boost::asio::io_service ios_;
public:
void run() override
void
run() override
{
std::string const s =
"HTTP/1.1 200 OK\r\n"
@@ -38,7 +39,8 @@ public:
streambuf sb;
read(ss, sb, p);
auto const& m = p.get();
BEAST_EXPECT(to_string(m.body.data()) == "xyz");
BEAST_EXPECT(boost::lexical_cast<std::string>(
buffers(m.body.data())) == "xyz");
BEAST_EXPECT(boost::lexical_cast<std::string>(m) == s);
}
};

View File

@@ -10,9 +10,10 @@
#include <beast/http.hpp>
#include <beast/core/consuming_buffers.hpp>
#include <beast/core/ostream.hpp>
#include <beast/core/streambuf.hpp>
#include <beast/core/to_string.hpp>
#include <beast/unit_test/suite.hpp>
#include <boost/lexical_cast.hpp>
#include <chrono>
#include <iostream>
#include <vector>
@@ -31,6 +32,15 @@ public:
corpus cres_;
std::size_t size_ = 0;
template<class ConstBufferSequence>
static
std::string
to_string(ConstBufferSequence const& bs)
{
return boost::lexical_cast<
std::string>(buffers(bs));
}
parser_bench_test()
{
creq_ = build_corpus(N/2, std::true_type{});

View File

@@ -14,7 +14,6 @@
#include <beast/http/write.hpp>
#include <beast/core/error.hpp>
#include <beast/core/streambuf.hpp>
#include <beast/core/to_string.hpp>
#include <beast/test/fail_stream.hpp>
#include <beast/test/string_ostream.hpp>
#include <beast/test/yield_to.hpp>

View File

@@ -8,7 +8,7 @@
#include "websocket_async_ssl_echo_server.hpp"
#include <beast/websocket/stream.hpp>
#include <beast/core/to_string.hpp>
#include <beast/core/ostream.hpp>
#include <beast/unit_test/suite.hpp>
#include <beast/test/yield_to.hpp>
#include <boost/asio.hpp>
@@ -126,7 +126,7 @@ public:
// Secure WebSocket connect and send message using Beast
beast::websocket::stream<stream_type&> ws{stream};
ws.handshake("localhost", "/");
ws.write(boost::asio::buffer("Hello, world!"));
ws.write(boost::asio::buffer("Hello, world!", 13));
// Receive Secure WebSocket message, print and close using Beast
beast::streambuf sb;
@@ -146,9 +146,8 @@ public:
if(se.code() != beast::websocket::error::closed)
throw;
}
log << to_string(sb.data()) << std::endl;
pass();
BEAST_EXPECT(boost::lexical_cast<std::string>(
buffers(sb.data())) == "Hello, world!");
}
};

View File

@@ -11,8 +11,8 @@
#include "websocket_async_echo_server.hpp"
#include "websocket_sync_echo_server.hpp"
#include <beast/core/ostream.hpp>
#include <beast/core/streambuf.hpp>
#include <beast/core/to_string.hpp>
#include <beast/test/fail_stream.hpp>
#include <beast/test/string_istream.hpp>
#include <beast/test/string_iostream.hpp>
@@ -38,6 +38,15 @@ public:
using address_type = boost::asio::ip::address;
using socket_type = boost::asio::ip::tcp::socket;
template<class ConstBufferSequence>
static
std::string
to_string(ConstBufferSequence const& bs)
{
return boost::lexical_cast<
std::string>(buffers(bs));
}
struct con
{
stream<socket_type> ws;
@@ -1169,7 +1178,8 @@ public:
);
}
void testMask(endpoint_type const& ep,
void
testMask(endpoint_type const& ep,
yield_context do_yield)
{
{