pop_from_queue.hpp
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>
16 #include <mutex>
19 
31 namespace BT
32 {
33 
34 template <typename T>
36 {
37  std::list<T> items;
39 };
40 
41 /*
42  * Few words about why we represent the queue as std::shared_ptr<ProtectedQueue>:
43  *
44  * Since we will pop from the queue, the fact that the blackboard uses
45  * a value semantic is not very convenient, since it would oblige us to
46  * copy the entire std::list from the BB and than copy again a new one with one less element.
47  *
48  * We avoid this using reference semantic (wrapping the object in a shared_ptr).
49  * Unfortunately, remember that this makes our access to the list not thread-safe!
50  * This is the reason why we add a mutex to be used when modyfying the ProtectedQueue::items
51  *
52  * */
53 
54 template <typename T>
56 {
57 public:
58  PopFromQueue(const std::string& name, const NodeConfig& config)
60  {}
61 
62  NodeStatus tick() override
63  {
64  std::shared_ptr<ProtectedQueue<T>> queue;
65  if(getInput("queue", queue) && queue)
66  {
67  std::unique_lock<std::mutex> lk(queue->mtx);
68  auto& items = queue->items;
69 
70  if(items.empty())
71  {
72  return NodeStatus::FAILURE;
73  }
74  else
75  {
76  T val = items.front();
77  items.pop_front();
78  setOutput("popped_item", val);
79  return NodeStatus::SUCCESS;
80  }
81  }
82  else
83  {
84  return NodeStatus::FAILURE;
85  }
86  }
87 
89  {
90  return { InputPort<std::shared_ptr<ProtectedQueue<T>>>("queue"), OutputPort<T>("poppe"
91  "d_"
92  "ite"
93  "m") };
94  }
95 };
96 
108 template <typename T>
109 class QueueSize : public SyncActionNode
110 {
111 public:
112  QueueSize(const std::string& name, const NodeConfig& config)
114  {}
115 
116  NodeStatus tick() override
117  {
118  std::shared_ptr<ProtectedQueue<T>> queue;
119  if(getInput("queue", queue) && queue)
120  {
121  std::unique_lock<std::mutex> lk(queue->mtx);
122  auto& items = queue->items;
123 
124  if(items.empty())
125  {
126  return NodeStatus::FAILURE;
127  }
128  else
129  {
130  setOutput("size", int(items.size()));
131  return NodeStatus::SUCCESS;
132  }
133  }
134  return NodeStatus::FAILURE;
135  }
136 
138  {
139  return { InputPort<std::shared_ptr<ProtectedQueue<T>>>("queue"),
140  OutputPort<int>("size") };
141  }
142 };
143 
144 } // namespace BT
BT::TreeNode::getInput
Result getInput(const std::string &key, T &destination) const
Definition: tree_node.h:547
BT
Definition: ex01_wrap_legacy.cpp:29
BT::PopFromQueue::PopFromQueue
PopFromQueue(const std::string &name, const NodeConfig &config)
Definition: pop_from_queue.hpp:58
BT::TreeNode::config
const NodeConfig & config() const
Definition: tree_node.cpp:351
mutex
static pthread_mutex_t mutex
Definition: minitrace.cpp:77
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:585
BT::NodeStatus::FAILURE
@ FAILURE
action_node.h
BT::QueueSize::tick
NodeStatus tick() override
Method to be implemented by the user.
Definition: pop_from_queue.hpp:116
BT::PopFromQueue
Definition: pop_from_queue.hpp:55
BT::PopFromQueue::tick
NodeStatus tick() override
Method to be implemented by the user.
Definition: pop_from_queue.hpp:62
BT::PopFromQueue::providedPorts
static PortsList providedPorts()
Definition: pop_from_queue.hpp:88
BT::TreeNode::setOutput
Result setOutput(const std::string &key, const T &value)
setOutput modifies the content of an Output port
Definition: tree_node.h:558
BT::TreeNode::name
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:302
BT::NodeStatus::SUCCESS
@ SUCCESS
BT::QueueSize::QueueSize
QueueSize(const std::string &name, const NodeConfig &config)
Definition: pop_from_queue.hpp:112
BT::ProtectedQueue::mtx
std::mutex mtx
Definition: pop_from_queue.hpp:38
BT::ProtectedQueue::items
std::list< T > items
Definition: pop_from_queue.hpp:37
BT::QueueSize
Definition: pop_from_queue.hpp:109
BT::NodeConfig
Definition: tree_node.h:73
decorator_node.h
BT::ProtectedQueue
Definition: pop_from_queue.hpp:35
BT::SyncActionNode
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn't require a...
Definition: action_node.h:52
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33
BT::QueueSize::providedPorts
static PortsList providedPorts()
Definition: pop_from_queue.hpp:137


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:17