rippled
Loading...
Searching...
No Matches
Buffer.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_BASICS_BUFFER_H_INCLUDED
21#define RIPPLE_BASICS_BUFFER_H_INCLUDED
22
23#include <xrpl/basics/Slice.h>
24#include <xrpl/beast/utility/instrumentation.h>
25
26#include <cstdint>
27#include <cstring>
28#include <memory>
29
30namespace ripple {
31
35class Buffer
36{
37private:
40
41public:
43
44 Buffer() = default;
45
48 : p_(size ? new std::uint8_t[size] : nullptr), size_(size)
49 {
50 }
51
59 {
60 if (size)
62 }
63
65 Buffer(Buffer const& other) : Buffer(other.p_.get(), other.size_)
66 {
67 }
68
70 Buffer&
71 operator=(Buffer const& other)
72 {
73 if (this != &other)
74 {
75 if (auto p = alloc(other.size_))
76 std::memcpy(p, other.p_.get(), size_);
77 }
78 return *this;
79 }
80
84 Buffer(Buffer&& other) noexcept
85 : p_(std::move(other.p_)), size_(other.size_)
86 {
87 other.size_ = 0;
88 }
89
93 Buffer&
94 operator=(Buffer&& other) noexcept
95 {
96 if (this != &other)
97 {
98 p_ = std::move(other.p_);
99 size_ = other.size_;
100 other.size_ = 0;
101 }
102 return *this;
103 }
104
106 explicit Buffer(Slice s) : Buffer(s.data(), s.size())
107 {
108 }
109
111 Buffer&
113 {
114 // Ensure the slice isn't a subset of the buffer.
115 XRPL_ASSERT(
116 s.size() == 0 || size_ == 0 || s.data() < p_.get() ||
117 s.data() >= p_.get() + size_,
118 "ripple::Buffer::operator=(Slice) : input not a subset");
119
120 if (auto p = alloc(s.size()))
121 std::memcpy(p, s.data(), s.size());
122 return *this;
123 }
124
127 size() const noexcept
128 {
129 return size_;
130 }
131
132 bool
133 empty() const noexcept
134 {
135 return 0 == size_;
136 }
137
138 operator Slice() const noexcept
139 {
140 if (!size_)
141 return Slice{};
142 return Slice{p_.get(), size_};
143 }
144
150 std::uint8_t const*
151 data() const noexcept
152 {
153 return p_.get();
154 }
155
157 data() noexcept
158 {
159 return p_.get();
160 }
166 void
167 clear() noexcept
168 {
169 p_.reset();
170 size_ = 0;
171 }
172
178 {
179 if (n != size_)
180 {
181 p_.reset(n ? new std::uint8_t[n] : nullptr);
182 size_ = n;
183 }
184 return p_.get();
185 }
186
187 // Meet the requirements of BufferFactory
188 void*
190 {
191 return alloc(n);
192 }
193
195 begin() const noexcept
196 {
197 return p_.get();
198 }
199
201 cbegin() const noexcept
202 {
203 return p_.get();
204 }
205
207 end() const noexcept
208 {
209 return p_.get() + size_;
210 }
211
213 cend() const noexcept
214 {
215 return p_.get() + size_;
216 }
217};
218
219inline bool
220operator==(Buffer const& lhs, Buffer const& rhs) noexcept
221{
222 if (lhs.size() != rhs.size())
223 return false;
224
225 if (lhs.size() == 0)
226 return true;
227
228 return std::memcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
229}
230
231inline bool
232operator!=(Buffer const& lhs, Buffer const& rhs) noexcept
233{
234 return !(lhs == rhs);
235}
236
237} // namespace ripple
238
239#endif
Like std::vector<char> but better.
Definition Buffer.h:36
const_iterator cbegin() const noexcept
Definition Buffer.h:201
void clear() noexcept
Reset the buffer.
Definition Buffer.h:167
const_iterator cend() const noexcept
Definition Buffer.h:213
Buffer & operator=(Buffer &&other) noexcept
Move-assign.
Definition Buffer.h:94
Buffer(std::size_t size)
Create an uninitialized buffer with the given size.
Definition Buffer.h:47
const_iterator begin() const noexcept
Definition Buffer.h:195
Buffer(Buffer &&other) noexcept
Move-construct.
Definition Buffer.h:84
Buffer(Buffer const &other)
Copy-construct.
Definition Buffer.h:65
std::size_t size() const noexcept
Returns the number of bytes in the buffer.
Definition Buffer.h:127
const_iterator end() const noexcept
Definition Buffer.h:207
Buffer()=default
std::uint8_t * data() noexcept
Definition Buffer.h:157
Buffer(void const *data, std::size_t size)
Create a buffer as a copy of existing memory.
Definition Buffer.h:58
std::uint8_t * alloc(std::size_t n)
Reallocate the storage.
Definition Buffer.h:177
std::unique_ptr< std::uint8_t[]> p_
Definition Buffer.h:38
Buffer & operator=(Slice s)
Assign from slice.
Definition Buffer.h:112
Buffer(Slice s)
Construct from a slice.
Definition Buffer.h:106
std::uint8_t const * const_iterator
Definition Buffer.h:42
void * operator()(std::size_t n)
Definition Buffer.h:189
bool empty() const noexcept
Definition Buffer.h:133
std::size_t size_
Definition Buffer.h:39
Buffer & operator=(Buffer const &other)
Copy assign.
Definition Buffer.h:71
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Buffer.h:151
An immutable linear range of bytes.
Definition Slice.h:46
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Slice.h:98
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition Slice.h:81
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:25
bool operator!=(Buffer const &lhs, Buffer const &rhs) noexcept
Definition Buffer.h:232
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:585
STL namespace.
T reset(T... args)