Skip to content

Commit 47f067c

Browse files
committed
feat: clean up old PR for new version of BTCPP
1 parent 8c6cfac commit 47f067c

File tree

7 files changed

+51
-48
lines changed

7 files changed

+51
-48
lines changed

include/behaviortree_cpp/json_export.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ class JsonExporter
7878
template <typename T>
7979
Expected<T> fromJson(const nlohmann::json& source) const;
8080

81+
template <typename T>
82+
void fromJsonHelper(const nlohmann::json& src, T& dst) const
83+
{
84+
dst = *fromJson<T>(src);
85+
}
86+
8187
/// Register new JSON converters with addConverter<Foo>().
8288
/// You should have used first the macro BT_JSON_CONVERTER
8389
template <typename T>

include/behaviortree_cpp/python/types.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <pybind11/pybind11.h>
44

5+
#include "behaviortree_cpp/basic_types.h"
56
#include "behaviortree_cpp/json_export.h"
67
#include "behaviortree_cpp/contrib/json.hpp"
78
#include "behaviortree_cpp/contrib/pybind11_json.hpp"
@@ -19,9 +20,9 @@ namespace BT
1920
template <typename T>
2021
bool fromPythonObject(const pybind11::object& obj, T& dest)
2122
{
22-
if constexpr (nlohmann::detail::is_getable<nlohmann::json, T>::value)
23+
if constexpr(nlohmann::detail::is_getable<nlohmann::json, T>::value)
2324
{
24-
JsonExporter::get().fromJson<T>(obj, dest);
25+
JsonExporter::get().fromJsonHelper<T>(obj, dest);
2526
return true;
2627
}
2728

@@ -36,4 +37,4 @@ bool fromPythonObject(const pybind11::object& obj, T& dest)
3637
*/
3738
bool toPythonObject(const BT::Any& val, pybind11::object& dest);
3839

39-
} // namespace BT
40+
} // namespace BT

include/behaviortree_cpp/tree_node.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -511,12 +511,6 @@ inline Expected<Timestamp> TreeNode::getInputStamped(const std::string& key,
511511
{
512512
try
513513
{
514-
// Trivial conversion (T -> T)
515-
if (val->type() == typeid(T))
516-
{
517-
destination = val->cast<T>();
518-
}
519-
else if (!std::is_same_v<T, std::string> && val->type() == typeid(std::string))
520514
destination = parseString<T>(port_value_str);
521515
}
522516
catch(std::exception& ex)
@@ -553,17 +547,17 @@ inline Expected<Timestamp> TreeNode::getInputStamped(const std::string& key,
553547
}
554548
#ifdef BTCPP_PYTHON
555549
// py::object -> C++
556-
else if (val->type() == typeid(pybind11::object))
550+
else if(any_value.type() == typeid(pybind11::object))
557551
{
558-
if (!fromPythonObject<T>(val->cast<pybind11::object>(), destination))
552+
if(!fromPythonObject<T>(any_value.cast<pybind11::object>(), destination))
559553
{
560554
return nonstd::make_unexpected("Cannot convert from Python object");
561555
}
562556
}
563557
// C++ -> py::object
564-
else if constexpr (std::is_same_v<T, pybind11::object>)
558+
else if constexpr(std::is_same_v<T, pybind11::object>)
565559
{
566-
if (!toPythonObject(*val, destination))
560+
if(!toPythonObject(any_value, destination))
567561
{
568562
return nonstd::make_unexpected("Cannot convert to Python object");
569563
}

sample_nodes/dummy_nodes.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ BT::NodeStatus SaySomethingSimple(BT::TreeNode& self)
7474

7575
void to_json(nlohmann::json& j, const Vector3& p)
7676
{
77-
j = nlohmann::json{{"x", p.x}, {"y", p.y}, {"z", p.z}};
77+
j = nlohmann::json{ { "x", p.x }, { "y", p.y }, { "z", p.z } };
7878
}
7979

8080
void from_json(const nlohmann::json& j, Vector3& p)
@@ -84,4 +84,4 @@ void from_json(const nlohmann::json& j, Vector3& p)
8484
j.at("z").get_to(p.z);
8585
}
8686

87-
} // namespace DummyNodes
87+
} // namespace DummyNodes

