avr/cpp/collection/BaseArray.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_BASE_ARRAY_H__
00029 #define __AVR_CPP_BASE_ARRAY_H__
00030 
00031 
00032 /**********************************************************************************************************************\
00033 
00034         AVRCppLib collection objects
00035 
00036 \**********************************************************************************************************************/
00037 
00038 namespace AVRCpp
00039 {
00040         namespace Collection
00041         {
00042         
00043 
00044 /**********************************************************************************************************************\
00045 
00046         Base array class
00047 
00048         This class should never be instantized directly!
00049 
00050 \**********************************************************************************************************************/
00051 
00052 template <typename DataType, typename SizeType, typename ListType> class BaseArray
00053 {
00054         protected:
00055                 ListType data;
00056                 SizeType current_size;
00057 
00061                 inline bool Resize(const SizeType size, const DataType &values = DataType())
00062                 {
00063                         // Fill new sections with default value
00064                         for (register SizeType index = this->current_size; index < size; index++)
00065                         {
00066                                 this->data[index] = values;
00067                         }
00068 
00069                         this->current_size = size;
00070 
00071                         return true;
00072                 }
00073 
00077                 inline bool Add(const DataType &value)
00078                 {
00079                         // Increase size and add item
00080                         this->data[this->current_size++] = value;
00081 
00082                         return true;
00083                 }
00084 
00088                 inline bool Insert(const DataType &value, const SizeType pos)
00089                 {
00090                         // Shift data to right when adding to middle
00091                         if (pos < this->current_size)
00092                         {
00093                                 for (register SizeType index = this->current_size; index > pos; index--)
00094                                 {
00095                                         this->data[index] = this->data[index - 1];
00096                                 }
00097                         }
00098 
00099                         // Increase size
00100                         this->current_size++;
00101                         this->data[pos] = value;
00102 
00103                         return true;
00104                 }
00105 
00109                 inline bool Remove(const SizeType pos)
00110                 {
00111                         // Check constraints
00112                         if (pos >= this->current_size)
00113                         {
00114                                 return false;
00115                         }
00116 
00117                         // Shift data to left when removing from middle
00118                         if (pos < this->current_size - 1)
00119                         {
00120                                 for (register SizeType index = pos + 1; index < this->current_size; index++)
00121                                 {
00122                                         this->data[index - 1] = this->data[index];
00123                                 }
00124                         }
00125 
00126                         // Decrease size
00127                         this->current_size--;
00128 
00129                         return true;
00130                 }
00131 
00132         public:
00133 
00137                 BaseArray()
00138                 {
00139                         this->current_size      = 0;
00140                 }
00141 
00145                 inline SizeType GetSize(void)
00146                 {
00147                         return this->current_size;
00148                 }
00149 
00153                 inline bool IsEmpty(void)
00154                 {
00155                         return (this->current_size == 0);
00156                 }
00157 
00161                 inline bool Clear()
00162                 {
00163                         this->current_size = 0;
00164 
00165                         return true;
00166                 }
00167 
00172                 inline DataType &At(SizeType pos)
00173                 {
00174                         return this->data[pos];
00175                 }
00176 
00177 }; // class BaseArray
00178 
00179 
00180 /**********************************************************************************************************************/
00181 
00182         } // namespace Collection
00183         
00184 } // namespace AVRCpp
00185 
00186 
00187 /**********************************************************************************************************************/
00188 
00189 #endif // ifndef __AVR_CPP_BASE_ARRAY_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