[Bf-blender-cvs] [46fff97604a] master: Nodes: refactor socket declarations

Jacques Lucke noreply at git.blender.org
Wed Sep 15 16:15:55 CEST 2021


Commit: 46fff97604ae167473ecd4c0cd9108dc20ba22e3
Author: Jacques Lucke
Date:   Wed Sep 15 16:09:00 2021 +0200
Branches: master
https://developer.blender.org/rB46fff97604ae167473ecd4c0cd9108dc20ba22e3

Nodes: refactor socket declarations

This commits adds a few common flags to `SocketDeclaration`
so that they are available for all socket types (hide label, hide
value, is multi input). This allows porting over the remaining
geometry nodes to the new declaration system.

Furthermore, this commit separates the concepts of the socket
declaration and corresponding builders. The builders are used
by nodes to declare which sockets they have (e.g. `FloatBuilder`).
The ready build socket declarations can then be consumed by
other systems such as the versioning code. Both use cases
need different APIs and those will change for independent reasons,
so it makes sense to separate the classes.

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

M	source/blender/nodes/NOD_node_declaration.hh
M	source/blender/nodes/NOD_socket_declarations.hh
M	source/blender/nodes/geometry/nodes/node_geo_attribute_remove.cc
M	source/blender/nodes/geometry/nodes/node_geo_attribute_vector_rotate.cc
M	source/blender/nodes/geometry/nodes/node_geo_boolean.cc
M	source/blender/nodes/geometry/nodes/node_geo_join_geometry.cc
M	source/blender/nodes/intern/node_declaration.cc
M	source/blender/nodes/intern/node_socket_declarations.cc

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

