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


sick_scan
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Wed May 5 2021 03:05:48