2
3
4
5
6
7
8
9
10
11
15#include "behaviortree_cpp/control_node.h"
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
45bool CheckStringEquality(
const std::string& v1,
const std::string& v2,
46 const ScriptingEnumsRegistry* enums);
49template <size_t NUM_CASES>
53 SwitchNode(
const std::string& name,
const BT::
NodeConfig& config);
55 ~SwitchNode()
override =
default;
64 static PortsList providedPorts();
67 int running_child_ = -1;
68 std::vector<std::string> case_keys_;
75template <size_t NUM_CASES>
76inline SwitchNode<NUM_CASES>::SwitchNode(
const std::string& name,
80 setRegistrationID(
"Switch");
81 for(
unsigned i = 1; i <= NUM_CASES; i++)
83 case_keys_.push_back(std::string(
"case_") + std::to_string(i));
87template <size_t NUM_CASES>
94template <size_t NUM_CASES>
95inline PortsList
SwitchNode<NUM_CASES>::providedPorts()
97 static PortsList provided_ports = []() {
99 ports.insert(BT::InputPort<std::string>(
"variable"));
100 for(
unsigned i = 1; i <= NUM_CASES; i++)
102 auto key = std::string(
"case_") + std::to_string(i);
103 ports.insert(BT::InputPort<std::string>(key));
108 return provided_ports;
111template <size_t NUM_CASES>
114 if(childrenCount() != NUM_CASES + 1)
116 throw LogicError(
"Wrong number of children in SwitchNode; "
117 "must be (num_cases + default)");
120 std::string variable;
122 int match_index =
int(NUM_CASES);
125 if(getInput(
"variable", variable))
128 for(
int index = 0; index <
int(NUM_CASES); ++index)
130 const std::string& case_key = case_keys_[index];
131 if(getInput(case_key, value))
133 if(details::CheckStringEquality(variable, value,
this->config().enums.get()))
143 if(running_child_ != -1 && running_child_ != match_index)
145 haltChild(running_child_);
148 auto& selected_child = children_nodes_[match_index];
149 NodeStatus ret = selected_child->executeTick();
159 running_child_ = match_index;
The ControlNode is the base class for nodes that can have multiple children.
Definition: control_node.h:32
virtual void halt() override
Definition: switch_node.h:51
void halt() override
Definition: switch_node.h:88
The SwitchNode is equivalent to a switch statement, where a certain branch (child) is executed accord...
Definition: basic_types.h:515
Definition: action_node.h:24
NodeStatus
Definition: basic_types.h:34
Definition: tree_node.h:105