rippled
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 <ripple/basics/Slice.h>
24 #include <cassert>
25 #include <cstdint>
26 #include <cstring>
27 #include <memory>
28 #include <utility>
29 
30 namespace ripple {
31 
35 class Buffer
36 {
37 private:
40 
41 public:
42  using const_iterator = std::uint8_t const*;
43 
44  Buffer() = default;
45 
48  : p_(size ? new std::uint8_t[size] : nullptr), size_(size)
49  {
50  }
51 
59  {
60  if (size)
61  std::memcpy(p_.get(), data, 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  assert(
116  s.size() == 0 || size_ == 0 || s.data() < p_.get() ||
117  s.data() >= p_.get() + size_);
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 
155  std::uint8_t*
156  data() noexcept
157  {
158  return p_.get();
159  }
165  void
166  clear() noexcept
167  {
168  p_.reset();
169  size_ = 0;
170  }
171 
175  std::uint8_t*
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 
218 inline bool
219 operator==(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 
230 inline bool
231 operator!=(Buffer const& lhs, Buffer const& rhs) noexcept
232 {
233  return !(lhs == rhs);
234 }
235 
236 } // namespace ripple
237 
238 #endif
ripple::Slice::size
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition: Slice.h:80
ripple::Buffer::cbegin
const_iterator cbegin() const noexcept
Definition: Buffer.h:200
ripple::Buffer::size_
std::size_t size_
Definition: Buffer.h:39
ripple::Dir::const_iterator
Definition: Directory.h:49
utility
cstring
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:44
ripple::Buffer::cend
const_iterator cend() const noexcept
Definition: Buffer.h:212
ripple::Buffer::operator=
Buffer & operator=(Buffer &&other) noexcept
Move-assign.
Definition: Buffer.h:94
ripple::Slice::data
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition: Slice.h:97
std::unique_ptr::get
T get(T... args)
ripple::Buffer::empty
bool empty() const noexcept
Definition: Buffer.h:132
ripple::Buffer
Like std::vector<char> but better.
Definition: Buffer.h:35
ripple::Buffer::operator()
void * operator()(std::size_t n)
Definition: Buffer.h:188
ripple::Buffer::Buffer
Buffer(Buffer const &other)
Copy-construct.
Definition: Buffer.h:65
ripple::Buffer::Buffer
Buffer(Buffer &&other) noexcept
Move-construct.
Definition: Buffer.h:84
std::unique_ptr::reset
T reset(T... args)
ripple::operator==
bool operator==(Manifest const &lhs, Manifest const &rhs)
Definition: Manifest.h:165
ripple::Buffer::end
const_iterator end() const noexcept
Definition: Buffer.h:206
ripple::Buffer::clear
void clear() noexcept
Reset the buffer.
Definition: Buffer.h:166
ripple::Buffer::Buffer
Buffer(Slice s)
Construct from a slice.
Definition: Buffer.h:106
ripple::operator!=
bool operator!=(Manifest const &lhs, Manifest const &rhs)
Definition: Manifest.h:175
ripple::Buffer::size
std::size_t size() const noexcept
Returns the number of bytes in the buffer.
Definition: Buffer.h:126
cstdint
ripple::Buffer::data
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition: Buffer.h:150
std::uint8_t
ripple::Buffer::data
std::uint8_t * data() noexcept
Definition: Buffer.h:156
memory
ripple::Buffer::begin
const_iterator begin() const noexcept
Definition: Buffer.h:194
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Buffer::Buffer
Buffer(void const *data, std::size_t size)
Create a buffer as a copy of existing memory.
Definition: Buffer.h:58
ripple::Buffer::const_iterator
std::uint8_t const * const_iterator
Definition: Buffer.h:42
std
STL namespace.
cassert
ripple::Buffer::operator=
Buffer & operator=(Slice s)
Assign from slice.
Definition: Buffer.h:112
std::size_t
std::memcpy
T memcpy(T... args)
ripple::Buffer::p_
std::unique_ptr< std::uint8_t[]> p_
Definition: Buffer.h:38
std::memcmp
T memcmp(T... args)
ripple::Buffer::alloc
std::uint8_t * alloc(std::size_t n)
Reallocate the storage.
Definition: Buffer.h:176
std::unique_ptr< std::uint8_t[]>
ripple::Buffer::Buffer
Buffer(std::size_t size)
Create an uninitialized buffer with the given size.
Definition: Buffer.h:47
ripple::Buffer::operator=
Buffer & operator=(Buffer const &other)
Copy assign.
Definition: Buffer.h:71
ripple::Buffer::Buffer
Buffer()=default
ripple::get
T & get(EitherAmount &amt)
Definition: AmountSpec.h:118