diff options
author | Sergey Sharybin <sergey@blender.org> | 2022-03-29 11:53:25 +0300 |
---|---|---|
committer | Sergey Sharybin <sergey@blender.org> | 2022-03-29 12:04:50 +0300 |
commit | c772461741462a8c0b83309676a49e41eb0e81cc (patch) | |
tree | fbd8189adeeb5a639c7e33eaf694cb468c1b6d79 /source/blender/makesdna/DNA_defs.h | |
parent | a264dff4fa7dd0bb2e3c9abe805376b730d28c34 (diff) |
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<Object>()
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<MyDNAStruct>();
Differential Revision: https://developer.blender.org/D14486
Diffstat (limited to 'source/blender/makesdna/DNA_defs.h')
-rw-r--r-- | source/blender/makesdna/DNA_defs.h | 24 |
1 files changed, 20 insertions, 4 deletions
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 T> class ShallowDataConstRef { const T &ref_; }; +template<class T> class ShallowZeroInitializeTag { +}; + } // namespace blender::dna::internal # define DNA_DEFINE_CXX_METHODS(class_name) \ @@ -108,6 +111,18 @@ template<class T> 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<class_name> /*tag*/) \ + : class_name() \ + { \ + _DNA_internal_memzero(this, sizeof(class_name)); \ + } \ + class_name &operator=( \ + const blender::dna::internal::ShallowZeroInitializeTag<class_name> /*tag*/) \ + { \ + _DNA_internal_memzero(this, sizeof(class_name)); \ + return *this; \ } namespace blender::dna { @@ -126,11 +141,12 @@ template<class T> return internal::ShallowDataConstRef(other); } -/* Fill underlying memory used by DNA object with zeroes. */ -template<class T> inline void zero_memory(T &object) +/* DNA object initializer which leads to an object which underlying memory is filled with zeroes. + */ +template<class T> +[[nodiscard]] inline internal::ShallowZeroInitializeTag<T> shallow_zero_initialize() { - /* TODO(sergey): Consider adding static assert for T being a trivial type. */ - _DNA_internal_memzero(&object, sizeof(T)); + return internal::ShallowZeroInitializeTag<T>(); } } // namespace blender::dna |