[Bf-blender-cvs] [b7efaadb8e4] functions: get socket data type from idname

Jacques Lucke noreply at git.blender.org
Mon Jul 22 18:12:56 CEST 2019


Commit: b7efaadb8e4b70179b5e060a105086f2638e781a
Author: Jacques Lucke
Date:   Mon Jul 22 12:04:42 2019 +0200
Branches: functions
https://developer.blender.org/rBb7efaadb8e4b70179b5e060a105086f2638e781a

get socket data type from idname

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

M	source/blender/blenlib/BLI_string_ref.hpp
M	source/blender/functions/CMakeLists.txt
M	source/blender/functions/frontends/data_flow_nodes/builder.cpp
M	source/blender/functions/frontends/data_flow_nodes/builder.hpp
M	source/blender/functions/frontends/data_flow_nodes/inserters.cpp
M	source/blender/functions/frontends/data_flow_nodes/inserters.hpp
M	source/blender/functions/frontends/data_flow_nodes/inserters/conversions.cpp
A	source/blender/functions/frontends/data_flow_nodes/type_mappings.cpp
A	source/blender/functions/frontends/data_flow_nodes/type_mappings.hpp

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

diff --git a/source/blender/blenlib/BLI_string_ref.hpp b/source/blender/blenlib/BLI_string_ref.hpp
index bd688ac673b..ac300f487b2 100644
--- a/source/blender/blenlib/BLI_string_ref.hpp
+++ b/source/blender/blenlib/BLI_string_ref.hpp
@@ -116,6 +116,11 @@ class StringRefNull : public StringRefBase {
   StringRefNull(const std::string &str) : StringRefNull(str.data())
   {
   }
+
+  operator const char *()
+  {
+    return m_data;
+  }
 };
 
 class StringRef : public StringRefBase {
@@ -169,6 +174,12 @@ inline std::ostream &operator<<(std::ostream &stream, StringRef ref)
   return stream;
 }
 
