Skip to content

Commit 35bced2

Browse files
committed
Add bind as header library
1 parent 16d2d33 commit 35bced2

File tree

5 files changed

+167
-74
lines changed

5 files changed

+167
-74
lines changed

.clang-format

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
BasedOnStyle: Google
3+
ColumnLimit: 120
4+
5+
BinPackArguments: false
6+
BinPackParameters: false
7+
8+
DerivePointerAlignment: false
9+
PointerAlignment: Left
10+
QualifierAlignment: Left
11+
ReferenceAlignment: Left

.clang-tidy

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
# https://github.com/googleapis/google-cloud-cpp/blob/main/.clang-tidy
3+
Checks: >
4+
-*,
5+
abseil-*,
6+
bugprone-*,
7+
google-*,
8+
misc-*,
9+
modernize-*,
10+
performance-*,
11+
portability-*,
12+
readability-*,
13+
-google-readability-braces-around-statements,
14+
-google-readability-namespace-comments,
15+
-google-runtime-references,
16+
-misc-non-private-member-variables-in-classes,
17+
-misc-const-correctness,
18+
-misc-include-cleaner,
19+
-modernize-return-braced-init-list,
20+
-modernize-use-trailing-return-type,
21+
-modernize-concat-nested-namespaces,
22+
-modernize-use-nodiscard,
23+
-modernize-avoid-c-arrays,
24+
-performance-move-const-arg,
25+
-performance-avoid-endl,
26+
-performance-enum-size,
27+
-readability-braces-around-statements,
28+
-readability-identifier-length,
29+
-readability-magic-numbers,
30+
-readability-named-parameter,
31+
-readability-redundant-declaration,
32+
-readability-avoid-return-with-void-value,
33+
-readability-function-cognitive-complexity,
34+
-bugprone-narrowing-conversions,
35+
-bugprone-easily-swappable-parameters,
36+
-bugprone-inc-dec-in-conditions,
37+
-bugprone-implicit-widening-of-multiplication-result,
38+
-bugprone-unchecked-optional-access,
39+
-bugprone-unused-local-non-trivial-variable,
40+
-bugprone-unused-return-value
41+
42+
# Turn all the warnings from the checks above into errors.
43+
WarningsAsErrors: "*"
44+
45+
CheckOptions:
46+
- { key: readability-identifier-naming.NamespaceCase, value: lower_case }
47+
- { key: readability-identifier-naming.ClassCase, value: CamelCase }
48+
- { key: readability-identifier-naming.StructCase, value: CamelCase }
49+
- { key: readability-identifier-naming.TemplateParameterCase, value: CamelCase }
50+
- { key: readability-identifier-naming.FunctionCase, value: aNy_CasE }
51+
- { key: readability-identifier-naming.VariableCase, value: lower_case }
52+
- { key: readability-identifier-naming.ClassMemberCase, value: lower_case }
53+
- { key: readability-identifier-naming.ClassMemberSuffix, value: _ }
54+
- { key: readability-identifier-naming.PrivateMemberSuffix, value: _ }
55+
- { key: readability-identifier-naming.ProtectedMemberSuffix, value: _ }
56+
- { key: readability-identifier-naming.EnumConstantCase, value: CamelCase }
57+
- { key: readability-identifier-naming.EnumConstantPrefix, value: k }
58+
- { key: readability-identifier-naming.ConstexprVariableCase, value: CamelCase }
59+
- { key: readability-identifier-naming.ConstexprVariablePrefix, value: k }
60+
- { key: readability-identifier-naming.GlobalConstantCase, value: CamelCase }
61+
- { key: readability-identifier-naming.GlobalConstantPrefix, value: k }
62+
- { key: readability-identifier-naming.MemberConstantCase, value: CamelCase }
63+
- { key: readability-identifier-naming.MemberConstantPrefix, value: k }
64+
- { key: readability-identifier-naming.StaticConstantCase, value: CamelCase }
65+
- { key: readability-identifier-naming.StaticConstantPrefix, value: k }
66+
- { key: readability-implicit-bool-conversion.AllowIntegerConditions, value: 1 }
67+
- { key: readability-implicit-bool-conversion.AllowPointerConditions, value: 1 }
68+
- { key: readability-function-cognitive-complexity.IgnoreMacros, value: 1 }

