rippled
Loading...
Searching...
No Matches
Buffer.h
1#ifndef XRPL_BASICS_BUFFER_H_INCLUDED
2#define XRPL_BASICS_BUFFER_H_INCLUDED
3
4#include <xrpl/basics/Slice.h>
5#include <xrpl/beast/utility/instrumentation.h>
6
7#include <cstdint>
8#include <cstring>
9#include <memory>
10
11namespace ripple {
12
16class Buffer
17{
18private:
21
22public:
24
25 Buffer() = default;
26
29 : p_(size ? new std::uint8_t[size] : nullptr), size_(size)
30 {
31 }
32
40 {
41 if (size)
43 }
44
46 Buffer(Buffer const& other) : Buffer(other.p_.get(), other.size_)
47 {
48 }
49
51 Buffer&
52 operator=(Buffer const& other)
53 {
54 if (this != &other)
55 {
56 if (auto p = alloc(other.size_))
57 std::memcpy(p, other.p_.get(), size_);
58 }
59 return *this;
60 }
61
65 Buffer(Buffer&& other) noexcept
66 : p_(std::move(other.p_)), size_(other.size_)
67 {
68 other.size_ = 0;
69 }
70
74 Buffer&
75 operator=(Buffer&& other) noexcept
76 {
77 if (this != &other)
78 {
79 p_ = std::move(other.p_);
80 size_ = other.size_;
81 other.size_ = 0;
82 }
83 return *this;
84 }
85
87 explicit Buffer(Slice s) : Buffer(s.data(), s.size())
88 {
89 }
90
92 Buffer&
94 {
95 // Ensure the slice isn't a subset of the buffer.
96 XRPL_ASSERT(
97 s.size() == 0 || size_ == 0 || s.data() < p_.get() ||
98 s.data() >= p_.get() + size_,
99 "ripple::Buffer::operator=(Slice) : input not a subset");
100
101 if (auto p = alloc(s.size()))
102 std::memcpy(p, s.data(), s.size());
103 return *this;
104 }
105
108 size() const noexcept
109 {
110 return size_;
111 }
112
113 bool
114 empty() const noexcept
115 {
116 return 0 == size_;
117 }
118
119 operator Slice() const noexcept
120 {
121 if (!size_)
122 return Slice{};
123 return Slice{p_.get(), size_};
124 }
125
131 std::uint8_t const*
132 data() const noexcept
133 {
134 return p_.get();
135 }
136
138 data() noexcept
139 {
140 return p_.get();
141 }
147 void
148 clear() noexcept
149 {
150 p_.reset();
151 size_ = 0;
152 }
153
159 {
160 if (n != size_)
161 {
162 p_.reset(n ? new std::uint8_t[n] : nullptr);
163 size_ = n;
164 }
165 return p_.get();
166 }
167
168 // Meet the requirements of BufferFactory
169 void*
171 {
172 return alloc(n);
173 }
174
176 begin() const noexcept
177 {
178 return p_.get();
179 }
180
182 cbegin() const noexcept
183 {
184 return p_.get();
185 }
186
188 end() const noexcept
189 {
190 return p_.get() + size_;
191 }
192
194 cend() const noexcept
195 {
196 return p_.get() + size_;
197 }
198};
199
200inline bool
201operator==(Buffer const& lhs, Buffer const& rhs) noexcept
202{
203 if (lhs.size() != rhs.size())
204 return false;
205
206 if (lhs.size() == 0)
207 return true;
208
209 return std::memcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
210}
211
212inline bool
213operator!=(Buffer const& lhs, Buffer const& rhs) noexcept
214{
215 return !(lhs == rhs);
216}
217
218} // namespace ripple
219
220#endif
Like std::vector<char> but better.
Definition Buffer.h:17
const_iterator cbegin() const noexcept
Definition Buffer.h:182
void clear() noexcept
Reset the buffer.
Definition Buffer.h:148
const_iterator cend() const noexcept
Definition Buffer.h:194
Buffer & operator=(Buffer &&other) noexcept
Move-assign.
Definition Buffer.h:75
Buffer(std::size_t size)
Create an uninitialized buffer with the given size.
Definition Buffer.h:28
const_iterator begin() const noexcept
Definition Buffer.h:176
Buffer(Buffer &&other) noexcept
Move-construct.
Definition Buffer.h:65
Buffer(Buffer const &other)
Copy-construct.
Definition Buffer.h:46
std::size_t size() const noexcept
Returns the number of bytes in the buffer.
Definition Buffer.h:108
const_iterator end() const noexcept
Definition Buffer.h:188
Buffer()=default
std::uint8_t * data() noexcept
Definition Buffer.h:138
Buffer(void const *data, std::size_t size)
Create a buffer as a copy of existing memory.
Definition Buffer.h:39
std::uint8_t * alloc(std::size_t n)
Reallocate the storage.
Definition Buffer.h:158
std::unique_ptr< std::uint8_t[]> p_
Definition Buffer.h:19
Buffer & operator=(Slice s)
Assign from slice.
Definition Buffer.h:93
Buffer(Slice s)
Construct from a slice.
Definition Buffer.h:87
std::uint8_t const * const_iterator
Definition Buffer.h:23
void * operator()(std::size_t n)
Definition Buffer.h:170
bool empty() const noexcept
Definition Buffer.h:114
std::size_t size_
Definition Buffer.h:20
Buffer & operator=(Buffer const &other)
Copy assign.
Definition Buffer.h:52
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Buffer.h:132
An immutable linear range of bytes.
Definition Slice.h:27
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Slice.h:79
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition Slice.h:62
T get(T... args)
T memcmp(T... args)
T memcpy(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
bool operator!=(Buffer const &lhs, Buffer const &rhs) noexcept
Definition Buffer.h:213
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:566
STL namespace.
T reset(T... args)