avr/cpp/collection/PeekableStaticQueue.h

Go to the documentation of this file.
00001 /**********************************************************************************************************************\
00002 
00003         C++ library for Atmel AVR microcontrollers
00004         Copyright (C) 2007 Lauri Kirikal, Mikk Leini, MT� TT� Robotiklubi
00005 
00006         This program is free software; you can redistribute it and/or
00007         modify it under the terms of the GNU General Public License
00008         as published by the Free Software Foundation; either version 2
00009         of the License, or (at your option) any later version.
00010 
00011         This program is distributed in the hope that it will be useful,
00012         but WITHOUT ANY WARRANTY; without even the implied warranty of
00013         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014         GNU General Public License for more details.
00015 
00016         You should have received a copy of the GNU General Public License
00017         along with this program; if not, write to the Free Software
00018         Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019 
00020         See http://creativecommons.org/licenses/GPL/2.0/
00021 
00022         MT� TT� Robotiklubi  http://www.robotiklubi.ee robotiklubi@gmail.com
00023         Lauri Kirikal        laurikirikal@gmail.com
00024         Mikk Leini           mikk.leini@gmail.com
00025 
00026 \**********************************************************************************************************************/
00027 
00028 #ifndef __AVR_CPP_PEEKABLE_STATIC_QUEUE_H__
00029 #define __AVR_CPP_PEEKABLE_STATIC_QUEUE_H__
00030 
00031 #include "StaticQueue.h"
00032 
00033 
00034 /**********************************************************************************************************************\
00035 
00036         AVRCppLib collection objects
00037 
00038 \**********************************************************************************************************************/
00039 
00040 namespace AVRCpp
00041 {
00042         namespace Collection
00043         {
00044         
00049 template <typename DataType, typename SizeType, SizeType queue_capacity> class PeekableStaticQueue : public StaticQueue/*<int, uint8_t, 15>*/<DataType, SizeType, queue_capacity>
00050 {
00051 private:
00052 
00053         SizeType peek_pointer;
00054         
00055 public:
00056         
00057         PeekableStaticQueue<DataType, SizeType, queue_capacity>()
00058         {
00059                 StaticQueue<DataType, SizeType, queue_capacity>();
00060         }
00061         
00065         inline void PeekReset()
00066         {
00067                 peek_pointer = this->read_pointer;
00068                 
00069         } // PeekReset
00070         
00099         bool Peek(DataType &value)
00100         {
00101                 // If queue empty then exit
00102                 if (this->IsEmpty()) return false;
00103                 
00104                 // Read item and increase read pointer
00105                 value = this->data[this->peek_pointer];
00106                 
00107                 // Queue can't be full now
00108                 this->is_full = 0;
00109                 
00110                 return true;
00111                 
00112         } // Peek
00113         
00118         bool Pop(DataType &value = 0)
00119         {
00120                 bool result = ((StaticQueue<DataType, SizeType, queue_capacity> *)this)->Pop(value);
00121                 
00122                 peek_pointer = this->read_pointer;
00123                 
00124                 return result;
00125                 
00126         } // Pop
00127         
00132         bool CanPeekNext(void)
00133         {
00134                 if (CanPeek() )
00135                 {
00136                         peek_pointer++;
00137                         peek_pointer %= queue_capacity;
00138                         
00139                         return CanPeek();
00140                 }
00141                 
00142                 return false;
00143                 
00144         } // CanPeekNext
00145         
00149         inline bool CanPeek(void)
00150         {
00151                 return this->peek_pointer != this->write_pointer;
00152                 
00153         } // CanPeek
00154 
00158         inline void PopAllPeeked()
00159         {
00160                 this->read_pointer = peek_pointer;
00161                 
00162         } // FlushPeeked
00163 
00164 
00165 }; // class PeekableStaticQueue
00166 
00167 /**********************************************************************************************************************/
00168 
00169         } // namespace Collection
00170 
00171 } // namespace AVRCpp
00172 
00173 
00174 /**********************************************************************************************************************/
00175 
00176 #endif // ifndef __AVR_CPP_PEEKABLE_STATIC_QUEUE_H__

Generated on Sat Sep 15 23:41:46 2007 for AVR C++ Lib (common) by  doxygen 1.5.2
SourceForge.net Logo MTÜ TTÜ Robotiklubi