]> git.proxmox.com Git - ceph.git/blob - ceph/src/Beast/extras/beast/test/string_istream.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / Beast / extras / beast / test / string_istream.hpp
1 //
2 // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
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 //
7
8 #ifndef BEAST_TEST_STRING_ISTREAM_HPP
9 #define BEAST_TEST_STRING_ISTREAM_HPP
10
11 #include <beast/core/async_completion.hpp>
12 #include <beast/core/bind_handler.hpp>
13 #include <beast/core/error.hpp>
14 #include <beast/core/prepare_buffer.hpp>
15 #include <boost/asio/buffer.hpp>
16 #include <boost/asio/io_service.hpp>
17 #include <string>
18
19 namespace beast {
20 namespace test {
21
22 /** A SyncStream and AsyncStream that reads from a string.
23
24 This class behaves like a socket, except that written data is simply
25 discarded, and when data is read it comes from a string provided
26 at construction.
27 */
28 class string_istream
29 {
30 std::string s_;
31 boost::asio::const_buffer cb_;
32 boost::asio::io_service& ios_;
33 std::size_t read_max_;
34
35 public:
36 string_istream(boost::asio::io_service& ios,
37 std::string s, std::size_t read_max =
38 (std::numeric_limits<std::size_t>::max)())
39 : s_(std::move(s))
40 , cb_(boost::asio::buffer(s_))
41 , ios_(ios)
42 , read_max_(read_max)
43 {
44 }
45
46 boost::asio::io_service&
47 get_io_service()
48 {
49 return ios_;
50 }
51
52 template<class MutableBufferSequence>
53 std::size_t
54 read_some(MutableBufferSequence const& buffers)
55 {
56 error_code ec;
57 auto const n = read_some(buffers, ec);
58 if(ec)
59 throw system_error{ec};
60 return n;
61 }
62
63 template<class MutableBufferSequence>
64 std::size_t
65 read_some(MutableBufferSequence const& buffers,
66 error_code& ec)
67 {
68 auto const n = boost::asio::buffer_copy(
69 buffers, prepare_buffer(read_max_, cb_));
70 if(n > 0)
71 cb_ = cb_ + n;
72 else
73 ec = boost::asio::error::eof;
74 return n;
75 }
76
77 template<class MutableBufferSequence, class ReadHandler>
78 typename async_completion<ReadHandler,
79 void(error_code, std::size_t)>::result_type
80 async_read_some(MutableBufferSequence const& buffers,
81 ReadHandler&& handler)
82 {
83 auto const n = boost::asio::buffer_copy(
84 buffers, boost::asio::buffer(s_));
85 error_code ec;
86 if(n > 0)
87 s_.erase(0, n);
88 else
89 ec = boost::asio::error::eof;
90 async_completion<ReadHandler,
91 void(error_code, std::size_t)> completion{handler};
92 ios_.post(bind_handler(
93 completion.handler, ec, n));
94 return completion.result.get();
95 }
96
97 template<class ConstBufferSequence>
98 std::size_t
99 write_some(ConstBufferSequence const& buffers)
100 {
101 error_code ec;
102 auto const n = write_some(buffers, ec);
103 if(ec)
104 throw system_error{ec};
105 return n;
106 }
107
108 template<class ConstBufferSequence>
109 std::size_t
110 write_some(ConstBufferSequence const& buffers,
111 error_code&)
112 {
113 return boost::asio::buffer_size(buffers);
114 }
115
116 template<class ConstBuffeSequence, class WriteHandler>
117 typename async_completion<WriteHandler,
118 void(error_code, std::size_t)>::result_type
119 async_write_some(ConstBuffeSequence const& buffers,
120 WriteHandler&& handler)
121 {
122 async_completion<WriteHandler,
123 void(error_code, std::size_t)> completion{handler};
124 ios_.post(bind_handler(completion.handler,
125 error_code{}, boost::asio::buffer_size(buffers)));
126 return completion.result.get();
127 }
128 };
129
130 } // test
131 } // beast
132
133 #endif