[Bf-blender-cvs] [fbe0a3950f7] functions: more accurate runtime type checking in debug builds

Jacques Lucke noreply at git.blender.org
Wed Sep 4 10:51:20 CEST 2019


Commit: fbe0a3950f7a0328fb799047479d28c148c5bc0d
Author: Jacques Lucke
Date:   Wed Sep 4 10:51:06 2019 +0200
Branches: functions
https://developer.blender.org/rBfbe0a3950f7a0328fb799047479d28c148c5bc0d

more accurate runtime type checking in debug builds

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

M	source/blender/functions/backends/cpp/cpp_type_info.hpp
M	source/blender/functions/backends/cpp/tuple.hpp
M	source/blender/simulations/bparticles/node_frontend.cpp

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

diff --git a/source/blender/functions/backends/cpp/cpp_type_info.hpp b/source/blender/functions/backends/cpp/cpp_type_info.hpp
index aa5b8415a29..3c11753834e 100644
--- a/source/blender/functions/backends/cpp/cpp_type_info.hpp
+++ b/source/blender/functions/backends/cpp/cpp_type_info.hpp
@@ -10,6 +10,10 @@
 
 #include "FN_core.hpp"
 
+#ifdef DEBUG
+#  include <typeinfo>
+#endif
+
 namespace FN {
 
 class CPPTypeInfo : public TypeExtension {
@@ -95,6 +99,10 @@ class CPPTypeInfo : public TypeExtension {
    */
   virtual void relocate_to_uninitialized(void *src, void *dst) const = 0;
   virtual void relocate_to_uninitialized_n(void *src, void *dst, uint n) const = 0;
+
+#ifdef DEBUG
+  virtual bool has_type_info(const std::type_info &type) const = 0;
+#endif
 };
 
 template<typename T> class CPPTypeInfoForType : public CPPTypeInfo {
@@ -166,10 +174,17 @@ template<typename T> class CPPTypeInfoForType : public CPPTypeInfo {
     BLI::uninitialized_relocate((T *)src, (T *)dst);
   }
 
-  virtual void relocate_to_uninitialized_n(void *src, void *dst, uint n) const override
+  void relocate_to_uninitialized_n(void *src, void *dst, uint n) const override
   {
     BLI::uninitialized_relocate_n((T *)src, n, (T *)dst);
   }
+
+#ifdef DEBUG
+  bool has_type_info(const std::type_info &type) const override
+  {
+    return type == typeid(T);
+  }
+#endif
 };
 
 } /* namespace FN */
diff --git a/source/blender/functions/backends/cpp/tuple.hpp b/source/blender/functions/backends/cpp/tuple.hpp
index 041edb86380..08b84db0e54 100644
--- a/source/blender/functions/backends/cpp/tuple.hpp
+++ b/source/blender/functions/backends/cpp/tuple.hpp
@@ -127,6 +127,13 @@ class TupleMeta : public RefCounter {
   {
     return m_all_trivially_destructible;
   }
+
+#ifdef DEBUG
+  template<typename T> bool element_has_type(uint index) const
+  {
+    return m_type_info[index]->has_type_info(typeid(T));
+  }
+#endif
 };
 
 using SharedTupleMeta = AutoRefCount<TupleMeta>;
@@ -205,7 +212,7 @@ class Tuple {
   template<typename T> inline void copy_in(uint index, const T &value)
   {
     BLI_assert(index < m_meta->size());
-    BLI_assert(sizeof(T) == m_meta->element_size(index));
+    BLI_assert(m_meta->element_has_type<T>(index));
 
     T *dst = (T *)this->element_ptr(index);
     if (std::is_trivial<T>::value) {
@@ -252,7 +259,7 @@ class Tuple {
   template<typename T> inline void move_in(uint index, T &value)
   {
     BLI_assert(index < m_meta->size());
-    BLI_assert(sizeof(T) == m_meta->element_size(index));
+    BLI_assert(m_meta->element_has_type<T>(index));
 
     T *dst = (T *)this->element_ptr(index);
 
@@ -303,7 +310,7 @@ class Tuple {
   template<typename T> inline T copy_out(uint index) const
   {
     BLI_assert(index < m_meta->size());
-    BLI_assert(sizeof(T) == m_meta->element_size(index));
+    BLI_assert(m_meta->element_has_type<T>(index));
     BLI_assert(m_initialized[index]);
 
     return *(T *)this->element_ptr(index);
@@ -318,7 +325,7 @@ class Tuple {
   template<typename T> inline T relocate_out(uint index) const
   {
     BLI_assert(index < m_meta->size());
-    BLI_assert(sizeof(T) == m_meta->element_size(index));
+    BLI_assert(m_meta->element_has_type<T>(index));
     BLI_assert(m_initialized[index]);
 
     T &value = this->element_ref<T>(index);
@@ -366,6 +373,7 @@ class Tuple {
   template<typename T> inline T &get_ref(uint index) const
   {
     BLI_assert(index < m_meta->size());
+    BLI_assert(m_meta->element_has_type<T>(index));
     BLI_assert(m_initialized[index]);
     return this->element_ref<T>(index);
   }
diff --git a/source/blender/simulations/bparticles/node_frontend.cpp b/source/blender/simulations/bparticles/node_frontend.cpp
index 43f2e7b652f..c9f9337a19d 100644
--- a/source/blender/simulations/bparticles/node_frontend.cpp
+++ b/source/blender/simulations/bparticles/node_frontend.cpp
@@ -389,12 +389,13 @@ static void PARSE_initial_grid_emitter(BehaviorCollector &collector,
   body.call__setup_execution_context(fn_in, fn_out);
 
   Vector<std::string> type_names = find_connected_particle_type_names(vnode->output(0, "Emitter"));
-  Emitter *emitter = new InitialGridEmitter(std::move(type_names),
-                                            body.get_output<uint>(fn_out, 0, "Amount X"),
-                                            body.get_output<uint>(fn_out, 1, "Amount Y"),
-                                            body.get_output<float>(fn_out, 2, "Step X"),
-                                            body.get_output<float>(fn_out, 3, "Step Y"),
-                                            body.get_output<float>(fn_out, 4, "Size"));
+  Emitter *emitter = new InitialGridEmitter(
+      std::move(type_names),
+      std::max(0, body.get_output<int>(fn_out, 0, "Amount X")),
+      std::max(0, body.get_output<int>(fn_out, 1, "Amount Y")),
+      body.get_output<float>(fn_out, 2, "Step X"),
+      body.get_output<float>(fn_out, 3, "Step Y"),
+      body.get_output<float>(fn_out, 4, "Size"));
   collector.m_emitters.append(emitter);
 }



More information about the Bf-blender-cvs mailing list