LockedQueue.hpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: Peter Soetens  Thu Oct 22 11:59:08 CEST 2009  LockedQueue.hpp
00003 
00004                         LockedQueue.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 #ifndef ORO_LOCKED_QUEUE_HPP
00040 #define ORO_LOCKED_QUEUE_HPP
00041 
00042 #include <deque>
00043 #include "../os/Mutex.hpp"
00044 #include "../os/MutexLock.hpp"
00045 
00046 namespace RTT
00047 { namespace internal {
00048 
00057     template< class T>
00058     class LockedQueue
00059     {
00060     public:
00061         typedef T value_t;
00062     private:
00063         typedef std::deque<value_t> BufferType;
00064         typedef typename BufferType::iterator Iterator;
00065         typedef typename BufferType::const_iterator CIterator;
00066         mutable os::Mutex lock;
00067         BufferType data;
00068 
00069         unsigned int cap;
00070     public:
00071         typedef unsigned int size_type;
00072 
00077         LockedQueue(unsigned int lsize)
00078             : cap(lsize)
00079         {
00080             data.resize(lsize);
00081             data.resize(0);
00082         }
00083 
00084         ~LockedQueue() {
00085         }
00086 
00087         size_type capacity() const
00088         {
00089             return cap;
00090         }
00091 
00092         size_type size() const
00093         {
00094             os::MutexLock locker(lock);
00095             return data.size();
00096         }
00097 
00102         bool isEmpty() const
00103         {
00104             os::MutexLock locker(lock);
00105             return data.empty();
00106         }
00107 
00112         bool isFull() const
00113         {
00114             os::MutexLock locker(lock);
00115             return data.size() ==  cap;
00116         }
00117 
00118         void clear()
00119         {
00120             os::MutexLock locker(lock);
00121             data.clear();
00122         }
00123 
00129         bool enqueue(const T& value)
00130         {
00131             {
00132                 os::MutexLock locker(lock);
00133                 if (cap == data.size() )
00134                     return false;
00135                 data.push_back(value);
00136             }
00137             return true;
00138         }
00139 
00145         bool dequeue( T& result )
00146         {
00147             {
00148                 os::MutexLock locker(lock);
00149                 if ( data.empty() )
00150                     return false;
00151                 result = data.front();
00152                 data.pop_front();
00153             }
00154             return true;
00155         }
00156 
00160         value_t front() const
00161         {
00162             os::MutexLock locker(lock);
00163             value_t item = value_t();
00164             if ( !data.empty() )
00165                 item = data.front();
00166             return item;
00167         }
00168 
00172         value_t back() const
00173         {
00174             os::MutexLock locker(lock);
00175             value_t item = value_t();
00176             if ( !data.empty() )
00177                 item = data.back();
00178             return item;
00179         }
00180 
00181     };
00182 
00183 }}
00184 
00185 #endif


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