Skip to content

Commit 2da7906

Browse files
committed
change TestNodeConfig preferred constructor
1 parent e0d1526 commit 2da7906

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

include/behaviortree_cpp/actions/test_node.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,14 @@ struct TestNodeConfig
6464
class TestNode : public BT::StatefulActionNode
6565
{
6666
public:
67-
TestNode(const std::string& name, const NodeConfig& config, TestNodeConfig test_config);
67+
// This constructor is deprecated, because it may cause problems if TestNodeConfig::complete_func is capturing
68+
// a reference to the TestNode, i.e. [this]. Use the constructor with std::shared_ptr<TestNodeConfig> instead.
69+
// For more details, see https://github.com/BehaviorTree/BehaviorTree.CPP/pull/967
70+
[[deprecated("prefer the constructor with std::shared_ptr<TestNodeConfig>")]] TestNode(
71+
const std::string& name, const NodeConfig& config, TestNodeConfig test_config);
72+
73+
TestNode(const std::string& name, const NodeConfig& config,
74+
std::shared_ptr<TestNodeConfig> test_config);
6875

6976
static PortsList providedPorts()
7077
{
@@ -80,7 +87,7 @@ class TestNode : public BT::StatefulActionNode
8087

8188
NodeStatus onCompleted();
8289

83-
TestNodeConfig _test_config;
90+
std::shared_ptr<TestNodeConfig> _config;
8491
ScriptFunction _success_executor;
8592
ScriptFunction _failure_executor;
8693
ScriptFunction _post_executor;

src/actions/test_node.cpp

+26-15
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
#include "behaviortree_cpp/actions/test_node.h"
22

3-
BT::TestNode::TestNode(const std::string& name, const NodeConfig& config,
4-
TestNodeConfig test_config)
5-
: StatefulActionNode(name, config), _test_config(std::move(test_config))
3+
namespace BT
4+
{
5+
6+
TestNode::TestNode(const std::string& name, const NodeConfig& config,
7+
TestNodeConfig test_config)
8+
: TestNode(name, config, std::make_shared<TestNodeConfig>(std::move(test_config)))
9+
{}
10+
11+
TestNode::TestNode(const std::string& name, const NodeConfig& config,
12+
std::shared_ptr<TestNodeConfig> test_config)
13+
: StatefulActionNode(name, config), _config(std::move(test_config))
614
{
715
setRegistrationID("TestNode");
816

9-
if(_test_config.return_status == NodeStatus::IDLE)
17+
if(_config->return_status == NodeStatus::IDLE)
1018
{
1119
throw RuntimeError("TestNode can not return IDLE");
1220
}
@@ -22,21 +30,21 @@ BT::TestNode::TestNode(const std::string& name, const NodeConfig& config,
2230
executor = result.value();
2331
}
2432
};
25-
prepareScript(_test_config.success_script, _success_executor);
26-
prepareScript(_test_config.failure_script, _failure_executor);
27-
prepareScript(_test_config.post_script, _post_executor);
33+
prepareScript(_config->success_script, _success_executor);
34+
prepareScript(_config->failure_script, _failure_executor);
35+
prepareScript(_config->post_script, _post_executor);
2836
}
2937

30-
BT::NodeStatus BT::TestNode::onStart()
38+
NodeStatus TestNode::onStart()
3139
{
32-
if(_test_config.async_delay <= std::chrono::milliseconds(0))
40+
if(_config->async_delay <= std::chrono::milliseconds(0))
3341
{
3442
return onCompleted();
3543
}
3644
// convert this in an asynchronous operation. Use another thread to count
3745
// a certain amount of time.
3846
_completed = false;
39-
_timer.add(std::chrono::milliseconds(_test_config.async_delay), [this](bool aborted) {
47+
_timer.add(std::chrono::milliseconds(_config->async_delay), [this](bool aborted) {
4048
if(!aborted)
4149
{
4250
_completed.store(true);
@@ -50,7 +58,7 @@ BT::NodeStatus BT::TestNode::onStart()
5058
return NodeStatus::RUNNING;
5159
}
5260

53-
BT::NodeStatus BT::TestNode::onRunning()
61+
NodeStatus TestNode::onRunning()
5462
{
5563
if(_completed)
5664
{
@@ -59,17 +67,18 @@ BT::NodeStatus BT::TestNode::onRunning()
5967
return NodeStatus::RUNNING;
6068
}
6169

62-
void BT::TestNode::onHalted()
70+
void TestNode::onHalted()
6371
{
6472
_timer.cancelAll();
6573
}
6674

67-
BT::NodeStatus BT::TestNode::onCompleted()
75+
NodeStatus TestNode::onCompleted()
6876
{
6977
Ast::Environment env = { config().blackboard, config().enums };
7078

71-
auto status = (_test_config.complete_func) ? _test_config.complete_func() :
72-
_test_config.return_status;
79+
auto status =
80+
(_config->complete_func) ? _config->complete_func() : _config->return_status;
81+
7382
if(status == NodeStatus::SUCCESS && _success_executor)
7483
{
7584
_success_executor(env);
@@ -84,3 +93,5 @@ BT::NodeStatus BT::TestNode::onCompleted()
8493
}
8594
return status;
8695
}
96+
97+
} // namespace BT

src/bt_factory.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,9 @@ std::unique_ptr<TreeNode> BehaviorTreeFactory::instantiateTreeNode(
263263
}
264264
else if(const auto test_config = std::get_if<TestNodeConfig>(&rule))
265265
{
266-
// second case, the varian is a TestNodeConfig
267-
auto test_node = new TestNode(name, config, *test_config);
266+
// second case, the variant is a TestNodeConfig
267+
auto test_node =
268+
new TestNode(name, config, std::make_shared<TestNodeConfig>(*test_config));
268269
node.reset(test_node);
269270
substituted = true;
270271
break;

0 commit comments

Comments
 (0)