@@ -19,22 +19,25 @@ class PushIntoVector : public SyncActionNode
19
19
const int number = getInput<int >(" value" ).value ();
20
20
if (auto any_ptr = getLockedPortContent (" vector" ))
21
21
{
22
- // inside this scope, any_ptr provides a mutex-protected,
23
- // thread-safe way to access an element inside the blackboard.
24
- if (auto vect_ptr = any_ptr->castPtr <std::vector<int >>())
22
+ // inside this scope (as long as any_ptr exists) the access to
23
+ // the entry in the blackboard is mutex-protected and thread-safe.
24
+
25
+ // check if the entry contains a valid element
26
+ if (any_ptr->empty ())
25
27
{
26
- vect_ptr->push_back (number);
27
- std::cout << " Value [" << number <<" ] pushed into the vector. New size: "
28
- << vect_ptr->size () << " \n " ;
29
- }
30
- else {
31
- // This happens when the entry of the blackboard is empty or if
32
- // we tried to cast to the wrong type.
33
- // Let's insert a new vector<int> with a single value
28
+ // The entry hasn't been initialized by any other node, yet.
29
+ // Let's initialize it ourselves
34
30
std::vector<int > vect = {number};
35
31
any_ptr.assign (vect);
36
32
std::cout << " We created a new vector, containing value [" << number <<" ]\n " ;
37
33
}
34
+ else if (auto * vect_ptr = any_ptr->castPtr <std::vector<int >>())
35
+ {
36
+ // NOTE: vect_ptr would be nullptr, if we try to cast it to the wrong type
37
+ vect_ptr->push_back (number);
38
+ std::cout << " Value [" << number <<" ] pushed into the vector. New size: "
39
+ << vect_ptr->size () << " \n " ;
40
+ }
38
41
return NodeStatus::SUCCESS;
39
42
}
40
43
else {
0 commit comments