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 solidity::util 14 { 15 16 /** 17 * A modifiable reference to an existing object or vector in memory. 18 */ 19 template <class T> 20 class vector_ref 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 using string_type = typename std::conditional<std::is_const<T>::value, std::string const, std::string>::type; 27 using vector_type = typename std::conditional<std::is_const<T>::value, std::vector<typename std::remove_const<T>::type> const, std::vector<T>>::type; 28 using iterator = T*; 29 using const_iterator = T const*; 30 31 static_assert(std::is_pod<value_type>::value, "vector_ref can only be used with PODs due to its low-level treatment of data."); 32 vector_ref()33 vector_ref(): m_data(nullptr), m_count(0) {} 34 /// Creates a new vector_ref to point to @a _count elements starting at @a _data. vector_ref(T * _data,size_t _count)35 vector_ref(T* _data, size_t _count): m_data(_data), m_count(_count) {} 36 /// Creates a new vector_ref pointing to the data part of a string (given as pointer). vector_ref(string_type * _data)37 vector_ref(string_type* _data): m_data(reinterpret_cast<T*>(_data->data())), m_count(_data->size() / sizeof(T)) {} 38 /// Creates a new vector_ref pointing to the data part of a string (given as reference). vector_ref(string_type & _data)39 vector_ref(string_type& _data): vector_ref(&_data) {} 40 /// Creates a new vector_ref pointing to the data part of a vector (given as pointer). vector_ref(vector_type * _data)41 vector_ref(vector_type* _data): m_data(_data->data()), m_count(_data->size()) {} 42 explicit operator bool() const { return m_data && m_count; } 43 toBytes()44 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)); } toString()45 std::string toString() const { return std::string((char const*)m_data, ((char const*)m_data) + m_count * sizeof(T)); } 46 47 operator vector_ref<T const>() const { return vector_ref<T const>(m_data, m_count); } 48 data()49 T* data() const { return m_data; } 50 /// @returns the number of elements referenced (not necessarily number of bytes). size()51 size_t size() const { return m_count; } empty()52 bool empty() const { return !m_count; } 53 /// @returns a new vector_ref which is a shifted and shortened view of the original data. 54 /// If this goes out of bounds in any way, returns an empty vector_ref. 55 /// If @a _count is ~size_t(0), extends the view to the end of the data. cropped(size_t _begin,size_t _count)56 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>(); } 57 /// @returns a new vector_ref which is a shifted view of the original data (not going beyond it). cropped(size_t _begin)58 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>(); } 59 begin()60 T* begin() { return m_data; } end()61 T* end() { return m_data + m_count; } begin()62 T const* begin() const { return m_data; } end()63 T const* end() const { return m_data + m_count; } 64 65 T& operator[](size_t _i) { assert(m_data); assert(_i < m_count); return m_data[_i]; } 66 T const& operator[](size_t _i) const { assert(m_data); assert(_i < m_count); return m_data[_i]; } 67 68 bool operator==(vector_ref<T> const& _cmp) const { return m_data == _cmp.m_data && m_count == _cmp.m_count; } 69 bool operator!=(vector_ref<T> const& _cmp) const { return !operator==(_cmp); } 70 reset()71 void reset() { m_data = nullptr; m_count = 0; } 72 73 private: 74 T* m_data = nullptr; 75 size_t m_count = 0; 76 }; 77 78 } 79