t13_access_by_ref.cpp
Go to the documentation of this file.
2 
3 using namespace BT;
4 
11 {
12 public:
13  PushIntoVector(const std::string& name, const NodeConfig& config)
14  : SyncActionNode(name, config)
15  {}
16 
17  NodeStatus tick() override
18  {
19  const int number = getInput<int>("value").value();
20  if(auto any_ptr = getLockedPortContent("vector"))
21  {
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())
27  {
28  // The entry hasn't been initialized by any other node, yet.
29  // Let's initialize it ourselves
30  std::vector<int> vect = { number };
31  any_ptr.assign(vect);
32  std::cout << "We created a new vector, containing value [" << number << "]\n";
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
39  << "] pushed into the vector. New size: " << vect_ptr->size() << "\n";
40  }
41  return NodeStatus::SUCCESS;
42  }
43  else
44  {
45  return NodeStatus::FAILURE;
46  }
47  }
48 
50  {
51  return { BT::BidirectionalPort<std::vector<int>>("vector"), BT::InputPort<int>("valu"
52  "e") };
53  }
54 };
55 
56 //--------------------------------------------------------------
57 
58 // clang-format off
59 static const char* xml_tree = R"(
60  <root BTCPP_format="4" >
61  <BehaviorTree ID="TreeA">
62  <Sequence>
63  <PushIntoVector vector="{vect}" value="3"/>
64  <PushIntoVector vector="{vect}" value="5"/>
65  <PushIntoVector vector="{vect}" value="7"/>
66  </Sequence>
67  </BehaviorTree>
68  </root>
69  )";
70 
71 // clang-format on
72 
73 int main()
74 {
75  BehaviorTreeFactory factory;
76  factory.registerNodeType<PushIntoVector>("PushIntoVector");
77 
78  auto tree = factory.createTreeFromText(xml_tree);
79  tree.tickWhileRunning();
80  return 0;
81 }
BT
Definition: ex01_wrap_legacy.cpp:29
PushIntoVector
Show how to access an entry in the blackboard by pointer. This approach is more verbose,...
Definition: t13_access_by_ref.cpp:10
main
int main()
Definition: t13_access_by_ref.cpp:73
xml_tree
static const char * xml_tree
Definition: t13_access_by_ref.cpp:59
bt_factory.h
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:585
BT::NodeStatus::FAILURE
@ FAILURE
BT::BehaviorTreeFactory::registerNodeType
void registerNodeType(const std::string &ID, const PortsList &ports, ExtraArgs... args)
Definition: bt_factory.h:326
BT::BehaviorTreeFactory::createTreeFromText
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
createTreeFromText will parse the XML directly from string. The XML needs to contain either a single ...
Definition: bt_factory.cpp:395
BT::BehaviorTreeFactory
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:209
BT::NodeStatus::SUCCESS
@ SUCCESS
PushIntoVector::providedPorts
static PortsList providedPorts()
Definition: t13_access_by_ref.cpp:49
PushIntoVector::PushIntoVector
PushIntoVector(const std::string &name, const NodeConfig &config)
Definition: t13_access_by_ref.cpp:13
PushIntoVector::tick
NodeStatus tick() override
Method to be implemented by the user.
Definition: t13_access_by_ref.cpp:17
BT::NodeConfig
Definition: tree_node.h:73
BT::SyncActionNode
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn't require a...
Definition: action_node.h:52
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:17