Skip to content

Commit 1f9db33

Browse files
committed
Eliminate some code duplication.
1 parent f560500 commit 1f9db33

File tree

1 file changed

+19
-31
lines changed

1 file changed

+19
-31
lines changed

src/python_bindings.cpp

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,6 @@ class Py_SyncActionNode : public SyncActionNode
3030
py::gil_scoped_acquire gil;
3131
return py::get_overload(this, "tick")().cast<NodeStatus>();
3232
}
33-
34-
py::object Py_getInput(const std::string& name)
35-
{
36-
py::object obj;
37-
getInput(name, obj);
38-
return obj;
39-
}
40-
41-
void Py_setOutput(const std::string& name, const py::object& value)
42-
{
43-
setOutput(name, value);
44-
}
4533
};
4634

4735
class Py_StatefulActionNode final : public StatefulActionNode
@@ -68,21 +56,21 @@ class Py_StatefulActionNode final : public StatefulActionNode
6856
py::gil_scoped_acquire gil;
6957
py::get_overload(this, "on_halted")();
7058
}
59+
};
7160

72-
// TODO: Share these duplicated methods with other node types.
73-
py::object Py_getInput(const std::string& name)
74-
{
75-
py::object obj;
76-
getInput(name, obj);
77-
return obj;
78-
}
61+
template <typename T>
62+
py::object Py_getInput(const T& node, const std::string& name)
63+
{
64+
py::object obj;
65+
node.getInput(name, obj);
66+
return obj;
67+
}
7968

80-
// TODO: Share these duplicated methods with other node types.
81-
void Py_setOutput(const std::string& name, const py::object& value)
82-
{
83-
setOutput(name, value);
84-
}
85-
};
69+
template <typename T>
70+
void Py_setOutput(T& node, const std::string& name, const py::object& value)
71+
{
72+
node.setOutput(name, value);
73+
}
8674

8775
// Add a conversion specialization from string values into general py::objects
8876
// by evaluating as a Python expression.
@@ -190,17 +178,17 @@ PYBIND11_MODULE(btpy_cpp, m)
190178

191179
py::class_<Py_SyncActionNode>(m, "SyncActionNode")
192180
.def(py::init<const std::string&, const NodeConfig&>())
193-
.def("tick", &Py_SyncActionNode::tick)
194-
.def("get_input", &Py_SyncActionNode::Py_getInput)
195-
.def("set_output", &Py_SyncActionNode::Py_setOutput);
181+
.def("get_input", &Py_getInput<Py_SyncActionNode>)
182+
.def("set_output", &Py_setOutput<Py_SyncActionNode>)
183+
.def("tick", &Py_SyncActionNode::tick);
196184

197185
py::class_<Py_StatefulActionNode>(m, "StatefulActionNode")
198186
.def(py::init<const std::string&, const NodeConfig&>())
187+
.def("get_input", &Py_getInput<Py_StatefulActionNode>)
188+
.def("set_output", &Py_setOutput<Py_StatefulActionNode>)
199189
.def("on_start", &Py_StatefulActionNode::onStart)
200190
.def("on_running", &Py_StatefulActionNode::onRunning)
201-
.def("on_halted", &Py_StatefulActionNode::onHalted)
202-
.def("get_input", &Py_StatefulActionNode::Py_getInput)
203-
.def("set_output", &Py_StatefulActionNode::Py_setOutput);
191+
.def("on_halted", &Py_StatefulActionNode::onHalted);
204192
}
205193

206194
} // namespace BT

0 commit comments

Comments
 (0)