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_STRING_H__ 00029 #define __AVR_CPP_STRING_H__ 00030 00031 #include "BaseString.h" 00032 00033 namespace AVRCpp 00034 { 00035 namespace Collection 00036 { 00037 00041 template <uint16_t capacity> class StaticString : public BaseString 00042 { 00043 private: 00044 00049 bool EnsureCapacity(uint16_t required); 00050 00051 protected: 00052 00054 char content[capacity]; 00055 00056 public: 00057 00059 StaticString<capacity>() : BaseString() {} 00060 00061 }; // class StaticString 00062 00063 00068 template <uint16_t blockSize = 1> class DynamicString : public BaseString 00069 { 00070 private: 00071 00073 uint16_t GetBlockSize(uint16_t required); 00074 00079 bool EnsureCapacity(uint16_t required); 00080 00081 public: 00082 00084 DynamicString<blockSize>() : BaseString() {} 00086 ~DynamicString<blockSize>() { if (me) free(me); } 00087 00088 }; // class DynamicString 00089 00093 template <> class DynamicString<1> : public BaseString 00094 { 00095 private: 00096 00101 bool EnsureCapacity(uint16_t required); 00102 00103 public: 00104 00106 DynamicString<1>() : BaseString() {} 00108 ~DynamicString<1>() { if (me) free(me); } 00109 00110 }; // class StaticString SPECIALISATION 00111 00112 00114 typedef DynamicString<1> String; 00115 00116 00117 template <uint16_t blockSize> uint16_t DynamicString<blockSize>::GetBlockSize(uint16_t required) 00118 { 00119 uint16_t k = required / blockSize + 1; 00120 return k * blockSize; 00121 00122 } // GetBlockSize 00123 00124 00125 template <uint16_t blockSize> bool DynamicString<blockSize>::EnsureCapacity(uint16_t required) 00126 { 00127 if (me) 00128 { 00129 if (size < required) 00130 { 00131 required = GetBlockSize(required); 00132 me = StrRealloc(me, required); 00133 } 00134 else 00135 return true; 00136 } 00137 else 00138 { 00139 required = GetBlockSize(required); 00140 me = StrAlloc(required); 00141 } 00142 00143 if (me) 00144 { 00145 size = required; 00146 return true; 00147 } 00148 00149 size = 0; 00150 length = 0; 00151 return false; 00152 00153 } // EnsureCapasity 00154 00155 00156 template <uint16_t capacity> bool StaticString<capacity>::EnsureCapacity(uint16_t required) 00157 { 00158 if (required > capacity) 00159 return false; 00160 00161 me = content; 00162 00163 return true; 00164 00165 } // EnsureCapasity 00166 00167 } // namespace Collection 00168 00169 } // namespace AVRCpp 00170 00171 #endif // ifndef __AVR_CPP_STRING_H__ 00172
MTÜ TTÜ Robotiklubi |