BufferLocked.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Wed Jan 18 14:11:39 CET 2006 BufferLocked.hpp
3 
4  BufferLocked.hpp - description
5  -------------------
6  begin : Wed January 18 2006
7  copyright : (C) 2006 Peter Soetens
8  email : peter.soetens@mech.kuleuven.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
39 
40 
41 
42 #ifndef ORO_CORELIB_BUFFER_LOCKED_HPP
43 #define ORO_CORELIB_BUFFER_LOCKED_HPP
44 
45 #include "../os/Mutex.hpp"
46 #include "../os/MutexLock.hpp"
47 #include "BufferInterface.hpp"
48 #include <deque>
49 
50 namespace RTT
51 { namespace base {
52 
53 
60  template<class T>
62  :public BufferInterface<T>
63  {
64  public:
65  typedef typename BufferBase::Options Options;
69  typedef T value_t;
70 
75  BufferLocked( size_type size, const Options &options = Options() )
76  : cap(size), buf(), mcircular(options.circular()), initialized(false), droppedSamples(0)
77  {
78  }
79 
85  BufferLocked( size_type size, param_t initial_value, const Options &options = Options() )
86  : cap(size), buf(), mcircular(options.circular()), droppedSamples(0)
87  {
88  data_sample(initial_value);
89  }
90 
91  virtual bool data_sample( param_t sample, bool reset = true )
92  {
93  os::MutexLock locker(lock);
94  if (!initialized || reset) {
95  buf.resize(cap, sample);
96  buf.resize(0);
97  lastSample = sample;
98  initialized = true;
99  return true;
100  } else {
101  return initialized;
102  }
103  }
104 
105  virtual value_t data_sample() const
106  {
107  return lastSample;
108  }
109 
114 
115  bool Push( param_t item )
116  {
117  os::MutexLock locker(lock);
118  if ( cap == (size_type)buf.size() ) {
119  //buffer is full, we either overwrite a sample, or drop the given one
120  droppedSamples++;
121  if (!mcircular)
122  {
123  return false;
124  }
125  else
126  buf.pop_front();
127  }
128  buf.push_back( item );
129  return true;
130  }
131 
132  size_type Push(const std::vector<value_t>& items)
133  {
134  os::MutexLock locker(lock);
135  typename std::vector<value_t>::const_iterator itl( items.begin() );
136  if (mcircular && (size_type)items.size() >= cap ) {
137  // clear out current data and reset iterator to first element we're going to take.
138  buf.clear();
139  //note the ignored samples are added below to the dropped samples.
140  droppedSamples += cap;
141  itl = items.begin() + ( items.size() - cap );
142  } else if ( mcircular && (size_type)(buf.size() + items.size()) > cap) {
143  // drop excess elements from front
144  assert( (size_type)items.size() < cap );
145  while ( (size_type)(buf.size() + items.size()) > cap )
146  {
147  droppedSamples++;
148  buf.pop_front();
149  }
150  // itl still points at first element of items.
151  }
152  while ( ((size_type)buf.size() != cap) && (itl != items.end()) ) {
153  buf.push_back( *itl );
154  ++itl;
155  }
156  // this is in any case the number of elements taken from items.
157  size_type writtenSamples = itl - items.begin();
158 
159  if (mcircular)
160  assert( writtenSamples == (size_type)items.size() );
161 
162  droppedSamples += items.size() - writtenSamples;
163 
164  return writtenSamples;
165  }
166 
167  FlowStatus Pop( reference_t item )
168  {
169  os::MutexLock locker(lock);
170  if ( buf.empty() ) {
171  return NoData;
172  }
173  item = buf.front();
174  buf.pop_front();
175  return NewData;
176  }
177 
178  size_type Pop(std::vector<value_t>& items )
179  {
180  os::MutexLock locker(lock);
181  int quant = 0;
182  items.clear();
183  while ( !buf.empty() ) {
184  items.push_back( buf.front() );
185  buf.pop_front();
186  ++quant;
187  }
188  return quant;
189  }
190 
191  value_t* PopWithoutRelease()
192  {
193  os::MutexLock locker(lock);
194  if(buf.empty())
195  return 0;
196 
197  //note we need to copy the sample, as
198  //front is not garanteed to be valid after
199  //any other operation on the deque
200  lastSample = buf.front();
201  buf.pop_front();
202  return &lastSample;
203  }
204 
205  void Release(value_t *item)
206  {
207  //we do not need to release any memory, but we can check
208  //if the other side messed up
209  assert(item == &lastSample && "Wrong pointer given back to buffer");
210  }
211 
212  size_type capacity() const {
213  os::MutexLock locker(lock);
214  return cap;
215  }
216 
217  size_type size() const {
218  os::MutexLock locker(lock);
219  return buf.size();
220  }
221 
222  void clear() {
223  os::MutexLock locker(lock);
224  buf.clear();
225  }
226 
227  bool empty() const {
228  os::MutexLock locker(lock);
229  return buf.empty();
230  }
231 
232  bool full() const {
233  os::MutexLock locker(lock);
234  return (size_type)buf.size() == cap;
235  }
236 
237  size_type dropped() const
238  {
239  return droppedSamples;
240  }
241  private:
242  size_type cap;
243  std::deque<value_t> buf;
244  value_t lastSample;
245  mutable os::Mutex lock;
246  const bool mcircular;
248  size_type droppedSamples;
249  };
250 }}
251 
252 #endif // BUFFERSIMPLE_HPP
BufferLocked(size_type size, const Options &options=Options())
virtual bool data_sample(param_t sample, bool reset=true)
size_type Push(const std::vector< value_t > &items)
boost::call_traits< T >::reference reference_t
FlowStatus
Definition: FlowStatus.hpp:56
boost::call_traits< T >::param_type param_t
size_type dropped() const
BufferBase::Options Options
void Release(value_t *item)
bool Push(param_t item)
BufferBase::size_type size_type
std::deque< value_t > buf
BufferInterface< T >::size_type size_type
size_type Pop(std::vector< value_t > &items)
BufferInterface< T >::param_t param_t
BufferInterface< T >::reference_t reference_t
size_type size() const
virtual value_t data_sample() const
size_type capacity() const
BufferLocked(size_type size, param_t initial_value, const Options &options=Options())
An object oriented wrapper around a non recursive mutex.
Definition: Mutex.hpp:92
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
FlowStatus Pop(reference_t item)
MutexLock is a scope based Monitor, protecting critical sections with a Mutex object through locking ...
Definition: MutexLock.hpp:51


rtt
Author(s): RTT Developers
autogenerated on Fri Oct 25 2019 03:59:30