@@ -36,47 +36,73 @@ namespace BT
36
36
When the SwitchNode is executed (Switch3 is a node with 3 cases)
37
37
the "variable" will be compared to the cases and execute the correct child
38
38
or the default one (last).
39
-
40
39
*
41
40
*/
41
+
42
+ namespace details {
43
+
44
+ bool CheckStringEquality (const std::string& v1, const std::string& v2,
45
+ const ScriptingEnumsRegistry *enums);
46
+ }
47
+
42
48
template <size_t NUM_CASES>
43
49
class SwitchNode : public ControlNode
44
50
{
45
51
public:
46
- SwitchNode (const std::string& name, const BT::NodeConfig& config) :
47
- ControlNode::ControlNode (name, config), running_child_(-1 )
48
- {
49
- setRegistrationID (" Switch" );
50
- }
52
+ SwitchNode (const std::string& name, const BT::NodeConfig& config);
51
53
52
54
virtual ~SwitchNode () override = default ;
53
55
54
- void halt () override
56
+ void halt () override ;
57
+
58
+ static PortsList providedPorts ();
59
+
60
+ private:
61
+ int running_child_;
62
+ std::vector<std::string> case_keys_;
63
+ virtual BT::NodeStatus tick () override ;
64
+ };
65
+
66
+ // -----------------------------------------------
67
+ // -----------------------------------------------
68
+
69
+ template <size_t NUM_CASES> inline
70
+ SwitchNode<NUM_CASES>::SwitchNode(const std::string &name, const NodeConfig &config) :
71
+ ControlNode::ControlNode (name, config), running_child_(-1 )
72
+ {
73
+ setRegistrationID (" Switch" );
74
+ for (unsigned i = 1 ; i <= NUM_CASES; i++)
55
75
{
56
- running_child_ = -1 ;
57
- ControlNode::halt ();
76
+ case_keys_.push_back ( std::string (" case_" ) + std::to_string (i));
58
77
}
78
+ }
59
79
60
- static PortsList providedPorts ()
61
- {
80
+ template <size_t NUM_CASES> inline
81
+ void SwitchNode<NUM_CASES>::halt()
82
+ {
83
+ running_child_ = -1 ;
84
+ ControlNode::halt ();
85
+ }
86
+
87
+ template <size_t NUM_CASES> inline
88
+ PortsList SwitchNode<NUM_CASES>::providedPorts()
89
+ {
90
+ static PortsList ports = []() {
62
91
PortsList ports;
63
92
ports.insert (BT::InputPort<std::string>(" variable" ));
64
- for (unsigned i = 0 ; i < NUM_CASES; i++)
93
+ for (unsigned i = 1 ; i <= NUM_CASES; i++)
65
94
{
66
- char case_str[20 ];
67
- sprintf (case_str, " case_%d" , i + 1 );
68
- ports.insert (BT::InputPort<std::string>(case_str));
95
+ auto key = std::string (" case_" ) + std::to_string (i);
96
+ ports.insert (BT::InputPort<std::string>(key));
69
97
}
70
98
return ports;
71
- }
99
+ }();
72
100
73
- private:
74
- int running_child_;
75
- virtual BT::NodeStatus tick () override ;
76
- };
101
+ return ports;
102
+ }
77
103
78
- template <size_t NUM_CASES>
79
- inline NodeStatus SwitchNode<NUM_CASES>::tick()
104
+ template <size_t NUM_CASES> inline
105
+ NodeStatus SwitchNode<NUM_CASES>::tick()
80
106
{
81
107
if (childrenCount () != NUM_CASES + 1 )
82
108
{
@@ -88,19 +114,21 @@ inline NodeStatus SwitchNode<NUM_CASES>::tick()
88
114
std::string value;
89
115
int match_index = int (NUM_CASES); // default index;
90
116
91
- if (getInput (" variable" , variable)) // no variable? jump to default
117
+ // no variable? jump to default
118
+ if (getInput (" variable" , variable))
92
119
{
93
120
// check each case until you find a match
94
121
for (int index = 0 ; index < int (NUM_CASES); ++index )
95
122
{
96
- char case_key[20 ];
97
- sprintf (case_key, " case_%d" , int (index + 1 ));
98
- bool found = static_cast <bool >(getInput (case_key, value));
99
-
100
- if (found && variable == value)
123
+ const std::string& case_key = case_keys_[index ];
124
+ if (getInput (case_key, value))
101
125
{
102
- match_index = index ;
103
- break ;
126
+ if (details::CheckStringEquality (
127
+ variable, value, this ->config ().enums .get ()))
128
+ {
129
+ match_index = index ;
130
+ break ;
131
+ }
104
132
}
105
133
}
106
134
}
0 commit comments