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:🧬: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:🧬:shallow_zero_initialize<MyDNAStruct>();

Differential Revision: https://developer.blender.org/D14486
This commit is contained in:
Sergey Sharybin 2022-03-29 10:53:25 +02:00
parent a264dff4fa
commit c772461741
3 changed files with 23 additions and 9 deletions

View File

@ -910,8 +910,7 @@ static void curve_to_mesh_eval_ensure(Object &object)
*
* So we create temporary copy of the object which will use same data as the original bevel, but
* will have no modifiers. */
Object bevel_object;
blender::dna::zero_memory(bevel_object);
Object bevel_object = blender::dna::shallow_zero_initialize<Object>();
if (curve.bevobj != nullptr) {
bevel_object = blender::dna::shallow_copy(*curve.bevobj);
BLI_listbase_clear(&bevel_object.modifiers);
@ -920,8 +919,7 @@ static void curve_to_mesh_eval_ensure(Object &object)
}
/* Same thing for taper. */
Object taper_object;
blender::dna::zero_memory(taper_object);
Object taper_object = blender::dna::shallow_zero_initialize<Object>();
if (curve.taperobj != nullptr) {
taper_object = blender::dna::shallow_copy(*curve.taperobj);
BLI_listbase_clear(&taper_object.modifiers);

View File

@ -1236,7 +1236,7 @@ IDTypeInfo IDType_ID_OB = {
void BKE_object_workob_clear(Object *workob)
{
blender::dna::zero_memory(*workob);
*workob = blender::dna::shallow_zero_initialize<Object>();
workob->scale[0] = workob->scale[1] = workob->scale[2] = 1.0f;
workob->dscale[0] = workob->dscale[1] = workob->dscale[2] = 1.0f;

View File

@ -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