rippled
Slice.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_SLICE_H_INCLUDED
21 #define RIPPLE_BASICS_SLICE_H_INCLUDED
22 
23 #include <ripple/basics/contract.h>
24 #include <ripple/basics/strHex.h>
25 #include <algorithm>
26 #include <array>
27 #include <cassert>
28 #include <cstdint>
29 #include <cstring>
30 #include <stdexcept>
31 #include <string>
32 #include <type_traits>
33 #include <vector>
34 
35 namespace ripple {
36 
43 class Slice
44 {
45 private:
46  std::uint8_t const* data_ = nullptr;
48 
49 public:
50  using const_iterator = std::uint8_t const*;
51 
53  Slice() noexcept = default;
54 
55  Slice(Slice const&) noexcept = default;
56  Slice&
57  operator=(Slice const&) noexcept = default;
58 
60  Slice(void const* data, std::size_t size) noexcept
61  : data_(reinterpret_cast<std::uint8_t const*>(data)), size_(size)
62  {
63  }
64 
66  bool
67  empty() const noexcept
68  {
69  return size_ == 0;
70  }
71 
77  size() const noexcept
78  {
79  return size_;
80  }
81 
86  std::uint8_t const*
87  data() const noexcept
88  {
89  return data_;
90  }
91 
94  operator[](std::size_t i) const noexcept
95  {
96  assert(i < size_);
97  return data_[i];
98  }
99 
102  Slice&
104  {
105  if (n > size_)
106  Throw<std::domain_error>("too small");
107  data_ += n;
108  size_ -= n;
109  return *this;
110  }
111 
112  Slice
114  {
115  Slice temp = *this;
116  return temp += n;
117  }
121  begin() const noexcept
122  {
123  return data_;
124  }
125 
127  cbegin() const noexcept
128  {
129  return data_;
130  }
131 
133  end() const noexcept
134  {
135  return data_ + size_;
136  }
137 
139  cend() const noexcept
140  {
141  return data_ + size_;
142  }
143 };
144 
145 //------------------------------------------------------------------------------
146 
147 template <class Hasher>
148 inline void
149 hash_append(Hasher& h, Slice const& v)
150 {
151  h(v.data(), v.size());
152 }
153 
154 inline bool
155 operator==(Slice const& lhs, Slice const& rhs) noexcept
156 {
157  if (lhs.size() != rhs.size())
158  return false;
159 
160  if (lhs.size() == 0)
161  return true;
162 
163  return std::memcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
164 }
165 
166 inline bool
167 operator!=(Slice const& lhs, Slice const& rhs) noexcept
168 {
169  return !(lhs == rhs);
170 }
171 
172 inline bool
173 operator<(Slice const& lhs, Slice const& rhs) noexcept
174 {
176  lhs.data(),
177  lhs.data() + lhs.size(),
178  rhs.data(),
179  rhs.data() + rhs.size());
180 }
181 
182 template <class Stream>
183 Stream&
184 operator<<(Stream& s, Slice const& v)
185 {
186  s << strHex(v);
187  return s;
188 }
189 
190 template <class T, std::size_t N>
193  Slice>
195 {
196  return Slice(a.data(), a.size());
197 }
198 
199 template <class T, class Alloc>
202  Slice>
204 {
205  return Slice(v.data(), v.size());
206 }
207 
208 template <class Traits, class Alloc>
209 Slice
211 {
212  return Slice(s.data(), s.size());
213 }
214 
215 } // namespace ripple
216 
217 #endif
ripple::Slice::size
std::size_t size() const noexcept
Returns the number of bytes in the storage.
Definition: Slice.h:77
ripple::Slice::operator+=
Slice & operator+=(std::size_t n)
Advance the buffer.
Definition: Slice.h:103
std::is_same
ripple::makeSlice
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
Definition: Slice.h:194
ripple::Dir::const_iterator
Definition: Directory.h:49
std::basic_string
STL class.
cstring
ripple::Slice::Slice
Slice() noexcept=default
Default constructed Slice has length 0.
ripple::Slice::cend
const_iterator cend() const noexcept
Definition: Slice.h:139
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:43
ripple::Slice::operator+
Slice operator+(std::size_t n) const
Definition: Slice.h:113
vector
std::array::size
T size(T... args)
ripple::Slice::end
const_iterator end() const noexcept
Definition: Slice.h:133
ripple::Slice::data_
std::uint8_t const * data_
Definition: Slice.h:46
ripple::Slice::data
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition: Slice.h:87
ripple::Slice::operator[]
std::uint8_t operator[](std::size_t i) const noexcept
Access raw bytes.
Definition: Slice.h:94
ripple::Slice::empty
bool empty() const noexcept
Return true if the byte range is empty.
Definition: Slice.h:67
ripple::operator<<
std::ostream & operator<<(std::ostream &os, TOffer< TIn, TOut > const &offer)
Definition: Offer.h:242
algorithm
ripple::operator==
bool operator==(Manifest const &lhs, Manifest const &rhs)
Definition: Manifest.h:155
stdexcept
ripple::Slice::size_
std::size_t size_
Definition: Slice.h:47
std::enable_if_t
std::lexicographical_compare
T lexicographical_compare(T... args)
ripple::operator!=
bool operator!=(Manifest const &lhs, Manifest const &rhs)
Definition: Manifest.h:165
array
cstdint
std::uint8_t
ripple::Slice::const_iterator
std::uint8_t const * const_iterator
Definition: Slice.h:50
ripple::Slice::cbegin
const_iterator cbegin() const noexcept
Definition: Slice.h:127
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std
STL namespace.
cassert
ripple::hash_append
void hash_append(Hasher &h, Slice const &v)
Definition: Slice.h:149
std::size_t
ripple::strHex
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:67
std::memcmp
T memcmp(T... args)
ripple::operator<
bool operator<(base_uint< Bits, Tag > const &a, base_uint< Bits, Tag > const &b)
Definition: base_uint.h:514
std::array::data
T data(T... args)
type_traits
ripple::Slice::begin
const_iterator begin() const noexcept
Definition: Slice.h:121
string