rippled
Loading...
Searching...
No Matches
Buffer.h
1#pragma once
2
3#include <xrpl/basics/Slice.h>
4#include <xrpl/beast/utility/instrumentation.h>
5
6#include <cstdint>
7#include <cstring>
8#include <memory>
9
10namespace xrpl {
11
15class Buffer
16{
17private:
20
21public:
23
24 Buffer() = default;
25
27 explicit Buffer(std::size_t size) : p_(size ? new std::uint8_t[size] : nullptr), size_(size)
28 {
29 }
30
38 {
39 if (size)
41 }
42
44 Buffer(Buffer const& other) : Buffer(other.p_.get(), other.size_)
45 {
46 }
47
49 Buffer&
50 operator=(Buffer const& other)
51 {
52 if (this != &other)
53 {
54 if (auto p = alloc(other.size_))
55 std::memcpy(p, other.p_.get(), size_);
56 }
57 return *this;
58 }
59
63 Buffer(Buffer&& other) noexcept : p_(std::move(other.p_)), size_(other.size_)
64 {
65 other.size_ = 0;
66 }
67
71 Buffer&
72 operator=(Buffer&& other) noexcept
73 {
74 if (this != &other)
75 {
76 p_ = std::move(other.p_);
77 size_ = other.size_;
78 other.size_ = 0;
79 }
80 return *this;
81 }
82
84 explicit Buffer(Slice s) : Buffer(s.data(), s.size())
85 {
86 }
87
89 Buffer&
91 {
92 // Ensure the slice isn't a subset of the buffer.
93 XRPL_ASSERT(
94 s.size() == 0 || size_ == 0 || s.data() < p_.get() || s.data() >= p_.get() + size_,
95 "xrpl::Buffer::operator=(Slice) : input not a subset");
96
97 if (auto p = alloc(s.size()))
98 std::memcpy(p, s.data(), s.size());
99 return *this;
100 }
101
104 size() const noexcept
105 {
106 return size_;
107 }
108
109 bool
110 empty() const noexcept
111 {
112 return 0 == size_;
113 }
114
115 operator Slice() const noexcept
116 {
117 if (!size_)
118 return Slice{};
119 return Slice{p_.get(), size_};
120 }
121
127 std::uint8_t const*
128 data() const noexcept
129 {
130 return p_.get();
131 }
132
134 data() noexcept
135 {
136 return p_.get();
137 }
143 void
144 clear() noexcept
145 {
146 p_.reset();
147 size_ = 0;
148 }
149
155 {
156 if (n != size_)
157 {
158 p_.reset(n ? new std::uint8_t[n] : nullptr);
159 size_ = n;
160 }
161 return p_.get();
162 }
163
164 // Meet the requirements of BufferFactory
165 void*
167 {
168 return alloc(n);
169 }
170
172 begin() const noexcept
173 {
174 return p_.get();
175 }
176
178 cbegin() const noexcept
179 {
180 return p_.get();
181 }
182
184 end() const noexcept
185 {
186 return p_.get() + size_;
187 }
188
190 cend() const noexcept
191 {
192 return p_.get() + size_;
193 }
194};
195
196inline bool
197operator==(Buffer const& lhs, Buffer const& rhs) noexcept
198{
199 if (lhs.size() != rhs.size())
200 return false;
201
202 if (lhs.size() == 0)
203 return true;
204
205 return std::memcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
206}
207
208inline bool
209operator!=(Buffer const& lhs, Buffer const& rhs) noexcept
210{
211 return !(lhs == rhs);
212}
213
214} // namespace xrpl
Like std::vector<char> but better.
Definition Buffer.h:16
const_iterator end() const noexcept
Definition Buffer.h:184
Buffer(std::size_t size)
Create an uninitialized buffer with the given size.
Definition Buffer.h:27
std::uint8_t const * const_iterator
Definition Buffer.h:22
std::unique_ptr< std::uint8_t[]> p_
Definition Buffer.h:18
bool empty() const noexcept
Definition Buffer.h:110
Buffer(Slice s)
Construct from a slice.
Definition Buffer.h:84
void * operator()(std::size_t n)
Definition Buffer.h:166
Buffer & operator=(Buffer &&other) noexcept
Move-assign.
Definition Buffer.h:72
const_iterator cend() const noexcept
Definition Buffer.h:190
const_iterator cbegin() const noexcept
Definition Buffer.h:178
std::size_t size_
Definition Buffer.h:19
const_iterator begin() const noexcept
Definition Buffer.h:172
std::uint8_t * data() noexcept
Definition Buffer.h:134
Buffer & operator=(Slice s)
Assign from slice.
Definition Buffer.h:90
std::uint8_t * alloc(std::size_t n)
Reallocate the storage.
Definition Buffer.h:154
std::size_t size() const noexcept
Returns the number of bytes in the buffer.
Definition Buffer.h:104
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Buffer.h:128
void clear() noexcept
Reset the buffer.
Definition Buffer.h:144
Buffer(Buffer &&other) noexcept
Move-construct.
Definition Buffer.h:63
Buffer(void const *data, std::size_t size)
Create a buffer as a copy of existing memory.
Definition Buffer.h:37
Buffer()=default
Buffer(Buffer const &other)
Copy-construct.
Definition Buffer.h:44
Buffer & operator=(Buffer const &other)
Copy assign.
Definition Buffer.h:50
An immutable linear range of bytes.
Definition Slice.h:26
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Slice.h:77
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition Slice.h:60
T get(T... args)
T memcmp(T... args)
T memcpy(T... args)
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
T get(Section const &section, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition base_uint.h:552
bool operator!=(Buffer const &lhs, Buffer const &rhs) noexcept
Definition Buffer.h:209
T reset(T... args)