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
29namespace ripple {
30
34class Buffer
35{
36private:
39
40public:
42
43 Buffer() = default;
44
47 : p_(size ? new std::uint8_t[size] : nullptr), size_(size)
48 {
49 }
50
58 {
59 if (size)
61 }
62
64 Buffer(Buffer const& other) : Buffer(other.p_.get(), other.size_)
65 {
66 }
67
69 Buffer&
70 operator=(Buffer const& other)
71 {
72 if (this != &other)
73 {
74 if (auto p = alloc(other.size_))
75 std::memcpy(p, other.p_.get(), size_);
76 }
77 return *this;
78 }
79
83 Buffer(Buffer&& other) noexcept
84 : p_(std::move(other.p_)), size_(other.size_)
85 {
86 other.size_ = 0;
87 }
88
92 Buffer&
93 operator=(Buffer&& other) noexcept
94 {
95 if (this != &other)
96 {
97 p_ = std::move(other.p_);
98 size_ = other.size_;
99 other.size_ = 0;
100 }
101 return *this;
102 }
103
105 explicit Buffer(Slice s) : Buffer(s.data(), s.size())
106 {
107 }
108
110 Buffer&
112 {
113 // Ensure the slice isn't a subset of the buffer.
114 XRPL_ASSERT(
115 s.size() == 0 || size_ == 0 || s.data() < p_.get() ||
116 s.data() >= p_.get() + size_,
117 "ripple::Buffer::operator=(Slice) : input not a subset");
118
119 if (auto p = alloc(s.size()))
120 std::memcpy(p, s.data(), s.size());
121 return *this;
122 }
123
126 size() const noexcept
127 {
128 return size_;
129 }
130
131 bool
132 empty() const noexcept
133 {
134 return 0 == size_;
135 }
136
137 operator Slice() const noexcept
138 {
139 if (!size_)
140 return Slice{};
141 return Slice{p_.get(), size_};
142 }
143
149 std::uint8_t const*
150 data() const noexcept
151 {
152 return p_.get();
153 }
154
156 data() noexcept
157 {
158 return p_.get();
159 }
165 void
166 clear() noexcept
167 {
168 p_.reset();
169 size_ = 0;
170 }
171
177 {
178 if (n != size_)
179 {
180 p_.reset(n ? new std::uint8_t[n] : nullptr);
181 size_ = n;
182 }
183 return p_.get();
184 }
185
186 // Meet the requirements of BufferFactory
187 void*
189 {
190 return alloc(n);
191 }
192
194 begin() const noexcept
195 {
196 return p_.get();
197 }
198
200 cbegin() const noexcept
201 {
202 return p_.get();
203 }
204
206 end() const noexcept
207 {
208 return p_.get() + size_;
209 }
210
212 cend() const noexcept
213 {
214 return p_.get() + size_;
215 }
216};
217
218inline bool
219operator==(Buffer const& lhs, Buffer const& rhs) noexcept
220{
221 if (lhs.size() != rhs.size())
222 return false;
223
224 if (lhs.size() == 0)
225 return true;
226
227 return std::memcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
228}
229
230inline bool
231operator!=(Buffer const& lhs, Buffer const& rhs) noexcept
232{
233 return !(lhs == rhs);
234}
235
236} // namespace ripple
237
238#endif
Like std::vector<char> but better.
Definition: Buffer.h:35
const_iterator cbegin() const noexcept
Definition: Buffer.h:200
void clear() noexcept
Reset the buffer.
Definition: Buffer.h:166
const_iterator cend() const noexcept
Definition: Buffer.h:212
Buffer & operator=(Buffer &&other) noexcept
Move-assign.
Definition: Buffer.h:93
Buffer(std::size_t size)
Create an uninitialized buffer with the given size.
Definition: Buffer.h:46
const_iterator begin() const noexcept
Definition: Buffer.h:194
Buffer(Buffer &&other) noexcept
Move-construct.
Definition: Buffer.h:83
Buffer(Buffer const &other)
Copy-construct.
Definition: Buffer.h:64
std::size_t size() const noexcept
Returns the number of bytes in the buffer.
Definition: Buffer.h:126
const_iterator end() const noexcept
Definition: Buffer.h:206
Buffer()=default
std::uint8_t * data() noexcept
Definition: Buffer.h:156
Buffer(void const *data, std::size_t size)
Create a buffer as a copy of existing memory.
Definition: Buffer.h:57
std::uint8_t * alloc(std::size_t n)
Reallocate the storage.
Definition: Buffer.h:176
std::unique_ptr< std::uint8_t[]> p_
Definition: Buffer.h:37
Buffer & operator=(Slice s)
Assign from slice.
Definition: Buffer.h:111
Buffer(Slice s)
Construct from a slice.
Definition: Buffer.h:105
std::uint8_t const * const_iterator
Definition: Buffer.h:41
void * operator()(std::size_t n)
Definition: Buffer.h:188
bool empty() const noexcept
Definition: Buffer.h:132
std::size_t size_
Definition: Buffer.h:38
Buffer & operator=(Buffer const &other)
Copy assign.
Definition: Buffer.h:70
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition: Buffer.h:150
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:26
bool operator!=(Buffer const &lhs, Buffer const &rhs) noexcept
Definition: Buffer.h:231
T get(Section const &section, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
Definition: BasicConfig.h:355
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)