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 
20 
32 namespace BT
33 {
34 
35 template <typename T>
37 {
38  std::list<T> items;
40 };
41 
42 /*
43  * Few words about why we represent the queue as std::shared_ptr<ProtectedQueue>:
44  *
45  * Since we will pop from the queue, the fact that the blackboard uses
46  * a value semantic is not very convenient, since it would oblige us to
47  * copy the entire std::list from the BB and than copy again a new one with one less element.
48  *
49  * We avoid this using reference semantic (wrapping the object in a shared_ptr).
50  * Unfortunately, remember that this makes our access to the list not thread-safe!
51  * This is the reason why we add a mutex to be used when modyfying the ProtectedQueue::items
52  *
53  * */
54 
55 
56 template <typename T>
58 {
59  public:
60  PopFromQueue(const std::string& name, const NodeConfiguration& config)
61  : SyncActionNode(name, config)
62  {
63  }
64 
65  NodeStatus tick() override
66  {
67  std::shared_ptr<ProtectedQueue<T>> queue;
68  if( getInput("queue", queue) && queue )
69  {
70  std::unique_lock<std::mutex> lk(queue->mtx);
71  auto& items = queue->items;
72 
73  if( items.empty() )
74  {
75  return NodeStatus::FAILURE;
76  }
77  else{
78  T val = items.front();
79  items.pop_front();
80  setOutput("popped_item", val);
81  return NodeStatus::SUCCESS;
82  }
83  }
84  else{
85  return NodeStatus::FAILURE;
86  }
87  }
88 
90  {
91  return { InputPort<std::shared_ptr<ProtectedQueue<T>>>("queue"),
92  OutputPort<T>("popped_item")};
93  }
94 };
95 
107 template <typename T>
108 class QueueSize : public SyncActionNode
109 {
110  public:
111  QueueSize(const std::string& name, const NodeConfiguration& config)
112  : SyncActionNode(name, config)
113  {
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  setOutput("size", int(items.size()) );
130  return NodeStatus::SUCCESS;
131  }
132  }
133  return NodeStatus::FAILURE;
134  }
135 
137  {
138  return { InputPort<std::shared_ptr<ProtectedQueue<T>>>("queue"),
139  OutputPort<int>("size")};
140  }
141 };
142 
143 
144 }
145 
static pthread_mutex_t mutex
Definition: minitrace.cpp:61
QueueSize(const std::string &name, const NodeConfiguration &config)
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn&#39;t require a...
Definition: action_node.h:52
PopFromQueue(const std::string &name, const NodeConfiguration &config)
NodeStatus tick() override
Method to be implemented by the user.
static PortsList providedPorts()
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:333
std::list< T > items
NodeStatus tick() override
Method to be implemented by the user.
NodeStatus
Definition: basic_types.h:35
static PortsList providedPorts()


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