2
2
3
3
using namespace BT ;
4
4
5
+ // To demonstrate how to pass arguments by reference, we
6
+ // use a simple non-copyable object
7
+ class NoCopyObj {
8
+ public:
9
+ NoCopyObj (int val): _value(val) {}
10
+
11
+ NoCopyObj (const NoCopyObj& ) = delete ;
12
+ NoCopyObj& operator =(const NoCopyObj& ) = delete ;
13
+ int value () { return _value; }
14
+ private:
15
+ int _value = 0 ;
16
+ };
17
+
5
18
/*
6
19
* Sometimes, it is convenient to pass additional (static) arguments to a Node.
7
20
* If these parameters are known at compilation time or at deployment-time
@@ -17,13 +30,14 @@ class Action_A : public SyncActionNode
17
30
public:
18
31
// additional arguments passed to the constructor
19
32
Action_A (const std::string& name, const NodeConfig& config, int arg_int,
20
- std::string arg_str) :
21
- SyncActionNode (name, config), _arg1(arg_int), _arg2(arg_str)
33
+ std::string arg_str, NoCopyObj& nc ) :
34
+ SyncActionNode (name, config), _arg1(arg_int), _arg2(arg_str), _nc(nc)
22
35
{}
23
36
24
37
NodeStatus tick () override
25
38
{
26
- std::cout << name () << " : " << _arg1 << " / " << _arg2 << std::endl;
39
+ std::cout << name () << " : " << _arg1 << " / "
40
+ << _arg2 << " / " << _nc.value () << std::endl;
27
41
return NodeStatus::SUCCESS;
28
42
}
29
43
static PortsList providedPorts ()
@@ -34,6 +48,7 @@ class Action_A : public SyncActionNode
34
48
private:
35
49
int _arg1;
36
50
std::string _arg2;
51
+ NoCopyObj& _nc;
37
52
};
38
53
39
54
// Action_B implements an init(...) method that must be called once at the beginning.
@@ -83,8 +98,13 @@ int main()
83
98
{
84
99
BehaviorTreeFactory factory;
85
100
101
+ NoCopyObj non_copyable (88 );
102
+
86
103
// Passing the extra parameters to the constructor of Action_A
87
- factory.registerNodeType <Action_A>(" Action_A" , 42 , " hello world" );
104
+ // note that if you want to pass an object by ref, instead of value
105
+ // (copy), you must use std::ref wrapper.
106
+ factory.registerNodeType <Action_A>(" Action_A" , 42 , " hello world" ,
107
+ std::ref (non_copyable));
88
108
89
109
// Action_B will require initialization
90
110
factory.registerNodeType <Action_B>(" Action_B" );
@@ -103,7 +123,7 @@ int main()
103
123
tree.tickWhileRunning ();
104
124
105
125
/* Expected output:
106
- Action_A: 42 / hello world
126
+ Action_A: 42 / hello world / 88
107
127
Action_B: 69 / interesting_value
108
128
*/
109
129
return 0 ;
0 commit comments