+inline std::ostream &operator<<(std::ostream &stream, StringRefNull ref)
+{
+  stream << ref.to_std_string();
+  return stream;
+}
+
 inline std::string operator+(StringRef a, StringRef b)
 {
   return a.to_std_string() + b.to_std_string();
diff --git a/source/blender/functions/CMakeLists.txt b/source/blender/functions/CMakeLists.txt
index 90be353ce02..e0892729cd4 100644
--- a/source/blender/functions/CMakeLists.txt
+++ b/source/blender/functions/CMakeLists.txt
@@ -143,6 +143,8 @@ set(SRC
   frontends/data_flow_nodes/inserters/nodes.cpp
   frontends/data_flow_nodes/inserters/sockets.cpp
   frontends/data_flow_nodes/inserters/conversions.cpp
+  frontends/data_flow_nodes/type_mappings.hpp
+  frontends/data_flow_nodes/type_mappings.cpp
   frontends/data_flow_nodes/data_flow_nodes-c.h
   frontends/data_flow_nodes/data_flow_nodes-c.cpp
 )
diff --git a/source/blender/functions/frontends/data_flow_nodes/builder.cpp b/source/blender/functions/frontends/data_flow_nodes/builder.cpp
index f2104f75f2a..5d3edd953a4 100644
--- a/source/blender/functions/frontends/data_flow_nodes/builder.cpp
+++ b/source/blender/functions/frontends/data_flow_nodes/builder.cpp
@@ -1,4 +1,5 @@
 #include "builder.hpp"
+#include "type_mappings.hpp"
 
 #include "DNA_node_types.h"
 #include "FN_types.hpp"
@@ -27,6 +28,17 @@ static PyObject *get_py_bnode(bNodeTree *btree, bNode *bnode)
 namespace FN {
 namespace DataFlowNodes {
 
+BTreeGraphBuilder::BTreeGraphBuilder(IndexedNodeTree &indexed_btree,
+                                     DataFlowGraphBuilder &graph,
+                                     SmallMap<struct bNodeSocket *, DFGB_Socket> &socket_map)
+    : m_graph(graph),
+      m_indexed_btree(indexed_btree),
+      m_socket_map(socket_map),
+      m_type_by_idname(get_type_by_idname_map()),
+      m_type_by_data_type(get_type_by_data_type_map())
+{
+}
+
 class NodeSource : public SourceInfo {
  private:
   bNodeTree *m_btree;
@@ -226,52 +238,17 @@ struct ID *BTreeGraphBuilder::btree_id() const
 
 bool BTreeGraphBuilder::is_data_socket(bNodeSocket *bsocket) const
 {
-  PointerRNA rna = this->get_rna(bsocket);
-  return RNA_struct_find_property(&rna, "data_type") != NULL;
+  return m_type_by_idname.contains(bsocket->idname);
 }
 
-SharedType &BTreeGraphBuilder::type_by_name(const char *data_type) const
+SharedType &BTreeGraphBuilder::type_by_name(StringRef data_type) const
 {
-  if (STREQ(data_type, "Float")) {
-    return Types::GET_TYPE_float();
-  }
-  else if (STREQ(data_type, "Integer")) {
-    return Types::GET_TYPE_int32();
-  }
-  else if (STREQ(data_type, "Vector")) {
-    return Types::GET_TYPE_float3();
-  }
-  else if (STREQ(data_type, "Boolean")) {
-    return Types::GET_TYPE_bool();
-  }
-  else if (STREQ(data_type, "Object")) {
-    return Types::GET_TYPE_object();
-  }
-  else if (STREQ(data_type, "Float List")) {
-    return Types::GET_TYPE_float_list();
-  }
-  else if (STREQ(data_type, "Vector List")) {
-    return Types::GET_TYPE_float3_list();
-  }
-  else if (STREQ(data_type, "Integer List")) {
-    return Types::GET_TYPE_int32_list();
-  }
-  else if (STREQ(data_type, "Boolean List")) {
-    return Types::GET_TYPE_bool_list();
-  }
-  else if (STREQ(data_type, "Object List")) {
-    return Types::GET_TYPE_object_list();
-  }
-  else {
-    BLI_assert(false);
-    return *(SharedType *)nullptr;
-  }
+  return m_type_by_data_type.lookup_ref(data_type);
 }
 
 SharedType &BTreeGraphBuilder::query_socket_type(bNodeSocket *bsocket) const
 {
-  std::string data_type = this->query_socket_type_name(bsocket);
-  return this->type_by_name(data_type.c_str());
+  return m_type_by_idname.lookup_ref(bsocket->idname);
 }
 
 std::string BTreeGraphBuilder::query_socket_name(bNodeSocket *bsocket) const
@@ -293,13 +270,13 @@ PointerRNA BTreeGraphBuilder::get_rna(bNodeSocket *bsocket) const
   return rna;
 }
 
-SharedType &BTreeGraphBuilder::query_type_property(bNode *bnode, const char *prop_name) const
+SharedType &BTreeGraphBuilder::query_type_property(bNode *bnode, StringRefNull prop_name) const
 {
   PointerRNA rna = this->get_rna(bnode);
   return this->type_from_rna(rna, prop_name);
 }
 
-SharedType &BTreeGraphBuilder::type_from_rna(PointerRNA &rna, const char *prop_name) const
+SharedType &BTreeGraphBuilder::type_from_rna(PointerRNA &rna, StringRefNull prop_name) const
 {
   char type_name[64];
   RNA_string_get(&rna, prop_name, type_name);
diff --git a/source/blender/functions/frontends/data_flow_nodes/builder.hpp b/source/blender/functions/frontends/data_flow_nodes/builder.hpp
index 613d8cf052c..586fffd38ab 100644
--- a/source/blender/functions/frontends/data_flow_nodes/builder.hpp
+++ b/source/blender/functions/frontends/data_flow_nodes/builder.hpp
@@ -2,6 +2,7 @@
 
 #include "FN_core.hpp"
 #include "BKE_node_tree.hpp"
+#include "BLI_string_map.hpp"
 
 struct ID;
 struct PointerRNA;
@@ -17,14 +18,13 @@ class BTreeGraphBuilder {
   DataFlowGraphBuilder &m_graph;
   IndexedNodeTree &m_indexed_btree;
   SmallMap<struct bNodeSocket *, DFGB_Socket> &m_socket_map;
+  StringMap<SharedType> &m_type_by_idname;
+  StringMap<SharedType> &m_type_by_data_type;
 
  public:
   BTreeGraphBuilder(IndexedNodeTree &indexed_btree,
                     DataFlowGraphBuilder &graph,
-                    SmallMap<struct bNodeSocket *, DFGB_Socket> &socket_map)
-      : m_graph(graph), m_indexed_btree(indexed_btree), m_socket_map(socket_map)
-  {
-  }
+                    SmallMap<struct bNodeSocket *, DFGB_Socket> &socket_map);
 
   /* Insert Function */
   DFGB_Node *insert_function(SharedFunction &fn);
@@ -46,7 +46,7 @@ class BTreeGraphBuilder {
   bool verify_data_sockets_mapped(struct bNode *bnode) const;
 
   /* Type Mapping */
-  SharedType &type_by_name(const char *data_type) const;
+  SharedType &type_by_name(StringRef data_type) const;
 
   /* Query Node Tree */
   IndexedNodeTree &indexed_btree() const;
@@ -62,11 +62,11 @@ class BTreeGraphBuilder {
 
   /* Query Node Information */
   PointerRNA get_rna(bNode *bnode) const;
-  SharedType &query_type_property(bNode *bnode, const char *prop_name) const;
+  SharedType &query_type_property(bNode *bnode, StringRefNull prop_name) const;
   bool has_data_socket(bNode *bnode) const;
 
   /* Query RNA */
-  SharedType &type_from_rna(PointerRNA &rna, const char *prop_name) const;
+  SharedType &type_from_rna(PointerRNA &rna, StringRefNull prop_name) const;
 
  private:
   bool check_if_sockets_are_mapped(struct bNode *bnode, bSocketList bsockets) const;
diff --git a/source/blender/functions/frontends/data_flow_nodes/inserters.cpp b/source/blender/functions/frontends/data_flow_nodes/inserters.cpp
index 53c7c467e7c..bb7420109cd 100644
--- a/source/blender/functions/frontends/data_flow_nodes/inserters.cpp
+++ b/source/blender/functions/frontends/data_flow_nodes/inserters.cpp
@@ -1,5 +1,6 @@
 #include "inserters.hpp"
 #include "registry.hpp"
+#include "type_mappings.hpp"
 
 #include "FN_dependencies.hpp"
 
@@ -12,7 +13,7 @@
 namespace FN {
 namespace DataFlowNodes {
 
-using StringPair = std::pair<std::string, std::string>;
+using TypePair = std::pair<SharedType, SharedType>;
 
 static void initialize_standard_inserters(GraphInserters &inserters)
 {
@@ -28,6 +29,10 @@ BLI_LAZY_INIT(GraphInserters, get_standard_inserters)
   return inserters;
 }
 
+GraphInserters::GraphInserters() : m_type_by_data_type(&get_type_by_data_type_map())
+{
+}
+
 void GraphInserters::reg_node_inserter(std::string idname, NodeInserter inserter)
 {
   m_node_inserters.add_new(idname, inserter);
@@ -48,17 +53,18 @@ void GraphInserters::reg_socket_loader(std::string idname, SocketLoader loader)
   m_socket_loaders.add_new(idname, loader);
 }
 
-void GraphInserters::reg_conversion_inserter(std::string from_type,
-                                             std::string to_type,
+void GraphInserters::reg_conversion_inserter(StringRef from_type,
+                                             StringRef to_type,
                                              ConversionInserter inserter)
 {
-  auto key = StringPair(from_type, to_type);
+  auto key = TypePair(m_type_by_data_type->lookup(from_type),
+                      m_type_by_data_type->lookup(to_type));
   BLI_assert(!m_conversion_inserters.contains(key));
   m_conversion_inserters.add(key, inserter);
 }
 
-void GraphInserters::reg_conversion_function(std::string from_type,
-                                             std::string to_type,
+void GraphInserters::reg_conversion_function(StringRef from_type,
+                                             StringRef to_type,
                                              FunctionGetter getter)
 {
   auto inserter = [getter](BTreeGraphBuilder &builder,
@@ -187,15 +193,15 @@ bool GraphInserters::insert_link(BTreeGraphBuilder &builder,
   DFGB_Socket from_socket = builder.lookup_socket(from_bsocket);
   DFGB_Socket to_socket = builder.lookup_socket(to_bsocket);
 
-  std::string from_type = builder.query_socket_type_name(from_bsocket);
-  std::string to_type = builder.query_socket_type_name(to_bsocket);
+  SharedType &from_type = builder.query_socket_type(from_bsocket);
+  SharedType &to_type = builder.query_socket_type(to_bsocket);
 
   if (from_type == to_type) {
     builder.insert_link(from_socket, to_socket);
     return true;
   }
 
-  auto key = StringPair(from_type, to_type);
+  auto key = TypePair(from_type, to_type);
   if (m_conversion_inserters.contains(key)) {
     auto inserter = m_conversion_inserters.lookup(key);
     inserter(builder, from_socket, to_socket, source_link);
@@ -209,14 +215,14 @@ bool GraphInserters::insert_link(BTreeGraphBuilder &builder,
 }  // namespace FN
 
 n

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list