-
Notifications
You must be signed in to change notification settings - Fork 825
Expand file tree
/
Copy pathpop_from_queue.hpp
More file actions
135 lines (121 loc) · 4.46 KB
/
pop_from_queue.hpp
File metadata and controls
135 lines (121 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* Copyright (C) 2022-2025 Davide Faconti - All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* 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:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* 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,
* 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.
*/
#pragma once
#include <list>
#include <mutex>
#include "behaviortree_cpp/action_node.h"
#include "behaviortree_cpp/decorator_node.h"
/**
* Template Action used in ex04_waypoints.cpp example.
*
* Its purpose is to do make it easy to create while loops which consume the elements of a queue.
*
* Note that modifying the queue is not thread safe, therefore the action that creates the queue
* or push elements into it, must be Synchronous.
*
* When ticked, we pop_front from the "queue" and insert that value in "popped_item".
* Return FAILURE if the queue is empty, SUCCESS otherwise.
*/
namespace BT
{
template <typename T>
struct ProtectedQueue
{
std::list<T> items;
std::mutex mtx;
};
/*
* Few words about why we represent the queue as std::shared_ptr<ProtectedQueue>:
*
* Since we will pop from the queue, the fact that the blackboard uses
* a value semantic is not very convenient, since it would oblige us to
* copy the entire std::list from the BB and than copy again a new one with one less element.
*
* We avoid this using reference semantic (wrapping the object in a shared_ptr).
* Unfortunately, remember that this makes our access to the list not thread-safe!
* This is the reason why we add a mutex to be used when modifying the ProtectedQueue::items
*
* */
template <typename T>
class PopFromQueue : public SyncActionNode
{
public:
PopFromQueue(const std::string& name, const NodeConfig& config)
: SyncActionNode(name, config)
{}
NodeStatus tick() override
{
std::shared_ptr<ProtectedQueue<T>> queue;
if(getInput("queue", queue) && queue)
{
std::unique_lock<std::mutex> lk(queue->mtx);
auto& items = queue->items;
if(items.empty())
{
return NodeStatus::FAILURE;
}
T val = items.front();
items.pop_front();
setOutput("popped_item", val);
return NodeStatus::SUCCESS;
}
return NodeStatus::FAILURE;
}
static PortsList providedPorts()
{
return { InputPort<std::shared_ptr<ProtectedQueue<T>>>("queue"), OutputPort<T>("poppe"
"d_"
"ite"
"m") };
}
};
/**
* Get the size of a queue. Useful when you want to write something like:
*
* <QueueSize queue="{waypoints}" size="{wp_size}" />
* <Repeat num_cycles="{wp_size}" >
* <Sequence>
* <PopFromQueue queue="{waypoints}" popped_item="{wp}" >
* <UseWaypoint waypoint="{wp}" />
* </Sequence>
* </Repeat>
*/
template <typename T>
class QueueSize : public SyncActionNode
{
public:
QueueSize(const std::string& name, const NodeConfig& config)
: SyncActionNode(name, config)
{}
NodeStatus tick() override
{
std::shared_ptr<ProtectedQueue<T>> queue;
if(getInput("queue", queue) && queue)
{
std::unique_lock<std::mutex> lk(queue->mtx);
auto& items = queue->items;
if(items.empty())
{
return NodeStatus::FAILURE;
}
setOutput("size", int(items.size()));
return NodeStatus::SUCCESS;
}
return NodeStatus::FAILURE;
}
static PortsList providedPorts()
{
return { InputPort<std::shared_ptr<ProtectedQueue<T>>>("queue"),
OutputPort<int>("size") };
}
};
} // namespace BT