xrpld
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 <algorithm>
7#include <cstdint>
8#include <cstring>
9#include <memory>
10#include <utility>
11
12namespace xrpl {
13
18class Buffer
19{
20private:
23
24public:
26
27 Buffer() = default;
28
33 : p_((size != 0u) ? new std::uint8_t[size] : nullptr), size_(size)
34 {
35 }
36
45 {
46 if (size != 0u)
47 std::memcpy(p_.get(), data, size);
48 }
49
53 Buffer(Buffer const& other) : Buffer(other.p_.get(), other.size_)
54 {
55 }
56
60 Buffer&
61 operator=(Buffer const& other)
62 {
63 if (this != &other)
64 {
65 if (auto p = alloc(other.size_))
66 std::memcpy(p, other.p_.get(), size_);
67 }
68 return *this;
69 }
70
75 Buffer(Buffer&& other) noexcept : p_(std::move(other.p_)), size_(other.size_)
76 {
77 other.size_ = 0;
78 }
79
84 Buffer&
85 operator=(Buffer&& other) noexcept
86 {
87 if (this != &other)
88 {
89 p_ = std::move(other.p_);
90 size_ = other.size_;
91 other.size_ = 0;
92 }
93 return *this;
94 }
95
99 explicit Buffer(Slice s) : Buffer(s.data(), s.size())
100 {
101 }
102
106 Buffer&
108 {
109 // Ensure the slice isn't a subset of the buffer.
110 XRPL_ASSERT(
111 s.empty() || size_ == 0 || s.data() < p_.get() || s.data() >= p_.get() + size_,
112 "xrpl::Buffer::operator=(Slice) : input not a subset");
113
114 if (auto p = alloc(s.size()))
115 std::memcpy(p, s.data(), s.size());
116 return *this;
117 }
118
122 [[nodiscard]] std::size_t
123 size() const noexcept
124 {
125 return size_;
126 }
127
128 [[nodiscard]] bool
129 empty() const noexcept
130 {
131 return 0 == size_;
132 }
133
134 operator Slice() const noexcept
135 {
136 if (size_ == 0u)
137 return Slice{};
138 return Slice{p_.get(), size_};
139 }
140
147 [[nodiscard]] std::uint8_t const*
148 data() const noexcept
149 {
150 return p_.get();
151 }
152
154 data() noexcept
155 {
156 return p_.get();
157 }
158
159
167 void
168 fill(std::uint8_t value) noexcept
169 {
170 std::fill_n(p_.get(), size_, value);
171 }
172
177 void
178 clear() noexcept
179 {
180 p_.reset();
181 size_ = 0;
182 }
183
190 {
191 if (n != size_)
192 {
193 p_.reset((n != 0u) ? new std::uint8_t[n] : nullptr);
194 size_ = n;
195 }
196 return p_.get();
197 }
198
199 // Meet the requirements of BufferFactory
200 void*
202 {
203 return alloc(n);
204 }
205
206 [[nodiscard]] const_iterator
207 begin() const noexcept
208 {
209 return p_.get();
210 }
211
212 [[nodiscard]] const_iterator
213 cbegin() const noexcept
214 {
215 return p_.get();
216 }
217
218 [[nodiscard]] const_iterator
219 end() const noexcept
220 {
221 return p_.get() + size_;
222 }
223
224 [[nodiscard]] const_iterator
225 cend() const noexcept
226 {
227 return p_.get() + size_;
228 }
229};
230
231inline bool
232operator==(Buffer const& lhs, Buffer const& rhs) noexcept
233{
234 if (lhs.size() != rhs.size())
235 return false;
236
237 if (lhs.empty())
238 return true;
239
240 return std::memcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
241}
242
243} // namespace xrpl
Like std::vector<char> but better.
Definition Buffer.h:19
const_iterator end() const noexcept
Definition Buffer.h:219
Buffer(std::size_t size)
Create an uninitialized buffer with the given size.
Definition Buffer.h:32
std::uint8_t const * const_iterator
Definition Buffer.h:25
std::unique_ptr< std::uint8_t[]> p_
Definition Buffer.h:21
bool empty() const noexcept
Definition Buffer.h:129
Buffer(Slice s)
Construct from a slice.
Definition Buffer.h:99
void * operator()(std::size_t n)
Definition Buffer.h:201
Buffer & operator=(Buffer &&other) noexcept
Move-assign.
Definition Buffer.h:85
const_iterator cend() const noexcept
Definition Buffer.h:225
const_iterator cbegin() const noexcept
Definition Buffer.h:213
std::size_t size_
Definition Buffer.h:22
const_iterator begin() const noexcept
Definition Buffer.h:207
std::uint8_t * data() noexcept
Definition Buffer.h:154
Buffer & operator=(Slice s)
Assign from slice.
Definition Buffer.h:107
std::uint8_t * alloc(std::size_t n)
Reallocate the storage.
Definition Buffer.h:189
std::size_t size() const noexcept
Returns the number of bytes in the buffer.
Definition Buffer.h:123
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Buffer.h:148
void clear() noexcept
Reset the buffer.
Definition Buffer.h:178
Buffer(Buffer &&other) noexcept
Move-construct.
Definition Buffer.h:75
Buffer(void const *data, std::size_t size)
Create a buffer as a copy of existing memory.
Definition Buffer.h:44
Buffer()=default
Buffer(Buffer const &other)
Copy-construct.
Definition Buffer.h:53
Buffer & operator=(Buffer const &other)
Copy assign.
Definition Buffer.h:61
void fill(std::uint8_t value) noexcept
Set every byte in the buffer to the given value.
Definition Buffer.h:168
An immutable linear range of bytes.
Definition Slice.h:28
bool empty() const noexcept
Return true if the byte range is empty.
Definition Slice.h:58
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Slice.h:88
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition Slice.h:70
T fill_n(T... args)
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
constexpr bool operator==(BaseUInt< Bits, Tag > const &lhs, BaseUInt< Bits, Tag > const &rhs)
Definition base_uint.h:606
T get(Section const &section, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
Dir::ConstIterator const_iterator
Definition Dir.cpp:16