BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
consume_queue.h
1/* Copyright (C) 2022-2025 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 "behaviortree_cpp/actions/pop_from_queue.hpp"
16#include "behaviortree_cpp/decorator_node.h"
17
18#include <list>
19
20namespace BT
21{
22/**
23 * Execute the child node as long as the queue is not empty.
24 * At each iteration, an item of type T is popped from the "queue" and
25 * inserted in "popped_item".
26 *
27 * An empty queue will return SUCCESS
28 */
29
30template <typename T>
31class [[deprecated("You are encouraged to use the LoopNode instead")]] ConsumeQueue
32 : public DecoratorNode
33{
34public:
35 ConsumeQueue(const std::string& name, const NodeConfig& config)
36 : DecoratorNode(name, config)
37 {}
38
39 NodeStatus tick() override
40 {
41 // by default, return SUCCESS, even if queue is empty
42 NodeStatus status_to_be_returned = NodeStatus::SUCCESS;
43
44 if(running_child_)
45 {
46 NodeStatus child_state = child_node_->executeTick();
47 running_child_ = (child_state == NodeStatus::RUNNING);
48 if(running_child_)
49 {
50 return NodeStatus::RUNNING;
51 }
52 else
53 {
55 status_to_be_returned = child_state;
56 }
57 }
58
59 std::shared_ptr<ProtectedQueue<T>> queue;
60 if(getInput("queue", queue) && queue)
61 {
62 std::unique_lock<std::mutex> lk(queue->mtx);
63 auto& items = queue->items;
64
65 while(!items.empty())
66 {
67 setStatus(NodeStatus::RUNNING);
68
69 T val = items.front();
70 items.pop_front();
71 setOutput("popped_item", val);
72
73 lk.unlock();
74 NodeStatus child_state = child_node_->executeTick();
75 lk.lock();
76
77 running_child_ = (child_state == NodeStatus::RUNNING);
78 if(running_child_)
79 {
80 return NodeStatus::RUNNING;
81 }
82 else
83 {
85 if(child_state == NodeStatus::FAILURE)
86 {
87 return NodeStatus::FAILURE;
88 }
89 status_to_be_returned = child_state;
90 }
91 }
92 }
93
94 return status_to_be_returned;
95 }
96
97 static PortsList providedPorts()
98 {
99 return { InputPort<std::shared_ptr<ProtectedQueue<T>>>("queue"), OutputPort<T>("poppe"
100 "d_"
101 "ite"
102 "m") };
103 }
104
105private:
106 bool running_child_ = false;
107};
108
109} // namespace BT
Definition: consume_queue.h:33
NodeStatus tick() override
Method to be implemented by the user.
Definition: consume_queue.h:39
The DecoratorNode is the base class for nodes that have exactly one child.
Definition: decorator_node.h:19
void haltChild()
Same as resetChild()
virtual BT::NodeStatus executeTick()
The method that should be used to invoke tick() and setStatus();.
void setStatus(NodeStatus new_status)
setStatus changes the status of the node. it will throw if you try to change the status to IDLE,...
Definition: action_node.h:24
NodeStatus
Definition: basic_types.h:34
Definition: tree_node.h:105
Definition: pop_from_queue.hpp:37