[Bf-blender-cvs] [5b9ee5a55eb] functions: use NamedTupleRef in more places

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


Commit: 5b9ee5a55ebdc77f354082f5ad10e018f1c5cb22
Author: Jacques Lucke
Date:   Fri Sep 13 15:47:35 2019 +0200
Branches: functions
https://developer.blender.org/rB5b9ee5a55ebdc77f354082f5ad10e018f1c5cb22

use NamedTupleRef in more places

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

M	source/blender/functions/backends/cpp/tuple.hpp
M	source/blender/functions/backends/tuple_call/tuple_call.hpp
M	source/blender/functions/functions/color.cpp
M	source/blender/functions/functions/falloffs.cpp
M	source/blender/functions/functions/random.cpp
M	source/blender/functions/functions/ranges.cpp

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

diff --git a/source/blender/functions/backends/cpp/tuple.hpp b/source/blender/functions/backends/cpp/tuple.hpp
index 33dbbc6d3b7..cd06478bc46 100644
--- a/source/blender/functions/backends/cpp/tuple.hpp
+++ b/source/blender/functions/backends/cpp/tuple.hpp
@@ -621,6 +621,20 @@ class NamedTupleRef {
     UNUSED_VARS_NDEBUG(expected_name);
     return m_tuple->get<T>(index);
   }
