[Bf-blender-cvs] [bbea79ce5ea] master: Cleanup: move type conversions to separate file

Jacques Lucke noreply at git.blender.org
Thu Apr 15 09:36:05 CEST 2021


Commit: bbea79ce5eadff621dcca668487cd1248b0433aa
Author: Jacques Lucke
Date:   Thu Apr 15 09:35:56 2021 +0200
Branches: master
https://developer.blender.org/rBbbea79ce5eadff621dcca668487cd1248b0433aa

Cleanup: move type conversions to separate file

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

M	source/blender/blenkernel/intern/attribute_access.cc
M	source/blender/modifiers/intern/MOD_nodes.cc
M	source/blender/nodes/CMakeLists.txt
M	source/blender/nodes/NOD_node_tree_multi_function.hh
A	source/blender/nodes/NOD_type_conversions.hh
M	source/blender/nodes/intern/node_tree_multi_function.cc
A	source/blender/nodes/intern/type_conversions.cc

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

diff --git a/source/blender/blenkernel/intern/attribute_access.cc b/source/blender/blenkernel/intern/attribute_access.cc
index 5bd3b990a63..8542c9d35a4 100644
--- a/source/blender/blenkernel/intern/attribute_access.cc
+++ b/source/blender/blenkernel/intern/attribute_access.cc
@@ -34,7 +34,7 @@
 
 #include "CLG_log.h"
 
-#include "NOD_node_tree_multi_function.hh"
+#include "NOD_type_conversions.hh"
 
 #include "attribute_access_intern.hh"
 
diff --git a/source/blender/modifiers/intern/MOD_nodes.cc b/source/blender/modifiers/intern/MOD_nodes.cc
index fd798b743c9..9329bc85e66 100644
--- a/source/blender/modifiers/intern/MOD_nodes.cc
+++ b/source/blender/modifiers/intern/MOD_nodes.cc
@@ -84,6 +84,7 @@
 #include "NOD_geometry_exec.hh"
 #include "NOD_node_tree_multi_function.hh"
 #include "NOD_type_callbacks.hh"
+#include "NOD_type_conversions.hh"
 
 using blender::float3;
 using blender::FunctionRef;
