[Bf-blender-cvs] [c328049535a] master: Initial step for IDTypeInfo refactor 'cleanup' project.

Bastien Montagne noreply at git.blender.org
Thu Mar 5 10:59:07 CET 2020


Commit: c328049535a4bc9453f5e93365a00759347e3a05
Author: Bastien Montagne
Date:   Thu Mar 5 10:54:00 2020 +0100
Branches: master
https://developer.blender.org/rBc328049535a4bc9453f5e93365a00759347e3a05

Initial step for IDTypeInfo refactor 'cleanup' project.

Introduce new IDTypeInfo structure.

Each ID type will have its own, with some minimal basic common info,
and ID management callbacks.

This patch only does it for Object type, for demo/testing purpose.
Moving all existing IDs is a goal of next "cleanup Friday".

Note that BKE_idcode features should then be merged back into BKE_idtype -
but this will have to be done later, once all ID types have been properly
converted to the new system.

Another later TODO might be to try and add callbacks for file read/write,
and lib_query ID usages looper.

This is part of T73719.

Thanks to @brecht for initial idea, and reviewing the patch.

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

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

A	source/blender/blenkernel/BKE_idtype.h
M	source/blender/blenkernel/CMakeLists.txt
A	source/blender/blenkernel/intern/idtype.c
M	source/blender/blenkernel/intern/lib_id.c
M	source/blender/blenkernel/intern/lib_id_delete.c
M	source/blender/blenkernel/intern/object.c
M	source/creator/creator.c
M	tests/gtests/blenloader/blendfile_loading_base_test.cc

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

