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 "BufferPolicy.hpp"
00049 #include <deque>
00050 
00051 namespace RTT
00052 {
00053 
00060     template<class T, class ReadPolicy = NonBlockingPolicy, class WritePolicy = NonBlockingPolicy>
00061     class BufferLocked
00062         :public BufferInterface<T>
00063     {
00064     public:
00065 
00066         typedef typename ReadInterface<T>::reference_t reference_t;
00067         typedef typename WriteInterface<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(), write_policy(size), read_policy(0)
00076         {
00077             buf.resize(size, initial_value);
00078             buf.resize(0);
00079         }
00080 
00084         ~BufferLocked() {}
00085 
00086         bool Push( param_t item )
00087         {
00088             write_policy.pop();
00089             OS::MutexLock locker(lock);
00090             if (cap == (size_type)buf.size() ) {
00091                 write_policy.push();
00092                 return false;
00093             }
00094             buf.push_back( item );
00095             read_policy.push();
00096             return true;
00097         }
00098 
00099         size_type Push(const std::vector<T>& items)
00100         {
00101             write_policy.pop( items.size() );
00102             OS::MutexLock locker(lock);
00103             typename std::vector<T>::const_iterator itl( items.begin() );
00104             while ( ((size_type)buf.size() != cap) && (itl != items.end()) ) {
00105                 buf.push_back( *itl );
00106                 ++itl;
00107                 read_policy.push();
00108             }
00109             write_policy.push( itl - items.begin() );
00110             return (itl - items.begin());
00111 
00112         }
00113         bool Pop( reference_t item )
00114         {
00115             read_policy.pop();
00116             OS::MutexLock locker(lock);
00117             if ( buf.empty() ) {
00118                 read_policy.push();
00119                 return false;
00120             }
00121             item = buf.front();
00122             buf.pop_front();
00123             write_policy.push();
00124             return true;
00125         }
00126 
00127         size_type Pop(std::vector<T>& items )
00128         {
00129             OS::MutexLock locker(lock);
00130             int quant = 0;
00131             while ( !buf.empty() ) {
00132                 items.push_back( buf.front() );
00133                 buf.pop_front();
00134                 ++quant;
00135                 read_policy.pop();
00136                 write_policy.push();
00137             }
00138             return quant;
00139         }
00140 
00141         value_t front() const
00142         {
00143             OS::MutexLock locker(lock);
00144             value_t item = value_t();
00145             if ( !buf.empty() )
00146                 item = buf.front();
00147             return item;
00148         }
00149 
00150         size_type capacity() const {
00151             OS::MutexLock locker(lock);
00152             return cap;
00153         }
00154 
00155         size_type size() const {
00156             OS::MutexLock locker(lock);
00157             return buf.size();
00158         }
00159 
00160         void clear() {
00161             OS::MutexLock locker(lock);
00162             buf.clear();
00163         }
00164 
00165         bool empty() const {
00166             OS::MutexLock locker(lock);
00167             return buf.empty();
00168         }
00169 
00170         bool full() const {
00171             OS::MutexLock locker(lock);
00172             return (size_type)buf.size() ==  cap;
00173         }
00174     private:
00175         size_type cap;
00176         std::deque<T> buf;
00177         mutable OS::Mutex lock;
00178         WritePolicy write_policy;
00179         ReadPolicy read_policy;
00180     };
00181 }
00182 
00183 #endif // BUFFERSIMPLE_HPP
Generated on Thu Dec 23 13:22:36 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3