diff options
Diffstat (limited to 'source')
-rw-r--r-- | source/blender/blenlib/BLI_cpp_type.hh | 28 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_cpp_type_make.hh | 12 | ||||
-rw-r--r-- | source/blender/blenlib/tests/BLI_cpp_type_test.cc | 34 |
3 files changed, 74 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_cpp_type.hh b/source/blender/blenlib/BLI_cpp_type.hh index 881408f460b..8e8175c2265 100644 --- a/source/blender/blenlib/BLI_cpp_type.hh +++ b/source/blender/blenlib/BLI_cpp_type.hh @@ -110,6 +110,9 @@ class CPPType : NonCopyable, NonMovable { void (*default_construct_)(void *ptr) = nullptr; void (*default_construct_indices_)(void *ptr, IndexMask mask) = nullptr; + void (*value_initialize_)(void *ptr) = nullptr; + void (*value_initialize_indices_)(void *ptr, IndexMask mask) = nullptr; + void (*destruct_)(void *ptr) = nullptr; void (*destruct_indices_)(void *ptr, IndexMask mask) = nullptr; @@ -326,6 +329,31 @@ class CPPType : NonCopyable, NonMovable { } /** + * Same as #default_construct, but does zero initialization for trivial types. + * + * C++ equivalent: + * new (ptr) T(); + */ + void value_initialize(void *ptr) const + { + BLI_assert(this->pointer_can_point_to_instance(ptr)); + + value_initialize_(ptr); + } + + void value_initialize_n(void *ptr, int64_t n) const + { + this->value_initialize_indices(ptr, IndexMask(n)); + } + + void value_initialize_indices(void *ptr, IndexMask mask) const + { + BLI_assert(mask.size() == 0 || this->pointer_can_point_to_instance(ptr)); + + value_initialize_indices_(ptr, mask); + } + + /** * Call the destructor on the given instance of this type. The pointer must not be nullptr. * * For some trivial types, this does nothing. diff --git a/source/blender/blenlib/BLI_cpp_type_make.hh b/source/blender/blenlib/BLI_cpp_type_make.hh index 9100b2b9a0f..2612348075b 100644 --- a/source/blender/blenlib/BLI_cpp_type_make.hh +++ b/source/blender/blenlib/BLI_cpp_type_make.hh @@ -20,6 +20,16 @@ template<typename T> void default_construct_indices_cb(void *ptr, IndexMask mask mask.foreach_index([&](int64_t i) { new (static_cast<T *>(ptr) + i) T; }); } +template<typename T> void value_initialize_cb(void *ptr) +{ + new (ptr) T(); +} + +template<typename T> void value_initialize_indices_cb(void *ptr, IndexMask mask) +{ + mask.foreach_index([&](int64_t i) { new (static_cast<T *>(ptr) + i) T(); }); +} + template<typename T> void destruct_cb(void *ptr) { (static_cast<T *>(ptr))->~T(); @@ -186,6 +196,8 @@ CPPType::CPPType(CPPTypeParam<T, Flags> /* unused */, StringRef debug_name) if constexpr (std::is_default_constructible_v<T>) { default_construct_ = default_construct_cb<T>; default_construct_indices_ = default_construct_indices_cb<T>; + value_initialize_ = value_initialize_cb<T>; + value_initialize_indices_ = value_initialize_indices_cb<T>; static T default_value; default_value_ = (void *)&default_value; } diff --git a/source/blender/blenlib/tests/BLI_cpp_type_test.cc b/source/blender/blenlib/tests/BLI_cpp_type_test.cc index 94456e1ee28..f00767eda8c 100644 --- a/source/blender/blenlib/tests/BLI_cpp_type_test.cc +++ b/source/blender/blenlib/tests/BLI_cpp_type_test.cc @@ -118,6 +118,40 @@ TEST(cpp_type, DefaultConstruction) EXPECT_EQ(buffer[8], 0); } +TEST(cpp_type, DefaultConstructTrivial) +{ + int value = 5; + CPPType::get<int>().default_construct(&value); + EXPECT_EQ(value, 5); +} + +TEST(cpp_type, ValueInitialize) +{ + int buffer[10] = {0}; + CPPType_TestType.value_initialize((void *)buffer); + EXPECT_EQ(buffer[0], default_constructed_value); + EXPECT_EQ(buffer[1], 0); + CPPType_TestType.value_initialize_n((void *)buffer, 3); + EXPECT_EQ(buffer[0], default_constructed_value); + EXPECT_EQ(buffer[1], default_constructed_value); + EXPECT_EQ(buffer[2], default_constructed_value); + EXPECT_EQ(buffer[3], 0); + CPPType_TestType.value_initialize_indices((void *)buffer, {2, 5, 7}); + EXPECT_EQ(buffer[2], default_constructed_value); + EXPECT_EQ(buffer[4], 0); + EXPECT_EQ(buffer[5], default_constructed_value); + EXPECT_EQ(buffer[6], 0); + EXPECT_EQ(buffer[7], default_constructed_value); + EXPECT_EQ(buffer[8], 0); +} + +TEST(cpp_type, ValueInitializeTrivial) +{ + int value = 5; + CPPType::get<int>().value_initialize(&value); + EXPECT_EQ(value, 0); +} + TEST(cpp_type, Destruct) { int buffer[10] = {0}; |