BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
decorator_node.h
1#ifndef DECORATORNODE_H
2#define DECORATORNODE_H
3
4#include "behaviortree_cpp/tree_node.h"
5
6namespace BT
7{
8/**
9 * @brief The DecoratorNode is the base class for nodes that have exactly one child.
10 *
11 * DecoratorNodes modify the behavior of their child in some way.
12 * They may:
13 * - Transform the result received from the child (e.g., Inverter)
14 * - Control when or how many times the child is ticked (e.g., Repeat, Retry)
15 * - Add timing constraints (e.g., Timeout, Delay)
16 * - Conditionally execute the child (e.g., Precondition)
17 */
18class DecoratorNode : public TreeNode
19{
20protected:
21 TreeNode* child_node_;
22
23public:
24 DecoratorNode(const std::string& name, const NodeConfig& config);
25
26 ~DecoratorNode() override = default;
27
28 DecoratorNode(const DecoratorNode&) = delete;
29 DecoratorNode& operator=(const DecoratorNode&) = delete;
30 DecoratorNode(DecoratorNode&&) = delete;
31 DecoratorNode& operator=(DecoratorNode&&) = delete;
32
33 void setChild(TreeNode* child);
34
35 const TreeNode* child() const;
36
37 TreeNode* child();
38
39 /// The method used to interrupt the execution of this node
40 virtual void halt() override;
41
42 /// Same as resetChild()
43 void haltChild();
44
45 virtual NodeType type() const override
46 {
47 return NodeType::DECORATOR;
48 }
49
50 NodeStatus executeTick() override;
51
52 /// Set the status of the child to IDLE.
53 /// also send a halt() signal to a RUNNING child
54 void resetChild();
55};
56
57/**
58 * @brief The SimpleDecoratorNode provides an easy to use DecoratorNode.
59 * The user should simply provide a callback with this signature
60 *
61 * BT::NodeStatus functionName(BT::NodeStatus child_status)
62 *
63 * This avoids the hassle of inheriting from a DecoratorNode.
64 *
65 * Using lambdas or std::bind it is easy to pass a pointer to a method.
66 */
68{
69public:
70 using TickFunctor = std::function<NodeStatus(NodeStatus, TreeNode&)>;
71
72 // You must provide the function to call when tick() is invoked
73 SimpleDecoratorNode(const std::string& name, TickFunctor tick_functor,
74 const NodeConfig& config);
75
76 ~SimpleDecoratorNode() override = default;
77
78 SimpleDecoratorNode(const SimpleDecoratorNode&) = delete;
79 SimpleDecoratorNode& operator=(const SimpleDecoratorNode&) = delete;
80 SimpleDecoratorNode(SimpleDecoratorNode&&) = delete;
81 SimpleDecoratorNode& operator=(SimpleDecoratorNode&&) = delete;
82
83protected:
84 virtual NodeStatus tick() override;
85
86 TickFunctor tick_functor_;
87};
88} // namespace BT
89
90#endif
The DecoratorNode is the base class for nodes that have exactly one child.
Definition: decorator_node.h:19
void haltChild()
Same as resetChild()
virtual void halt() override
The method used to interrupt the execution of this node.
NodeStatus executeTick() override
The method that should be used to invoke tick() and setStatus();.
The SimpleDecoratorNode provides an easy to use DecoratorNode. The user should simply provide a callb...
Definition: decorator_node.h:68
virtual NodeStatus tick() override
Method to be implemented by the user.
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:154
Definition: action_node.h:24
NodeStatus
Definition: basic_types.h:34
NodeType
Enumerates the possible types of nodes.
Definition: basic_types.h:21
Definition: tree_node.h:105