Orocos Real-Time Toolkit  2.8.3
BufferUnSync.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Thu Oct 22 11:59:07 CEST 2009 BufferUnSync.hpp
3 
4  BufferUnSync.hpp - description
5  -------------------
6  begin : Thu October 22 2009
7  copyright : (C) 2009 Peter Soetens
8  email : peter@thesourcworks.com
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 #ifndef ORO_CORELIB_BUFFER_UNSYNC_HPP
41 #define ORO_CORELIB_BUFFER_UNSYNC_HPP
42 
43 #include "BufferInterface.hpp"
44 #include <deque>
45 
46 namespace RTT
47 { namespace base {
48 
49 
57  template<class T>
59  :public BufferInterface<T>
60  {
61  public:
65  typedef T value_t;
66 
70  BufferUnSync( size_type size, const T& initial_value = T(), bool circular = false )
71  : cap(size), buf(), mcircular(circular), droppedSamples(0)
72  {
73  data_sample(initial_value);
74  }
75 
76  virtual void data_sample( const T& sample )
77  {
78  buf.resize(cap, sample);
79  buf.resize(0);
80  }
81 
82  virtual T data_sample() const
83  {
84  return lastSample;
85  }
86 
91 
92  bool Push( param_t item )
93  {
94  if (cap == (size_type)buf.size() ) {
95  //buffer is full, we either overwrite a sample, or drop the given one
96  droppedSamples++;
97  if (!mcircular)
98  {
99  return false;
100  }
101  else
102  {
103  buf.pop_front();
104  }
105  }
106  buf.push_back( item );
107  return true;
108  }
109 
110  size_type Push(const std::vector<T>& items)
111  {
112  typename std::vector<T>::const_iterator itl( items.begin() );
113  if (mcircular && (size_type)items.size() >= cap ) {
114  // clear out current data and reset iterator to first element we're going to take.
115  buf.clear();
116  //note the ignored samples are added below to the dropped samples.
117  droppedSamples += cap;
118  itl = items.begin() + ( items.size() - cap );
119  } else if ( mcircular && (size_type)(buf.size() + items.size()) > cap) {
120  // drop excess elements from front
121  assert( (size_type)items.size() < cap );
122  while ( (size_type)(buf.size() + items.size()) > cap )
123  {
124  droppedSamples++;
125  buf.pop_front();
126  }
127  // itl still points at first element of items.
128  }
129  while ( ((size_type)buf.size() != cap) && (itl != items.end()) ) {
130  buf.push_back( *itl );
131  ++itl;
132  }
133 
134  size_type written = (itl - items.begin());
135 
136  droppedSamples += items.size() - written;
137 
138  return written;
139  }
140 
141  bool Pop( reference_t item )
142  {
143  if ( buf.empty() ) {
144  return false;
145  }
146  item = buf.front();
147  buf.pop_front();
148  return true;
149  }
150 
151  size_type Pop(std::vector<T>& items )
152  {
153  int quant = 0;
154  items.clear();
155  while ( !buf.empty() ) {
156  items.push_back( buf.front() );
157  buf.pop_front();
158  ++quant;
159  }
160  return quant;
161  }
162 
163  value_t* PopWithoutRelease()
164  {
165  if(buf.empty())
166  return 0;
167 
168  //note we need to copy the sample, as
169  //front is not garanteed to be valid after
170  //any other operation on the deque
171  lastSample = buf.front();
172  buf.pop_front();
173  return &lastSample;
174 
175  return 0;
176  }
177 
178  void Release(value_t *item)
179  {
180  //we do not need to release any memory, but we can check
181  //if the other side messed up
182  assert(item == &lastSample && "Wrong pointer given back to buffer");
183  }
184 
185  size_type capacity() const {
186  return cap;
187  }
188 
189  size_type size() const {
190  return buf.size();
191  }
192 
193  void clear() {
194  buf.clear();
195  }
196 
197  bool empty() const {
198  return buf.empty();
199  }
200 
201  bool full() const {
202  return (size_type)buf.size() == cap;
203  }
204 
205  virtual size_type dropped() const
206  {
207  return droppedSamples;
208  }
209  private:
210  size_type cap;
211  std::deque<T> buf;
212  value_t lastSample;
213  const bool mcircular;
214  size_type droppedSamples;
215  };
216 }}
217 
218 #endif // BUFFERSIMPLE_HPP
value_t * PopWithoutRelease()
Returns a pointer to the first element in the buffer.
size_type size() const
Returns the actual number of items that are stored in the buffer.
BufferInterface< T >::size_type size_type
virtual T data_sample() const
Reads back a data sample.
bool empty() const
Check if this buffer is empty.
BufferInterface< T >::param_t param_t
boost::call_traits< T >::reference reference_t
A Buffer is an object which is used to store (Push) and retrieve (Pop) values from.
size_type capacity() const
Returns the maximum number of items that can be stored in the buffer.
boost::call_traits< T >::param_type param_t
BufferUnSync(size_type size, const T &initial_value=T(), bool circular=false)
Create a buffer of size size.
BufferInterface< T >::reference_t reference_t
~BufferUnSync()
Destructor.
Implements a not threadsafe buffer.
BufferBase::size_type size_type
virtual void data_sample(const T &sample)
Initializes this buffer with a data sample, such that for dynamical allocated types T...
size_type Push(const std::vector< T > &items)
Write a sequence of values to the buffer.
bool Push(param_t item)
Write a single value to the buffer.
bool full() const
Check if this buffer is full.
void clear()
Clears all contents of this buffer.
size_type Pop(std::vector< T > &items)
Read the whole buffer.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
bool Pop(reference_t item)
Read the oldest value from the buffer.
virtual size_type dropped() const
Returns the number of dropped samples, because the buffer was full.
void Release(value_t *item)
Releases the pointer.