consume_queue.h
Go to the documentation of this file.
1 /* Copyright (C) 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 
15 #include <list>
18 
19 namespace BT
20 {
28 template <typename T>
30 {
31 public:
32  ConsumeQueue(const std::string& name, const NodeConfiguration& config) :
33  DecoratorNode(name, config)
34  {}
35 
36  NodeStatus tick() override
37  {
38  if (running_child_)
39  {
40  NodeStatus child_state = child_node_->executeTick();
41  running_child_ = (child_state == NodeStatus::RUNNING);
42  if (running_child_)
43  {
44  return NodeStatus::RUNNING;
45  }
46  else
47  {
48  haltChild();
49  }
50  }
51 
52  std::shared_ptr<ProtectedQueue<T>> queue;
53  if (getInput("queue", queue) && queue)
54  {
55  std::unique_lock<std::mutex> lk(queue->mtx);
56  auto& items = queue->items;
57 
58  while (!items.empty())
59  {
61 
62  T val = items.front();
63  items.pop_front();
64  setOutput("popped_item", val);
65 
66  lk.unlock();
67  NodeStatus child_state = child_node_->executeTick();
68  lk.lock();
69 
70  running_child_ = (child_state == NodeStatus::RUNNING);
71  if (running_child_)
72  {
73  return NodeStatus::RUNNING;
74  }
75  else
76  {
77  haltChild();
78  if (child_state == NodeStatus::FAILURE)
79  {
80  return NodeStatus::FAILURE;
81  }
82  }
83  }
84  }
85 
86  return NodeStatus::SUCCESS;
87  }
88 
90  {
91  return {InputPort<std::shared_ptr<ProtectedQueue<T>>>("queue"), OutputPort<T>("popped"
92  "_"
93  "ite"
94  "m")};
95  }
96 
97 private:
98  bool running_child_ = false;
99 };
100 
101 } // namespace BT
static PortsList providedPorts()
Definition: consume_queue.h:89
const NodeConfiguration & config() const
Definition: tree_node.cpp:127
Result setOutput(const std::string &key, const T &value)
Definition: tree_node.h:293
NodeStatus tick() override
Method to be implemented by the user.
Definition: consume_queue.h:36
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:101
TreeNode * child_node_
void haltChild()
Same as resetChild()
Result getInput(const std::string &key, T &destination) const
Definition: tree_node.h:232
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:333
ConsumeQueue(const std::string &name, const NodeConfiguration &config)
Definition: consume_queue.h:32
NodeStatus
Definition: basic_types.h:35
virtual BT::NodeStatus executeTick()
The method that should be used to invoke tick() and setStatus();.
Definition: tree_node.cpp:32
void setStatus(NodeStatus new_status)
Definition: tree_node.cpp:63


behaviortree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Mon Jul 3 2023 02:50:14