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 <cstdint>
7#include <cstring>
8#include <memory>
9
10namespace xrpl {
11
15class Buffer
16{
17private:
20
21public:
23
24 Buffer() = default;
25
28 : p_((size != 0u) ? new std::uint8_t[size] : nullptr), size_(size)
29 {
30 }
31
39 {
40 if (size != 0u)
41 std::memcpy(p_.get(), data, size);
42 }
43
45 Buffer(Buffer const& other) : Buffer(other.p_.get(), other.size_)
46 {
47 }
48
50 Buffer&
51 operator=(Buffer const& other)
52 {
53 if (this != &other)
54 {
55 if (auto p = alloc(other.size_))
56 std::memcpy(p, other.p_.get(), size_);
57 }
58 return *this;
59 }
60
64 Buffer(Buffer&& other) noexcept : p_(std::move(other.p_)), size_(other.size_)
65 {
66 other.size_ = 0;
67 }
68
72 Buffer&
73 operator=(Buffer&& other) noexcept
74 {
75 if (this != &other)
76 {
77 p_ = std::move(other.p_);
78 size_ = other.size_;
79 other.size_ = 0;
80 }
81 return *this;
82 }
83
85 explicit Buffer(Slice s) : Buffer(s.data(), s.size())
86 {
87 }
88
90 Buffer&
92 {
93 // Ensure the slice isn't a subset of the buffer.
94 XRPL_ASSERT(
95 s.empty() || size_ == 0 || s.data() < p_.get() || s.data() >= p_.get() + size_,
96 "xrpl::Buffer::operator=(Slice) : input not a subset");
97
98 if (auto p = alloc(s.size()))
99 std::memcpy(p, s.data(), s.size());
100 return *this;
101 }
102
104 [[nodiscard]] std::size_t
105 size() const noexcept
106 {
107 return size_;
108 }
109
110 [[nodiscard]] bool
111 empty() const noexcept
112 {
113 return 0 == size_;
114 }
115
116 operator Slice() const noexcept
117 {
118 if (size_ == 0u)
119 return Slice{};
120 return Slice{p_.get(), size_};
121 }
122
128 [[nodiscard]] std::uint8_t const*
129 data() const noexcept
130 {
131 return p_.get();
132 }
133
135 data() noexcept
136 {
137 return p_.get();
138 }
139
140
144 void
145 clear() noexcept
146 {
147 p_.reset();
148 size_ = 0;
149 }
150
156 {
157 if (n != size_)
158 {
159 p_.reset((n != 0u) ? new std::uint8_t[n] : nullptr);
160 size_ = n;
161 }
162 return p_.get();
163 }
164
165 // Meet the requirements of BufferFactory
166 void*
168 {
169 return alloc(n);
170 }
171
172 [[nodiscard]] const_iterator
173 begin() const noexcept
174 {
175 return p_.get();
176 }
177
178 [[nodiscard]] const_iterator
179 cbegin() const noexcept
180 {
181 return p_.get();
182 }
183
184 [[nodiscard]] const_iterator
185 end() const noexcept
186 {
187 return p_.get() + size_;
188 }
189
190 [[nodiscard]] const_iterator
191 cend() const noexcept
192 {
193 return p_.get() + size_;
194 }
195};
196
197inline bool
198operator==(Buffer const& lhs, Buffer const& rhs) noexcept
199{
200 if (lhs.size() != rhs.size())
201 return false;
202
203 if (lhs.empty())
204 return true;
205
206 return std::memcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
207}
208
209inline bool
210operator!=(Buffer const& lhs, Buffer const& rhs) noexcept
211{
212 return !(lhs == rhs);
213}
214
215} // namespace xrpl
Like std::vector<char> but better.
Definition Buffer.h:16
const_iterator end() const noexcept
Definition Buffer.h:185
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:111
Buffer(Slice s)
Construct from a slice.
Definition Buffer.h:85
void * operator()(std::size_t n)
Definition Buffer.h:167
Buffer & operator=(Buffer &&other) noexcept
Move-assign.
Definition Buffer.h:73
const_iterator cend() const noexcept
Definition Buffer.h:191
const_iterator cbegin() const noexcept
Definition Buffer.h:179
std::size_t size_
Definition Buffer.h:19
const_iterator begin() const noexcept
Definition Buffer.h:173
std::uint8_t * data() noexcept
Definition Buffer.h:135
Buffer & operator=(Slice s)
Assign from slice.
Definition Buffer.h:91
std::uint8_t * alloc(std::size_t n)
Reallocate the storage.
Definition Buffer.h:155
std::size_t size() const noexcept
Returns the number of bytes in the buffer.
Definition Buffer.h:105
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Buffer.h:129
void clear() noexcept
Reset the buffer.
Definition Buffer.h:145
Buffer(Buffer &&other) noexcept
Move-construct.
Definition Buffer.h:64
Buffer(void const *data, std::size_t size)
Create a buffer as a copy of existing memory.
Definition Buffer.h:38
Buffer()=default
Buffer(Buffer const &other)
Copy-construct.
Definition Buffer.h:45
Buffer & operator=(Buffer const &other)
Copy assign.
Definition Buffer.h:51
An immutable linear range of bytes.
Definition Slice.h:26
bool empty() const noexcept
Return true if the byte range is empty.
Definition Slice.h:50
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Slice.h:78
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition Slice.h:61
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:588
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
bool operator!=(Buffer const &lhs, Buffer const &rhs) noexcept
Definition Buffer.h:210