fixedsizearray.hpp

Go to the documentation of this file.
00001 /* $Id: fixedsizearray.hpp 22886 2011-09-03 18:56:34Z frosch $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #ifndef FIXEDSIZEARRAY_HPP
00013 #define FIXEDSIZEARRAY_HPP
00014 
00015 #include "../core/alloc_func.hpp"
00016 
00023 template <class T, uint C>
00024 struct FixedSizeArray {
00025 protected:
00027   struct ArrayHeader
00028   {
00029     uint items;           
00030     uint reference_count; 
00031   };
00032 
00033   /* make constants visible from outside */
00034   static const uint Tsize = sizeof(T);                // size of item
00035   static const uint HeaderSize = sizeof(ArrayHeader); // size of header
00036 
00041   T *data;
00042 
00044   FORCEINLINE ArrayHeader& Hdr() { return *(ArrayHeader*)(((byte*)data) - HeaderSize); }
00046   FORCEINLINE const ArrayHeader& Hdr() const { return *(ArrayHeader*)(((byte*)data) - HeaderSize); }
00048   FORCEINLINE uint& RefCnt() { return Hdr().reference_count; }
00050   FORCEINLINE uint& SizeRef() { return Hdr().items; }
00051 
00052 public:
00054   FixedSizeArray()
00055   {
00056     /* Ensure the size won't overflow. */
00057     assert_compile(C < (SIZE_MAX - HeaderSize) / Tsize);
00058 
00059     /* allocate block for header + items (don't construct items) */
00060     data = (T*)((MallocT<byte>(HeaderSize + C * Tsize)) + HeaderSize);
00061     SizeRef() = 0; // initial number of items
00062     RefCnt() = 1; // initial reference counter
00063   }
00064 
00066   FixedSizeArray(const FixedSizeArray<T, C>& src)
00067   {
00068     /* share block (header + items) with the source array */
00069     data = src.data;
00070     RefCnt()++; // now we share block with the source
00071   }
00072 
00074   ~FixedSizeArray()
00075   {
00076     /* release one reference to the shared block */
00077     if ((--RefCnt()) > 0) return; // and return if there is still some owner
00078 
00079     Clear();
00080     /* free the memory block occupied by items */
00081     free(((byte*)data) - HeaderSize);
00082     data = NULL;
00083   }
00084 
00086   FORCEINLINE void Clear()
00087   {
00088     /* Walk through all allocated items backward and destroy them
00089      * Note: this->Length() can be zero. In that case data[this->Length() - 1] is evaluated unsigned
00090      *       on some compilers with some architectures. (e.g. gcc with x86) */
00091     for (T *pItem = this->data + this->Length() - 1; pItem >= this->data; pItem--) {
00092       pItem->~T();
00093     }
00094     /* number of items become zero */
00095     SizeRef() = 0;
00096   }
00097 
00099   FORCEINLINE uint Length() const { return Hdr().items; }
00101   FORCEINLINE bool IsFull() const { return Length() >= C; }
00103   FORCEINLINE bool IsEmpty() const { return Length() <= 0; }
00105   FORCEINLINE T *Append() { assert(!IsFull()); return &data[SizeRef()++]; }
00107   FORCEINLINE T *AppendC() { T *item = Append(); new(item)T; return item; }
00109   FORCEINLINE T& operator [] (uint index) { assert(index < Length()); return data[index]; }
00111   FORCEINLINE const T& operator [] (uint index) const { assert(index < Length()); return data[index]; }
00112 };
00113 
00114 #endif /* FIXEDSIZEARRAY_HPP */