Orocos Real-Time Toolkit  2.9.0
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 #include "../ConnPolicy.hpp"
45 
46 namespace RTT { namespace internal {
47 
49  {
50  public:
52  virtual size_t getBufferSize() const = 0;
53  virtual size_t getBufferFillSize() const = 0;
54  virtual size_t getNumDroppedSamples() const = 0;
55  };
56 
59  template<typename T>
61  {
63  typename base::ChannelElement<T>::value_t *last_sample_p;
64  const ConnPolicy policy;
65 
66  public:
70 
72  : buffer(buffer), last_sample_p(0), policy(policy) {}
73 
75  {
76  if(last_sample_p)
77  buffer->Release(last_sample_p);
78  }
79 
80  virtual size_t getBufferSize() const
81  {
82  return buffer->capacity();
83  }
84 
85  virtual size_t getBufferFillSize() const
86  {
87  return buffer->size();
88  }
89 
90  virtual size_t getNumDroppedSamples() const
91  {
92  return buffer->dropped();
93  }
94 
99  virtual WriteStatus write(param_t sample)
100  {
101  if (!buffer->Push(sample)) return WriteFailure;
102  return this->signal() ? WriteSuccess : NotConnected;
103  }
104 
109  virtual FlowStatus read(reference_t sample, bool copy_old_data)
110  {
111  value_t *new_sample_p;
112  if ( (new_sample_p = buffer->PopWithoutRelease()) ) {
113  if(last_sample_p)
114  buffer->Release(last_sample_p);
115 
116  sample = *new_sample_p;
117 
118  // In the PerOutputPort or Shared buffer policy case this buffer element may be read by multiple readers.
119  // ==> We cannot store the last_sample and release immediately.
120  // ==> WriteShared buffer connections will never return OldData.
121  if (policy.buffer_policy != PerOutputPort && policy.buffer_policy != Shared)
122  last_sample_p = new_sample_p;
123  else
124  buffer->Release(new_sample_p);
125 
126  return NewData;
127  }
128  if (last_sample_p) {
129  if(copy_old_data)
130  sample = *(last_sample_p);
131  return OldData;
132  }
133  return NoData;
134  }
135 
140  virtual void clear()
141  {
142  if(last_sample_p)
143  buffer->Release(last_sample_p);
144  last_sample_p = 0;
145  buffer->clear();
147  }
148 
149  virtual WriteStatus data_sample(param_t sample, bool reset = true)
150  {
151  if (!buffer->data_sample(sample, reset)) return WriteFailure;
152  return base::ChannelElement<T>::data_sample(sample, reset);
153  }
154 
155  virtual value_t data_sample()
156  {
157  return buffer->data_sample();
158  }
159 
162  virtual const ConnPolicy* getConnPolicy() const
163  {
164  return &policy;
165  }
166 
167  virtual std::string getElementName() const
168  {
169  return "ChannelBufferElement";
170  }
171  };
172 }}
173 
174 #endif
175 
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 operation.
Definition: FlowStatus.hpp:56
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 bool data_sample(param_t sample, bool reset=true)=0
Initializes this buffer with a data sample, such that for dynamical allocated types T...
virtual const ConnPolicy * getConnPolicy() const
Returns a pointer to the ConnPolicy that has been used to construct the underlying buffer...
virtual void Release(value_t *item)=0
Releases the pointer.
virtual value_t * PopWithoutRelease()=0
Returns a pointer to the first element in the buffer.
A connection policy object describes how a given connection should behave.
Definition: ConnPolicy.hpp:107
virtual WriteStatus write(param_t sample)
Appends a sample at the end of the FIFO.
virtual size_t getBufferFillSize() const =0
virtual void clear()=0
Clears all contents of this buffer.
virtual size_t getNumDroppedSamples() const =0
A typed version of ChannelElementBase.
virtual size_t getNumDroppedSamples() const
base::ChannelElement< T >::value_t value_t
virtual WriteStatus data_sample(param_t sample, bool reset=true)
Provides a data sample to initialize this connection.
boost::call_traits< T >::reference reference_t
virtual value_t data_sample()
base::ChannelElement< T >::reference_t reference_t
virtual std::string getElementName() const
Returns the class name of this element.
int buffer_policy
The policy on how buffer elements will be installed for this connection, which influences the behavio...
Definition: ConnPolicy.hpp:216
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:52
ChannelBufferElement(typename base::BufferInterface< T >::shared_ptr buffer, const ConnPolicy &policy=ConnPolicy())
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.
WriteStatus
Returns the status of a data flow write operation.
Definition: FlowStatus.hpp:66
boost::shared_ptr< BufferInterface< T > > shared_ptr