diff options
author | Jacques Lucke <jacques@blender.org> | 2022-03-29 10:19:35 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2022-03-29 10:29:09 +0300 |
commit | d7c644211898185579597588bb4fc08edc1a5093 (patch) | |
tree | dbfbfe7e6a324db29c658e4562583fdebbe39f6f /source/blender/blenlib/BLI_cpp_type.hh | |
parent | d4bdf2192964f786520c774d1a2ee44617302bc1 (diff) |
BLI: support value initialization in CPPType
Value initialization differs from default-construction in that it
also zero-initializes trivial types.
Diffstat (limited to 'source/blender/blenlib/BLI_cpp_type.hh')
-rw-r--r-- | source/blender/blenlib/BLI_cpp_type.hh | 28 |
1 files changed, 28 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. |