You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
3.6 KiB
74 lines
3.6 KiB
11 years ago
|
#pragma once
|
||
|
|
||
11 years ago
|
#include <type_traits>
|
||
11 years ago
|
#include <cassert>
|
||
|
#include <vector>
|
||
|
#include <string>
|
||
11 years ago
|
|
||
|
#pragma warning(push)
|
||
11 years ago
|
#pragma warning(disable: 4100 4267)
|
||
11 years ago
|
#include <leveldb/db.h>
|
||
11 years ago
|
#pragma warning(pop)
|
||
11 years ago
|
|
||
|
namespace eth
|
||
|
{
|
||
|
|
||
|
template <class _T>
|
||
|
class vector_ref
|
||
|
{
|
||
|
public:
|
||
|
typedef _T value_type;
|
||
|
typedef _T element_type;
|
||
11 years ago
|
typedef typename std::conditional<std::is_const<_T>::value, typename std::remove_const<_T>::type, _T>::type mutable_value_type;
|
||
11 years ago
|
|
||
|
vector_ref(): m_data(nullptr), m_count(0) {}
|
||
11 years ago
|
vector_ref(_T* _data, size_t _count): m_data(_data), m_count(_count) {}
|
||
11 years ago
|
vector_ref(std::string* _data): m_data((_T*)_data->data()), m_count(_data->size() / sizeof(_T)) {}
|
||
11 years ago
|
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()) {}
|
||
11 years ago
|
vector_ref(typename std::conditional<std::is_const<_T>::value, std::string const&, std::string&>::type _data): m_data((_T*)_data.data()), m_count(_data.size() / sizeof(_T)) {}
|
||
11 years ago
|
vector_ref(leveldb::Slice const& _s): m_data(_s.data()), m_count(_s.size() / sizeof(_T)) {}
|
||
11 years ago
|
|
||
|
explicit operator bool() const { return m_data && m_count; }
|
||
|
|
||
11 years ago
|
bool contentsEqual(std::vector<mutable_value_type> const& _c) const { return _c.size() == m_count && !memcmp(_c.data(), m_data, m_count); }
|
||
|
std::vector<mutable_value_type> toVector() const { return std::vector<mutable_value_type>(m_data, m_data + m_count); }
|
||
11 years ago
|
std::vector<unsigned char> toBytes() const { return std::vector<unsigned char>((unsigned char const*)m_data, m_data + m_count * sizeof(_T)); }
|
||
11 years ago
|
std::string toString() const { return std::string((char const*)m_data, ((char const*)m_data) + m_count); }
|
||
|
template <class _T2> operator vector_ref<_T2>() const { assert(m_count * sizeof(_T) / sizeof(_T2) * sizeof(_T2) / sizeof(_T) == m_count); return vector_ref<_T2>((_T2*)m_data, m_count * sizeof(_T) / sizeof(_T2)); }
|
||
|
|
||
|
_T* data() const { return m_data; }
|
||
11 years ago
|
size_t count() const { return m_count; }
|
||
|
size_t size() const { return m_count; }
|
||
|
bool empty() const { return !m_count; }
|
||
11 years ago
|
vector_ref<_T> next() const { return vector_ref<_T>(m_data + m_count, m_count); }
|
||
11 years ago
|
vector_ref<_T> cropped(size_t _begin, size_t _count = ~size_t(0)) const { if (m_data && _begin + std::max(size_t(0), _count) <= m_count) return vector_ref<_T>(m_data + _begin, _count == ~size_t(0) ? m_count - _begin : _count); else return vector_ref<_T>(); }
|
||
11 years ago
|
void retarget(_T const* _d, size_t _s) { m_data = _d; m_count = _s; }
|
||
|
void retarget(std::vector<_T> const& _t) { m_data = _t.data(); m_count = _t.size(); }
|
||
|
|
||
|
_T* begin() { return m_data; }
|
||
|
_T* end() { return m_data + m_count; }
|
||
|
_T const* begin() const { return m_data; }
|
||
|
_T const* end() const { return m_data + m_count; }
|
||
|
|
||
11 years ago
|
_T& operator[](size_t _i) { assert(m_data); assert(_i < m_count); return m_data[_i]; }
|
||
|
_T const& operator[](size_t _i) const { assert(m_data); assert(_i < m_count); return m_data[_i]; }
|
||
11 years ago
|
|
||
|
bool operator==(vector_ref<_T> const& _cmp) const { return m_data == _cmp.m_data && m_count == _cmp.m_count; }
|
||
|
bool operator!=(vector_ref<_T> const& _cmp) const { return !operator==(); }
|
||
|
|
||
11 years ago
|
operator leveldb::Slice() const { return leveldb::Slice((char const*)m_data, m_count * sizeof(_T)); }
|
||
|
|
||
11 years ago
|
void reset() { m_data = nullptr; m_count = 0; }
|
||
|
|
||
|
private:
|
||
|
_T* m_data;
|
||
11 years ago
|
size_t m_count;
|
||
11 years ago
|
};
|
||
|
|
||
11 years ago
|
template<class _T> vector_ref<_T const> ref(_T const& _t) { return vector_ref<_T const>(&_t, 1); }
|
||
|
template<class _T> vector_ref<_T> ref(_T& _t) { return vector_ref<_T>(&_t, 1); }
|
||
|
template<class _T> vector_ref<_T const> ref(std::vector<_T> const& _t) { return vector_ref<_T const>(&_t); }
|
||
|
template<class _T> vector_ref<_T> ref(std::vector<_T>& _t) { return vector_ref<_T>(&_t); }
|
||
|
|
||
11 years ago
|
}
|