Orocos Real-Time Toolkit  2.6.0
ChannelBufferElement.hpp
00001 /***************************************************************************
00002   tag: Peter Soetens  Thu Oct 22 11:59:08 CEST 2009  ChannelBufferElement.hpp
00003 
00004                         ChannelBufferElement.hpp -  description
00005                            -------------------
00006     begin                : Thu October 22 2009
00007     copyright            : (C) 2009 Sylvain Joyeux
00008     email                : sylvain.joyeux@m4x.org
00009 
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU General Public                   *
00013  *   License as published by the Free Software Foundation;                 *
00014  *   version 2 of the License.                                             *
00015  *                                                                         *
00016  *   As a special exception, you may use this file as part of a free       *
00017  *   software library without restriction.  Specifically, if other files   *
00018  *   instantiate templates or use macros or inline functions from this     *
00019  *   file, or you compile this file and link it with other files to        *
00020  *   produce an executable, this file does not by itself cause the         *
00021  *   resulting executable to be covered by the GNU General Public          *
00022  *   License.  This exception does not however invalidate any other        *
00023  *   reasons why the executable file might be covered by the GNU General   *
00024  *   Public License.                                                       *
00025  *                                                                         *
00026  *   This library is distributed in the hope that it will be useful,       *
00027  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00028  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00029  *   Lesser General Public License for more details.                       *
00030  *                                                                         *
00031  *   You should have received a copy of the GNU General Public             *
00032  *   License along with this library; if not, write to the Free Software   *
00033  *   Foundation, Inc., 59 Temple Place,                                    *
00034  *   Suite 330, Boston, MA  02111-1307  USA                                *
00035  *                                                                         *
00036  ***************************************************************************/
00037 
00038 
00039 #ifndef ORO_CHANNEL_BUFFER_ELEMENT_HPP
00040 #define ORO_CHANNEL_BUFFER_ELEMENT_HPP
00041 
00042 #include "../base/ChannelElement.hpp"
00043 #include "../base/BufferInterface.hpp"
00044 
00045 namespace RTT { namespace internal {
00046 
00049     template<typename T>
00050     class ChannelBufferElement : public base::ChannelElement<T>
00051     {
00052         typename base::BufferInterface<T>::shared_ptr buffer;
00053         typename base::ChannelElement<T>::value_t *last_sample_p;
00054     public:
00055         typedef typename base::ChannelElement<T>::param_t param_t;
00056         typedef typename base::ChannelElement<T>::reference_t reference_t;
00057     typedef typename base::ChannelElement<T>::value_t value_t;
00058 
00059         ChannelBufferElement(typename base::BufferInterface<T>::shared_ptr buffer)
00060             : buffer(buffer), last_sample_p(0) {}
00061             
00062     virtual ~ChannelBufferElement()
00063     {
00064         if(last_sample_p)
00065         buffer->Release(last_sample_p);
00066     }
00067  
00072         virtual bool write(param_t sample)
00073         {
00074             if (buffer->Push(sample))
00075                 return this->signal();
00076             return true;
00077         }
00078 
00083         virtual FlowStatus read(reference_t sample, bool copy_old_data)
00084         {
00085         value_t *new_sample_p;
00086             if ( (new_sample_p = buffer->PopWithoutRelease()) ) {
00087         if(last_sample_p)
00088             buffer->Release(last_sample_p);
00089         
00090         last_sample_p = new_sample_p;
00091         sample = *new_sample_p;
00092                 return NewData;
00093             }
00094             if (last_sample_p) {
00095         if(copy_old_data)
00096             sample = *(last_sample_p);
00097                 return OldData;
00098             }
00099             return NoData;
00100         }
00101 
00106         virtual void clear()
00107         {
00108         if(last_sample_p)
00109         buffer->Release(last_sample_p);
00110         last_sample_p = 0;
00111             buffer->clear();
00112             base::ChannelElement<T>::clear();
00113         }
00114 
00115         virtual bool data_sample(param_t sample)
00116         {
00117             buffer->data_sample(sample);
00118             return base::ChannelElement<T>::data_sample(sample);
00119         }
00120 
00121         virtual T data_sample()
00122         {
00123             return buffer->data_sample();
00124         }
00125     };
00126 }}
00127 
00128 #endif
00129