Skip to content

Commit 2df11a0

Browse files
committed
use PImpl with TreeNode
1 parent 16af46c commit 2df11a0

File tree

3 files changed

+151
-97
lines changed

3 files changed

+151
-97
lines changed

include/behaviortree_cpp/tree_node.h

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,13 @@ class TreeNode
134134
*/
135135
TreeNode(std::string name, NodeConfig config);
136136

137-
virtual ~TreeNode() = default;
137+
TreeNode(const TreeNode& other) = delete;
138+
TreeNode& operator=(const TreeNode& other) = delete;
139+
140+
TreeNode(TreeNode&& other);
141+
TreeNode& operator=(TreeNode&& other);
142+
143+
virtual ~TreeNode();
138144

139145
/// The method that should be used to invoke tick() and setStatus();
140146
virtual BT::NodeStatus executeTick();
@@ -314,7 +320,7 @@ class TreeNode
314320
else if constexpr (hasNodeNameCtor<DerivedT>())
315321
{
316322
auto node_ptr = new DerivedT(name, args...);
317-
node_ptr->config_ = config;
323+
node_ptr->config() = config;
318324
return std::unique_ptr<DerivedT>(node_ptr);
319325
}
320326
}
@@ -325,6 +331,8 @@ class TreeNode
325331
friend class ControlNode;
326332
friend class Tree;
327333

334+
[[nodiscard]] NodeConfig& config();
335+
328336
/// Method to be implemented by the user
329337
virtual BT::NodeStatus tick() = 0;
330338

@@ -345,31 +353,16 @@ class TreeNode
345353
*/
346354
void setStatus(NodeStatus new_status);
347355

348-
private:
349-
const std::string name_;
350-
351-
NodeStatus status_;
352-
353-
std::condition_variable state_condition_variable_;
354-
355-
mutable std::mutex state_mutex_;
356+
using PreScripts = std::array<ScriptFunction, size_t(PreCond::COUNT_)>;
357+
using PostScripts = std::array<ScriptFunction, size_t(PostCond::COUNT_)>;
356358

357-
StatusChangeSignal state_change_signal_;
359+
PreScripts& preConditionsScripts();
360+
PostScripts& postConditionsScripts();
358361

359-
NodeConfig config_;
360-
361-
std::string registration_ID_;
362-
363-
PreTickCallback substitution_callback_;
364-
365-
PostTickCallback post_condition_callback_;
366-
367-
std::mutex callback_injection_mutex_;
368-
369-
std::shared_ptr<WakeUpSignal> wake_up_;
362+
private:
370363

371-
std::array<ScriptFunction, size_t(PreCond::COUNT_)> pre_parsed_;
372-
std::array<ScriptFunction, size_t(PostCond::COUNT_)> post_parsed_;
364+
struct PImpl;
365+
PImpl* p = nullptr;
373366

374367
Expected<NodeStatus> checkPreConditions();
375368
void checkPostConditions(NodeStatus status);
@@ -388,9 +381,9 @@ inline Result TreeNode::getInput(const std::string& key, T& destination) const
388381
{
389382
if constexpr (std::is_enum_v<T> && !std::is_same_v<T, NodeStatus>)
390383
{
391-
auto it = config_.enums->find(str);
384+
auto it = config().enums->find(str);
392385
// conversion available
393-
if( it != config_.enums->end() )
386+
if( it != config().enums->end() )
394387
{
395388
return static_cast<T>(it->second);
396389
}
@@ -404,8 +397,8 @@ inline Result TreeNode::getInput(const std::string& key, T& destination) const
404397
}
405398
};
406399

407-
auto remap_it = config_.input_ports.find(key);
408-
if (remap_it == config_.input_ports.end())
400+
auto remap_it = config().input_ports.find(key);
401+
if (remap_it == config().input_ports.end())
409402
{
410403
return nonstd::make_unexpected(StrCat("getInput() failed because "
411404
"NodeConfig::input_ports "
@@ -418,9 +411,9 @@ inline Result TreeNode::getInput(const std::string& key, T& destination) const
418411
// BUT, it the port type is a string, then an empty string might be
419412
// a valid value
420413
const std::string& port_value_str = remap_it->second;
421-
if(port_value_str.empty() && config_.manifest)
414+
if(port_value_str.empty() && config().manifest)
422415
{
423-
const auto& port_manifest = config_.manifest->ports.at(key);
416+
const auto& port_manifest = config().manifest->ports.at(key);
424417
const auto& default_value = port_manifest.defaultValue();
425418
if(!default_value.empty() && !default_value.isString())
426419
{
@@ -440,12 +433,12 @@ inline Result TreeNode::getInput(const std::string& key, T& destination) const
440433
}
441434
const auto& remapped_key = remapped_res.value();
442435

443-
if (!config_.blackboard)
436+
if (!config().blackboard)
444437
{
445438
return nonstd::make_unexpected("getInput(): trying to access an invalid Blackboard");
446439
}
447440

448-
if (auto any_ref = config_.blackboard->getAnyLocked(std::string(remapped_key)))
441+
if (auto any_ref = config().blackboard->getAnyLocked(std::string(remapped_key)))
449442
{
450443
auto val = any_ref.get();
451444
if(!val->empty())
@@ -476,14 +469,14 @@ inline Result TreeNode::getInput(const std::string& key, T& destination) const
476469
template <typename T>
477470
inline Result TreeNode::setOutput(const std::string& key, const T& value)
478471
{
479-
if (!config_.blackboard)
472+
if (!config().blackboard)
480473
{
481474
return nonstd::make_unexpected("setOutput() failed: trying to access a "
482475
"Blackboard(BB) entry, but BB is invalid");
483476
}
484477

485-
auto remap_it = config_.output_ports.find(key);
486-
if (remap_it == config_.output_ports.end())
478+
auto remap_it = config().output_ports.find(key);
479+
if (remap_it == config().output_ports.end())
487480
{
488481
return nonstd::make_unexpected(StrCat("setOutput() failed: "
489482
"NodeConfig::output_ports "
@@ -499,7 +492,7 @@ inline Result TreeNode::setOutput(const std::string& key, const T& value)
499492
{
500493
remapped_key = stripBlackboardPointer(remapped_key);
501494
}
502-
config_.blackboard->set(static_cast<std::string>(remapped_key), value);
495+
config().blackboard->set(static_cast<std::string>(remapped_key), value);
503496

504497
return {};
505498
}

src/bt_factory.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ std::unique_ptr<TreeNode> BehaviorTreeFactory::instantiateTreeNode(
324324
}
325325

326326
node->setRegistrationID(ID);
327-
node->config_.enums = scripting_enums_;
327+
node->config().enums = scripting_enums_;
328328

329329
auto AssignConditions = [](auto& conditions, auto& executors) {
330330
for (const auto& [cond_id, script] : conditions)
@@ -339,8 +339,8 @@ std::unique_ptr<TreeNode> BehaviorTreeFactory::instantiateTreeNode(
339339
}
340340
}
341341
};
342-
AssignConditions(config.pre_conditions, node->pre_parsed_);
343-
AssignConditions(config.post_conditions, node->post_parsed_);
342+
AssignConditions(config.pre_conditions, node->preConditionsScripts());
343+
AssignConditions(config.post_conditions, node->postConditionsScripts());
344344

345345
return node;
346346
}

0 commit comments

Comments
 (0)