Fabcoin Core  0.16.2
P2P Digital Currency
vector_ref.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstring>
4 #include <cassert>
5 #include <type_traits>
6 #include <vector>
7 #include <string>
8 
9 #ifdef __INTEL_COMPILER
10 #pragma warning(disable:597) //will not be called for implicit or explicit conversions
11 #endif
12 
13 namespace dev
14 {
15 
19 template <class _T>
21 {
22 public:
23  using value_type = _T;
24  using element_type = _T;
25  using mutable_value_type = typename std::conditional<std::is_const<_T>::value, typename std::remove_const<_T>::type, _T>::type;
26 
27  static_assert(std::is_pod<value_type>::value, "vector_ref can only be used with PODs due to its low-level treatment of data.");
28 
31  vector_ref(_T* _data, size_t _count): m_data(_data), m_count(_count) {}
33  vector_ref(typename std::conditional<std::is_const<_T>::value, std::string const*, std::string*>::type _data): m_data(reinterpret_cast<_T*>(_data->data())), m_count(_data->size() / sizeof(_T)) {}
35  vector_ref(typename std::conditional<std::is_const<_T>::value, std::vector<typename std::remove_const<_T>::type> const*, std::vector<_T>*>::type _data): m_data(_data->data()), m_count(_data->size()) {}
37  vector_ref(typename std::conditional<std::is_const<_T>::value, std::string const&, std::string&>::type _data): m_data(reinterpret_cast<_T*>(_data.data())), m_count(_data.size() / sizeof(_T)) {}
38 #if DEV_LDB
39  vector_ref(ldb::Slice const& _s): m_data(reinterpret_cast<_T*>(_s.data())), m_count(_s.size() / sizeof(_T)) {}
40 #endif
41  explicit operator bool() const { return m_data && m_count; }
42 
43  bool contentsEqual(std::vector<mutable_value_type> const& _c) const { if (!m_data || m_count == 0) return _c.empty(); else return _c.size() == m_count && !memcmp(_c.data(), m_data, m_count * sizeof(_T)); }
44  std::vector<mutable_value_type> toVector() const { return std::vector<mutable_value_type>(m_data, m_data + m_count); }
45  std::vector<unsigned char> toBytes() const { return std::vector<unsigned char>(reinterpret_cast<unsigned char const*>(m_data), reinterpret_cast<unsigned char const*>(m_data) + m_count * sizeof(_T)); }
46  std::string toString() const { return std::string((char const*)m_data, ((char const*)m_data) + m_count * sizeof(_T)); }
47 
48  template <class _T2> explicit operator vector_ref<_T2>() const { assert(m_count * sizeof(_T) / sizeof(_T2) * sizeof(_T2) / sizeof(_T) == m_count); return vector_ref<_T2>(reinterpret_cast<_T2*>(m_data), m_count * sizeof(_T) / sizeof(_T2)); }
50 
51  _T* data() const { return m_data; }
53  size_t count() const { return m_count; }
55  size_t size() const { return m_count; }
56  bool empty() const { return !m_count; }
58  vector_ref<_T> next() const { if (!m_data) return *this; else return vector_ref<_T>(m_data + m_count, m_count); }
62  vector_ref<_T> cropped(size_t _begin, size_t _count) const { if (m_data && _begin <= m_count && _count <= m_count && _begin + _count <= m_count) return vector_ref<_T>(m_data + _begin, _count == ~size_t(0) ? m_count - _begin : _count); else return vector_ref<_T>(); }
64  vector_ref<_T> cropped(size_t _begin) const { if (m_data && _begin <= m_count) return vector_ref<_T>(m_data + _begin, m_count - _begin); else return vector_ref<_T>(); }
65  void retarget(_T* _d, size_t _s) { m_data = _d; m_count = _s; }
66  void retarget(std::vector<_T> const& _t) { m_data = _t.data(); m_count = _t.size(); }
67  template <class T> bool overlapsWith(vector_ref<T> _t) const { void const* f1 = data(); void const* t1 = data() + size(); void const* f2 = _t.data(); void const* t2 = _t.data() + _t.size(); return f1 < t2 && t1 > f2; }
69  void copyTo(vector_ref<typename std::remove_const<_T>::type> _t) const { if (overlapsWith(_t)) memmove(_t.data(), m_data, std::min(_t.size(), m_count) * sizeof(_T)); else memcpy(_t.data(), m_data, std::min(_t.size(), m_count) * sizeof(_T)); }
71  void populate(vector_ref<typename std::remove_const<_T>::type> _t) const { copyTo(_t); memset(_t.data() + m_count, 0, std::max(_t.size(), m_count) - m_count); }
74  void cleanse()
75  {
76  static unsigned char s_cleanseCounter = 0;
77  uint8_t* p = (uint8_t*)begin();
78  size_t const len = (uint8_t*)end() - p;
79  size_t loop = len;
80  size_t count = s_cleanseCounter;
81  while (loop--)
82  {
83  *(p++) = (uint8_t)count;
84  count += (17 + ((size_t)p & 0xf));
85  }
86  p = (uint8_t*)memchr((uint8_t*)begin(), (uint8_t)count, len);
87  if (p)
88  count += (63 + (size_t)p);
89  s_cleanseCounter = (uint8_t)count;
90  memset((uint8_t*)begin(), 0, len);
91  }
92 
93  _T* begin() { return m_data; }
94  _T* end() { return m_data + m_count; }
95  _T const* begin() const { return m_data; }
96  _T const* end() const { return m_data + m_count; }
97 
98  _T& operator[](size_t _i) { assert(m_data); assert(_i < m_count); return m_data[_i]; }
99  _T const& operator[](size_t _i) const { assert(m_data); assert(_i < m_count); return m_data[_i]; }
100 
101  bool operator==(vector_ref<_T> const& _cmp) const { return m_data == _cmp.m_data && m_count == _cmp.m_count; }
102  bool operator!=(vector_ref<_T> const& _cmp) const { return !operator==(_cmp); }
103 
104 #if DEV_LDB
105  operator ldb::Slice() const { return ldb::Slice((char const*)m_data, m_count * sizeof(_T)); }
106 #endif
107 
108  void reset() { m_data = nullptr; m_count = 0; }
109 
110 private:
111  _T* m_data;
112  size_t m_count;
113 };
114 
115 template<class _T> vector_ref<_T const> ref(_T const& _t) { return vector_ref<_T const>(&_t, 1); }
116 template<class _T> vector_ref<_T> ref(_T& _t) { return vector_ref<_T>(&_t, 1); }
117 template<class _T> vector_ref<_T const> ref(std::vector<_T> const& _t) { return vector_ref<_T const>(&_t); }
118 template<class _T> vector_ref<_T> ref(std::vector<_T>& _t) { return vector_ref<_T>(&_t); }
119 
120 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
_T & operator[](size_t _i)
Definition: vector_ref.h:98
#define f1(l, r, km, kr)
Definition: cast.cpp:17
A modifiable reference to an existing object or vector in memory.
Definition: vector_ref.h:20
vector_ref< _T const > ref(_T const &_t)
Definition: vector_ref.h:115
#define f2(l, r, km, kr)
Definition: cast.cpp:21
void cleanse()
Securely overwrite the memory.
Definition: vector_ref.h:74
vector_ref(_T *_data, size_t _count)
Creates a new vector_ref to point to _count elements starting at _data.
Definition: vector_ref.h:31
assert(len-trim+(2 *lenIndices)<=WIDTH)
vector_ref< _T > cropped(size_t _begin) const
Definition: vector_ref.h:64
size_t count() const
Definition: vector_ref.h:53
vector_ref< _T > cropped(size_t _begin, size_t _count) const
Definition: vector_ref.h:62
ExecStats::duration min
Definition: ExecStats.cpp:35
#define nullptr
Definition: eqcuda.hpp:22
bool contentsEqual(std::vector< mutable_value_type > const &_c) const
Definition: vector_ref.h:43
vector_ref(typename std::conditional< std::is_const< _T >::value, std::string const *, std::string * >::type _data)
Creates a new vector_ref pointing to the data part of a string (given as pointer).
Definition: vector_ref.h:33
std::vector< mutable_value_type > toVector() const
Definition: vector_ref.h:44
bool operator==(vector_ref< _T > const &_cmp) const
Definition: vector_ref.h:101
bool empty() const
Definition: vector_ref.h:56
void retarget(_T *_d, size_t _s)
Definition: vector_ref.h:65
std::vector< unsigned char > toBytes() const
Definition: vector_ref.h:45
#define t1
vector_ref< _T > next() const
Definition: vector_ref.h:58
size_t size() const
Definition: vector_ref.h:55
#define t2
typename std::conditional< std::is_const< byte const >::value, typename std::remove_const< byte const >::type, byte const >::type mutable_value_type
Definition: vector_ref.h:25
void populate(vector_ref< typename std::remove_const< _T >::type > _t) const
Copies the contents of this vector_ref to the contents of _t, and zeros further trailing elements in ...
Definition: vector_ref.h:71
bool operator!=(vector_ref< _T > const &_cmp) const
Definition: vector_ref.h:102
bool overlapsWith(vector_ref< T > _t) const
Definition: vector_ref.h:67
_T * data() const
Definition: vector_ref.h:51
PlatformStyle::TableColorType type
Definition: rpcconsole.cpp:61
void retarget(std::vector< _T > const &_t)
Definition: vector_ref.h:66
void * memcpy(void *a, const void *b, size_t c)
void copyTo(vector_ref< typename std::remove_const< _T >::type > _t) const
Copies the contents of this vector_ref to the contents of _t, up to the max size of _t...
Definition: vector_ref.h:69
vector_ref(typename std::conditional< std::is_const< _T >::value, std::string const &, std::string & >::type _data)
Creates a new vector_ref pointing to the data part of a string (given as reference).
Definition: vector_ref.h:37
_T const * end() const
Definition: vector_ref.h:96
dev::WithExisting max(dev::WithExisting _a, dev::WithExisting _b)
Definition: Common.h:326
vector_ref(typename std::conditional< std::is_const< _T >::value, std::vector< typename std::remove_const< _T >::type > const *, std::vector< _T > * >::type _data)
Creates a new vector_ref pointing to the data part of a vector (given as pointer).
Definition: vector_ref.h:35
_T const * begin() const
Definition: vector_ref.h:95
void * memmove(void *a, const void *b, size_t c)
std::string toString() const
Definition: vector_ref.h:46
_T const & operator[](size_t _i) const
Definition: vector_ref.h:99