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 <vector>
33 #include <type_traits>
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& operator= (Slice const&) noexcept = default;
57 
59  Slice (void const* data, std::size_t size) noexcept
60  : data_ (reinterpret_cast<std::uint8_t const*>(data))
61  , 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  }
122  begin() const noexcept
123  {
124  return data_;
125  }
126 
128  cbegin() const noexcept
129  {
130  return data_;
131  }
132 
134  end() const noexcept
135  {
136  return data_ + size_;
137  }
138 
140  cend() const noexcept
141  {
142  return data_ + size_;
143  }
144 };
145 
146 //------------------------------------------------------------------------------
147 
148 template <class Hasher>
149 inline
150 void
151 hash_append (Hasher& h, Slice const& v)
152 {
153  h(v.data(), v.size());
154 }
155 
156 inline
157 bool
158 operator== (Slice const& lhs, Slice const& rhs) noexcept
159 {
160  if (lhs.size() != rhs.size())
161  return false;
162 
163  if (lhs.size() == 0)
164  return true;
165 
166  return std::memcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
167 }
168 
169 inline
170 bool
171 operator!= (Slice const& lhs, Slice const& rhs) noexcept
172 {
173  return !(lhs == rhs);
174 }
175 
176 inline
177 bool
178 operator< (Slice const& lhs, Slice const& rhs) noexcept
179 {
181  lhs.data(), lhs.data() + lhs.size(),
182  rhs.data(), rhs.data() + rhs.size());
183 }
184 
185 
186 template <class Stream>
187 Stream& operator<<(Stream& s, Slice const& v)
188 {
189  s << strHex(v);
190  return s;
191 }
192 
193 template <class T, std::size_t N>
197  Slice
198 >
200 {
201  return Slice(a.data(), a.size());
202 }
203 
204 template <class T, class Alloc>
208  Slice
209 >
211 {
212  return Slice(v.data(), v.size());
213 }
214 
215 template <class Traits, class Alloc>
216 Slice
218 {
219  return Slice(s.data(), s.size());
220 }
221 
222 } // ripple
223 
224 #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:199
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:140
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:134
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:237
algorithm
ripple::operator==
bool operator==(Manifest const &lhs, Manifest const &rhs)
Definition: Manifest.h:148
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:161
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:128
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:151
std::size_t
ripple::strHex
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:70
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:456
std::array::data
T data(T... args)
type_traits
ripple::Slice::begin
const_iterator begin() const noexcept
Definition: Slice.h:122
string