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 {
29 template <typename T>
30 class [[deprecated("You are encouraged to use the LoopNode instead")]] ConsumeQueue
31  : public DecoratorNode
32 {
33 public:
34  ConsumeQueue(const std::string& name, const NodeConfig& config)
35  : DecoratorNode(name, config)
36  {}
37 
38  NodeStatus tick() override
39  {
40  // by default, return SUCCESS, even if queue is empty
41  NodeStatus status_to_be_returned = NodeStatus::SUCCESS;
42 
43  if(running_child_)
44  {
45  NodeStatus child_state = child_node_->executeTick();
46  running_child_ = (child_state == NodeStatus::RUNNING);
47  if(running_child_)
48  {
49  return NodeStatus::RUNNING;
50  }
51  else
52  {
53  haltChild();
54  status_to_be_returned = child_state;
55  }
56  }
57 
58  std::shared_ptr<ProtectedQueue<T>> queue;
59  if(getInput("queue", queue) && queue)
60  {
61  std::unique_lock<std::mutex> lk(queue->mtx);
62  auto& items = queue->items;
63 
64  while(!items.empty())
65  {
66  setStatus(NodeStatus::RUNNING);
67 
68  T val = items.front();
69  items.pop_front();
70  setOutput("popped_item", val);
71 
72  lk.unlock();
73  NodeStatus child_state = child_node_->executeTick();
74  lk.lock();
75 
76  running_child_ = (child_state == NodeStatus::RUNNING);
77  if(running_child_)
78  {
79  return NodeStatus::RUNNING;
80  }
81  else
82  {
83  haltChild();
84  if(child_state == NodeStatus::FAILURE)
85  {
86  return NodeStatus::FAILURE;
87  }
88  status_to_be_returned = child_state;
89  }
90  }
91  }
92 
93  return status_to_be_returned;
94  }
95 
97  {
98  return { InputPort<std::shared_ptr<ProtectedQueue<T>>>("queue"), OutputPort<T>("poppe"
99  "d_"
100  "ite"
101  "m") };
102  }
103 
104 private:
105  bool running_child_ = false;
106 };
107 
108 } // namespace BT
BT
Definition: ex01_wrap_legacy.cpp:29
BT::LoopNode
The LoopNode class is used to pop_front elements from a std::deque. This element is copied into the p...
Definition: loop_node.h:37
BT::ConsumeQueue
Definition: consume_queue.h:30
BT::DecoratorNode
Definition: decorator_node.h:8
BT::ConsumeQueue::ConsumeQueue
ConsumeQueue(const std::string &name, const NodeConfig &config)
Definition: consume_queue.h:34
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:585
BT::NodeStatus::FAILURE
@ FAILURE
BT::ConsumeQueue::tick
NodeStatus tick() override
Method to be implemented by the user.
Definition: consume_queue.h:38
BT::NodeStatus::SUCCESS
@ SUCCESS
BT::NodeStatus::RUNNING
@ RUNNING
BT::ConsumeQueue::providedPorts
static PortsList providedPorts()
Definition: consume_queue.h:96
pop_from_queue.hpp
BT::NodeConfig
Definition: tree_node.h:73
decorator_node.h
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:07