switch_node.h
Go to the documentation of this file.
1 /* Copyright (C) 2020-2022 Davide Faconti - All Rights Reserved
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
4 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
5 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
10 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 */
12 
13 #pragma once
14 
16 
17 namespace BT
18 {
42 template <size_t NUM_CASES>
43 class SwitchNode : public ControlNode
44 {
45 public:
46  SwitchNode(const std::string& name, const BT::NodeConfiguration& config) :
48  {
49  setRegistrationID("Switch");
50  }
51 
52  virtual ~SwitchNode() override = default;
53 
54  void halt() override
55  {
56  running_child_ = -1;
58  }
59 
61  {
62  PortsList ports;
63  ports.insert(BT::InputPort<std::string>("variable"));
64  for (unsigned i = 0; i < NUM_CASES; i++)
65  {
66  char case_str[20];
67  sprintf(case_str, "case_%d", i + 1);
68  ports.insert(BT::InputPort<std::string>(case_str));
69  }
70  return ports;
71  }
72 
73 private:
75  virtual BT::NodeStatus tick() override;
76 };
77 
78 template <size_t NUM_CASES>
80 {
81  if (childrenCount() != NUM_CASES + 1)
82  {
83  throw LogicError("Wrong number of children in SwitchNode; "
84  "must be (num_cases + default)");
85  }
86 
87  std::string variable;
88  std::string value;
89  int match_index = int(NUM_CASES); // default index;
90 
91  if (getInput("variable", variable)) // no variable? jump to default
92  {
93  // check each case until you find a match
94  for (int index = 0; index < int(NUM_CASES); ++index)
95  {
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)
101  {
102  match_index = index;
103  break;
104  }
105  }
106  }
107 
108  // if another one was running earlier, halt it
109  if (running_child_ != -1 && running_child_ != match_index)
110  {
111  haltChild(running_child_);
112  }
113 
114  auto& selected_child = children_nodes_[match_index];
115  NodeStatus ret = selected_child->executeTick();
116  if (ret == NodeStatus::RUNNING)
117  {
118  running_child_ = match_index;
119  }
120  else
121  {
122  resetChildren();
123  running_child_ = -1;
124  }
125  return ret;
126 }
127 
128 } // namespace BT
BT
Definition: ex01_wrap_legacy.cpp:29
BT::SwitchNode::halt
void halt() override
Definition: switch_node.h:54
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:342
BT::TreeNode::config
const NodeConfiguration & config() const
Definition: tree_node.cpp:127
BT::LogicError
Definition: exceptions.h:45
BT::SwitchNode::running_child_
int running_child_
Definition: switch_node.h:74
BT::NodeConfiguration
Definition: tree_node.h:44
BT::SwitchNode::tick
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: switch_node.h:79
BT::SwitchNode::~SwitchNode
virtual ~SwitchNode() override=default
BT::SwitchNode::SwitchNode
SwitchNode(const std::string &name, const BT::NodeConfiguration &config)
Definition: switch_node.h:46
BT::TreeNode::setRegistrationID
void setRegistrationID(StringView ID)
Definition: tree_node.cpp:201
control_node.h
BT::SwitchNode
The SwitchNode is equivalent to a switch statement, where a certain branch (child) is executed accord...
Definition: switch_node.h:43
BT::TreeNode::name
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:101
BT::NodeStatus::RUNNING
@ RUNNING
BT::SwitchNode::providedPorts
static PortsList providedPorts()
Definition: switch_node.h:60
BT::ControlNode::halt
virtual void halt() override
Definition: control_node.cpp:32
BT::ControlNode
Definition: control_node.h:21
BT::NodeStatus
NodeStatus
Definition: basic_types.h:35


behaviortree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Wed Jun 26 2024 02:51:19