CMakeLists.txt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@ find_package(fmt REQUIRED)
1616

1717
ament_python_install_package(behaviortree_py PACKAGE_DIR behaviortree_py)
1818

19+
add_library(${PROJECT_NAME}_headers INTERFACE)
20+
21+
target_include_directories(
22+
${PROJECT_NAME}_headers
23+
INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
24+
"$<INSTALL_INTERFACE:include>")
25+
1926
pybind11_add_module(behaviortree_py src/behaviortree_py.cpp)
2027
target_compile_features(behaviortree_py PRIVATE cxx_std_20)
2128
target_link_libraries(behaviortree_py PRIVATE behaviortree_cpp::behaviortree_cpp
22-
fmt::fmt)
23-
target_include_directories(
24-
behaviortree_py PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
25-
$<INSTALL_INTERFACE:include>)
29+
fmt::fmt ${PROJECT_NAME}_headers)
2630

2731
add_custom_command(
2832
TARGET behaviortree_py
@@ -35,10 +39,20 @@ add_custom_command(
3539
install(DIRECTORY include/ DESTINATION include/${PROJECT_NAME})
3640
install(TARGETS behaviortree_py
3741
LIBRARY DESTINATION ${PYTHON_INSTALL_DIR}/behaviortree_py)
42+
43+
install(
44+
TARGETS ${PROJECT_NAME}_headers
45+
EXPORT ${PROJECT_NAME}Targets
46+
ARCHIVE DESTINATION lib
47+
LIBRARY DESTINATION lib
48+
RUNTIME DESTINATION lib
49+
INCLUDES
50+
DESTINATION include)
3851
install(
3952
FILES
4053
$<TARGET_FILE_DIR:behaviortree_py>/$<TARGET_FILE_BASE_NAME:behaviortree_py>.pyi
4154
COMPONENT python
4255
DESTINATION ${PYTHON_INSTALL_DIR}/$<TARGET_FILE_BASE_NAME:behaviortree_py>)
4356

57+
ament_export_targets(${PROJECT_NAME}Targets)
4458
ament_package()

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#
2+
3+
sudo apt install mypy

src/behaviortree_py.cpp

Lines changed: 67 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
#include <behaviortree_cpp/behavior_tree.h>
2-
#include <memory>
3-
#include <pybind11/chrono.h>
4-
#include <pybind11/functional.h>
5-
#include <pybind11/pybind11.h>
6-
#include <pybind11/stl.h>
7-
81
#include <behaviortree_cpp/actions/always_success_node.h>
92
#include <behaviortree_cpp/basic_types.h>
3+
#include <behaviortree_cpp/behavior_tree.h>
104
#include <behaviortree_cpp/bt_factory.h>
115
#include <behaviortree_cpp/controls/fallback_node.h>
126
#include <behaviortree_cpp/controls/parallel_node.h>
@@ -16,110 +10,113 @@
1610
#include <behaviortree_cpp/loggers/bt_cout_logger.h>
1711
#include <behaviortree_cpp/loggers/groot2_publisher.h>
1812
#include <behaviortree_cpp/tree_node.h>
13+
#include <fmt/args.h>
14+
#include <pybind11/chrono.h>
15+
#include <pybind11/functional.h>
16+
#include <pybind11/pybind11.h>
17+
#include <pybind11/stl.h>
18+
1919
#include <behaviortree_py/behaviortree_py.hpp>
20+
#include <memory>
21+
#include <utility>
2022

2123
namespace py = pybind11;
22-
using namespace BT;
2324

2425
PYBIND11_MODULE(behaviortree_py, m) {
2526
m.doc() = "Python wrapper for BehaviorTree.CPP";
2627

27-
py::register_exception<nonstd::bad_expected_access<std::string>>(
28-
m, "BadExpectedAccess");
28+
py::register_exception<nonstd::bad_expected_access<std::string>>(m, "BadExpectedAccess");
2929

30-
py::enum_<NodeStatus>(m, "NodeStatus")
31-
.value("SUCCESS", NodeStatus::SUCCESS)
32-
.value("FAILURE", NodeStatus::FAILURE)
33-
.value("RUNNING", NodeStatus::RUNNING)
34-
.value("IDLE", NodeStatus::IDLE)
30+
py::enum_<BT::NodeStatus>(m, "NodeStatus")
31+
.value("SUCCESS", BT::NodeStatus::SUCCESS)
32+
.value("FAILURE", BT::NodeStatus::FAILURE)
33+
.value("RUNNING", BT::NodeStatus::RUNNING)
34+
.value("IDLE", BT::NodeStatus::IDLE)
3535
.export_values();
3636

37-
py::class_<BT::Result>(m, "Result")
38-
.def("__bool__", &BT::Result::operator bool)
39-
.def("error", [](BT::Result *self) { return self->error(); });
37+
py::class_<BT::Result>(m, "Result").def("__bool__", &BT::Result::operator bool).def("error", [](BT::Result* self) {
38+
return self->error();
39+
});
4040

41-
py::class_<Blackboard, std::shared_ptr<Blackboard>>(m, "Blackboard")
42-
.def_static("create", &Blackboard::create);
41+
py::class_<BT::Blackboard, std::shared_ptr<BT::Blackboard>>(m, "Blackboard")
42+
.def_static("create", &BT::Blackboard::create);
4343

44-
py::class_<Tree>(m, "Tree")
45-
.def("root_node", &Tree::rootNode,
46-
py::return_value_policy::reference_internal)
47-
.def("tick_while_running", &Tree::tickWhileRunning,
48-
py::arg("sleep_time") = std::chrono::milliseconds(10));
44+
py::class_<BT::Tree>(m, "Tree")
45+
.def("root_node", &BT::Tree::rootNode, py::return_value_policy::reference_internal)
46+
.def("tick_while_running", &BT::Tree::tickWhileRunning, py::arg("sleep_time") = std::chrono::milliseconds(10));
4947

50-
py::class_<TreeNode, std::shared_ptr<TreeNode>>(m, "TreeNode")
51-
.def("name", &TreeNode::name)
52-
.def("status", &TreeNode::status)
53-
.def("type", &TreeNode::type);
48+
py::class_<BT::TreeNode, std::shared_ptr<BT::TreeNode>>(m, "TreeNode")
49+
.def("name", &BT::TreeNode::name)
50+
.def("status", &BT::TreeNode::status)
51+
.def("type", &BT::TreeNode::type);
5452

55-
py::class_<BehaviorTreeFactory>(m, "BehaviorTreeFactory")
53+
py::class_<BT::BehaviorTreeFactory>(m, "BehaviorTreeFactory")
5654
.def(py::init<>())
57-
.def("create_tree_from_text", &BehaviorTreeFactory::createTreeFromText,
58-
py::arg("xml_text"), py::arg("blackboard") = Blackboard::create())
59-
.def("create_tree", &BehaviorTreeFactory::createTree,
60-
py::arg("tree_name"), py::arg("blackboard") = Blackboard::create())
55+
.def("create_tree_from_text",
56+
&BT::BehaviorTreeFactory::createTreeFromText,
57+
py::arg("xml_text"),
58+
py::arg("blackboard") = BT::Blackboard::create())
59+
.def("create_tree",
60+
&BT::BehaviorTreeFactory::createTree,
61+
py::arg("tree_name"),
62+
py::arg("blackboard") = BT::Blackboard::create())
6163
.def("register_behavior_tree_from_text",
62-
&BehaviorTreeFactory::registerBehaviorTreeFromText,
64+
&BT::BehaviorTreeFactory::registerBehaviorTreeFromText,
6365
py::arg("xml_text"))
64-
.def("register_scripting_enum",
65-
&BehaviorTreeFactory::registerScriptingEnum)
66+
.def("register_scripting_enum", &BT::BehaviorTreeFactory::registerScriptingEnum)
6667
.def(
6768
"register_simple_action",
68-
[](BehaviorTreeFactory *self, const std::string &id,
69-
std::function<NodeStatus(TreeNode *)> tick_functor,
70-
PortsList ports) {
69+
[](BT::BehaviorTreeFactory* self,
70+
const std::string& id,
71+
std::function<BT::NodeStatus(BT::TreeNode*)> tick_functor,
72+
BT::PortsList ports) {
7173
self->registerSimpleAction(
72-
id,
73-
[tick_functor](TreeNode &tree_node) {
74-
return tick_functor(&tree_node);
75-
},
76-
ports);
74+
id, [tick_functor](BT::TreeNode& tree_node) { return tick_functor(&tree_node); }, std::move(ports));
7775
},
78-
py::arg("id"), py::arg("tick_functor"),
79-
py::arg("ports") = PortsList())
76+
py::arg("id"),
77+
py::arg("tick_functor"),
78+
py::arg("ports") = BT::PortsList())
8079
.def(
8180
"register_simple_decorator",
8281
// We need to use a pointer cause pybind11 does copy for references
8382
// https://github.com/pybind/pybind11/issues/1123
84-
[](BehaviorTreeFactory *self, const std::string &id,
85-
std::function<NodeStatus(NodeStatus, TreeNode *)> tick_functor,
86-
PortsList ports) {
83+
[](BT::BehaviorTreeFactory* self,
84+
const std::string& id,
85+
std::function<BT::NodeStatus(BT::NodeStatus, BT::TreeNode*)> tick_functor,
86+
BT::PortsList ports) {
8787
self->registerSimpleDecorator(
8888
id,
89-
[tick_functor](NodeStatus node_status, TreeNode &tree_node) {
89+
[tick_functor](BT::NodeStatus node_status, BT::TreeNode& tree_node) {
9090
return tick_functor(node_status, &tree_node);
9191
},
92-
ports);
92+
std::move(ports));
9393
},
94-
py::arg("id"), py::arg("tick_functor"),
95-
py::arg("ports") = PortsList())
94+
py::arg("id"),
95+
py::arg("tick_functor"),
96+
py::arg("ports") = BT::PortsList())
9697
.def(
9798
"register_simple_condition",
98-
[](BehaviorTreeFactory *self, const std::string &id,
99-
std::function<NodeStatus(TreeNode *)> tick_functor,
100-
PortsList ports) {
99+
[](BT::BehaviorTreeFactory* self,
100+
const std::string& id,
101+
std::function<BT::NodeStatus(BT::TreeNode*)> tick_functor,
102+
BT::PortsList ports) {
101103
self->registerSimpleCondition(
102-
id,
103-
[tick_functor](TreeNode &tree_node) {
104-
return tick_functor(&tree_node);
105-
},
106-
ports);
104+
id, [tick_functor](BT::TreeNode& tree_node) { return tick_functor(&tree_node); }, std::move(ports));
107105
},
108-
py::arg("id"), py::arg("tick_functor"),
109-
py::arg("ports") = PortsList());
106+
py::arg("id"),
107+
py::arg("tick_functor"),
108+
py::arg("ports") = BT::PortsList());
110109

111-
py::class_<StdCoutLogger>(m, "StdCoutLogger")
112-
.def(py::init<const BT::Tree &>(), py::arg("tree"));
110+
py::class_<BT::StdCoutLogger>(m, "StdCoutLogger").def(py::init<const BT::Tree&>(), py::arg("tree"));
113111

114112
py::class_<BT::Groot2Publisher>(m, "Groot2Publisher")
115-
.def(py::init<const BT::Tree &, unsigned>(), py::arg("tree"),
116-
py::arg("port") = 1667);
113+
.def(py::init<const BT::Tree&, unsigned>(), py::arg("tree"), py::arg("port") = 1667);
117114

118-
py::class_<BT::PortInfo>(m, "PortInfo");
115+
py::class_<BT::PortInfo> give_me_a_name(m, "PortInfo");
119116

120117
m.def(
121118
"get_tree_recursively",
122-
[](const TreeNode *root_node) {
119+
[](const BT::TreeNode* root_node) {
123120
std::ostringstream s;
124121
BT::printTreeRecursively(root_node, s);
125122
return s.str();

0 commit comments

Comments
 (0)