rippled
Loading...
Searching...
No Matches
ZeroCopyStream.h
1#pragma once
2
3#include <xrpl/beast/utility/instrumentation.h>
4
5#include <boost/asio/buffer.hpp>
6
7#include <google/protobuf/io/zero_copy_stream.h>
8
9namespace xrpl {
10
16template <class Buffers>
17class ZeroCopyInputStream : public ::google::protobuf::io::ZeroCopyInputStream
18{
19private:
20 using iterator = typename Buffers::const_iterator;
21 using const_buffer = boost::asio::const_buffer;
22
23 google::protobuf::int64 count_ = 0;
25 iterator first_; // Where pos_ comes from
26 const_buffer pos_; // What Next() will return
27
28public:
29 explicit ZeroCopyInputStream(Buffers const& buffers);
30
31 bool
32 Next(void const** data, int* size) override;
33
34 void
35 BackUp(int count) override;
36
37 bool
38 Skip(int count) override;
39
40 google::protobuf::int64
41 ByteCount() const override
42 {
43 return count_;
44 }
45};
46
47//------------------------------------------------------------------------------
48
49template <class Buffers>
51 : last_(buffers.end()), first_(buffers.begin()), pos_((first_ != last_) ? *first_ : const_buffer(nullptr, 0))
52{
53}
54
55template <class Buffers>
56bool
57ZeroCopyInputStream<Buffers>::Next(void const** data, int* size)
58{
59 *data = pos_.data();
60 *size = boost::asio::buffer_size(pos_);
61 if (first_ == last_)
62 return false;
63 count_ += *size;
64 pos_ = (++first_ != last_) ? *first_ : const_buffer(nullptr, 0);
65 return true;
66}
67
68template <class Buffers>
69void
71{
72 --first_;
73 pos_ = *first_ + (boost::asio::buffer_size(*first_) - count);
74 count_ -= count;
75}
76
77template <class Buffers>
78bool
80{
81 if (first_ == last_)
82 return false;
83 while (count > 0)
84 {
85 auto const size = boost::asio::buffer_size(pos_);
86 if (count < size)
87 {
88 pos_ = pos_ + count;
89 count_ += count;
90 return true;
91 }
92 count_ += size;
93 if (++first_ == last_)
94 return false;
95 count -= size;
96 pos_ = *first_;
97 }
98 return true;
99}
100
101//------------------------------------------------------------------------------
102
107template <class Streambuf>
108class ZeroCopyOutputStream : public ::google::protobuf::io::ZeroCopyOutputStream
109{
110private:
111 using buffers_type = typename Streambuf::mutable_buffers_type;
112 using iterator = typename buffers_type::const_iterator;
113 using mutable_buffer = boost::asio::mutable_buffer;
114
115 Streambuf& streambuf_;
117 google::protobuf::int64 count_ = 0;
121
122public:
123 explicit ZeroCopyOutputStream(Streambuf& streambuf, std::size_t blockSize);
124
126
127 bool
128 Next(void** data, int* size) override;
129
130 void
131 BackUp(int count) override;
132
133 google::protobuf::int64
134 ByteCount() const override
135 {
136 return count_;
137 }
138};
139
140//------------------------------------------------------------------------------
141
142template <class Streambuf>
144 : streambuf_(streambuf), blockSize_(blockSize), buffers_(streambuf_.prepare(blockSize_)), pos_(buffers_.begin())
145{
146}
147
148template <class Streambuf>
150{
151 if (commit_ != 0)
152 streambuf_.commit(commit_);
153}
154
155template <class Streambuf>
156bool
158{
159 if (commit_ != 0)
160 {
161 streambuf_.commit(commit_);
162 count_ += commit_;
163 }
164
165 if (pos_ == buffers_.end())
166 {
167 buffers_ = streambuf_.prepare(blockSize_);
168 pos_ = buffers_.begin();
169 }
170
171 *data = *pos_.data();
172 *size = boost::asio::buffer_size(*pos_);
173 commit_ = *size;
174 ++pos_;
175 return true;
176}
177
178template <class Streambuf>
179void
181{
182 XRPL_ASSERT(count <= commit_, "xrpl::ZeroCopyOutputStream::BackUp : valid input");
183 auto const n = commit_ - count;
184 streambuf_.commit(n);
185 count_ += n;
186 commit_ = 0;
187}
188
189} // namespace xrpl
Implements ZeroCopyInputStream around a buffer sequence.
boost::asio::const_buffer const_buffer
google::protobuf::int64 count_
typename Buffers::const_iterator iterator
bool Skip(int count) override
bool Next(void const **data, int *size) override
ZeroCopyInputStream(Buffers const &buffers)
google::protobuf::int64 ByteCount() const override
void BackUp(int count) override
Implements ZeroCopyOutputStream around a Streambuf.
typename Streambuf::mutable_buffers_type buffers_type
void BackUp(int count) override
google::protobuf::int64 count_
boost::asio::mutable_buffer mutable_buffer
ZeroCopyOutputStream(Streambuf &streambuf, std::size_t blockSize)
google::protobuf::int64 ByteCount() const override
typename buffers_type::const_iterator iterator
bool Next(void **data, int *size) override
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5