sample_nodes/dummy_nodes.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,46 +134,45 @@ void from_json(const nlohmann::json& j, Vector3& p);
134134
class RandomVector : public BT::SyncActionNode
135135
{
136136
public:
137-
RandomVector(const std::string& name, const BT::NodeConfig& config) :
138-
BT::SyncActionNode(name, config)
137+
RandomVector(const std::string& name, const BT::NodeConfig& config)
138+
: BT::SyncActionNode(name, config)
139139
{}
140140

141141
// You must override the virtual function tick()
142142
NodeStatus tick() override
143143
{
144-
setOutput("vector", Vector3{1.0, 2.0, 3.0});
144+
setOutput("vector", Vector3{ 1.0, 2.0, 3.0 });
145145
return BT::NodeStatus::SUCCESS;
146146
}
147147

148148
// It is mandatory to define this static method.
149149
static BT::PortsList providedPorts()
150150
{
151-
return {BT::OutputPort<Vector3>("vector")};
151+
return { BT::OutputPort<Vector3>("vector") };
152152
}
153153
};
154154

155155
class PrintMapOfVectors : public BT::SyncActionNode
156156
{
157157
public:
158-
PrintMapOfVectors(const std::string& name, const BT::NodeConfig& config) :
159-
BT::SyncActionNode(name, config)
158+
PrintMapOfVectors(const std::string& name, const BT::NodeConfig& config)
159+
: BT::SyncActionNode(name, config)
160160
{}
161161

162162
// You must override the virtual function tick()
163163
NodeStatus tick() override
164164
{
165165
auto input = getInput<std::unordered_map<std::string, Vector3>>("input");
166-
if (input.has_value())
166+
if(input.has_value())
167167
{
168168
std::cerr << "{";
169-
for (const auto& [key, value] : *input)
169+
for(const auto& [key, value] : *input)
170170
{
171-
std::cerr << key << ": ("
172-
<< value.x << ", "
173-
<< value.y << ", "
174-
<< value.z << "), ";
171+
std::cerr << key << ": (" << value.x << ", " << value.y << ", " << value.z
172+
<< "), ";
175173
}
176-
std::cerr << "}" << std::endl;;
174+
std::cerr << "}" << std::endl;
175+
;
177176
}
178177

179178
return BT::NodeStatus::SUCCESS;
@@ -182,7 +181,7 @@ class PrintMapOfVectors : public BT::SyncActionNode
182181
// It is mandatory to define this static method.
183182
static BT::PortsList providedPorts()
184183
{
185-
return {BT::InputPort<std::unordered_map<std::string, Vector3>>("input")};
184+
return { BT::InputPort<std::unordered_map<std::string, Vector3>>("input") };
186185
}
187186
};
188187

src/python/bindings.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ namespace py = pybind11;
2121
class Py_SyncActionNode : public SyncActionNode
2222
{
2323
public:
24-
Py_SyncActionNode(const std::string& name, const NodeConfig& config) :
25-
SyncActionNode(name, config)
24+
Py_SyncActionNode(const std::string& name, const NodeConfig& config)
25+
: SyncActionNode(name, config)
2626
{}
2727

2828
NodeStatus tick() override
@@ -34,8 +34,8 @@ class Py_SyncActionNode : public SyncActionNode
3434
class Py_StatefulActionNode : public StatefulActionNode
3535
{
3636
public:
37-
Py_StatefulActionNode(const std::string& name, const NodeConfig& config) :
38-
StatefulActionNode(name, config)
37+
Py_StatefulActionNode(const std::string& name, const NodeConfig& config)
38+
: StatefulActionNode(name, config)
3939
{}
4040

4141
NodeStatus onStart() override
@@ -61,7 +61,7 @@ py::object Py_getInput(const TreeNode& node, const std::string& name)
6161

6262
// The input could not exist on the blackboard, in which case we return Python
6363
// `None` instead of an invalid object.
64-
if (!node.getInput(name, obj).has_value())
64+
if(!node.getInput(name, obj).has_value())
6565
{
6666
return py::none();
6767
}
@@ -84,7 +84,7 @@ inline py::object convertFromString(StringView str)
8484
// dict, an object, etc.
8585
return py::eval(str);
8686
}
87-
catch (py::error_already_set& e)
87+
catch(py::error_already_set& e)
8888
{
8989
// If that fails, then assume it's a string literal with quotation marks
9090
// omitted.
@@ -97,15 +97,15 @@ PortsList extractPortsList(const py::type& type)
9797
PortsList ports;
9898

9999
const auto input_ports = type.attr("input_ports").cast<py::list>();
100-
for (const auto& name : input_ports)
100+
for(const auto& name : input_ports)
101101
{
102-
ports.insert(InputPort<PortInfo::AnyTypeAllowed>(name.cast<std::string>()));
102+
ports.insert(InputPort<AnyTypeAllowed>(name.cast<std::string>()));
103103
}
104104

105105
const auto output_ports = type.attr("output_ports").cast<py::list>();
106-
for (const auto& name : output_ports)
106+
for(const auto& name : output_ports)
107107
{
108-
ports.insert(OutputPort<PortInfo::AnyTypeAllowed>(name.cast<std::string>()));
108+
ports.insert(OutputPort<AnyTypeAllowed>(name.cast<std::string>()));
109109
}
110110

111111
return ports;
@@ -114,8 +114,7 @@ PortsList extractPortsList(const py::type& type)
114114
NodeBuilder makeTreeNodeBuilderFn(const py::type& type, const py::args& args,
115115
const py::kwargs& kwargs)
116116
{
117-
return [=](const auto& name, const auto& config) -> auto
118-
{
117+
return [=](const auto& name, const auto& config) -> auto {
119118
py::object obj;
120119
obj = type(name, config, *args, **kwargs);
121120

@@ -125,7 +124,7 @@ NodeBuilder makeTreeNodeBuilderFn(const py::type& type, const py::args& args,
125124
// is destroyed, then the object will live forever.
126125
obj.inc_ref();
127126

128-
if (py::isinstance<ActionNodeBase>(obj))
127+
if(py::isinstance<ActionNodeBase>(obj))
129128
{
130129
return std::unique_ptr<TreeNode>(obj.cast<ActionNodeBase*>());
131130
}
@@ -149,12 +148,16 @@ PYBIND11_MODULE(btpy_cpp, m)
149148
manifest.type = NodeType::ACTION;
150149
manifest.registration_ID = name;
151150
manifest.ports = extractPortsList(type);
152-
manifest.description = "";
151+
manifest.metadata = KeyValueVector{
152+
{ "description", "" },
153+
};
153154

154155
// Use the type's docstring as the node description, if it exists.
155-
if (const auto doc = type.attr("__doc__"); !doc.is_none())
156+
if(const auto doc = type.attr("__doc__"); !doc.is_none())
156157
{
157-
manifest.description = doc.cast<std::string>();
158+
manifest.metadata = KeyValueVector{
159+
{ "description", doc.cast<std::string>() },
160+
};
158161
}
159162

160163
factory.registerBuilder(manifest, makeTreeNodeBuilderFn(type, args, kwargs));
@@ -202,4 +205,4 @@ PYBIND11_MODULE(btpy_cpp, m)
202205
.def("on_halted", &Py_StatefulActionNode::onHalted);
203206
}
204207

205-
} // namespace BT
208+
} // namespace BT

src/python/types.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace BT
1313
bool toPythonObject(const BT::Any& val, pybind11::object& dest)
1414
{
1515
nlohmann::json json;
16-
if (JsonExporter::get().toJson(val, json))
16+
if(JsonExporter::get().toJson(val, json))
1717
{
1818
dest = json;
1919
return true;
@@ -22,4 +22,4 @@ bool toPythonObject(const BT::Any& val, pybind11::object& dest)
2222
return false;
2323
}
2424

25-
} // namespace BT
25+
} // namespace BT

0 commit comments

Comments
 (0)