Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacques Lucke <jacques@blender.org>2022-04-07 10:34:07 +0300
committerJacques Lucke <jacques@blender.org>2022-04-07 10:34:07 +0300
commit120a17a45a6d6c7f5520c8bfa32a0938f29a46be (patch)
tree22c2c63a975af9fd66cd1aa10205541075d27259
parente2f4c4db8d6cbe4694c24d599e16ee3889871bdd (diff)
BLI: add CPPType utility to copy elements to a shorter array
-rw-r--r--source/blender/blenlib/BLI_cpp_type.hh26
-rw-r--r--source/blender/blenlib/BLI_cpp_type_make.hh24
-rw-r--r--source/blender/blenlib/tests/BLI_cpp_type_test.cc10
3 files changed, 60 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_cpp_type.hh b/source/blender/blenlib/BLI_cpp_type.hh
index 9453eb89a2e..dd14cca7a0b 100644
--- a/source/blender/blenlib/BLI_cpp_type.hh
+++ b/source/blender/blenlib/BLI_cpp_type.hh
@@ -118,9 +118,11 @@ class CPPType : NonCopyable, NonMovable {
void (*copy_assign_)(const void *src, void *dst) = nullptr;
void (*copy_assign_indices_)(const void *src, void *dst, IndexMask mask) = nullptr;
+ void (*copy_assign_compressed_)(const void *src, void *dst, IndexMask mask) = nullptr;
void (*copy_construct_)(const void *src, void *dst) = nullptr;
void (*copy_construct_indices_)(const void *src, void *dst, IndexMask mask) = nullptr;
+ void (*copy_construct_compressed_)(const void *src, void *dst, IndexMask mask) = nullptr;
void (*move_assign_)(void *src, void *dst) = nullptr;
void (*move_assign_indices_)(void *src, void *dst, IndexMask mask) = nullptr;
@@ -409,6 +411,18 @@ class CPPType : NonCopyable, NonMovable {
}
/**
+ * Similar to #copy_assign_indices, but does not leave gaps in the #dst array.
+ */
+ void copy_assign_compressed(const void *src, void *dst, IndexMask mask) const
+ {
+ BLI_assert(mask.size() == 0 || src != dst);
+ BLI_assert(mask.size() == 0 || this->pointer_can_point_to_instance(src));
+ BLI_assert(mask.size() == 0 || this->pointer_can_point_to_instance(dst));
+
+ copy_assign_compressed_(src, dst, mask);
+ }
+
+ /**
* Copy an instance of this type from src to dst.
*
* The memory pointed to by dst should be uninitialized.
@@ -440,6 +454,18 @@ class CPPType : NonCopyable, NonMovable {
}
/**
+ * Similar to #copy_construct_indices, but does not leave gaps in the #dst array.
+ */
+ void copy_construct_compressed(const void *src, void *dst, IndexMask mask) const
+ {
+ BLI_assert(mask.size() == 0 || src != dst);
+ BLI_assert(mask.size() == 0 || this->pointer_can_point_to_instance(src));
+ BLI_assert(mask.size() == 0 || this->pointer_can_point_to_instance(dst));
+
+ copy_construct_compressed_(src, dst, mask);
+ }
+
+ /**
* Move an instance of this type from src to dst.
*
* The memory pointed to by dst should be initialized.
diff --git a/source/blender/blenlib/BLI_cpp_type_make.hh b/source/blender/blenlib/BLI_cpp_type_make.hh
index 2612348075b..b0dbbff7ca8 100644
--- a/source/blender/blenlib/BLI_cpp_type_make.hh
+++ b/source/blender/blenlib/BLI_cpp_type_make.hh
@@ -51,6 +51,17 @@ template<typename T> void copy_assign_indices_cb(const void *src, void *dst, Ind
mask.foreach_index([&](int64_t i) { dst_[i] = src_[i]; });
}
+template<typename T> void copy_assign_compressed_cb(const void *src, void *dst, IndexMask mask)
+{
+ const T *src_ = static_cast<const T *>(src);
+ T *dst_ = static_cast<T *>(dst);
+
+ mask.to_best_mask_type([&](auto best_mask) {
+ for (const int64_t i : IndexRange(best_mask.size())) {
+ dst_[i] = src_[best_mask[i]];
+ }
+ });
+}
template<typename T> void copy_construct_cb(const void *src, void *dst)
{
@@ -63,6 +74,17 @@ template<typename T> void copy_construct_indices_cb(const void *src, void *dst,
mask.foreach_index([&](int64_t i) { new (dst_ + i) T(src_[i]); });
}
+template<typename T> void copy_construct_compressed_cb(const void *src, void *dst, IndexMask mask)
+{
+ const T *src_ = static_cast<const T *>(src);
+ T *dst_ = static_cast<T *>(dst);
+
+ mask.to_best_mask_type([&](auto best_mask) {
+ for (const int64_t i : IndexRange(best_mask.size())) {
+ new (dst_ + i) T(src_[best_mask[i]]);
+ }
+ });
+}
template<typename T> void move_assign_cb(void *src, void *dst)
{
@@ -208,10 +230,12 @@ CPPType::CPPType(CPPTypeParam<T, Flags> /* unused */, StringRef debug_name)
if constexpr (std::is_copy_assignable_v<T>) {
copy_assign_ = copy_assign_cb<T>;
copy_assign_indices_ = copy_assign_indices_cb<T>;
+ copy_assign_compressed_ = copy_assign_compressed_cb<T>;
}
if constexpr (std::is_copy_constructible_v<T>) {
copy_construct_ = copy_construct_cb<T>;
copy_construct_indices_ = copy_construct_indices_cb<T>;
+ copy_construct_compressed_ = copy_construct_compressed_cb<T>;
}
if constexpr (std::is_move_assignable_v<T>) {
move_assign_ = move_assign_cb<T>;
diff --git a/source/blender/blenlib/tests/BLI_cpp_type_test.cc b/source/blender/blenlib/tests/BLI_cpp_type_test.cc
index f00767eda8c..6a59bedc649 100644
--- a/source/blender/blenlib/tests/BLI_cpp_type_test.cc
+++ b/source/blender/blenlib/tests/BLI_cpp_type_test.cc
@@ -381,4 +381,14 @@ TEST(cpp_type, ToStaticType)
EXPECT_EQ(types[1], &CPPType::get<float>());
}
+TEST(cpp_type, CopyAssignCompressed)
+{
+ std::array<std::string, 5> array = {"a", "b", "c", "d", "e"};
+ std::array<std::string, 3> array_compressed;
+ CPPType::get<std::string>().copy_assign_compressed(&array, &array_compressed, {0, 2, 3});
+ EXPECT_EQ(array_compressed[0], "a");
+ EXPECT_EQ(array_compressed[1], "c");
+ EXPECT_EQ(array_compressed[2], "d");
+}
+
} // namespace blender::tests