Orocos Real-Time Toolkit  2.5.0
BufferLocked.hpp
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 
00074         BufferLocked( size_type size, const T& initial_value = T() )
00075             : cap(size), buf()
00076         {
00077             data_sample(initial_value);
00078         }
00079 
00080         virtual void data_sample( const T& sample )
00081         {
00082             buf.resize(cap, sample);
00083             buf.resize(0);
00084         }
00085 
00089         ~BufferLocked() {}
00090 
00091         bool Push( param_t item )
00092         {
00093             os::MutexLock locker(lock);
00094             if (cap == (size_type)buf.size() ) {
00095                 return false;
00096             }
00097             buf.push_back( item );
00098             return true;
00099         }
00100 
00101         size_type Push(const std::vector<T>& items)
00102         {
00103             os::MutexLock locker(lock);
00104             typename std::vector<T>::const_iterator itl( items.begin() );
00105             while ( ((size_type)buf.size() != cap) && (itl != items.end()) ) {
00106                 buf.push_back( *itl );
00107                 ++itl;
00108             }
00109             return (itl - items.begin());
00110 
00111         }
00112         bool Pop( reference_t item )
00113         {
00114             os::MutexLock locker(lock);
00115             if ( buf.empty() ) {
00116                 return false;
00117             }
00118             item = buf.front();
00119             buf.pop_front();
00120             return true;
00121         }
00122 
00123         size_type Pop(std::vector<T>& items )
00124         {
00125             os::MutexLock locker(lock);
00126             int quant = 0;
00127             while ( !buf.empty() ) {
00128                 items.push_back( buf.front() );
00129                 buf.pop_front();
00130                 ++quant;
00131             }
00132             return quant;
00133         }
00134 
00135     value_t* PopWithoutRelease()
00136     {
00137             os::MutexLock locker(lock);
00138         if(buf.empty())
00139         return 0;
00140         
00141         //note we need to copy the sample, as 
00142         //front is not garanteed to be valid after
00143         //any other operation on the deque
00144         lastSample = buf.front();
00145         buf.pop_front();
00146         return &lastSample;
00147     }
00148     
00149     void Release(value_t *item)
00150     {
00151         //we do not need to release any memory, but we can check
00152         //if the other side messed up
00153         assert(item == &lastSample && "Wrong pointer given back to buffer");
00154     }
00155 
00156         size_type capacity() const {
00157             os::MutexLock locker(lock);
00158             return cap;
00159         }
00160 
00161         size_type size() const {
00162             os::MutexLock locker(lock);
00163             return buf.size();
00164         }
00165 
00166         void clear() {
00167             os::MutexLock locker(lock);
00168             buf.clear();
00169         }
00170 
00171         bool empty() const {
00172             os::MutexLock locker(lock);
00173             return buf.empty();
00174         }
00175 
00176         bool full() const {
00177             os::MutexLock locker(lock);
00178             return (size_type)buf.size() ==  cap;
00179         }
00180     private:
00181         size_type cap;
00182         std::deque<T> buf;
00183     value_t lastSample;
00184         mutable os::Mutex lock;
00185     };
00186 }}
00187 
00188 #endif // BUFFERSIMPLE_HPP