diff options
author | Jacques Lucke <jacques@blender.org> | 2021-04-29 16:42:32 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2021-04-29 16:42:32 +0300 |
commit | 4225a18b35f071ae1ff01c54b475ad396c77febc (patch) | |
tree | 4b9be641b022708619f505f58bfabd27a4c49df3 /source/blender/functions/intern/generic_virtual_array.cc | |
parent | 41945454f712e6892b4e01e093ceeac13cf71a5c (diff) |
Function: add method to create shallow copy of virtual array
Creating a shallow copy is sometimes useful to get a unique ptr
for a virtual array when one only has a reference. It shouldn't
be used usually, but sometimes its the fastest way to do correct
ownership handling.
Diffstat (limited to 'source/blender/functions/intern/generic_virtual_array.cc')
-rw-r--r-- | source/blender/functions/intern/generic_virtual_array.cc | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/source/blender/functions/intern/generic_virtual_array.cc b/source/blender/functions/intern/generic_virtual_array.cc index 754a2156a65..ca2bd0f806f 100644 --- a/source/blender/functions/intern/generic_virtual_array.cc +++ b/source/blender/functions/intern/generic_virtual_array.cc @@ -19,6 +19,37 @@ namespace blender::fn { /* -------------------------------------------------------------------- + * GVArray_For_ShallowCopy. + */ + +class GVArray_For_ShallowCopy : public GVArray { + private: + const GVArray &varray_; + + public: + GVArray_For_ShallowCopy(const GVArray &varray) + : GVArray(varray.type(), varray.size()), varray_(varray) + { + } + + private: + void get_impl(const int64_t index, void *r_value) const override + { + varray_.get(index, r_value); + } + + void get_to_uninitialized_impl(const int64_t index, void *r_value) const override + { + varray_.get_to_uninitialized(index, r_value); + } + + void materialize_to_uninitialized_impl(const IndexMask mask, void *dst) const override + { + varray_.materialize_to_uninitialized(mask, dst); + } +}; + +/* -------------------------------------------------------------------- * GVArray. */ @@ -73,6 +104,26 @@ const void *GVArray::try_get_internal_varray_impl() const return nullptr; } +/** + * Creates a new `std::unique_ptr<GVArray>` based on this `GVArray`. + * The lifetime of the returned virtual array must not be longer than the lifetime of this virtual + * array. + */ +GVArrayPtr GVArray::shallow_copy() const +{ + if (this->is_span()) { + return std::make_unique<GVArray_For_GSpan>(this->get_internal_span()); + } + if (this->is_single()) { + BUFFER_FOR_CPP_TYPE_VALUE(*type_, buffer); + this->get_internal_single(buffer); + std::unique_ptr new_varray = std::make_unique<GVArray_For_SingleValue>(*type_, size_, buffer); + type_->destruct(buffer); + return new_varray; + } + return std::make_unique<GVArray_For_ShallowCopy>(*this); +} + /* -------------------------------------------------------------------- * GVMutableArray. */ |