+
+  template<typename T> void move_in(uint index, StringRef expected_name, T &value)
+  {
+    BLI_assert(this->name_is_correct(index, expected_name));
+    UNUSED_VARS_NDEBUG(expected_name);
+    m_tuple->move_in(index, value);
+  }
+
+  template<typename T> void set(uint index, StringRef expected_name, T &value)
+  {
+    BLI_assert(this->name_is_correct(index, expected_name));
+    UNUSED_VARS_NDEBUG(expected_name);
+    m_tuple->set(index, value);
+  }
 };
 
 } /* 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 4a56a2b0b5e..cb6edced536 100644
--- a/source/blender/functions/backends/tuple_call/tuple_call.hpp
+++ b/source/blender/functions/backends/tuple_call/tuple_call.hpp
@@ -39,48 +39,6 @@ class TupleCallBodyBase : public FunctionBody {
   {
     return m_meta_out;
   }
-
-  /**
-   * Same as tuple.get<T>(index), but checks if the name is correct in debug builds.
-   */
-  template<typename T> T get_input(Tuple &tuple, uint index, StringRef expected_name) const
-  {
-#ifdef DEBUG
-    StringRef real_name = this->owner()->input_name(index);
-    BLI_assert(real_name == expected_name);
-#endif
-    UNUSED_VARS_NDEBUG(expected_name);
-    return tuple.get<T>(index);
-  }
-  template<typename T> T get_output(Tuple &tuple, uint index, StringRef expected_name) const
-  {
-#ifdef DEBUG
-    StringRef real_name = this->owner()->output_name(index);
-    BLI_assert(real_name == expected_name);
-#endif
-    UNUSED_VARS_NDEBUG(expected_name);
-    return tuple.get<T>(index);
-  }
-  template<typename T>
-  void set_input(Tuple &tuple, uint index, StringRef expected_name, const T &value) const
-  {
-#ifdef DEBUG
-    StringRef real_name = this->owner()->input_name(index);
-    BLI_assert(real_name == expected_name);
-#endif
-    UNUSED_VARS_NDEBUG(expected_name);
-    tuple.set<T>(index, value);
-  }
-  template<typename T>
-  void set_output(Tuple &tuple, uint index, StringRef expected_name, const T &value) const
-  {
-#ifdef DEBUG
-    StringRef real_name = this->owner()->output_name(index);
-    BLI_assert(real_name == expected_name);
-#endif
-    UNUSED_VARS_NDEBUG(expected_name);
-    tuple.set<T>(index, value);
-  }
 };
 
 class TupleCallBody : public TupleCallBodyBase {
@@ -293,3 +251,9 @@ class FunctionOutputNamesProvider final : public TupleElementNameProvider {
 #define FN_TUPLE_CALL_ALLOC_TUPLES(body, name_in, name_out) \
   FN_TUPLE_STACK_ALLOC(name_in, (body).meta_in().ref()); \
   FN_TUPLE_STACK_ALLOC(name_out, (body).meta_out().ref())
+
+#define FN_TUPLE_CALL_NAMED_REF(THIS, FN_IN, FN_OUT, R_INPUTS, R_OUTPUTS) \
+  FN::FunctionInputNamesProvider _input_names(THIS->owner()); \
+  FN::FunctionOutputNamesProvider _output_names(THIS->owner()); \
+  FN::NamedTupleRef R_INPUTS(&FN_IN, &_input_names); \
+  FN::NamedTupleRef R_OUTPUTS(&FN_OUT, &_output_names)
diff --git a/source/blender/functions/functions/color.cpp b/source/blender/functions/functions/color.cpp
index 683c0c99cf3..4d9eec42c69 100644
--- a/source/blender/functions/functions/color.cpp
+++ b/source/blender/functions/functions/color.cpp
@@ -12,11 +12,13 @@ using namespace Types;
 class SeparateColor : public TupleCallBody {
   void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override
   {
-    rgba_f color = this->get_input<rgba_f>(fn_in, 0, "Color");
-    this->set_output<float>(fn_out, 0, "Red", color.r);
-    this->set_output<float>(fn_out, 1, "Green", color.g);
-    this->set_output<float>(fn_out, 2, "Blue", color.b);
-    this->set_output<float>(fn_out, 3, "Alpha", color.a);
+    FN_TUPLE_CALL_NAMED_REF(this, fn_in, fn_out, inputs, outputs);
+
+    rgba_f color = inputs.get<rgba_f>(0, "Color");
+    outputs.set<float>(0, "Red", color.r);
+    outputs.set<float>(1, "Green", color.g);
+    outputs.set<float>(2, "Blue", color.b);
+    outputs.set<float>(3, "Alpha", color.a);
   }
 };
 
@@ -37,12 +39,14 @@ BLI_LAZY_INIT(SharedFunction, GET_FN_separate_color)
 class CombineColor : public TupleCallBody {
   void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override
   {
+    FN_TUPLE_CALL_NAMED_REF(this, fn_in, fn_out, inputs, outputs);
+
     rgba_f color;
-    color.r = this->get_input<float>(fn_in, 0, "Red");
-    color.g = this->get_input<float>(fn_in, 1, "Green");
-    color.b = this->get_input<float>(fn_in, 2, "Blue");
-    color.a = this->get_input<float>(fn_in, 3, "Alpha");
-    this->set_output<rgba_f>(fn_out, 0, "Color", color);
+    color.r = inputs.get<float>(0, "Red");
+    color.g = inputs.get<float>(1, "Green");
+    color.b = inputs.get<float>(2, "Blue");
+    color.a = inputs.get<float>(3, "Alpha");
+    outputs.set<rgba_f>(0, "Color", color);
   }
 };
 
diff --git a/source/blender/functions/functions/falloffs.cpp b/source/blender/functions/functions/falloffs.cpp
index 7782fd9668f..93cb6a43cb8 100644
--- a/source/blender/functions/functions/falloffs.cpp
+++ b/source/blender/functions/functions/falloffs.cpp
@@ -12,12 +12,14 @@ using namespace Types;
 class PointDistanceFalloff : public TupleCallBody {
   void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override
   {
-    float3 point = this->get_input<float3>(fn_in, 0, "Point");
-    float min_distance = this->get_input<float>(fn_in, 1, "Min Distance");
-    float max_distance = this->get_input<float>(fn_in, 2, "Max Distance");
+    FN_TUPLE_CALL_NAMED_REF(this, fn_in, fn_out, inputs, outputs);
+
+    float3 point = inputs.get<float3>(0, "Point");
+    float min_distance = inputs.get<float>(1, "Min Distance");
+    float max_distance = inputs.get<float>(2, "Max Distance");
 
     FalloffW falloff = new BKE::PointDistanceFalloff(point, min_distance, max_distance);
-    fn_out.move_in(0, falloff);
+    outputs.move_in(0, "Falloff", falloff);
   }
 };
 
@@ -37,10 +39,12 @@ BLI_LAZY_INIT(SharedFunction, GET_FN_point_distance_falloff)
 class ConstantFalloff : public TupleCallBody {
   void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override
   {
-    float weight = this->get_input<float>(fn_in, 0, "Weight");
+    FN_TUPLE_CALL_NAMED_REF(this, fn_in, fn_out, inputs, outputs);
+
+    float weight = inputs.get<float>(0, "Weight");
 
     FalloffW falloff = new BKE::ConstantFalloff(weight);
-    fn_out.move_in(0, falloff);
+    outputs.move_in(0, "Falloff", falloff);
   }
 };
 
@@ -58,15 +62,17 @@ BLI_LAZY_INIT(SharedFunction, GET_FN_constant_falloff)
 class MeshDistanceFalloff : public TupleCallBody {
   void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override
   {
-    Object *object = fn_in.relocate_out<ObjectW>(0).ptr();
+    FN_TUPLE_CALL_NAMED_REF(this, fn_in, fn_out, inputs, outputs);
+
+    Object *object = inputs.relocate_out<ObjectW>(0, "Object").ptr();
     if (object == nullptr || object->type != OB_MESH) {
       FalloffW fallback = new BKE::ConstantFalloff(1.0f);
-      fn_out.move_in(0, fallback);
+      outputs.move_in(0, "Falloff", fallback);
       return;
     }
 
-    float inner_distance = fn_in.get<float>(1);
-    float outer_distance = fn_in.get<float>(2);
+    float inner_distance = inputs.get<float>(1, "Inner Distance");
+    float outer_distance = inputs.get<float>(2, "Outer Distance");
     FalloffW falloff = new BKE::MeshDistanceFalloff(object, inner_distance, outer_distance);
     fn_out.move_in(0, falloff);
   }
diff --git a/source/blender/functions/functions/random.cpp b/source/blender/functions/functions/random.cpp
index 2c39da0ac91..e8a05fec4ae 100644
--- a/source/blender/functions/functions/random.cpp
+++ b/source/blender/functions/functions/random.cpp
@@ -26,11 +26,13 @@ static float random_float(uint32_t x)
 class RandomNumber : public TupleCallBody {
   void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override
   {
-    float seed = this->get_input<float>(fn_in, 0, "Seed");
-    float min = this->get_input<float>(fn_in, 1, "Min");
-    float max = this->get_input<float>(fn_in, 2, "Max");
+    FN_TUPLE_CALL_NAMED_REF(this, fn_in, fn_out, inputs, outputs);
+
+    float seed = inputs.get<float>(0, "Seed");
+    float min = inputs.get<float>(1, "Min");
+    float max = inputs.get<float>(2, "Max");
     float result = random_float(float_as_uint(seed)) * (max - min) + min;
-    this->set_output<float>(fn_out, 0, "Value", result);
+    outputs.set<float>(0, "Value", result);
   }
 };
 
diff --git a/source/blender/functions/functions/ranges.cpp b/source/blender/functions/functions/ranges.cpp
index 1220dc4239a..05abf6f6ab7 100644
--- a/source/blender/functions/functions/ranges.cpp
+++ b/source/blender/functions/functions/ranges.cpp
@@ -13,9 +13,11 @@ using namespace Types;
 class FloatRange : public TupleCallBody {
   void call(Tuple &fn_in, Tuple &fn_out, ExecutionContext &UNUSED(ctx)) const override
   {
-    int amount = this->get_input<int>(fn_in, 0, "Amount");
-    float start = this->get_input<float>(fn_in, 1, "Start");
-    float step = this->get_input<float>(fn_in, 2, "Step");
+    FN_TUPLE_CALL_NAMED_REF(this, fn_in, fn_out, inputs, outputs);
+
+    int amount = inputs.get<int>(0, "Amount");
+    float start = inputs.get<float>(1, "Start");
+    float step = inputs.get<float>(2, "Step");
 
     if (amount < 0) {
       amount = 0;
@@ -31,7 +33,7 @@ class FloatRange : public TupleCallBody {
       value += step;
     }
 
-    fn_out.move_in(0, list);
+    outputs.move_in(0, "List", list);
   }
 };



More information about the Bf-blender-cvs mailing list