[Bf-blender-cvs] [c5c05aa40b9] functions: improve NameTupleRef abstraction

Jacques Lucke noreply at git.blender.org
Fri Sep 13 15:47:42 CEST 2019


Commit: c5c05aa40b9841f57090d7b3e8b4897c711f49e2
Author: Jacques Lucke
Date:   Fri Sep 13 14:55:48 2019 +0200
Branches: functions
https://developer.blender.org/rBc5c05aa40b9841f57090d7b3e8b4897c711f49e2

improve NameTupleRef abstraction

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

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

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

diff --git a/source/blender/functions/backends/cpp/tuple.hpp b/source/blender/functions/backends/cpp/tuple.hpp
index 08b84db0e54..33dbbc6d3b7 100644
--- a/source/blender/functions/backends/cpp/tuple.hpp
+++ b/source/blender/functions/backends/cpp/tuple.hpp
@@ -586,6 +586,43 @@ inline uint TupleMeta::size_of_full_tuple() const
   return sizeof(Tuple) + this->size_of_data_and_init();
 }
 
+class TupleElementNameProvider {
+ public:
+  virtual StringRefNull get_element_name(uint index) const = 0;
+};
+
+class NamedTupleRef {
+ private:
+  Tuple *m_tuple;
+  TupleElementNameProvider *m_name_provider;
+
+ public:
+  NamedTupleRef(Tuple *tuple, TupleElementNameProvider *name_provider)
+      : m_tuple(tuple), m_name_provider(name_provider)
+  {
+  }
+
+  bool name_is_correct(uint index, StringRef name) const
+  {
+    StringRef real_name = m_name_provider->get_element_name(index);
+    return real_name == name;
+  }
+
+  template<typename T> T relocate_out(uint index, StringRef expected_name)
+  {
+    BLI_assert(this->name_is_correct(index, expected_name));
+    UNUSED_VARS_NDEBUG(expected_name);
+    return m_tuple->relocate_out<T>(index);
+  }
+
+  template<typename T> T get(uint index, StringRef expected_name)
+  {
+    BLI_assert(this->name_is_correct(index, expected_name));
+    UNUSED_VARS_NDEBUG(expected_name);
+    return m_tuple->get<T>(index);
+  }
+};
+
 } /* namespace FN */
 
 /**
diff --git a/source/blender/functions/backends/tuple_call/tuple_call.hpp b/source/blender/functions/backends/tuple_call/tuple_call.hpp
index c4dc4fa6d55..4a56a2b0b5e 100644
--- a/source/blender/functions/backends/tuple_call/tuple_call.hpp
+++ b/source/blender/functions/backends/tuple_call/tuple_call.hpp
@@ -255,34 +255,33 @@ class LazyInTupleCallBody : public TupleCallBodyBase {
   }
 };
 
-class OutputTupleRef {
+class FunctionInputNamesProvider final : public TupleElementNameProvider {
  private:
-  Tuple *m_tuple;
   Function *m_function;
 
  public:
-  OutputTupleRef(Tuple *tuple, Function *function) : m_tuple(tuple), m_function(function)
+  FunctionInputNamesProvider(Function *function) : m_function(function)
   {
   }
 
-  template<typename T> T relocate_out(uint index, StringRef expected_name)
+  StringRefNull get_element_name(uint index) const override
+  {
+    return m_function->input_name(index);
+  }
+};
+
+class FunctionOutputNamesProvider final : public TupleElementNameProvider {
+ private:
+  Function *m_function;
+
+ public:
+  FunctionOutputNamesProvider(Function *function) : m_function(function)
   {
-#ifdef DEBUG
-    StringRef real_name = m_function->output_name(index);
-    BLI_assert(real_name == expected_name);
-#endif
-    UNUSED_VARS_NDEBUG(expected_name);
-    return m_tuple->relocate_out<T>(index);
   }
 
-  template<typename T> T get(uint index, StringRef expected_name)
+  StringRefNull get_element_name(uint index) const override
   {
-#ifdef DEBUG
-    StringRef real_name = m_function->output_name(index);
-    BLI_assert(real_name == expected_name);
-#endif
-    UNUSED_VARS_NDEBUG(expected_name);
-    return m_tuple->get<T>(index);
+    return m_function->output_name(index);
   }
 };
 
diff --git a/source/blender/simulations/bparticles/node_frontend.cpp b/source/blender/simulations/bparticles/node_frontend.cpp
index a3c8e9d50e0..bfcd0f97555 100644
--- a/source/blender/simulations/bparticles/node_frontend.cpp
+++ b/source/blender/simulations/bparticles/node_frontend.cpp
@@ -25,8 +25,10 @@ using BLI::ValueOrError;
 using FN::Function;
 using FN::FunctionBuilder;
 using FN::FunctionGraph;
+using FN::NamedTupleRef;
 using FN::SharedDataGraph;
 using FN::DataFlowNodes::VTreeDataGraph;
+using FN::Types::FalloffW;
 using FN::Types::ObjectW;
 using FN::Types::StringW;
 
@@ -44,6 +46,7 @@ class VTreeData {
   Vector<std::unique_ptr<ParticleFunction>> m_particle_functions;
   Vector<SharedFunction> m_functions;
   Vector<std::unique_ptr<Tuple>> m_tuples;
+  Vector<std::unique_ptr<FN::FunctionOutputNamesProvider>> m_name_providers;
 
  public:
   VTreeData(VTreeDataGraph &vtree_data) : m_vtree_data_graph(vtree_data)
@@ -108,16 +111,19 @@ class VTreeData {
     return fn->body<TupleCallBody>();
   }
 
-  FN::OutputTupleRef compute_all_inputs(VirtualNode *vnode)
+  NamedTupleRef compute_all_inputs(VirtualNode *vnode)
   {
     TupleCallBody &body = this->function_body_for_all_inputs(vnode);
     FN_TUPLE_STACK_ALLOC(fn_in, body.meta_in().ref());
     FN::Tuple *fn_out = new FN::Tuple(body.meta_out());
 
     body.call__setup_execution_context(fn_in, *fn_out);
+    auto *name_provider = new FN::FunctionOutputNamesProvider(body.owner());
 
     m_tuples.append(std::unique_ptr<FN::Tuple>(fn_out));
-    return FN::OutputTupleRef(fn_out, body.owner());
+    m_name_providers.append(std::unique_ptr<FN::FunctionOutputNamesProvider>(name_provider));
+
+    return NamedTupleRef(fn_out, name_provider);
   }
 };
 
@@ -347,7 +353,7 @@ static void PARSE_point_emitter(InfluencesCollector &collector,
                                 WorldTransition &world_transition,
                                 VirtualNode *vnode)
 {
-  FN::OutputTupleRef inputs = vtree_data.compute_all_inputs(vnode);
+  NamedTupleRef inputs = vtree_data.compute_all_inputs(vnode);
   Vector<std::string> system_names = find_connected_particle_system_names(
       vnode->output(0, "Emitter"));
   std::string name = vnode->name();
@@ -363,7 +369,7 @@ static void PARSE_point_emitter(InfluencesCollector &collector,
 }
 
 static Vector<float> compute_emitter_vertex_weights(VirtualNode *vnode,
-                                                    FN::OutputTupleRef &inputs,
+                                                    NamedTupleRef inputs,
                                                     Object *object)
 {
   PointerRNA rna = vnode->rna();
@@ -418,7 +424,7 @@ static void PARSE_mesh_emitter(InfluencesCollector &collector,
                                WorldTransition &world_transition,
                                VirtualNode *vnode)
 {
-  FN::OutputTupleRef inputs = vtree_data.compute_all_inputs(vnode);
+  NamedTupleRef inputs = vtree_data.compute_all_inputs(vnode);
 
   std::unique_ptr<Action> on_birth_action = build_action_list(
       vtree_data, vnode, "Execute on Birth");
@@ -516,7 +522,7 @@ static void PARSE_initial_grid_emitter(InfluencesCollector &collector,
                                        WorldTransition &UNUSED(world_transition),
                                        VirtualNode *vnode)
 {
-  FN::OutputTupleRef inputs = vtree_data.compute_all_inputs(vnode);
+  NamedTupleRef inputs = vtree_data.compute_all_inputs(vnode);
 
   Vector<std::string> system_names = find_connected_particle_system_names(
       vnode->output(0, "Emitter"));



More information about the Bf-blender-cvs mailing list