BufferLocked.hpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: Peter Soetens  Wed Jan 18 14:11:39 CET 2006  BufferLocked.hpp
00003 
00004                         BufferLocked.hpp -  description
00005                            -------------------
00006     begin                : Wed January 18 2006
00007     copyright            : (C) 2006 Peter Soetens
00008     email                : peter.soetens@mech.kuleuven.be
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 
00041 
00042 #ifndef ORO_CORELIB_BUFFER_LOCKED_HPP
00043 #define ORO_CORELIB_BUFFER_LOCKED_HPP
00044 
00045 #include "../os/Mutex.hpp"
00046 #include "../os/MutexLock.hpp"
00047 #include "BufferInterface.hpp"
00048 #include <deque>
00049 
00050 namespace RTT
00051 { namespace base {
00052 
00053 
00060     template<class T>
00061     class BufferLocked
00062         :public BufferInterface<T>
00063     {
00064     public:
00065 
00066         typedef typename BufferInterface<T>::reference_t reference_t;
00067         typedef typename BufferInterface<T>::param_t param_t;
00068         typedef typename BufferInterface<T>::size_type size_type;
00069         typedef T value_t;
00070 
00077         BufferLocked( size_type size, const T& initial_value = T(), bool circular = false )
00078             : cap(size), buf(), mcircular(circular)
00079         {
00080             data_sample(initial_value);
00081         }
00082 
00083         virtual void data_sample( const T& sample )
00084         {
00085             buf.resize(cap, sample);
00086             buf.resize(0);
00087             lastSample = sample;
00088         }
00089 
00090         virtual T data_sample() const
00091         {
00092             return lastSample;
00093         }
00094 
00098         ~BufferLocked() {}
00099 
00100         bool Push( param_t item )
00101         {
00102             os::MutexLock locker(lock);
00103             if ( cap == (size_type)buf.size() ) {
00104                 if (!mcircular)
00105                     return false;
00106                 else
00107                     buf.pop_front();
00108             }
00109             buf.push_back( item );
00110             return true;
00111         }
00112 
00113         size_type Push(const std::vector<T>& items)
00114         {
00115             os::MutexLock locker(lock);
00116             typename std::vector<T>::const_iterator itl( items.begin() );
00117             if (mcircular && (size_type)items.size() >= cap ) {
00118                 // clear out current data and reset iterator to first element we're going to take.
00119                 buf.clear();
00120                 itl = items.begin() + ( items.size() - cap );
00121             } else if ( mcircular && (size_type)(buf.size() + items.size()) > cap) {
00122                 // drop excess elements from front
00123                 assert( (size_type)items.size() < cap );
00124                 while ( (size_type)(buf.size() + items.size()) > cap )
00125                     buf.pop_front();
00126                 // itl still points at first element of items.
00127             }
00128             while ( ((size_type)buf.size() != cap) && (itl != items.end()) ) {
00129                 buf.push_back( *itl );
00130                 ++itl;
00131             }
00132             // this is in any case the number of elements taken from items.
00133             if (mcircular)
00134                 assert( (size_type)(itl - items.begin() ) == (size_type)items.size() );
00135             return (itl - items.begin());
00136 
00137         }
00138         bool Pop( reference_t item )
00139         {
00140             os::MutexLock locker(lock);
00141             if ( buf.empty() ) {
00142                 return false;
00143             }
00144             item = buf.front();
00145             buf.pop_front();
00146             return true;
00147         }
00148 
00149         size_type Pop(std::vector<T>& items )
00150         {
00151             os::MutexLock locker(lock);
00152             int quant = 0;
00153             items.clear();
00154             while ( !buf.empty() ) {
00155                 items.push_back( buf.front() );
00156                 buf.pop_front();
00157                 ++quant;
00158             }
00159             return quant;
00160         }
00161 
00162         value_t* PopWithoutRelease()
00163         {
00164             os::MutexLock locker(lock);
00165             if(buf.empty())
00166                 return 0;
00167             
00168             //note we need to copy the sample, as 
00169             //front is not garanteed to be valid after
00170             //any other operation on the deque
00171             lastSample = buf.front();
00172             buf.pop_front();
00173             return &lastSample;
00174         }
00175         
00176         void Release(value_t *item)
00177         {
00178             //we do not need to release any memory, but we can check
00179             //if the other side messed up
00180             assert(item == &lastSample && "Wrong pointer given back to buffer");
00181         }
00182 
00183         size_type capacity() const {
00184             os::MutexLock locker(lock);
00185             return cap;
00186         }
00187 
00188         size_type size() const {
00189             os::MutexLock locker(lock);
00190             return buf.size();
00191         }
00192 
00193         void clear() {
00194             os::MutexLock locker(lock);
00195             buf.clear();
00196         }
00197 
00198         bool empty() const {
00199             os::MutexLock locker(lock);
00200             return buf.empty();
00201         }
00202 
00203         bool full() const {
00204             os::MutexLock locker(lock);
00205             return (size_type)buf.size() ==  cap;
00206         }
00207     private:
00208         size_type cap;
00209         std::deque<T> buf;
00210         value_t lastSample;
00211         mutable os::Mutex lock;
00212         const bool mcircular;
00213     };
00214 }}
00215 
00216 #endif // BUFFERSIMPLE_HPP


rtt
Author(s): RTT Developers
autogenerated on Thu Jan 2 2014 11:35:19