fifo_buffer.h
Go to the documentation of this file.
1 #include "sick_scan/sick_scan_base.h" /* Base definitions included in all header files, added by add_sick_scan_base_header.py. Do not edit this line. */
2 /*
3  * @brief sim_loc_fifo implements a threadsafe fifo-buffer ("first in, first out").
4  *
5  * Copyright (C) 2019 Ing.-Buero Dr. Michael Lehning, Hildesheim
6  * Copyright (C) 2019 SICK AG, Waldkirch
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * All rights reserved.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions are met:
24  *
25  * * Redistributions of source code must retain the above copyright
26  * notice, this list of conditions and the following disclaimer.
27  * * Redistributions in binary form must reproduce the above copyright
28  * notice, this list of conditions and the following disclaimer in the
29  * documentation and/or other materials provided with the distribution.
30  * * Neither the name of SICK AG nor the names of its
31  * contributors may be used to endorse or promote products derived from
32  * this software without specific prior written permission
33  * * Neither the name of Ing.-Buero Dr. Michael Lehning nor the names of its
34  * contributors may be used to endorse or promote products derived from
35  * this software without specific prior written permission
36  *
37  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
41  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47  * POSSIBILITY OF SUCH DAMAGE.
48  *
49  * Authors:
50  * Michael Lehning <michael.lehning@lehning.de>
51  *
52  * Copyright 2019 SICK AG
53  * Copyright 2019 Ing.-Buero Dr. Michael Lehning
54  *
55  */
56 #ifndef __SIM_LOC_FIFO_H_INCLUDED
57 #define __SIM_LOC_FIFO_H_INCLUDED
58 
59 #include <thread>
60 #include <list>
61 
62 namespace sick_scan_xd
63 {
67  template<typename ElementType, typename MutexType = std::mutex> class FifoBuffer
68  {
69  public:
70 
75 
80  {
81  notify(); // interrupt a possible wait in waitForNotify()
82  }
83 
87  bool empty(void)
88  {
89  std::lock_guard<MutexType> message_lockguard(m_fifo_mutex);
90  return m_fifo_buffer.empty();
91  }
92 
96  size_t size(void)
97  {
98  std::lock_guard<MutexType> message_lockguard(m_fifo_mutex);
99  return m_fifo_buffer.size();
100  }
101 
105  void push(const ElementType & elem)
106  {
107  push_back(elem);
108  notifyAll();
109  }
110 
115  ElementType pop(void)
116  {
117  std::lock_guard<MutexType> message_lockguard(m_fifo_mutex);
118  if(!m_fifo_buffer.empty())
119  {
120  ElementType elem(m_fifo_buffer.front());
121  m_fifo_buffer.pop_front();
122  return elem;
123  }
124  return ElementType();
125  }
126 
131  {
132  while (ROS::ok() && empty())
133  {
134  waitForNotify();
135  }
136  }
137 
142  {
143  if (ROS::ok() && empty())
144  {
145  waitForNotify();
146  }
147  }
148 
152  void notify(void)
153  {
154  notifyAll();
155  }
156 
161  {
162  public:
164  virtual bool condition(const ElementType & element) = 0;
165  };
166 
174  ElementType findFirstIf(UnaryConditionIf & condition_impl, bool erase_if_found = false)
175  {
176  std::lock_guard<MutexType> message_lockguard(m_fifo_mutex);
177  for(auto iter = m_fifo_buffer.begin(); iter != m_fifo_buffer.end(); iter++)
178  {
179  if(condition_impl.condition(*iter))
180  {
181  ElementType elem = *iter;
182  if(erase_if_found)
183  {
184  m_fifo_buffer.erase(iter);
185  }
186  return elem;
187  }
188  }
189  return ElementType();
190  }
191 
192  protected:
193 
195  void push_back(const ElementType & elem)
196  {
197  std::lock_guard<MutexType> message_lockguard(m_fifo_mutex);
198  m_fifo_buffer.push_back(elem);
199  }
200 
202  void notifyAll(void)
203  {
204  m_buffer_condition.notify_all();
205  }
206 
208  void waitForNotify(void)
209  {
210  std::unique_lock<std::mutex> lock(m_condition_mutex);
211  m_buffer_condition.wait(lock);
212  }
213 
214  /*
215  * member data
216  */
217 
218  std::list<ElementType> m_fifo_buffer;
219  MutexType m_fifo_mutex;
220  std::mutex m_condition_mutex;
221  std::condition_variable m_buffer_condition;
222 
223  }; // class FifoBuffer
224 
225 } // namespace sick_scan_xd
226 #endif // __SIM_LOC_FIFO_H_INCLUDED
sick_scan_xd::FifoBuffer::m_fifo_buffer
std::list< ElementType > m_fifo_buffer
list of all elements of the fifo buffer
Definition: fifo_buffer.h:218
sick_scan_xd::FifoBuffer::push_back
void push_back(const ElementType &elem)
Definition: fifo_buffer.h:195
sick_scan_xd::FifoBuffer::FifoBuffer
FifoBuffer()
Definition: fifo_buffer.h:74
sick_scan_xd::FifoBuffer::m_buffer_condition
std::condition_variable m_buffer_condition
condition variable to signal changes in buffer size
Definition: fifo_buffer.h:221
sick_scan_xd::FifoBuffer::m_condition_mutex
std::mutex m_condition_mutex
mutex to lock m_buffer_condition
Definition: fifo_buffer.h:220
sick_scan_xd::FifoBuffer::size
size_t size(void)
Definition: fifo_buffer.h:96
sick_scan_xd
Definition: abstract_parser.cpp:65
sick_scan_xd::FifoBuffer
Definition: fifo_buffer.h:67
sick_scan_xd::FifoBuffer::waitForElement
void waitForElement()
Definition: fifo_buffer.h:130
sick_scan_xd::FifoBuffer::UnaryConditionIf::condition
virtual bool condition(const ElementType &element)=0
sick_scan_xd::FifoBuffer::findFirstIf
ElementType findFirstIf(UnaryConditionIf &condition_impl, bool erase_if_found=false)
Definition: fifo_buffer.h:174
sick_scan_xd::FifoBuffer::UnaryConditionIf
Definition: fifo_buffer.h:160
sick_scan_xd::FifoBuffer::pop
ElementType pop(void)
Definition: fifo_buffer.h:115
sick_scan_xd::FifoBuffer::waitOnceForElement
void waitOnceForElement()
Definition: fifo_buffer.h:141
sick_scan_xd::FifoBuffer::empty
bool empty(void)
Definition: fifo_buffer.h:87
sick_scan_xd::FifoBuffer::push
void push(const ElementType &elem)
Definition: fifo_buffer.h:105
sick_scan_xd::FifoBuffer::~FifoBuffer
~FifoBuffer()
Definition: fifo_buffer.h:79
sick_scan_base.h
roswrap::ok
ROSCPP_DECL bool ok()
Check whether it's time to exit.
Definition: rossimu.cpp:273
sick_scan_xd::FifoBuffer::waitForNotify
void waitForNotify(void)
Definition: fifo_buffer.h:208
sick_scan_xd::FifoBuffer::notifyAll
void notifyAll(void)
Definition: fifo_buffer.h:202
sick_scan_xd::FifoBuffer::m_fifo_mutex
MutexType m_fifo_mutex
mutex to lock m_fifo_buffer
Definition: fifo_buffer.h:219
sick_scan_xd::FifoBuffer::notify
void notify(void)
Definition: fifo_buffer.h:152


sick_scan_xd
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Fri Oct 25 2024 02:47:08