diff --git a/source/blender/nodes/CMakeLists.txt b/source/blender/nodes/CMakeLists.txt
index 8a426042039..8168f0dba73 100644
--- a/source/blender/nodes/CMakeLists.txt
+++ b/source/blender/nodes/CMakeLists.txt
@@ -316,6 +316,7 @@ set(SRC
   intern/node_tree_ref.cc
   intern/node_util.c
   intern/type_callbacks.cc
+  intern/type_conversions.cc
 
   composite/node_composite_util.h
   function/node_function_util.hh
@@ -337,6 +338,7 @@ set(SRC
   NOD_static_types.h
   NOD_texture.h
   NOD_type_callbacks.hh
+  NOD_type_conversions.hh
   intern/node_common.h
   intern/node_exec.h
   intern/node_util.h
diff --git a/source/blender/nodes/NOD_node_tree_multi_function.hh b/source/blender/nodes/NOD_node_tree_multi_function.hh
index de1f5a0dc70..e3f31e011e4 100644
--- a/source/blender/nodes/NOD_node_tree_multi_function.hh
+++ b/source/blender/nodes/NOD_node_tree_multi_function.hh
@@ -387,33 +387,4 @@ MFNetworkTreeMap insert_node_tree_into_mf_network(fn::MFNetwork &network,
 using MultiFunctionByNode = Map<DNode, const fn::MultiFunction *>;
 MultiFunctionByNode get_multi_function_per_node(const DerivedNodeTree &tree, ResourceScope &scope);
 
-class DataTypeConversions {
- private:
-  Map<std::pair<fn::MFDataType, fn::MFDataType>, const fn::MultiFunction *> conversions_;
-
- public:
-  void add(fn::MFDataType from_type, fn::MFDataType to_type, const fn::MultiFunction &fn)
-  {
-    conversions_.add_new({from_type, to_type}, &fn);
-  }
-
-  const fn::MultiFunction *get_conversion(fn::MFDataType from, fn::MFDataType to) const
-  {
-    return conversions_.lookup_default({from, to}, nullptr);
-  }
-
-  bool is_convertible(const CPPType &from_type, const CPPType &to_type) const
-  {
-    return conversions_.contains(
-        {fn::MFDataType::ForSingle(from_type), fn::MFDataType::ForSingle(to_type)});
-  }
-
-  void convert(const CPPType &from_type,
-               const CPPType &to_type,
-               const void *from_value,
-               void *to_value) const;
-};
-
-const DataTypeConversions &get_implicit_type_conversions();
-
 }  // namespace blender::nodes
diff --git a/source/blender/nodes/NOD_type_conversions.hh b/source/blender/nodes/NOD_type_conversions.hh
new file mode 100644
index 00000000000..e23fddab58b
--- /dev/null
+++ b/source/blender/nodes/NOD_type_conversions.hh
@@ -0,0 +1,54 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include "FN_multi_function.hh"
+
+namespace blender::nodes {
+
+using fn::CPPType;
+
+class DataTypeConversions {
+ private:
+  Map<std::pair<fn::MFDataType, fn::MFDataType>, const fn::MultiFunction *> conversions_;
+
+ public:
+  void add(fn::MFDataType from_type, fn::MFDataType to_type, const fn::MultiFunction &fn)
+  {
+    conversions_.add_new({from_type, to_type}, &fn);
+  }
+
+  const fn::MultiFunction *get_conversion(fn::MFDataType from, fn::MFDataType to) const
+  {
+    return conversions_.lookup_default({from, to}, nullptr);
+  }
+
+  bool is_convertible(const CPPType &from_type, const CPPType &to_type) const
+  {
+    return conversions_.contains(
+        {fn::MFDataType::ForSingle(from_type), fn::MFDataType::ForSingle(to_type)});
+  }
+
+  void convert(const CPPType &from_type,
+               const CPPType &to_type,
+               const void *from_value,
+               void *to_value) const;
+};
+
+const DataTypeConversions &get_implicit_type_conversions();
+
+}  // namespace blender::nodes
diff --git a/source/blender/nodes/intern/node_tree_multi_function.cc b/source/blender/nodes/intern/node_tree_multi_function.cc
index 1fb86661dff..ea6b2e870ca 100644
--- a/source/blender/nodes/intern/node_tree_multi_function.cc
+++ b/source/blender/nodes/intern/node_tree_multi_function.cc
@@ -15,6 +15,7 @@
  */
 
 #include "NOD_node_tree_multi_function.hh"
+#include "NOD_type_conversions.hh"
 
 #include "FN_multi_function_network_evaluation.hh"
 
@@ -142,109 +143,6 @@ static void insert_nodes(CommonMFNetworkBuilderData &common)
   });
 }
 
