[Bf-blender-cvs] [a04a39b] depsgraph_refactor: Added upcast functions for safely converting a generic ID pointer into one of the DNA structs.

Lukas Tönne noreply at git.blender.org
Tue May 13 18:41:09 CEST 2014


Commit: a04a39bcc9228298629ac9f9fb07b0a05426790f
Author: Lukas Tönne
Date:   Mon May 12 18:23:26 2014 +0200
https://developer.blender.org/rBa04a39bcc9228298629ac9f9fb07b0a05426790f

Added upcast functions for safely converting a generic ID pointer into
one of the DNA structs.

This uses the GS macro to compare an ID block's prefix to the associated
DNA struct. The static_cast_id variant only does contains an assert,
while the dynamic_cast_id variant safely checks and returns a NULL
pointer in case the ID type doesn't match.

A new utility header for type traits has been added. These are standard
techniques used in boost and C++11, but added as own implementation here
to avoid nasty dependencies.

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

M	source/blender/depsgraph/CMakeLists.txt
M	source/blender/depsgraph/util/depsgraph_util_id.h
A	source/blender/depsgraph/util/depsgraph_util_type_traits.h

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

diff --git a/source/blender/depsgraph/CMakeLists.txt b/source/blender/depsgraph/CMakeLists.txt
index 463a38b..f0a435b 100644
--- a/source/blender/depsgraph/CMakeLists.txt
+++ b/source/blender/depsgraph/CMakeLists.txt
@@ -74,6 +74,7 @@ set(SRC
 	util/depsgraph_util_rna.h
 	util/depsgraph_util_set.h
 	util/depsgraph_util_string.h
+	util/depsgraph_util_type_traits.h
 )
 
 TEST_UNORDERED_MAP_SUPPORT()
diff --git a/source/blender/depsgraph/util/depsgraph_util_id.h b/source/blender/depsgraph/util/depsgraph_util_id.h
index c092730..0a76675 100644
--- a/source/blender/depsgraph/util/depsgraph_util_id.h
+++ b/source/blender/depsgraph/util/depsgraph_util_id.h
@@ -25,8 +25,108 @@
 #ifndef __DEPSGRAPH_UTIL_ID_H__
 #define __DEPSGRAPH_UTIL_ID_H__
 
