Orocos Real-Time Toolkit  2.8.3
ChannelBufferElement.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Thu Oct 22 11:59:08 CEST 2009 ChannelBufferElement.hpp
3 
4  ChannelBufferElement.hpp - description
5  -------------------
6  begin : Thu October 22 2009
7  copyright : (C) 2009 Sylvain Joyeux
8  email : sylvain.joyeux@m4x.org
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 #ifndef ORO_CHANNEL_BUFFER_ELEMENT_HPP
40 #define ORO_CHANNEL_BUFFER_ELEMENT_HPP
41 
42 #include "../base/ChannelElement.hpp"
43 #include "../base/BufferInterface.hpp"
44 
45 namespace RTT { namespace internal {
46 
48  {
49  public:
51  virtual size_t getBufferSize() const = 0;
52  virtual size_t getBufferFillSize() const = 0;
53  virtual size_t getNumDroppedSamples() const = 0;
54  };
55 
58  template<typename T>
60  {
62  typename base::ChannelElement<T>::value_t *last_sample_p;
63  public:
67 
69  : buffer(buffer), last_sample_p(0) {}
70 
72  {
73  if(last_sample_p)
74  buffer->Release(last_sample_p);
75  }
76 
77  virtual size_t getBufferSize() const
78  {
79  return buffer->capacity();
80  }
81 
82  virtual size_t getBufferFillSize() const
83  {
84  return buffer->size();
85  }
86 
87  virtual size_t getNumDroppedSamples() const
88  {
89  return buffer->dropped();
90  }
91 
96  virtual bool write(param_t sample)
97  {
98  if (buffer->Push(sample))
99  return this->signal();
100  return true;
101  }
102 
107  virtual FlowStatus read(reference_t sample, bool copy_old_data)
108  {
109  value_t *new_sample_p;
110  if ( (new_sample_p = buffer->PopWithoutRelease()) ) {
111  if(last_sample_p)
112  buffer->Release(last_sample_p);
113 
114  last_sample_p = new_sample_p;
115  sample = *new_sample_p;
116  return NewData;
117  }
118  if (last_sample_p) {
119  if(copy_old_data)
120  sample = *(last_sample_p);
121  return OldData;
122  }
123  return NoData;
124  }
125 
130  virtual void clear()
131  {
132  if(last_sample_p)
133  buffer->Release(last_sample_p);
134  last_sample_p = 0;
135  buffer->clear();
137  }
138 
139  virtual bool data_sample(param_t sample)
140  {
141  buffer->data_sample(sample);
143  }
144 
145  virtual T data_sample()
146  {
147  return buffer->data_sample();
148  }
149 
150  virtual std::string getElementName() const
151  {
152  return "ChannelBufferElement";
153  }
154  };
155 }}
156 
157 #endif
158 
virtual size_t getBufferSize() const =0
boost::call_traits< T >::param_type param_t
virtual void clear()
Removes all elements in the FIFO.
virtual void clear()
Clears any data stored by the channel.
virtual size_type capacity() const =0
Returns the maximum number of items that can be stored in the buffer.
virtual FlowStatus read(reference_t sample, bool copy_old_data)
Pops and returns the first element of the FIFO.
FlowStatus
Returns the status of a data flow read.
Definition: FlowStatus.hpp:54
virtual bool Push(param_t item)=0
Write a single value to the buffer.
A connection element that can store a fixed number of data samples.
virtual void Release(value_t *item)=0
Releases the pointer.
virtual bool data_sample(param_t sample)
Provides a data sample to initialize this connection.
virtual value_t * PopWithoutRelease()=0
Returns a pointer to the first element in the buffer.
ChannelBufferElement(typename base::BufferInterface< T >::shared_ptr buffer)
virtual size_t getBufferFillSize() const =0
virtual void clear()=0
Clears all contents of this buffer.
virtual void data_sample(const T &sample)=0
Initializes this buffer with a data sample, such that for dynamical allocated types T...
virtual size_t getNumDroppedSamples() const =0
A typed version of ChannelElementBase.
virtual size_t getNumDroppedSamples() const
base::ChannelElement< T >::value_t value_t
boost::call_traits< T >::reference reference_t
virtual bool write(param_t sample)
Appends a sample at the end of the FIFO.
virtual value_t data_sample()
base::ChannelElement< T >::reference_t reference_t
virtual std::string getElementName() const
Returns the class name of this element.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
base::ChannelElement< T >::param_t param_t
virtual size_type dropped() const =0
Returns the number of dropped samples, because the buffer was full.
virtual size_type size() const =0
Returns the actual number of items that are stored in the buffer.
boost::shared_ptr< BufferInterface< T > > shared_ptr