From ac02ccb53d2a299e43807f81f589cdb677787677 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Fri, 3 Feb 2012 19:13:02 +0000 Subject: [PATCH] add a memoryview class memoryview is a non owning typed view over a memory range, usefull to pass arrays as parameters without data copying. --- src/memoryview.hh | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/memoryview.hh diff --git a/src/memoryview.hh b/src/memoryview.hh new file mode 100644 index 00000000..30e474e8 --- /dev/null +++ b/src/memoryview.hh @@ -0,0 +1,58 @@ +#ifndef memoryview_hh_INCLUDED +#define memoryview_hh_INCLUDED + +#include +#include + +namespace Kakoune +{ + +// A memoryview provides a typed, non owning view of a memory +// range with an interface similar to std::vector. +template +class memoryview +{ +public: + memoryview() + : m_pointer(nullptr), m_size(0) {} + + memoryview(const T& oneval) + : m_pointer(&oneval), m_size(1) {} + + memoryview(const T* pointer, size_t size) + : m_pointer(pointer), m_size(size) {} + + memoryview(const T* begin, const T* end) + : m_pointer(begin), m_size(end - begin) {} + + memoryview(const std::vector& v) + : m_pointer(&v[0]), m_size(v.size()) {} + + memoryview(const std::initializer_list& v) + : m_pointer(v.begin()), m_size(v.size()) {} + + size_t size() const { return m_size; } + const T& operator[](size_t n) const { return *(m_pointer + n); } + + const T* begin() const { return m_pointer; } + const T* end() const { return m_pointer+m_size; } + + const T& front() const { return *m_pointer; } + const T& back() const { return *(m_pointer + m_size - 1); } + + bool empty() const { return m_size == 0; } + + memoryview subrange(size_t first, size_t count) const + { + return memoryview(m_pointer + first, count); + } + +private: + const T* m_pointer; + size_t m_size; +}; + +} + +#endif // memoryview_hh_INCLUDED +