-template<typename From, typename To>
-static void add_implicit_conversion(DataTypeConversions &conversions)
-{
-  static fn::CustomMF_Convert<From, To> function;
-  conversions.add(fn::MFDataType::ForSingle<From>(), fn::MFDataType::ForSingle<To>(), function);
-}
-
-template<typename From, typename To, typename ConversionF>
-static void add_implicit_conversion(DataTypeConversions &conversions,
-                                    StringRef name,
-                                    ConversionF conversion)
-{
-  static fn::CustomMF_SI_SO<From, To> function{name, conversion};
-  conversions.add(fn::MFDataType::ForSingle<From>(), fn::MFDataType::ForSingle<To>(), function);
-}
-
-static DataTypeConversions create_implicit_conversions()
-{
-  DataTypeConversions conversions;
-  add_implicit_conversion<float, float2>(conversions);
-  add_implicit_conversion<float, float3>(conversions);
-  add_implicit_conversion<float, int32_t>(conversions);
-  add_implicit_conversion<float, bool>(
-      conversions, "float to boolean", [](float a) { return a > 0.0f; });
-  add_implicit_conversion<float, Color4f>(
-      conversions, "float to Color4f", [](float a) { return Color4f(a, a, a, 1.0f); });
-
-  add_implicit_conversion<float2, float3>(
-      conversions, "float2 to float3", [](float2 a) { return float3(a.x, a.y, 0.0f); });
-  add_implicit_conversion<float2, float>(
-      conversions, "float2 to float", [](float2 a) { return (a.x + a.y) / 2.0f; });
-  add_implicit_conversion<float2, int32_t>(
-      conversions, "float2 to int32_t", [](float2 a) { return (int32_t)((a.x + a.y) / 2.0f); });
-  add_implicit_conversion<float2, bool>(
-      conversions, "float2 to bool", [](float2 a) { return !is_zero_v2(a); });
-  add_implicit_conversion<float2, Color4f>(
-      conversions, "float2 to Color4f", [](float2 a) { return Color4f(a.x, a.y, 0.0f, 1.0f); });
-
-  add_implicit_conversion<float3, bool>(
-      conversions, "float3 to boolean", [](float3 a) { return !is_zero_v3(a); });
-  add_implicit_conversion<float3, float>(
-      conversions, "float3 to float", [](float3 a) { return (a.x + a.y + a.z) / 3.0f; });
-  add_implicit_conversion<float3, int32_t>(
-      conversions, "float3 to int32_t", [](float3 a) { return (int)((a.x + a.y + a.z) / 3.0f); });
-  add_implicit_conversion<float3, float2>(conversions);
-  add_implicit_conversion<float3, Color4f>(
-      conversions, "float3 to Color4f", [](float3 a) { return Color4f(a.x, a.y, a.z, 1.0f); });
-
-  add_implicit_conversion<int32_t, bool>(
-      conversions, "int32 to boolean", [](int32_t a) { return a > 0; });
-  add_implicit_conversion<int32_t, float>(conversions);
-  add_implicit_conversion<int32_t, float2>(
-      conversions, "int32 to float2", [](int32_t a) { return float2((float)a); });
-  add_implicit_conversion<int32_t, float3>(
-      conversions, "int32 to float3", [](int32_t a) { return float3((float)a); });
-  add_implicit_conversion<int32_t, Color4f>(conversions, "int32 to Color4f", [](int32_t a) {
-    return Color4f((float)a, (float)a, (float)a, 1.0f);
-  });
-
-  add_implicit_conversion<bool, float>(conversions);
-  add_implicit_conversion<bool, int32_t>(conversions);
-  add_implicit_conversion<bool, float2>(
-      conversions, "boolean to float2", [](bool a) { return (a) ? float2(1.0f) : float2(0.0f); });
-  add_implicit_conversion<bool, float3>(
-      conversions, "boolean to float3", [](bool a) { return (a) ? float3(1.0f) : float3(0.0f); });
-  add_implicit_conversion<bool, Color4f>(conversions, "boolean to Color4f", [](bool a) {
-    return (a) ? Color4f(1.0f, 1.0f, 1.0f, 1.0f) : Color4f(0.0f, 0.0f, 0.0f, 1.0f);
-  });
-
-  add_implicit_conversion<Color4f, bool>(
-      conversions, "Color4f to boolean", [](Color4f a) { return rgb_to_grayscale(a) > 0.0f; });
-  add_implicit_conversion<Color4f, float>(
-      conversions, "Color4f to float", [](Color4f a) { return rgb_to_grayscale(a); });
-  add_implicit_conversion<Color4f, float2>(
-      conversions, "Color4f to float2", [](Color4f a) { return float2(a.r, a.g); });
-  add_implicit_conversion<Color4f, float3>(
-      conversions, "Color4f to float3", [](Color4f a) { return float3(a.r, a.g, a.b); });
-
-  return conversions;
-}
-
-const DataTypeConversions &get_implicit_type_conversions()
-{
-  static const DataTypeConversions conversions = create_implicit_conversions();
-  return conversions;
-}
-
-void DataTypeConversions::convert(const CPPType &from_type,
-                                  const CPPType &to_type,
-                                  const void *from_value,
-                                  void *to_value) const
-{
-  const fn::MultiFunction *fn = this->get_conversion(M

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list