]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // |
b32b8144 | 2 | // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com) |
7c673cae FG |
3 | // |
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | // | |
b32b8144 FG |
7 | // Official repository: https://github.com/boostorg/beast |
8 | // | |
7c673cae | 9 | |
b32b8144 FG |
10 | #ifndef BOOST_BEAST_TEST_BUFFER_TEST_HPP |
11 | #define BOOST_BEAST_TEST_BUFFER_TEST_HPP | |
7c673cae | 12 | |
b32b8144 FG |
13 | #include <boost/beast/core/string.hpp> |
14 | #include <boost/beast/core/read_size.hpp> | |
15 | #include <boost/beast/core/type_traits.hpp> | |
16 | #include <boost/beast/core/detail/type_traits.hpp> | |
7c673cae FG |
17 | #include <boost/asio/buffer.hpp> |
18 | #include <algorithm> | |
b32b8144 | 19 | #include <string> |
7c673cae FG |
20 | #include <type_traits> |
21 | ||
b32b8144 | 22 | namespace boost { |
7c673cae FG |
23 | namespace beast { |
24 | namespace test { | |
25 | ||
26 | template<class ConstBufferSequence> | |
27 | typename std::enable_if< | |
b32b8144 FG |
28 | boost::asio::is_const_buffer_sequence<ConstBufferSequence>::value, |
29 | std::string>::type | |
30 | to_string(ConstBufferSequence const& bs) | |
31 | { | |
32 | std::string s; | |
33 | s.reserve(buffer_size(bs)); | |
34 | for(auto b : beast::detail::buffers_range(bs)) | |
35 | s.append(reinterpret_cast<char const*>(b.data()), | |
36 | b.size()); | |
37 | return s; | |
38 | } | |
39 | ||
40 | template<class DynamicBuffer> | |
41 | void | |
42 | write_buffer(DynamicBuffer& b, string_view s) | |
43 | { | |
44 | b.commit(boost::asio::buffer_copy( | |
45 | b.prepare(s.size()), boost::asio::buffer( | |
46 | s.data(), s.size()))); | |
47 | } | |
48 | ||
49 | template<class ConstBufferSequence> | |
50 | typename std::enable_if< | |
51 | boost::asio::is_const_buffer_sequence<ConstBufferSequence>::value, | |
7c673cae FG |
52 | std::size_t>::type |
53 | buffer_count(ConstBufferSequence const& buffers) | |
54 | { | |
55 | return std::distance(buffers.begin(), buffers.end()); | |
56 | } | |
57 | ||
58 | template<class ConstBufferSequence> | |
59 | typename std::enable_if< | |
b32b8144 | 60 | boost::asio::is_const_buffer_sequence<ConstBufferSequence>::value, |
7c673cae FG |
61 | std::size_t>::type |
62 | size_pre(ConstBufferSequence const& buffers) | |
63 | { | |
64 | std::size_t n = 0; | |
65 | for(auto it = buffers.begin(); it != buffers.end(); ++it) | |
66 | { | |
67 | typename ConstBufferSequence::const_iterator it0(std::move(it)); | |
68 | typename ConstBufferSequence::const_iterator it1(it0); | |
69 | typename ConstBufferSequence::const_iterator it2; | |
70 | it2 = it1; | |
71 | n += boost::asio::buffer_size(*it2); | |
72 | it = std::move(it2); | |
73 | } | |
74 | return n; | |
75 | } | |
76 | ||
77 | template<class ConstBufferSequence> | |
78 | typename std::enable_if< | |
b32b8144 | 79 | boost::asio::is_const_buffer_sequence<ConstBufferSequence>::value, |
7c673cae FG |
80 | std::size_t>::type |
81 | size_post(ConstBufferSequence const& buffers) | |
82 | { | |
83 | std::size_t n = 0; | |
84 | for(auto it = buffers.begin(); it != buffers.end(); it++) | |
85 | n += boost::asio::buffer_size(*it); | |
86 | return n; | |
87 | } | |
88 | ||
89 | template<class ConstBufferSequence> | |
90 | typename std::enable_if< | |
b32b8144 | 91 | boost::asio::is_const_buffer_sequence<ConstBufferSequence>::value, |
7c673cae FG |
92 | std::size_t>::type |
93 | size_rev_pre(ConstBufferSequence const& buffers) | |
94 | { | |
95 | std::size_t n = 0; | |
96 | for(auto it = buffers.end(); it != buffers.begin();) | |
97 | n += boost::asio::buffer_size(*--it); | |
98 | return n; | |
99 | } | |
100 | ||
101 | template<class ConstBufferSequence> | |
102 | typename std::enable_if< | |
b32b8144 | 103 | boost::asio::is_const_buffer_sequence<ConstBufferSequence>::value, |
7c673cae FG |
104 | std::size_t>::type |
105 | size_rev_post(ConstBufferSequence const& buffers) | |
106 | { | |
107 | std::size_t n = 0; | |
108 | for(auto it = buffers.end(); it != buffers.begin();) | |
109 | { | |
110 | it--; | |
111 | n += boost::asio::buffer_size(*it); | |
112 | } | |
113 | return n; | |
114 | } | |
115 | ||
116 | } // test | |
117 | } // beast | |
b32b8144 | 118 | } // boost |
7c673cae FG |
119 | |
120 | #endif |