BufferUnSync.hpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: Peter Soetens  Thu Oct 22 11:59:07 CEST 2009  BufferUnSync.hpp
00003 
00004                         BufferUnSync.hpp -  description
00005                            -------------------
00006     begin                : Thu October 22 2009
00007     copyright            : (C) 2009 Peter Soetens
00008     email                : peter@thesourcworks.com
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 
00040 #ifndef ORO_CORELIB_BUFFER_UNSYNC_HPP
00041 #define ORO_CORELIB_BUFFER_UNSYNC_HPP
00042 
00043 #include "BufferInterface.hpp"
00044 #include <deque>
00045 
00046 namespace RTT
00047 { namespace base {
00048 
00049 
00057     template<class T>
00058     class BufferUnSync
00059         :public BufferInterface<T>
00060     {
00061     public:
00062         typedef typename BufferInterface<T>::reference_t reference_t;
00063         typedef typename BufferInterface<T>::param_t param_t;
00064         typedef typename BufferInterface<T>::size_type size_type;
00065         typedef T value_t;
00066 
00070         BufferUnSync( size_type size, const T& initial_value = T(), bool circular = false )
00071             : cap(size), buf(), mcircular(circular)
00072         {
00073             data_sample(initial_value);
00074         }
00075 
00076         virtual void data_sample( const T& sample )
00077         {
00078             buf.resize(cap, sample);
00079             buf.resize(0);
00080         }
00081 
00082         virtual T data_sample() const
00083         {
00084             return lastSample;
00085         }
00086 
00090         ~BufferUnSync() {}
00091 
00092         bool Push( param_t item )
00093         {
00094             if (cap == (size_type)buf.size() ) {
00095                 if (!mcircular)
00096                     return false;
00097                 else
00098                     buf.pop_front();
00099             }
00100             buf.push_back( item );
00101             return true;
00102         }
00103 
00104         size_type Push(const std::vector<T>& items)
00105         {
00106             typename std::vector<T>::const_iterator itl( items.begin() );
00107             if (mcircular && (size_type)items.size() >= cap ) {
00108                 // clear out current data and reset iterator to first element we're going to take.
00109                 buf.clear();
00110                 itl = items.begin() + ( items.size() - cap );
00111             } else if ( mcircular && (size_type)(buf.size() + items.size()) > cap) {
00112                 // drop excess elements from front
00113                 assert( (size_type)items.size() < cap );
00114                 while ( (size_type)(buf.size() + items.size()) > cap )
00115                     buf.pop_front();
00116                 // itl still points at first element of items.
00117             }
00118             while ( ((size_type)buf.size() != cap) && (itl != items.end()) ) {
00119                 buf.push_back( *itl );
00120                 ++itl;
00121             }
00122             return (itl - items.begin());
00123         }
00124 
00125         bool Pop( reference_t item )
00126         {
00127             if ( buf.empty() ) {
00128                 return false;
00129             }
00130             item = buf.front();
00131             buf.pop_front();
00132             return true;
00133         }
00134 
00135         size_type Pop(std::vector<T>& items )
00136         {
00137             int quant = 0;
00138             items.clear();
00139             while ( !buf.empty() ) {
00140                 items.push_back( buf.front() );
00141                 buf.pop_front();
00142                 ++quant;
00143             }
00144             return quant;
00145         }
00146 
00147         value_t* PopWithoutRelease()
00148         {
00149             if(buf.empty())
00150                 return 0;
00151             
00152             //note we need to copy the sample, as 
00153             //front is not garanteed to be valid after
00154             //any other operation on the deque
00155             lastSample = buf.front();
00156             buf.pop_front();
00157             return &lastSample;
00158 
00159             return 0;
00160         }
00161         
00162         void Release(value_t *item)
00163         {
00164             //we do not need to release any memory, but we can check
00165             //if the other side messed up
00166             assert(item == &lastSample && "Wrong pointer given back to buffer");
00167         }
00168         
00169         size_type capacity() const {
00170             return cap;
00171         }
00172 
00173         size_type size() const {
00174             return buf.size();
00175         }
00176 
00177         void clear() {
00178             buf.clear();
00179         }
00180 
00181         bool empty() const {
00182             return buf.empty();
00183         }
00184 
00185         bool full() const {
00186             return (size_type)buf.size() ==  cap;
00187         }
00188     private:
00189         size_type cap;
00190         std::deque<T> buf;
00191         value_t lastSample;
00192         const bool mcircular;
00193     };
00194 }}
00195 
00196 #endif // BUFFERSIMPLE_HPP


rtt
Author(s): RTT Developers
autogenerated on Mon Oct 6 2014 03:13:33