diff options
author | Jacques Lucke <jacques@blender.org> | 2022-03-19 10:26:29 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2022-03-19 10:26:29 +0300 |
commit | 3e16f3b3ef4b8f385b30fe4a1e00860620f610ee (patch) | |
tree | cea8e2a3ea8a8a7dbce98263d166b4782d83721b /source/blender/blenlib/BLI_generic_span.hh | |
parent | c655146b87fe20853e52b87991b46732a04d749e (diff) |
BLI: move generic data structures to blenlib
This is a follow up to rB2252bc6a5527cd7360d1ccfe7a2d1bc640a8dfa6.
Diffstat (limited to 'source/blender/blenlib/BLI_generic_span.hh')
-rw-r--r-- | source/blender/blenlib/BLI_generic_span.hh | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_generic_span.hh b/source/blender/blenlib/BLI_generic_span.hh new file mode 100644 index 00000000000..f4f93735e06 --- /dev/null +++ b/source/blender/blenlib/BLI_generic_span.hh @@ -0,0 +1,169 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#pragma once + +/** \file + * \ingroup bli + */ + +#include "BLI_cpp_type.hh" +#include "BLI_span.hh" + +namespace blender { + +/** + * A generic span. It behaves just like a blender::Span<T>, but the type is only known at run-time. + */ +class GSpan { + protected: + const CPPType *type_; + const void *data_; + int64_t size_; + + public: + GSpan(const CPPType &type, const void *buffer, int64_t size) + : type_(&type), data_(buffer), size_(size) + { + BLI_assert(size >= 0); + BLI_assert(buffer != nullptr || size == 0); + BLI_assert(type.pointer_has_valid_alignment(buffer)); + } + + GSpan(const CPPType &type) : GSpan(type, nullptr, 0) + { + } + + template<typename T> + GSpan(Span<T> array) + : GSpan(CPPType::get<T>(), static_cast<const void *>(array.data()), array.size()) + { + } + + const CPPType &type() const + { + return *type_; + } + + bool is_empty() const + { + return size_ == 0; + } + + int64_t size() const + { + return size_; + } + + const void *data() const + { + return data_; + } + + const void *operator[](int64_t index) const + { + BLI_assert(index < size_); + return POINTER_OFFSET(data_, type_->size() * index); + } + + template<typename T> Span<T> typed() const + { + BLI_assert(type_->is<T>()); + return Span<T>(static_cast<const T *>(data_), size_); + } + + GSpan slice(const int64_t start, int64_t size) const + { + BLI_assert(start >= 0); + BLI_assert(size >= 0); + const int64_t new_size = std::max<int64_t>(0, std::min(size, size_ - start)); + return GSpan(*type_, POINTER_OFFSET(data_, type_->size() * start), new_size); + } + + GSpan slice(const IndexRange range) const + { + return this->slice(range.start(), range.size()); + } +}; + +/** + * A generic mutable span. It behaves just like a blender::MutableSpan<T>, but the type is only + * known at run-time. + */ +class GMutableSpan { + protected: + const CPPType *type_; + void *data_; + int64_t size_; + + public: + GMutableSpan(const CPPType &type, void *buffer, int64_t size) + : type_(&type), data_(buffer), size_(size) + { + BLI_assert(size >= 0); + BLI_assert(buffer != nullptr || size == 0); + BLI_assert(type.pointer_has_valid_alignment(buffer)); + } + + GMutableSpan(const CPPType &type) : GMutableSpan(type, nullptr, 0) + { + } + + template<typename T> + GMutableSpan(MutableSpan<T> array) + : GMutableSpan(CPPType::get<T>(), static_cast<void *>(array.begin()), array.size()) + { + } + + operator GSpan() const + { + return GSpan(*type_, data_, size_); + } + + const CPPType &type() const + { + return *type_; + } + + bool is_empty() const + { + return size_ == 0; + } + + int64_t size() const + { + return size_; + } + + void *data() const + { + return data_; + } + + void *operator[](int64_t index) const + { + BLI_assert(index >= 0); + BLI_assert(index < size_); + return POINTER_OFFSET(data_, type_->size() * index); + } + + template<typename T> MutableSpan<T> typed() const + { + BLI_assert(type_->is<T>()); + return MutableSpan<T>(static_cast<T *>(data_), size_); + } + + GMutableSpan slice(const int64_t start, int64_t size) const + { + BLI_assert(start >= 0); + BLI_assert(size >= 0); + const int64_t new_size = std::max<int64_t>(0, std::min(size, size_ - start)); + return GMutableSpan(*type_, POINTER_OFFSET(data_, type_->size() * start), new_size); + } + + GMutableSpan slice(IndexRange range) const + { + return this->slice(range.start(), range.size()); + } +}; + +} // namespace blender |