[Bf-blender-cvs] [d9df1550045] virtual-array-attributes: cleanup
Jacques Lucke
noreply at git.blender.org
Sat Apr 17 14:25:50 CEST 2021
Commit: d9df1550045f268e5e32eec97e8dacc8b0bcc0bc
Author: Jacques Lucke
Date: Sat Apr 17 14:23:21 2021 +0200
Branches: virtual-array-attributes
https://developer.blender.org/rBd9df1550045f268e5e32eec97e8dacc8b0bcc0bc
cleanup
===================================================================
M source/blender/blenlib/BLI_virtual_array.hh
M source/blender/functions/FN_generic_virtual_array.hh
M source/blender/functions/intern/generic_virtual_array.cc
===================================================================
diff --git a/source/blender/blenlib/BLI_virtual_array.hh b/source/blender/blenlib/BLI_virtual_array.hh
index fced85bc090..3868f5acae9 100644
--- a/source/blender/blenlib/BLI_virtual_array.hh
+++ b/source/blender/blenlib/BLI_virtual_array.hh
@@ -117,11 +117,14 @@ template<typename T> class VArray {
return this->get_internal_single_impl();
}
+ /* Get the element at a specific index. Note that this operator cannot be used to assign values
+ * to an index, because the return value is not a reference. */
T operator[](const int64_t index) const
{
return this->get(index);
}
+ /* Copy the entire virtual array into a span. */
void materialize(MutableSpan<T> r_span) const
{
BLI_assert(size_ == r_span.size());
@@ -199,6 +202,7 @@ template<typename T> class VArray {
}
};
+/* Similar to VArray, but the elements are mutable. */
template<typename T> class VMutableArray : public VArray<T> {
public:
VMutableArray(const int64_t size) : VArray<T>(size)
@@ -212,6 +216,7 @@ template<typename T> class VMutableArray : public VArray<T> {
this->set_impl(index, std::move(value));
}
+ /* Copy the values from the source span to all elements in the virtual array. */
void set_all(Span<T> src)
{
BLI_assert(src.size() == this->size_);
@@ -405,6 +410,13 @@ template<typename T> class VArray_Span final : public Span<T> {
}
};
+/**
+ * Same as VArray_Span, but for a mutable span.
+ * The important thing to note is that when changing this span, the results might not be
+ * immediately reflected in the underlying virtual array (only when the virtual array is a span
+ * internally). The #save method can be used to write all changes to the underlying virtual array,
+ * if necessary.
+ */
template<typename T> class VMutableArray_Span final : public MutableSpan<T> {
private:
VMutableArray<T> &varray_;
@@ -413,7 +425,9 @@ template<typename T> class VMutableArray_Span final : public MutableSpan<T> {
bool show_not_saved_warning_ = true;
public:
- VMutableArray_Span(VMutableArray<T> &varray, const bool materialize = true)
+ /* Create a span for any virtual array. This is cheap when the virtual array is a span itself. If
+ * not, a new array has to be allocated as a wrapper for the underlying virtual array. */
+ VMutableArray_Span(VMutableArray<T> &varray, const bool copy_values_to_span = true)
: MutableSpan<T>(), varray_(varray)
{
this->size_ = varray_.size();
@@ -421,7 +435,7 @@ template<typename T> class VMutableArray_Span final : public MutableSpan<T> {
this->data_ = varray_.get_internal_span().data();
}
else {
- if (materialize) {
+ if (copy_values_to_span) {
owned_data_.~Array();
new (&owned_data_) Array<T>(varray_.size(), NoInitialization{});
varray_.materialize_to_uninitialized(owned_data_);
@@ -442,6 +456,7 @@ template<typename T> class VMutableArray_Span final : public MutableSpan<T> {
}
}
+ /* Write back all values from a temporary allocated array to the underlying virtual array. */
void save()
{
save_has_been_called_ = true;
diff --git a/source/blender/functions/FN_generic_virtual_array.hh b/source/blender/functions/FN_generic_virtual_array.hh
index 86bfd1fbbba..c649623997d 100644
--- a/source/blender/functions/FN_generic_virtual_array.hh
+++ b/source/blender/functions/FN_generic_virtual_array.hh
@@ -134,6 +134,7 @@ class GVArray {
return (const VArray<T> *)this->try_get_internal_varray_impl();
}
+ /* Create a typed virtual array for this generic virtual array. */
template<typename T> GVArray_Typed<T> typed() const
{
return GVArray_Typed<T>(*this);
@@ -152,6 +153,7 @@ class GVArray {
virtual const void *try_get_internal_varray_impl() const;
};
+/* Similar to GVArray, but supports changing the elements in the virtual array. */
class GVMutableArray : public GVArray {
public:
GVMutableArray(const CPPType &type, const int64_t size) : GVArray(type, size)
@@ -192,6 +194,7 @@ class GVMutableArray : public GVArray {
return (VMutableArray<T> *)this->try_get_internal_mutable_varray_impl();
}
+ /* Create a typed virtual array for this generic virtual array. */
template<typename T> GVMutableArray_Typed<T> typed()
{
return GVMutableArray_Typed<T>(*this);
@@ -274,6 +277,7 @@ class GVMutableArray_For_GMutableSpan : public GVMutableArray {
GSpan get_internal_span_impl() const override;
};
+/* Generic virtual array where each element has the same value. The value is not owned. */
class GVArray_For_SingleValueRef : public GVArray {
protected:
const void *value_ = nullptr;
@@ -299,12 +303,14 @@ class GVArray_For_SingleValueRef : public GVArray {
void get_internal_single_impl(void *r_value) const override;
};
+/* Same as GVArray_For_SingleValueRef, but the value is owned. */
class GVArray_For_SingleValue : public GVArray_For_SingleValueRef {
public:
GVArray_For_SingleValue(const CPPType &type, const int64_t size, const void *value);
~GVArray_For_SingleValue();
};
+/* Used to convert a typed virtual array into a generic one. */
template<typename T> class GVArray_For_VArray : public GVArray {
protected:
const VArray<T> *varray_ = nullptr;
@@ -356,6 +362,7 @@ template<typename T> class GVArray_For_VArray : public GVArray {
}
};
+/* Used to convert any generic virtual array into a typed one. */
template<typename T> class VArray_For_GVArray : public VArray<T> {
protected:
const GVArray *varray_ = nullptr;
@@ -401,6 +408,7 @@ template<typename T> class VArray_For_GVArray : public VArray<T> {
}
};
+/* Used to convert an generic mutable virtual array into a typed one. */
template<typename T> class VMutableArray_For_GVMutableArray : public VMutableArray<T> {
protected:
GVMutableArray *varray_ = nullptr;
@@ -452,6 +460,7 @@ template<typename T> class VMutableArray_For_GVMutableArray : public VMutableArr
}
};
+/* Used to convert any typed virtual mutable array into a generic one. */
template<typename T> class GVMutableArray_For_VMutableArray : public GVMutableArray {
protected:
VMutableArray<T> *varray_ = nullptr;
@@ -528,6 +537,7 @@ template<typename T> class GVMutableArray_For_VMutableArray : public GVMutableAr
}
};
+/* A generic version of VArray_Span. */
class GVArray_GSpan : public GSpan {
private:
const GVArray &varray_;
@@ -538,6 +548,7 @@ class GVArray_GSpan : public GSpan {
~GVArray_GSpan();
};
+/* A generic version of VMutableArray_Span. */
class GVMutableArray_GSpan : public GMutableSpan {
private:
GVMutableArray &varray_;
@@ -546,13 +557,14 @@ class GVMutableArray_GSpan : public GMutableSpan {
bool show_not_saved_warning_ = true;
public:
- GVMutableArray_GSpan(GVMutableArray &varray, bool materialize = true);
+ GVMutableArray_GSpan(GVMutableArray &varray, bool copy_values_to_span = true);
~GVMutableArray_GSpan();
void save();
void disable_not_applied_warning();
};
+/* Similar to GVArray_GSpan, but the resulting span is typed. */
template<typename T> class GVArray_Span : public Span<T> {
private:
GVArray_GSpan varray_gspan_;
@@ -571,6 +583,7 @@ template<typename T> class GVArray_For_OwnedVArray : public GVArray_For_VArray<T
std::unique_ptr<VArray<T>> owned_varray_;
public:
+ /* Takes ownership of varray and passes a reference to the base class. */
GVArray_For_OwnedVArray(std::unique_ptr<VArray<T>> varray)
: GVArray_For_VArray<T>(*varray), owned_varray_(std::move(varray))
{
@@ -582,6 +595,7 @@ template<typename T> class VArray_For_OwnedGVArray : public VArray_For_GVArray<T
std::unique_ptr<VArray<T>> owned_varray_;
public:
+ /* Takes ownership of varray and passes a reference to the base class. */
VArray_For_OwnedGVArray(std::unique_ptr<GVArray> varray)
: VArray_For_GVArray<T>(*varray), owned_varray_(std::move(varray))
{
@@ -594,6 +608,7 @@ class GVMutableArray_For_OwnedVMutableArray : public GVMutableArray_For_VMutable
std::unique_ptr<VMutableArray<T>> owned_varray_;
public:
+ /* Takes ownership of varray and passes a reference to the base class. */
GVMutableArray_For_OwnedVMutableArray(std::unique_ptr<VMutableArray<T>> varray)
: GVMutableArray_For_VMutableArray<T>(*varray), owned_varray_(std::move(varray))
{
@@ -606,12 +621,15 @@ class VMutableArray_For_OwnedGVMutableArray : public VMutableArray_For_GVMutable
std::unique_ptr<GVMutableArray> owned_varray_;
public:
+ /* Takes ownership of varray and passes a reference to the base class. */
VMutableArray_For_OwnedGVMutableArray(std::unique_ptr<GVMutableArray> varray)
: VMutableArray_For_GVMutableArray<T>(*varray), owned_varray_(std::move(varray))
{
}
};
+/* Utility to embed a typed virtual array into a generic one. This avoids one allocation and give
+ * the compiler more opportunity to optimize the generic virtual array. */
template<typename T, typename VArrayT>
class GVArray_For_EmbeddedVArray : public GVArray_For_VArray<T> {
private:
@@ -626,6 +644,7 @@ class GVArray_For_EmbeddedVArray : public GVArray_For_VArray<T> {
}
};
+/* Same as GVArray_For_EmbeddedVArray, but for mutable virtual arrays. */
template<typename T, typename VMutableArrayT>
class GVMutableArray_For_EmbeddedVMutableArray : public GVMutableArray_For_VMutableArray<T> {
private:
@@ -640,6 +659,7 @@ class GVMutableArray_For_EmbeddedVMutableArray : public GVMutableArray_For_VMuta
}
};
+/* Same as VArray_For_ArrayContainer, but for a generic virtual array. */
template<typename Container, typename T = typename Container::value_type>
class GVArray_For_ArrayContainer
: public GVArray_For_EmbeddedVArray<T, VArray_For_ArrayContainer<Container, T>> {
@@ -651,6 +671,7 @@ class GVArray_For_ArrayContainer
}
};
+/* Same as VArray_For_DerivedSpan, but for a generic virtual array. */
template<typename StructT, typename ElemT, ElemT (*GetFunc)(const StructT &)>
class GVArray_For_DerivedSpan
: public GVArray_For_EmbeddedVArray<ElemT, VArray_For_DerivedSpan<StructT, ElemT, GetFunc>> {
@@ -662,6 +683,7 @@ class GVArray_For_DerivedSpan
}
};
+/* Same as VMutableArray_For_DerivedSpan, but for a generic virtua
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list