From f5c79abef05cf9a63b6272b9b661996af58f60ec Mon Sep 17 00:00:00 2001
From: cmoineau <cyril.moineau@cea.fr>
Date: Wed, 15 Nov 2023 09:46:59 +0000
Subject: [PATCH] [Binding] [Node] The operator() now use the python args
 system, avoiding the use of vector.

---
 python_binding/graph/pybind_Node.cpp | 264 ++++++++++++++-------------
 1 file changed, 139 insertions(+), 125 deletions(-)

diff --git a/python_binding/graph/pybind_Node.cpp b/python_binding/graph/pybind_Node.cpp
index e3666d247..29e9a7b66 100644
--- a/python_binding/graph/pybind_Node.cpp
+++ b/python_binding/graph/pybind_Node.cpp
@@ -16,136 +16,150 @@
 
 #include "aidge/graph/GraphView.hpp"
 #include "aidge/graph/Node.hpp"
+#include "aidge/graph/Connector.hpp"
 #include "aidge/utils/Types.h"
 
 namespace py = pybind11;
 namespace Aidge {
 void init_Node(py::module& m) {
     py::class_<Node, std::shared_ptr<Node>>(m, "Node")
-            .def("name", &Node::name,
-            R"mydelimiter(
-            Name of the Node.
-            )mydelimiter")
-
-            .def("type", &Node::type,
-            R"mydelimiter(
-            Type of the node.
-            )mydelimiter")
-
-            .def("get_operator", &Node::getOperator,
-            R"mydelimiter(
-            Get the Operator object of the Node.
-            )mydelimiter")
-
-            .def("set_name", &Node::setName, py::arg("name"),
-            R"mydelimiter(
-            Set the Node name.
-
-            :param name: New name for the node.
-            :type name: str
-            :rtype: str
-            )mydelimiter")
-
-            .def("add_child",
-                 (void (Node::*)(std::shared_ptr<Node>, const IOIndex_t, IOIndex_t)) &
-                         Node::addChild,
-                 py::arg("other_node"), py::arg("out_id") = 0, py::arg("other_in_id") = gk_IODefaultIndex,
-            R"mydelimiter(
-            Link another Node to an output of the current Node.
-
-            :param other_node: Pointer to the other Node.
-            :type other_node: :py:class: Node
-            :param out_id: ID of the current Node output to connect to the other Node. Default to 0.
-            :type out_id: int
-            :param other_in_id: ID of the other Node input to connect to the current Node. Default to the first avaible data input.
-            :type other_in_id: int
-            )mydelimiter")
-
-            .def("add_child",
-                 (void (Node::*)(std::shared_ptr<GraphView>, const IOIndex_t,
-                                 std::pair<std::shared_ptr<Node>, IOIndex_t>)) &
-                         Node::addChild,
-                 py::arg("other_graph"), py::arg("out_id") = 0,
-                 py::arg("other_in_id") =
-                         std::pair<std::shared_ptr<Node>, IOIndex_t>(nullptr, gk_IODefaultIndex),
-                        R"mydelimiter(
-            Link a Node from a specific GraphView to the current Node.
-
-            :param other_view: Pointer to the GraphView whose content should be linked to the current Node.
-            :type other_view: :py:class: GraphView
-            :param out_id: ID of the current Node output to connect to the other Node. Default to 0.
-            :type out_id: int
-            :param other_in_id: Pair of Node and input connection ID for specifying the connection. If the GraphView whose content is linked has only one input Node, then it defaults to the first available data input ID of this Node.
-            :type other_in_id: tuple[:py:class: Node, int]
-            )mydelimiter")
-
-            .def("inputs", &Node::inputs,
-            R"mydelimiter(
-            Get ordered list of parent Node and the associated output index connected to the current Node's inputs.
-
-            :return: List of connections. When an input is not linked to any parent, the default value is (None, default_index)
-            :rtype: list[tuple[Node, int]]
-            )mydelimiter")
-
-            .def("input", &Node::input, py::arg("in_id"),
-            R"mydelimiter(
-            Get the parent Node and the associated output index connected to the i-th input of the current Node.
-            
-            :param in_id: input index of the current Node object.
-            :type in_id: int
-            :return: i-th connection. When an input is not linked to any parent, the default value is (None, default_index)
-            :rtype: tuple[Node, int]
-            )mydelimiter")
-
-            .def("outputs", &Node::outputs,
-            R"mydelimiter(
-            Get, for each output of the Node, a list of the children Node and the associated input index connected to it.
-
-            :return: List of a list of connections. When an outut is not linked to any child,  its list a empty.
-            :rtype: list[list[tuple[Node, int]]]
-            )mydelimiter")
-
-            .def("output", &Node::output, py::arg("out_id"),
-            R"mydelimiter(
-            Get a list of the children Node for a specific output and the associated input index connected to it.
-            
-            :param out_id: input index of the current Node object.
-            :type out_id: int
-            :return: i-th connection. When an input is not linked to any parent, the default value is (None, default_index)
-            :rtype: list[tuple[Node, int]]
-            )mydelimiter")
-
-            .def("get_nb_inputs", &Node::nbInputs,
-            R"mydelimiter(
-            Number of inputs.
-
-            :rtype: int
-            )mydelimiter")
-
-            .def("get_nb_datainputs", &Node::nbDataInputs,
-            R"mydelimiter(
-            Number of data inputs.
-
-            :rtype: int
-            )mydelimiter")
-
-            .def("get_nb_outputs", &Node::nbOutputs,
-            R"mydelimiter(
-            Number of outputs.
-
-            :rtype: int
-            )mydelimiter")
-
-            .def("get_parents", &Node::getParents,
-            R"mydelimiter(
-            Get parents.
-            )mydelimiter")
-
-            .def("get_children", (std::set<std::shared_ptr<Node>> (Node::*)() const) &Node::getChildren,
-            R"mydelimiter(
-            Get children.
-            )mydelimiter")
-
-            .def("__call__", &Node::operator(), py::arg("connectors"));
+    .def("name", &Node::name,
+    R"mydelimiter(
+    Name of the Node.
+    )mydelimiter")
+
+    .def("type", &Node::type,
+    R"mydelimiter(
+    Type of the node.
+    )mydelimiter")
+
+    .def("get_operator", &Node::getOperator,
+    R"mydelimiter(
+    Get the Operator object of the Node.
+    )mydelimiter")
+
+    .def("set_name", &Node::setName, py::arg("name"),
+    R"mydelimiter(
+    Set the Node name.
+
+    :param name: New name for the node.
+    :type name: str
+    :rtype: str
+    )mydelimiter")
+
+    .def("add_child",
+         (void (Node::*)(std::shared_ptr<Node>, const IOIndex_t, IOIndex_t)) &
+                 Node::addChild,
+         py::arg("other_node"), py::arg("out_id") = 0, py::arg("other_in_id") = gk_IODefaultIndex,
+    R"mydelimiter(
+    Link another Node to an output of the current Node.
+
+    :param other_node: Pointer to the other Node.
+    :type other_node: :py:class: Node
+    :param out_id: ID of the current Node output to connect to the other Node. Default to 0.
+    :type out_id: int
+    :param other_in_id: ID of the other Node input to connect to the current Node. Default to the first avaible data input.
+    :type other_in_id: int
+    )mydelimiter")
+
+    .def("add_child",
+        (void (Node::*)(std::shared_ptr<GraphView>, const IOIndex_t,
+                        std::pair<std::shared_ptr<Node>, IOIndex_t>)) &
+                Node::addChild,
+        py::arg("other_graph"), py::arg("out_id") = 0,
+        py::arg("other_in_id") =
+                std::pair<std::shared_ptr<Node>, IOIndex_t>(nullptr, gk_IODefaultIndex),
+               R"mydelimiter(
+    Link a Node from a specific GraphView to the current Node.
+
+    :param other_view: Pointer to the GraphView whose content should be linked to the current Node.
+    :type other_view: :py:class: GraphView
+    :param out_id: ID of the current Node output to connect to the other Node. Default to 0.
+    :type out_id: int
+    :param other_in_id: Pair of Node and input connection ID for specifying the connection. If the GraphView whose content is linked has only one input Node,   then it defaults to the first available data input ID of this Node.
+    :type other_in_id: tuple[:py:class: Node, int]
+    )mydelimiter")
+
+    .def("inputs", &Node::inputs,
+    R"mydelimiter(
+    Get ordered list of parent Node and the associated output index connected to the current Node's inputs.
+
+    :return: List of connections. When an input is not linked to any parent, the default value is (None, default_index)
+    :rtype: list[tuple[Node, int]]
+    )mydelimiter")
+
+    .def("input", &Node::input, py::arg("in_id"),
+    R"mydelimiter(
+    Get the parent Node and the associated output index connected to the i-th input of the current Node.
+
+    :param in_id: input index of the current Node object.
+    :type in_id: int
+    :return: i-th connection. When an input is not linked to any parent, the default value is (None, default_index)
+    :rtype: tuple[Node, int]
+    )mydelimiter")
+
+    .def("outputs", &Node::outputs,
+    R"mydelimiter(
+    Get, for each output of the Node, a list of the children Node and the associated input index connected to it.
+
+    :return: List of a list of connections. When an outut is not linked to any child,  its list a empty.
+    :rtype: list[list[tuple[Node, int]]]
+    )mydelimiter")
+
+    .def("output", &Node::output, py::arg("out_id"),
+    R"mydelimiter(
+    Get a list of the children Node for a specific output and the associated input index connected to it.
+
+    :param out_id: input index of the current Node object.
+    :type out_id: int
+    :return: i-th connection. When an input is not linked to any parent, the default value is (None, default_index)
+    :rtype: list[tuple[Node, int]]
+    )mydelimiter")
+
+    .def("get_nb_inputs", &Node::nbInputs,
+    R"mydelimiter(
+    Number of inputs.
+
+    :rtype: int
+    )mydelimiter")
+
+    .def("get_nb_datainputs", &Node::nbDataInputs,
+    R"mydelimiter(
+    Number of data inputs.
+
+    :rtype: int
+    )mydelimiter")
+
+    .def("get_nb_outputs", &Node::nbOutputs,
+    R"mydelimiter(
+    Number of outputs.
+
+    :rtype: int
+    )mydelimiter")
+
+    .def("get_parents", &Node::getParents,
+    R"mydelimiter(
+    Get parents.
+    )mydelimiter")
+
+    .def("get_children", (std::set<std::shared_ptr<Node>> (Node::*)() const) &Node::getChildren,
+    R"mydelimiter(
+    Get children.
+    )mydelimiter")
+
+    .def("__call__",
+        [](Node &self, pybind11::args args) {
+            std::vector<Connector> connectors;
+            for (const auto &arg : args) {
+                // Check if the argument is an instance of Connector
+                if (pybind11::isinstance<Connector>(arg)) {
+                    // Convert Python object to C++ object adn push it ot vector
+                    connectors.push_back(arg.cast<Connector>());
+                } else {
+                    throw std::runtime_error("One of the arguments was not a Connector.");
+                }
+            }
+            return self(connectors);
+        });
 }
 }  // namespace Aidge
-- 
GitLab