diff --git a/source/blender/blenkernel/BKE_idtype.h b/source/blender/blenkernel/BKE_idtype.h
new file mode 100644
index 00000000000..ba0cf04dfc6
--- /dev/null
+++ b/source/blender/blenkernel/BKE_idtype.h
@@ -0,0 +1,142 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ */
+
+#ifndef __BKE_IDTYPE_H__
+#define __BKE_IDTYPE_H__
+
+/** \file
+ * \ingroup bke
+ *
+ * ID type structure, helping to factorize common operations and data for all data-block types.
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct ID;
+struct Main;
+
+/** IDTypeInfo.flags. */
+enum {
+  /** Indicates that the given IDType does not support copying. */
+  IDTYPE_FLAGS_NO_COPY = 1 << 0,
+  /** Indicates that the given IDType does not support linking/appending from a library file. */
+  IDTYPE_FLAGS_NO_LIBLINKING = 1 << 1,
+  /** Indicates that the given IDType does not support making a library-linked ID local. */
+  IDTYPE_FLAGS_NO_MAKELOCAL = 1 << 2,
+};
+
+/* ********** Prototypes for IDTypeInfo callbacks. ********** */
+
+typedef void (*IDTypeInitDataFunction)(struct ID *id);
+
+/** \param flag: Copying options (see BKE_lib_id.h's LIB_ID_COPY_... flags for more). */
+typedef void (*IDTypeCopyDataFunction)(struct Main *bmain,
+                                       struct ID *id_dst,
+                                       const struct ID *id_src,
+                                       const int flag);
+
+typedef void (*IDTypeFreeDataFunction)(struct ID *id);
+
+/** \param flag: See BKE_lib_id.h's LIB_ID_MAKELOCAL_... flags. */
+typedef void (*IDTypeMakeLocalFunction)(struct Main *bmain, struct ID *id, const int flags);
+
+typedef struct IDTypeInfo {
+  /* ********** General IDType data. ********** */
+
+  /**
+   * Unique identifier of this type, either as a short or an array of two chars, see DNA_ID.h's
+   * ID_XX enums.
+   */
+  short id_code;
+  /**
+   * Bitflag matching id_code, used for filtering (e.g. in file browser), see DNA_ID.h's
+   * FILTER_ID_XX enums.
+   */
+  int id_filter;
+
+  /**
+   * Define the position of this data-block type in the virtual list of all data in a Main that is
+   * returned by `set_listbasepointers()`.
+   * Very important, this has to be unique and below INDEX_ID_MAX, see DNA_ID.h.
+   */
+  short main_listbase_index;
+
+  /** Memory size of a data-block of that type. */
+  size_t struct_size;
+
+  /** The user visible name for this data-block, also used as default name for a new data-block. */
+  const char *name;
+  /** Plural version of the user-visble name. */
+  const char *name_plural;
+  /** Translation context to use for UI messages related to that type of data-block. */
+  const char *translation_context;
+
+  /** Generic info flags about that data-block type. */
+  int flags;
+
+  /* ********** ID management callbacks ********** */
+
+  /* TODO: Note about callbacks: Ideally we could also handle here `BKE_lib_query`'s behavior, as
+   * well as read/write of files. However, this is a bit more involved than basic ID management
+   * callbacks, so we'll check on this later. */
+
+  /**
+   * Initialize a new, empty calloc'ed data-block. May be NULL if there is nothing to do.
+   */
+  IDTypeInitDataFunction init_data;
+
+  /**
+   * Copy the given data-block's data from source to destination. May be NULL if mere memcopy of
+   * the ID struct itself is enough.
+   */
+  IDTypeCopyDataFunction copy_data;
+
+  /**
+   * Free the data of the data-block (NOT the ID itself). May be NULL if there is nothing to do.
+   */
+  IDTypeFreeDataFunction free_data;
+
+  /**
+   * Make a linked data-block local. May be NULL if default behavior from
+   * `BKE_lib_id_make_local_generic()` is enough.
+   */
+  IDTypeMakeLocalFunction make_local;
+} IDTypeInfo;
+
+/* ********** Declaration of each IDTypeInfo. ********** */
+
+/* Those are defined in the respective BKE files. */
+extern IDTypeInfo IDType_ID_OB;
+
+/* ********** Helpers/Utils API. ********** */
+
+/* Module initialization. */
+void BKE_idtype_init(void);
+
+/* General helpers. */
+const struct IDTypeInfo *BKE_idtype_get_info_from_idcode(const short id_code);
+const struct IDTypeInfo *BKE_idtype_get_info_from_id(const struct ID *id);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __BKE_IDTYPE_H__ */
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index 0a3b2450390..87ac82aaa55 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -125,6 +125,7 @@ set(SRC
   intern/idcode.c
   intern/idprop.c
   intern/idprop_utils.c
+  intern/idtype.c
   intern/image.c
   intern/image_gen.c
   intern/image_save.c
@@ -292,6 +293,7 @@ set(SRC
   BKE_icons.h
   BKE_idcode.h
   BKE_idprop.h
+  BKE_idtype.h
   BKE_image.h
   BKE_image_save.h
   BKE_ipo.h
diff --git a/source/blender/blenkernel/intern/idtype.c b/source/blender/blenkernel/intern/idtype.c
new file mode 100644
index 00000000000..494d83535b4
--- /dev/null
+++ b/source/blender/blenkernel/intern/idtype.c
@@ -0,0 +1,81 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software  Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2005 by the Blender Foundation.
+ * All rights reserved.
+ * Modifier stack implementation.
+ *
+ * BKE_modifier.h contains the function prototypes for this file.
+ */
+
+/** \file
+ * \ingroup bke
+ */
+
+#include "MEM_guardedalloc.h"
+
+#include "BLI_utildefines.h"
+
+#include "CLG_log.h"
+
+#include "BLT_translation.h"
+
+#include "DNA_ID.h"
+
+#include "BKE_idcode.h"
+
+#include "BKE_idtype.h"
+
+// static CLG_LogRef LOG = {"bke.idtype"};
+
+static IDTypeInfo *id_types[INDEX_ID_MAX] = {NULL};
+
+static void id_type_init(void)
+{
+#define INIT_TYPE(_id_code) \
+  { \
+    BLI_assert(IDType_##_id_code.main_listbase_index == INDEX_##_id_code); \
+    id_types[INDEX_##_id_code] = &IDType_##_id_code; \
+  } \
+  (void)0
+
+  INIT_TYPE(ID_OB);
+
+#undef INIT_TYPE
+}
+
+void BKE_idtype_init(void)
+{
+  /* Initialize data-block types. */
+  id_type_init();
+}
+
+const IDTypeInfo *BKE_idtype_get_info_from_idcode(const short id_code)
+{
+  int id_index = BKE_idcode_to_index(id_code);
+
+  if (id_index >= 0 && id_index < INDEX_ID_MAX && id_types[id_index] != NULL &&
+      id_types[id_index]->name[0] != '\0') {
+    return id_types[id_index];
+  }
+  else {
+    return NULL;
+  }
+}
+
+const IDTypeInfo *BKE_idtype_get_info_from_id(const ID *id)
+{
+  return BKE_idtype_get_info_from_idcode(GS(id->name));
+}
diff --git a/source/blender/blenkernel/intern/lib_id.c b/source/blender/blenkernel/intern/lib_id.c
index 73a03294c51..416de844691 100644
--- a/source/blender/blenkernel/intern/lib_id.c
+++ b/source/blender/blenkernel/intern/lib_id.c
@@ -91,6 +91,7 @@
 #include "BKE_gpencil.h"
 #include "BKE_idcode.h"
 #include "BKE_idprop.h"
+#include "BKE_idtype.h"
 #include "BKE_image.h"
 #include "BKE_key.h"
 #include "BKE_light.h"
@@ -447,6 +448,23 @@ bool BKE_lib_id_make_local(Main *bmain, ID *id, const bool test, const int flags
     return false;
   }
 
+  const IDTypeInfo *idtype_info = BKE_idtype_get_info_from_id(id);
+
+  if (idtype_info != NULL) {
+    if ((idtype_info->flags & IDTYPE_FLAGS_NO_MAKELOCAL) == 0) {
+      if (!test) {
+        if (idtype_info->make_local != NULL) {
+          idtype_info->make_local(bmain, id, flags);
+        }
+        else {
+          BKE_lib_id_make_local_generic(bmain, id, flags);
+        }
+      }
+      return true;
+    }
+    return false;
+  }
+
   switch ((ID_Type)GS(id->name)) {
     case ID_SCE:
       if (!test) {
@@ -689,115 +707,131 @@ bool BKE_id_copy_ex(Main *bmain, const ID *id, ID **r_newid, const int flag)
   if (id == NULL) {
     return false;
   }
-  if (!BKE_id_copy_is_allowed(id)) {
-    return false;
+
+  const IDTypeInfo *idtype_info = BKE_idtype_get_info_from_id(id);
+
+  if (idtype_info != NULL) {
+    if ((idtype_info->flags & IDTYPE_FLAGS_NO_COPY) != 0) {
+      return false;
+    }
+
+    BKE_libblock_copy_ex(bmain, id, r_newid, flag);
+
+    if (idtype_info->copy_data != NULL) {
+      idtype_info->copy_data(bmain, *r_newid, id, flag);
+    }
   }
+  else {
+    if (!BKE_id_copy_is_allowed(id)) {
+      return false;
+    }
 
-  BKE_libblock_copy_ex(bmain, id, r_newid, flag);
+    BKE_libblock_copy_ex(bmain, id, r_newid, flag);
 
-  switch ((ID_Type)GS(id->name)) {
-    case ID_SCE:
-      BKE_scene_copy_data(bmain, (Scene *)*r_newid, (Scene *)id, flag);
-      break;
-    case ID_OB:
-      BKE_object_copy_data(bmain, (Object *)*r_newid, (Object *)id, flag);
-      break;
-    case ID_ME:
-      BKE_mesh_copy_data(bmain, (Mesh *)*r_newid, (Mesh *)id, flag);
-      break;
-    case ID_CU:
-      BKE_curve_copy_data(bmain, (Curve *)*r_newid, (Curve *)id, flag);
-      break;
-    case ID_MB:
-      BKE_mball_copy_data(bmain, (Me

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list