[Bf-blender-cvs] [802c8203342] functions: improved static and dynamic type checking for function bodies

Jacques Lucke noreply at git.blender.org
Thu Sep 26 16:51:40 CEST 2019


Commit: 802c82033420837050ea9bafe89b855840b0ca78
Author: Jacques Lucke
Date:   Thu Sep 26 14:05:16 2019 +0200
Branches: functions
https://developer.blender.org/rB802c82033420837050ea9bafe89b855840b0ca78

improved static and dynamic type checking for function bodies

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

M	source/blender/functions/backends/dependencies/dependencies.hpp
M	source/blender/functions/backends/llvm/build_ir_body.hpp
M	source/blender/functions/backends/tuple_call/tuple_call.hpp
M	source/blender/functions/core/function.hpp
M	source/blender/functions/frontends/data_flow_nodes/unlinked_input_inserters.cpp
M	source/blender/functions/frontends/data_flow_nodes/vtree_data_graph.hpp

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

diff --git a/source/blender/functions/backends/dependencies/dependencies.hpp b/source/blender/functions/backends/dependencies/dependencies.hpp
index 41c5d6b0a84..faab25520f6 100644
--- a/source/blender/functions/backends/dependencies/dependencies.hpp
+++ b/source/blender/functions/backends/dependencies/dependencies.hpp
@@ -76,6 +76,7 @@ class FunctionDepsBuilder {
 class DepsBody : public FunctionBody {
  public:
   static const uint FUNCTION_BODY_ID = 0;
+  using FunctionBodyType = DepsBody;
 
   virtual ~DepsBody()
   {
diff --git a/source/blender/functions/backends/llvm/build_ir_body.hpp b/source/blender/functions/backends/llvm/build_ir_body.hpp
index ebd58bcb3f7..67586900482 100644
--- a/source/blender/functions/backends/llvm/build_ir_body.hpp
+++ b/source/blender/functions/backends/llvm/build_ir_body.hpp
@@ -65,6 +65,7 @@ class CodeInterface {
 class LLVMBuildIRBody : public FunctionBody {
  public:
   static const uint FUNCTION_BODY_ID = 3;
+  using FunctionBodyType = LLVMBuildIRBody;
 
   virtual ~LLVMBuildIRBody(){};
 
diff --git a/source/blender/functions/backends/tuple_call/tuple_call.hpp b/source/blender/functions/backends/tuple_call/tuple_call.hpp
index cb6edced536..ed0f7567743 100644
--- a/source/blender/functions/backends/tuple_call/tuple_call.hpp
+++ b/source/blender/functions/backends/tuple_call/tuple_call.hpp
@@ -44,6 +44,7 @@ class TupleCallBodyBase : public FunctionBody {
 class TupleCallBody : public TupleCallBodyBase {
  public:
   static const uint FUNCTION_BODY_ID = 1;
+  using FunctionBodyType = TupleCallBody;
 
   /**
    * Calls the function with additional stack frames.
@@ -158,6 +159,7 @@ class LazyState {
 class LazyInTupleCallBody : public TupleCallBodyBase {
  public:
   static const uint FUNCTION_BODY_ID = 2;
+  using FunctionBodyType = LazyInTupleCallBody;
 
   /**
    * Required buffer size for temporary data.
diff --git a/source/blender/functions/core/function.hpp b/source/blender/functions/core/function.hpp
index 611d4fbce04..a4bcd5c9322 100644
--- a/source/blender/functions/core/function.hpp
+++ b/source/blender/functions/core/function.hpp
@@ -196,25 +196,32 @@ inline const StringRefNull Function::name() const
 template<typename T> inline bool Function::has_body() const
 {
   STATIC_ASSERT_BODY_TYPE(T);
+  BLI_STATIC_ASSERT((std::is_same<T, typename T::FunctionBodyType>::value), "");
+
   return m_bodies[T::FUNCTION_BODY_ID] != nullptr;
 }
 
 template<typename T> inline T &Function::body() const
 {
   STATIC_ASSERT_BODY_TYPE(T);
+  BLI_STATIC_ASSERT((std::is_same<T, typename T::FunctionBodyType>::value), "");
   BLI_assert(this->has_body<T>());
-  return *(T *)m_bodies[T::FUNCTION_BODY_ID];
+
+  FunctionBody *body_ptr = m_bodies[T::FUNCTION_BODY_ID];
+  BLI_assert(dynamic_cast<T *>(body_ptr) == static_cast<T *>(body_ptr));
+  return *static_cast<T *>(body_ptr);
 }
 
 template<typename T, typename... Args> inline T *Function::add_body(Args &&... args)
 {
   STATIC_ASSERT_BODY_TYPE(T);
+  BLI_STATIC_ASSERT((std::is_base_of<FunctionBody, typename T::FunctionBodyType>::value), "")
 
   std::lock_guard<std::mutex> lock(m_modify_mutex);
   if (m_bodies[T::FUNCTION_BODY_ID] == nullptr) {
     T *new_body = new T(std::forward<Args>(args)...);
     new_body->set_owner(this);
-    m_bodies[T::FUNCTION_BODY_ID] = new_body;
+    m_bodies[T::FUNCTION_BODY_ID] = dynamic_cast<FunctionBody *>(new_body);
     return new_body;
   }
   else {
diff --git a/source/blender/functions/frontends/data_flow_nodes/unlinked_input_inserters.cpp b/source/blender/functions/frontends/data_flow_nodes/unlinked_input_inserters.cpp
index df2a04853f2..bbb7d4e92c2 100644
--- a/source/blender/functions/frontends/data_flow_nodes/unlinked_input_inserters.cpp
+++ b/source/blender/functions/frontends/data_flow_nodes/unlinked_input_inserters.cpp
@@ -213,8 +213,9 @@ class LoadFromAddresses : public TupleCallBody {
 ReloadableInputs::~ReloadableInputs()
 {
   for (SharedFunction &fn : m_functions) {
-    LoadFromAddresses &body = fn->body<LoadFromAddresses>();
-    body.set_deallocated();
+    TupleCallBody &body = fn->body<TupleCallBody>();
+    LoadFromAddresses &typed_body = dynamic_cast<LoadFromAddresses &>(body);
+    typed_body.set_deallocated();
   }
 }
 
diff --git a/source/blender/functions/frontends/data_flow_nodes/vtree_data_graph.hpp b/source/blender/functions/frontends/data_flow_nodes/vtree_data_graph.hpp
index 7284fcbe673..f2bfe79688a 100644
--- a/source/blender/functions/frontends/data_flow_nodes/vtree_data_graph.hpp
+++ b/source/blender/functions/frontends/data_flow_nodes/vtree_data_graph.hpp
@@ -80,6 +80,7 @@ class VNodePlaceholderBody : public FunctionBody {
 
  public:
   static const uint FUNCTION_BODY_ID = 4;
+  using FunctionBodyType = VNodePlaceholderBody;
 
   VNodePlaceholderBody(VirtualNode *vnode, Vector<VirtualSocket *> vsocket_inputs)
       : m_vnode(vnode), m_vsocket_inputs(std::move(vsocket_inputs))



More information about the Bf-blender-cvs mailing list