[Bf-blender-cvs] [f65eb7e600a] idtype-refactor: Initial demo step for IDType refactor 'cleanup' project.

Bastien Montagne noreply at git.blender.org
Fri Feb 28 14:46:59 CET 2020


Commit: f65eb7e600a9f625df0b4440e3bbcd3929abfc9b
Author: Bastien Montagne
Date:   Fri Feb 28 14:45:32 2020 +0100
Branches: idtype-refactor
https://developer.blender.org/rBf65eb7e600a9f625df0b4440e3bbcd3929abfc9b

Initial demo step for IDType refactor 'cleanup' project.

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

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/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..d9dbf560d1a
--- /dev/null
+++ b/source/blender/blenkernel/BKE_idtype.h
@@ -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) 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;
+
+enum {
+  IDTYPE_FLAGS_IS_LINKABLE = 1 << 0,
+};
+
+typedef void (*IDTypeInitDataFunction)(struct ID *id);
+
+typedef struct IDTypeInfo {
+  /* Unique identifier of this type, either as a short or an array of two chars. */
+  short id_code;
+  /* Bitflag matching id_type, used for filtering (e.g. in file browser). */
+  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. */
+  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 ********** */
+  IDTypeInitDataFunction init_data;
+} IDTypeInfo;
+
+/* Module initialization. */
+void BKE_idtype_init(void);
+
+const struct IDTypeInfo *BKE_idtype_get_info_from_idcode(short id_code);
+const struct IDTypeInfo *BKE_idtype_get_info_from_id(struct ID *id);
+
+extern IDTypeInfo IDType_ID_OB;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __BKE_IDTYPE_H__ */
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index f536b8d94fc..407b9d8e417 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..229cc9393c4
--- /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(short id_code)
+{
+  int id_index = BKE_idcode_to_index(id_code);
+
+  if (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(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 051b43a177e..39da370a178 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"
@@ -1258,6 +1259,18 @@ void *BKE_libblock_alloc(Main *bmain, short type, const char *name, const int fl
  */
 void BKE_libblock_init_empty(ID *id)
 {
+  const IDTypeInfo *idtype_info = BKE_idtype_get_info_from_id(id);
+
+  printf("%s: ", __func__);
+
+  if (idtype_info != NULL && idtype_info->init_data != NULL) {
+    printf("Found IDTypeInfo for %c%c, using new code...\n", id->name[0], id->name[1]);
+    idtype_info->init_data(id);
+    return;
+  }
+
+  printf("No IDTypeInfo for %c%c, using old code...\n", id->name[0], id->name[1]);
+
   /* Note that only ID types that are not valid when filled of zero should have a callback here. */
   switch ((ID_Type)GS(id->name)) {
     case ID_SCE:
diff --git a/source/blender/blenkernel/intern/object.c b/source/blender/blenkernel/intern/object.c
index 4d7d5861a09..52508257c95 100644
--- a/source/blender/blenkernel/intern/object.c
+++ b/source/blender/blenkernel/intern/object.c
@@ -85,6 +85,7 @@
 #include "BKE_fcurve.h"
 #include "BKE_gpencil_modifier.h"
 #include "BKE_icons.h"
+#include "BKE_idtype.h"
 #include "BKE_key.h"
 #include "BKE_light.h"
 #include "BKE_layer.h"
@@ -144,6 +145,35 @@ static CLG_LogRef LOG = {"bke.object"};
 static ThreadMutex vparent_lock = BLI_MUTEX_INITIALIZER;
 #endif
 
+static void object_init_data(ID *id)
+{
+  Object *ob = (Object *)id;
+  BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(ob, id));
+
+  MEMCPY_STRUCT_AFTER(ob, DNA_struct_default_get(Object), id);
+
+  ob->type = OB_EMPTY;
+
+  ob->trackflag = OB_POSY;
+  ob->upflag = OB_POSZ;
+
+  /* Animation Visualization defaults */
+  animviz_settings_init(&ob->avs);
+}
+
+IDTypeInfo IDType_ID_OB = {
+    .id_code = ID_OB,
+    .id_filter = FILTER_ID_OB,
+    .main_listbase_index = INDEX_ID_OB,
+    .struct_size = sizeof(Object),
+    .name = "Object",
+    .name_plural = "objects",
+    .translation_context = BLT_I18NCONTEXT_ID_ACTION,
+    .flags = IDTYPE_FLAGS_IS_LINKABLE,
+
+    .init_data = object_init_data,
+};
+
 void BKE_object_workob_clear(Object *workob)
 {
   memset(workob, 0, sizeof(Object));
@@ -872,9 +902,7 @@ void *BKE_object_obdata_add_from_type(Main *bmain, int type, const char *name)
 
 void BKE_object_init(Object *ob, const short ob_type)
 {
-  BLI_assert(MEMCMP_STRUCT_AFTER_IS_ZERO(ob, id));
-
-  MEMCPY_STRUCT_AFTER(ob, DNA_struct_default_get(Object), id);
+  object_init_data(&ob->id);
 
   ob->type = ob_type;
 
@@ -886,13 +914,6 @@ void BKE_object_init(Object *ob, const short ob_type)
     ob->trackflag = OB_NEGZ;
     ob->upflag = OB_POSY;
   }
-  else {
-    ob->trackflag = OB_POSY;
-    ob->upflag = OB_POSZ;
-  }
-
-  /* Animation Visualization defaults */
-  animviz_settings_init(&ob->avs);
 }
 
 /* more general add: creates minimum required data, but without vertices etc. */
diff --git a/source/creator/creator.c b/source/creator/creator.c
index cf25be95472..14db899b4fd 100644
--- a/source/creator/creator.c
+++ b/source/creator/creator.c
@@ -55,6 +55,7 @@
 #include "BKE_context.h"
 #include "BKE_font.h"
 #include "BKE_global.h"
+#include "BKE_idtype.h"
 #include "BKE_material.h"
 #include "BKE_modifier.h"
 #include "BKE_gpencil_modifier.h"
@@ -366,6 +367,7 @@ int main(int argc,
 
   BKE_blender_globals_init(); /* blender.c */
 
+  BKE_idtype_init();
   IMB_init();
   BKE_cachefiles_init();
   BKE_images_init();
diff --git a/tests/gtests/blenloader/blendfile_loading_base_test.cc b/tests/gtests/blenloader/blendfile_loading_base_test.cc
index c4e873c255f..4f871bc7ddf 100644
--- a/tests/gtests/blenloader/blendfile_loading_base_test.cc
+++ b/tests/gtests/blenloader/blendfile_loading_base_test.cc
@@ -24,6 +24,7 @@ extern "C" {
 #include "BKE_blender.h"
 #include "BKE_context.h"
 #include "BKE_global.h"
+#include "BKE_idtype.h"
 #include "BKE_image.h"
 #include "BKE_main.h"
 #include "BKE_modifier.h"
@@ -65,6 +66,8 @@ void BlendfileLoadingBaseTest::SetUpTestCase()
 
   DNA_sdna_current_init();
   BKE_blender_globals_init();
+
+  BKE_idtype_init();
   IMB_init();
   BKE_images_init();
   BKE_modifier_init();



More information about the Bf-blender-cvs mailing list