diff --git a/source/blender/nodes/NOD_node_declaration.hh b/source/blender/nodes/NOD_node_declaration.hh
index 7ba4ac52b86..8ca2b411a38 100644
--- a/source/blender/nodes/NOD_node_declaration.hh
+++ b/source/blender/nodes/NOD_node_declaration.hh
@@ -27,12 +27,19 @@ namespace blender::nodes {
 
 class NodeDeclarationBuilder;
 
+/**
+ * Describes a single input or output socket. This is subclassed for different socket types.
+ */
 class SocketDeclaration {
  protected:
   std::string name_;
   std::string identifier_;
+  bool hide_label_ = false;
+  bool hide_value_ = false;
+  bool is_multi_input_ = false;
 
   friend NodeDeclarationBuilder;
+  template<typename SocketDecl> friend class SocketDeclarationBuilder;
 
  public:
   virtual ~SocketDeclaration() = default;
@@ -43,6 +50,49 @@ class SocketDeclaration {
 
   StringRefNull name() const;
   StringRefNull identifier() const;
+
+ protected:
+  void set_common_flags(bNodeSocket &socket) const;
+  bool matches_common_data(const bNodeSocket &socket) const;
+};
+
+class BaseSocketDeclarationBuilder {
+ public:
+  virtual ~BaseSocketDeclarationBuilder() = default;
+};
+
+/**
+ * Wraps a #SocketDeclaration and provides methods to set it up correctly.
+ * This is separate from #SocketDeclaration, because it allows separating the API used by nodes to
+ * declare themselves from how the declaration is stored internally.
+ */
+template<typename SocketDecl>
+class SocketDeclarationBuilder : public BaseSocketDeclarationBuilder {
+ protected:
+  using Self = typename SocketDecl::Builder;
+  static_assert(std::is_base_of_v<SocketDeclaration, SocketDecl>);
+  SocketDecl *decl_;
+
+  friend class NodeDeclarationBuilder;
+
+ public:
+  Self &hide_label(bool value)
+  {
+    decl_->hide_label_ = value;
+    return *(Self *)this;
+  }
+
+  Self &hide_value(bool value)
+  {
+    decl_->hide_value_ = value;
+    return *(Self *)this;
+  }
+
+  Self &multi_input(bool value)
+  {
+    decl_->is_multi_input_ = value;
+    return *(Self *)this;
+  }
 };
 
 using SocketDeclarationPtr = std::unique_ptr<SocketDeclaration>;
@@ -67,12 +117,21 @@ class NodeDeclaration {
 class NodeDeclarationBuilder {
  private:
   NodeDeclaration &declaration_;
+  Vector<std::unique_ptr<BaseSocketDeclarationBuilder>> builders_;
 
  public:
   NodeDeclarationBuilder(NodeDeclaration &declaration);
 
-  template<typename DeclType> DeclType &add_input(StringRef name, StringRef identifier = "");
-  template<typename DeclType> DeclType &add_output(StringRef name, StringRef identifier = "");
+  template<typename DeclType>
+  typename DeclType::Builder &add_input(StringRef name, StringRef identifier = "");
+  template<typename DeclType>
+  typename DeclType::Builder &add_output(StringRef name, StringRef identifier = "");
+
+ private:
+  template<typename DeclType>
+  typename DeclType::Builder &add_socket(StringRef name,
+                                         StringRef identifier,
+                                         Vector<SocketDeclarationPtr> &r_decls);
 };
 
 /* --------------------------------------------------------------------
@@ -99,27 +158,34 @@ inline NodeDeclarationBuilder::NodeDeclarationBuilder(NodeDeclaration &declarati
 }
 
 template<typename DeclType>
-inline DeclType &NodeDeclarationBuilder::add_input(StringRef name, StringRef identifier)
+inline typename DeclType::Builder &NodeDeclarationBuilder::add_input(StringRef name,
+                                                                     StringRef identifier)
 {
-  static_assert(std::is_base_of_v<SocketDeclaration, DeclType>);
-  std::unique_ptr<DeclType> socket_decl = std::make_unique<DeclType>();
-  DeclType &ref = *socket_decl;
-  ref.name_ = name;
-  ref.identifier_ = identifier.is_empty() ? name : identifier;
-  declaration_.inputs_.append(std::move(socket_decl));
-  return ref;
+  return this->add_socket<DeclType>(name, identifier, declaration_.inputs_);
+}
+
+template<typename DeclType>
+inline typename DeclType::Builder &NodeDeclarationBuilder::add_output(StringRef name,
+                                                                      StringRef identifier)
+{
+  return this->add_socket<DeclType>(name, identifier, declaration_.outputs_);
 }
 
 template<typename DeclType>
-inline DeclType &NodeDeclarationBuilder::add_output(StringRef name, StringRef identifier)
+inline typename DeclType::Builder &NodeDeclarationBuilder::add_socket(
+    StringRef name, StringRef identifier, Vector<SocketDeclarationPtr> &r_decls)
 {
   static_assert(std::is_base_of_v<SocketDeclaration, DeclType>);
+  using Builder = typename DeclType::Builder;
   std::unique_ptr<DeclType> socket_decl = std::make_unique<DeclType>();
-  DeclType &ref = *socket_decl;
-  ref.name_ = name;
-  ref.identifier_ = identifier.is_empty() ? name : identifier;
-  declaration_.outputs_.append(std::move(socket_decl));
-  return ref;
+  std::unique_ptr<Builder> socket_decl_builder = std::make_unique<Builder>();
+  socket_decl_builder->decl_ = &*socket_decl;
+  socket_decl->name_ = name;
+  socket_decl->identifier_ = identifier.is_empty() ? name : identifier;
+  r_decls.append(std::move(socket_decl));
+  Builder &socket_decl_builder_ref = *socket_decl_builder;
+  builders_.append(std::move(socket_decl_builder));
+  return socket_decl_builder_ref;
 }
 
 /* --------------------------------------------------------------------
diff --git a/source/blender/nodes/NOD_socket_declarations.hh b/source/blender/nodes/NOD_socket_declarations.hh
index 11352111356..3d0cfdb5d5d 100644
--- a/source/blender/nodes/NOD_socket_declarations.hh
+++ b/source/blender/nodes/NOD_socket_declarations.hh
@@ -25,6 +25,8 @@
 
 namespace blender::nodes::decl {
 
+class FloatBuilder;
+
 class Float : public SocketDeclaration {
  private:
   float default_value_ = 0.0f;
@@ -32,36 +34,45 @@ class Float : public SocketDeclaration {
   float soft_max_value_ = FLT_MAX;
   PropertySubType subtype_ = PROP_NONE;
 
+  friend FloatBuilder;
+
  public:
-  Float &min(const float value)
+  using Builder = FloatBuilder;
+
+  bNodeSocket &build(bNodeTree &ntree, bNode &node, eNodeSocketInOut in_out) const override;
+  bool matches(const bNodeSocket &socket) const override;
+  bNodeSocket &update_or_build(bNodeTree &ntree, bNode &node, bNodeSocket &socket) const override;
+};
+
+class FloatBuilder : public SocketDeclarationBuilder<Float> {
+ public:
+  FloatBuilder &min(const float value)
   {
-    soft_min_value_ = value;
+    decl_->soft_min_value_ = value;
     return *this;
   }
 
-  Float &max(const float value)
+  FloatBuilder &max(const float value)
   {
-    soft_max_value_ = value;
+    decl_->soft_max_value_ = value;
     return *this;
   }
 
-  Float &default_value(const float value)
+  FloatBuilder &default_value(const float value)
   {
-    default_value_ = value;
+    decl_->default_value_ = value;
     return *this;
   }
 
-  Float &subtype(PropertySubType subtype)
+  FloatBuilder &subtype(PropertySubType subtype)
   {
-    subtype_ = subtype;
+    decl_->subtype_ = subtype;
     return *this;
   }
-
-  bNodeSocket &build(bNodeTree &ntree, bNode &node, eNodeSocketInOut in_out) const override;
-  bool matches(const bNodeSocket &socket) const override;
-  bNodeSocket &update_or_build(bNodeTree &ntree, bNode &node, bNodeSocket &socket) const override;
 };
 
+class IntBuilder;
+
 class Int : public SocketDeclaration {
  private:
   int default_value_ = 0;
@@ -69,36 +80,45 @@ class Int : public SocketDeclaration {
   int soft_max_value_ = INT32_MAX;
   PropertySubType subtype_ = PROP_NONE;
 
+  friend IntBuilder;
+
  public:
-  Int &min(const int value)
+  using Builder = IntBuilder;
+
+  bNodeSocket &build(bNodeTree &ntree, bNode &node, eNodeSocketInOut in_out) const override;
+  bool matches(const bNodeSocket &socket) const override;
+  bNodeSocket &update_or_build(bNodeTree &ntree, bNode &node, bNodeSocket &socket) const override;
+};
+
+class IntBuilder : public SocketDeclarationBuilder<Int> {
+ public:
+  IntBuilder &min(const int value)
   {
-    soft_min_value_ = value;
+    decl_->soft_min_value_ = value;
     return *this;
   }
 
-  Int &max(const int value)
+  IntBuilder &max(const int value)
   {
-    soft_max_value_ = value;
+    decl_->soft_max_value_ = value;
     return *this;
   }
 
-  Int &default_value(const int value)
+  IntBuilder &default_value(const int value)
   {
-    default_value_ = value;
+    decl_->default_value_ = value;
     return *this;
   }
 
-  Int &subtype(PropertySubType subtype)
+  IntBuilder &subtype(PropertySubType subtype)
   {
-    subtype_ = subtype;
+    decl_->subtype_ = subtype;
     return *this;
   }
-
-  bNodeSocket &build(bNodeTree &ntree, bNode &node, eNodeSocketInOut in_out) const override;
-  bool matches(const bNodeSocket &socket) const override;
-  bNodeSocket &update_or_build(bNodeTree &ntree, bNode &node, bNodeSocket &socket) const override;
 };
 
+class VectorBuilder;
+
 class Vector : public SocketDeclaration {
  private:
   float3 default_value_ = {0, 0, 0};
@@ -106,160 +126,152 @@ class Vector : public SocketDeclaration {
   float soft_max_value_ = FLT_MAX;
   PropertySubType subtype_ = PROP_NONE;
 
+  friend VectorBuilder;
+
+ public:
+  using Builder = VectorBuilder;
+
+  bNodeSocket &build(bNodeTree &ntree, bNode &node, eNodeSocketInOut in_out) const override;
+  bool matches(const bNodeSocket &socket) const override;
+  bNodeSocket &update_or_build(bNodeTree &ntree, bNode &node, bNodeSocket &socket) const override;
+};
+
+class VectorBuilder : public SocketDeclarationBuilder<Vector> {
  public:
-  Vector &default_value(const float3 value)
+  VectorBuilder &default_value(const float3 value)
   {
-    default_value_ = value;
+    decl_->default_value_ = value;
     return *this;
   }
 
-  Vector &subtype(PropertySubType subtype)
+  VectorBuilder &subtype(PropertySubType subtype)
   {
-    subtype_ = subtype;
+    decl_->subtype_ = subtype;
     return *this;
   }
 
-  Vector &min(const float min)
+  VectorBuilder &min(const float min)
   {
-    soft_min_value_ = min;
+    decl_->soft_min_value_ = min;
     return *this;
   }
 
-  Vector &max(const float max)
+  VectorBuilder &max(const float max)
   {
-    soft_max_value_ = max;
+    decl_->soft_max_value_ = max;
     return *this;
   

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list