From c772461741462a8c0b83309676a49e41eb0e81cc Mon Sep 17 00:00:00 2001 From: Sergey Sharybin Date: Tue, 29 Mar 2022 10:53:25 +0200 Subject: Cleanup: Use higher level semantic for zeroing DNA objects in C++ Replaces old-style memzero-style of call with zero-initializer. Allows to shorten typical initialization code to a single line: Object foo = blender::dna::shallow_zero_initialize() This will allow to more easily convert designated initializer which is often used to fill object with zeroes to the explicit function calls: MyDNAStruct foo = {}; will be translated to MyDNAStruct foo = blender::dna::shallow_zero_initialize(); Differential Revision: https://developer.blender.org/D14486 --- source/blender/makesdna/DNA_defs.h | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'source/blender/makesdna/DNA_defs.h') diff --git a/source/blender/makesdna/DNA_defs.h b/source/blender/makesdna/DNA_defs.h index 79477a56695..bbce4839506 100644 --- a/source/blender/makesdna/DNA_defs.h +++ b/source/blender/makesdna/DNA_defs.h @@ -86,6 +86,9 @@ template class ShallowDataConstRef { const T &ref_; }; +template class ShallowZeroInitializeTag { +}; + } // namespace blender::dna::internal # define DNA_DEFINE_CXX_METHODS(class_name) \ @@ -108,6 +111,18 @@ template class ShallowDataConstRef { _DNA_internal_memcpy(this, ref.get_pointer(), sizeof(class_name)); \ } \ return *this; \ + } \ + /* Create object which memory is filled with zeros. */ \ + class_name(const blender::dna::internal::ShallowZeroInitializeTag /*tag*/) \ + : class_name() \ + { \ + _DNA_internal_memzero(this, sizeof(class_name)); \ + } \ + class_name &operator=( \ + const blender::dna::internal::ShallowZeroInitializeTag /*tag*/) \ + { \ + _DNA_internal_memzero(this, sizeof(class_name)); \ + return *this; \ } namespace blender::dna { @@ -126,11 +141,12 @@ template return internal::ShallowDataConstRef(other); } -/* Fill underlying memory used by DNA object with zeroes. */ -template inline void zero_memory(T &object) +/* DNA object initializer which leads to an object which underlying memory is filled with zeroes. + */ +template +[[nodiscard]] inline internal::ShallowZeroInitializeTag shallow_zero_initialize() { - /* TODO(sergey): Consider adding static assert for T being a trivial type. */ - _DNA_internal_memzero(&object, sizeof(T)); + return internal::ShallowZeroInitializeTag(); } } // namespace blender::dna -- cgit v1.2.3