+#include "depsgraph_util_type_traits.h"
+
 extern "C" {
 #include "BLI_utildefines.h"
+
+#include "DNA_ID.h"
+}
+
+/* Get the ID code based on associated DNA struct type */
+template <typename IDType>
+struct IDCode {
+	operator int() const { return 0; } /* return 'invalid' code 0 by default */
+};
+
+#define DEF_ID_CODE_TYPE(code, type) \
+	struct type; /* declare struct type */ \
+	\
+	template <> \
+	struct IDCode<type> { \
+		operator int() const { return code; } \
+	};
+
+/* XXX Copied from idcode.c, where this only exists in string form.
+ * Should be unified to provide real compile-time info about associated struct types!
+ */
+
+DEF_ID_CODE_TYPE(ID_AC,     Action)
+DEF_ID_CODE_TYPE(ID_AR,     Armature)
+DEF_ID_CODE_TYPE(ID_BR,     Brush)
+DEF_ID_CODE_TYPE(ID_CA,     Camera)
+DEF_ID_CODE_TYPE(ID_CU,     Curve)
+DEF_ID_CODE_TYPE(ID_GD,     GPencil)
+DEF_ID_CODE_TYPE(ID_GR,     Group)
+DEF_ID_CODE_TYPE(ID_ID,     ID)
+DEF_ID_CODE_TYPE(ID_IM,     Image)
+DEF_ID_CODE_TYPE(ID_IP,     Ipo)
+DEF_ID_CODE_TYPE(ID_KE,     Key)
+DEF_ID_CODE_TYPE(ID_LA,     Lamp)
+DEF_ID_CODE_TYPE(ID_LI,     Library)
+DEF_ID_CODE_TYPE(ID_LS,     FreestyleLineStyle)
+DEF_ID_CODE_TYPE(ID_LT,     Lattice)
+DEF_ID_CODE_TYPE(ID_MA,     Material)
+DEF_ID_CODE_TYPE(ID_MB,     Metaball)
+DEF_ID_CODE_TYPE(ID_MC,     MovieClip)
+DEF_ID_CODE_TYPE(ID_ME,     Mesh)
+DEF_ID_CODE_TYPE(ID_MSK,    Mask)
+DEF_ID_CODE_TYPE(ID_NT,     NodeTree)
+DEF_ID_CODE_TYPE(ID_OB,     Object)
+DEF_ID_CODE_TYPE(ID_PA,     ParticleSettings)
+DEF_ID_CODE_TYPE(ID_SCE,    Scene)
+DEF_ID_CODE_TYPE(ID_SCR,    Screen)
+DEF_ID_CODE_TYPE(ID_SEQ,    Sequence)
+DEF_ID_CODE_TYPE(ID_SPK,    Speaker)
+DEF_ID_CODE_TYPE(ID_SO,     Sound)
+DEF_ID_CODE_TYPE(ID_TE,     Texture)
+DEF_ID_CODE_TYPE(ID_TXT,    Text)
+DEF_ID_CODE_TYPE(ID_VF,     VFont)
+DEF_ID_CODE_TYPE(ID_WO,     World)
+DEF_ID_CODE_TYPE(ID_WM,     WindowManager)
+
+#undef DEF_ID_CODE_TYPE
+
+template <typename IDPtrType>
+IDPtrType static_cast_id(ID *id)
+{
+	typedef typename remove_pointer<IDPtrType>::type IDType;
+	
+	if (id)
+		BLI_assert(IDCode<IDType>() == GS(id->name));
+	return (IDType *)id;
+}
+
+template <typename IDPtrType>
+IDPtrType static_cast_id(const ID *id)
+{
+	typedef typename remove_pointer<IDPtrType>::type IDType;
+	
+	if (id)
+		BLI_assert(IDCode<IDType>() == GS(id->name));
+	return (const IDType *)id;
+}
+
+template <typename IDPtrType>
+IDPtrType *dynamic_cast_id(ID *id)
+{
+	typedef typename remove_pointer<IDPtrType>::type IDType;
+	
+	if (id && IDCode<IDType>() == GS(id->name))
+		return (IDType *)id;
+	else
+		return NULL;
+}
+
+template <typename IDPtrType>
+IDPtrType *dynamic_cast_id(const ID *id)
+{
+	typedef typename remove_pointer<IDPtrType>::type IDType;
+	
+	if (id && IDCode<IDType>() == GS(id->name))
+		return (const IDType *)id;
+	else
+		return NULL;
 }
 
 /* Helper types for handling ID subtypes in C
diff --git a/source/blender/depsgraph/util/depsgraph_util_type_traits.h b/source/blender/depsgraph/util/depsgraph_util_type_traits.h
new file mode 100644
index 0000000..b6e33fd
--- /dev/null
+++ b/source/blender/depsgraph/util/depsgraph_util_type_traits.h
@@ -0,0 +1,38 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * 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) 2014 Blender Foundation.
+ * All rights reserved.
+ *
+ * Original Author: Lukas Toenne
+ * Contributor(s): None yet
+ */
+
+#ifndef __DEPSGRAPH_UTIL_TYPE_TRAITS_H__
+#define __DEPSGRAPH_UTIL_TYPE_TRAITS_H__
+
+template <typename T>
+struct remove_pointer {
+	typedef T type;
+};
+
+template <typename T>
+struct remove_pointer<T*> {
+	typedef T type;
+};
+
+#endif /* __DEPSGRAPH_UTIL_TYPE_TRAITS_H__ */




More information about the Bf-blender-cvs mailing list