[Bf-blender-cvs] [42e13ea4bff] master: Cleanup: Deprecated field access in outliner_duplicate

Sergey Sharybin noreply at git.blender.org
Wed Mar 16 12:52:49 CET 2022


Commit: 42e13ea4bff4eafb46d16942436b89e166f9d844
Author: Sergey Sharybin
Date:   Wed Mar 16 12:25:44 2022 +0100
Branches: master
https://developer.blender.org/rB42e13ea4bff4eafb46d16942436b89e166f9d844

Cleanup: Deprecated field access in outliner_duplicate

Solved by introducing introducing a variant of MEM_cnew which behaves
as a copy-constructor for a trivial types.

Alternative approach would be to surround DNA structs with clang/gcc
diagnostics push/modify/pop so that implicitly defined constructors
and copy operators are allowed to access deprecated fields.

The downside of the DNA approach is that it will require some way to
easily apply diagnostics modifications to many structs, which is not
possible currently.

The newly added MEM_cnew has other good usecases, so is easiest to
use this route, at least for now.

Differential Revision: https://developer.blender.org/D14356

===================================================================

M	intern/guardedalloc/MEM_guardedalloc.h
M	source/blender/editors/space_outliner/space_outliner.cc

===================================================================

diff --git a/intern/guardedalloc/MEM_guardedalloc.h b/intern/guardedalloc/MEM_guardedalloc.h
index d8a8c1181e4..8cd2c9f94dd 100644
--- a/intern/guardedalloc/MEM_guardedalloc.h
+++ b/intern/guardedalloc/MEM_guardedalloc.h
@@ -278,6 +278,24 @@ template<typename T> inline T *MEM_cnew(const char *allocation_name)
   return static_cast<T *>(MEM_callocN(sizeof(T), allocation_name));
 }
 
+/**
+ * Allocate memory for an object of type #T and copy construct an object from `other`.
+ * Only applicable for a trivial types.
+ *
+ * This function works around problem of copy-constructing DNA structs which contains deprecated
+ * fields: some compilers will generate access deprecated field in implicitly defined copy
+ * constructors.
+ *
+ * This is a better alternative to #MEM_dupallocN.
+ */
+template<typename T> inline T *MEM_cnew(const char *allocation_name, const T &other)
+{
+  static_assert(std::is_trivial_v<T>, "For non-trivial types, MEM_new should be used.");
+  T *new_object = static_cast<T *>(MEM_mallocN(sizeof(T), allocation_name));
+  memcpy(new_object, &other, sizeof(T));
+  return new_object;
+}
+
 /**
  * Destructs and deallocates an object previously allocated with any `MEM_*` function.
  * Passing in null does nothing.
diff --git a/source/blender/editors/space_outliner/space_outliner.cc b/source/blender/editors/space_outliner/space_outliner.cc
index f75182d25a0..97dc659155f 100644
--- a/source/blender/editors/space_outliner/space_outliner.cc
+++ b/source/blender/editors/space_outliner/space_outliner.cc
@@ -371,7 +371,7 @@ static void outliner_init(wmWindowManager *UNUSED(wm), ScrArea *area)
 static SpaceLink *outliner_duplicate(SpaceLink *sl)
 {
   SpaceOutliner *space_outliner = (SpaceOutliner *)sl;
-  SpaceOutliner *space_outliner_new = MEM_new<SpaceOutliner>(__func__, *space_outliner);
+  SpaceOutliner *space_outliner_new = MEM_cnew<SpaceOutliner>(__func__, *space_outliner);
 
   BLI_listbase_clear(&space_outliner_new->tree);
   space_outliner_new->treestore = nullptr;



More information about the Bf-blender-cvs mailing list