FixedSizeByteArray.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #ifndef UTILS__FIXED_SIZE_BYTE_ARRAY_H
00024 #define UTILS__FIXED_SIZE_BYTE_ARRAY_H
00025 
00026 #include "Imports.h"
00027 
00028 namespace Utils {
00029 
00031 
00044 template <uint Size>
00045 class FixedSizeByteArray
00046 {
00047 public:
00049 
00053     FixedSizeByteArray()
00054         : array (Size, '\0')
00055     {
00056     }
00057 
00059 
00067     explicit FixedSizeByteArray (const QByteArray &byteArray)
00068         : array (byteArray)
00069     {
00070         Q_ASSERT (static_cast <uint> (array.size()) == Size);
00071         // This should never happen at it's here only to enforce array size
00072         // invariant even in release mode.
00073         array.resize (Size);
00074     }
00075 
00077 
00080     ~FixedSizeByteArray()
00081     {
00082     }
00083 
00085 
00090     bool operator== (const FixedSizeByteArray &other) const
00091     {
00092         return array == other.array;
00093     }
00094 
00096 
00101     bool operator!= (const FixedSizeByteArray &other) const
00102     {
00103         return !(*this == other);
00104     }
00105 
00107 
00112     bool operator< (const FixedSizeByteArray &other) const
00113     {
00114         return array < other.array;
00115     }
00116 
00118 
00123     bool operator<= (const FixedSizeByteArray &other) const
00124     {
00125         return array <= other.array;
00126     }
00127 
00129 
00134     bool operator> (const FixedSizeByteArray &other) const
00135     {
00136         return array > other.array;
00137     }
00138 
00140 
00145     bool operator>= (const FixedSizeByteArray &other) const
00146     {
00147         return array >= other.array;
00148     }
00149 
00151 
00157     char operator[] (uint index) const
00158     {
00159         Q_ASSERT (index < Size);
00160         return array [index];
00161     }
00162 
00164     QByteArray toQByteArray() const
00165     {
00166         return array;
00167     }
00168 
00170 
00179     static uint size()
00180     {
00181         return Size;
00182     }
00183 
00184 private:
00185     QByteArray array; 
00186 };
00187 
00188 } // namespace Utils
00189 
00190 #endif // UTILS__FIXED_SIZE_BYTE_ARRAY_H