Changeset View
Changeset View
Standalone View
Standalone View
source/blender/functions/FN_generic_span.hh
| Show All 24 Lines | |||||
| #include "FN_cpp_type.hh" | #include "FN_cpp_type.hh" | ||||
| namespace blender::fn { | namespace blender::fn { | ||||
| /** | /** | ||||
| * A generic span. It behaves just like a blender::Span<T>, but the type is only known at run-time. | * A generic span. It behaves just like a blender::Span<T>, but the type is only known at run-time. | ||||
| */ | */ | ||||
| class GSpan { | class GSpan { | ||||
| private: | protected: | ||||
| const CPPType *type_; | const CPPType *type_; | ||||
| const void *data_; | const void *data_; | ||||
| int64_t size_; | int64_t size_; | ||||
| public: | public: | ||||
| GSpan(const CPPType &type, const void *buffer, int64_t size) | GSpan(const CPPType &type, const void *buffer, int64_t size) | ||||
| : type_(&type), data_(buffer), size_(size) | : type_(&type), data_(buffer), size_(size) | ||||
| { | { | ||||
| Show All 38 Lines | const void *operator[](int64_t index) const | ||||
| return POINTER_OFFSET(data_, type_->size() * index); | return POINTER_OFFSET(data_, type_->size() * index); | ||||
| } | } | ||||
| template<typename T> Span<T> typed() const | template<typename T> Span<T> typed() const | ||||
| { | { | ||||
| BLI_assert(type_->is<T>()); | BLI_assert(type_->is<T>()); | ||||
| return Span<T>(static_cast<const T *>(data_), size_); | 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); | |||||
| } | |||||
| }; | }; | ||||
| /** | /** | ||||
| * A generic mutable span. It behaves just like a blender::MutableSpan<T>, but the type is only | * A generic mutable span. It behaves just like a blender::MutableSpan<T>, but the type is only | ||||
| * known at run-time. | * known at run-time. | ||||
| */ | */ | ||||
| class GMutableSpan { | class GMutableSpan { | ||||
| private: | protected: | ||||
| const CPPType *type_; | const CPPType *type_; | ||||
| void *data_; | void *data_; | ||||
| int64_t size_; | int64_t size_; | ||||
| public: | public: | ||||
| GMutableSpan(const CPPType &type, void *buffer, int64_t size) | GMutableSpan(const CPPType &type, void *buffer, int64_t size) | ||||
| : type_(&type), data_(buffer), size_(size) | : type_(&type), data_(buffer), size_(size) | ||||
| { | { | ||||
| ▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | void *operator[](int64_t index) const | ||||
| return POINTER_OFFSET(data_, type_->size() * index); | return POINTER_OFFSET(data_, type_->size() * index); | ||||
| } | } | ||||
| template<typename T> MutableSpan<T> typed() const | template<typename T> MutableSpan<T> typed() const | ||||
| { | { | ||||
| BLI_assert(type_->is<T>()); | BLI_assert(type_->is<T>()); | ||||
| return MutableSpan<T>(static_cast<T *>(data_), size_); | 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); | |||||
| } | |||||
| }; | }; | ||||
| } // namespace blender::fn | } // namespace blender::fn | ||||