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 
47  explicit
49  : p_ (size ? new std::uint8_t[size] : nullptr)
50  , size_ (size)
51  {
52  }
53 
60  Buffer (void const* data, std::size_t size)
61  : Buffer (size)
62  {
63  if (size)
64  std::memcpy(p_.get(), data, size);
65  }
66 
68  Buffer (Buffer const& other)
69  : Buffer (other.p_.get(), other.size_)
70  {
71  }
72 
74  Buffer& operator= (Buffer const& other)
75  {
76  if (this != &other)
77  {
78  if (auto p = alloc (other.size_))
79  std::memcpy (p, other.p_.get(), size_);
80  }
81  return *this;
82  }
83 
87  Buffer (Buffer&& other) noexcept
88  : p_ (std::move(other.p_))
89  , size_ (other.size_)
90  {
91  other.size_ = 0;
92  }
93 
97  Buffer& operator= (Buffer&& other) noexcept
98  {
99  if (this != &other)
100  {
101  p_ = std::move(other.p_);
102  size_ = other.size_;
103  other.size_ = 0;
104  }
105  return *this;
106  }
107 
109  explicit
111  : Buffer (s.data(), s.size())
112  {
113  }
114 
117  {
118  // Ensure the slice isn't a subset of the buffer.
119  assert (s.size() == 0 || size_ == 0 ||
120  s.data() < p_.get() ||
121  s.data() >= p_.get() + size_);
122 
123  if (auto p = alloc (s.size()))
124  std::memcpy (p, s.data(), s.size());
125  return *this;
126  }
127 
130  size() const noexcept
131  {
132  return size_;
133  }
134 
135  bool
136  empty () const noexcept
137  {
138  return 0 == size_;
139  }
140 
141  operator Slice() const noexcept
142  {
143  if (! size_)
144  return Slice{};
145  return Slice{ p_.get(), size_ };
146  }
147 
153  std::uint8_t const*
154  data() const noexcept
155  {
156  return p_.get();
157  }
158 
159  std::uint8_t*
160  data() noexcept
161  {
162  return p_.get();
163  }
169  void
170  clear() noexcept
171  {
172  p_.reset();
173  size_ = 0;
174  }
175 
179  std::uint8_t*
181  {
182  if (n != size_)
183  {
184  p_.reset(n ? new std::uint8_t[n] : nullptr);
185  size_ = n;
186  }
187  return p_.get();
188  }
189 
190  // Meet the requirements of BufferFactory
191  void*
193  {
194  return alloc(n);
195  }
196 
198  begin() const noexcept
199  {
200  return p_.get();
201  }
202 
204  cbegin() const noexcept
205  {
206  return p_.get();
207  }
208 
210  end() const noexcept
211  {
212  return p_.get() + size_;
213  }
214 
216  cend() const noexcept
217  {
218  return p_.get() + size_;
219  }
220 };
221 
222 inline bool operator==(Buffer const& lhs, Buffer const& rhs) noexcept
223 {
224  if (lhs.size() != rhs.size())
225  return false;
226 
227  if (lhs.size() == 0)
228  return true;
229 
230  return std::memcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
231 }
232 
233 inline bool operator!=(Buffer const& lhs, Buffer const& rhs) noexcept
234 {
235  return !(lhs == rhs);
236 }
237 
238 } // ripple
239 
240 #endif
ripple::Slice::size
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition: Slice.h:77
ripple::Buffer::cbegin
const_iterator cbegin() const noexcept
Definition: Buffer.h:204
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:43
ripple::Buffer::cend
const_iterator cend() const noexcept
Definition: Buffer.h:216
ripple::Slice::data
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition: Slice.h:87
std::unique_ptr::get
T get(T... args)
ripple::Buffer::empty
bool empty() const noexcept
Definition: Buffer.h:136
ripple::Buffer
Like std::vector<char> but better.
Definition: Buffer.h:35
ripple::Buffer::operator()
void * operator()(std::size_t n)
Definition: Buffer.h:192
ripple::Buffer::Buffer
Buffer(Buffer const &other)
Copy-construct.
Definition: Buffer.h:68
ripple::Buffer::Buffer
Buffer(Buffer &&other) noexcept
Move-construct.
Definition: Buffer.h:87
std::unique_ptr::reset
T reset(T... args)
ripple::operator==
bool operator==(Manifest const &lhs, Manifest const &rhs)
Definition: Manifest.h:148
ripple::Buffer::end
const_iterator end() const noexcept
Definition: Buffer.h:210
ripple::Buffer::clear
void clear() noexcept
Reset the buffer.
Definition: Buffer.h:170
ripple::Buffer::Buffer
Buffer(Slice s)
Construct from a slice.
Definition: Buffer.h:110
ripple::operator!=
bool operator!=(Manifest const &lhs, Manifest const &rhs)
Definition: Manifest.h:161
ripple::Buffer::size
std::size_t size() const noexcept
Returns the number of bytes in the buffer.
Definition: Buffer.h:130
cstdint
ripple::Buffer::data
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition: Buffer.h:154
std::uint8_t
ripple::Buffer::data
std::uint8_t * data() noexcept
Definition: Buffer.h:160
memory
ripple::Buffer::begin
const_iterator begin() const noexcept
Definition: Buffer.h:198
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:60
ripple::Buffer::const_iterator
std::uint8_t const * const_iterator
Definition: Buffer.h:42
std
STL namespace.
cassert
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:180
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:48
ripple::Buffer::operator=
Buffer & operator=(Buffer const &other)
Copy assign.
Definition: Buffer.h:74
ripple::Buffer::Buffer
Buffer()=default
ripple::get
T & get(EitherAmount &amt)
Definition: AmountSpec.h:124