fixedsizearray.hpp

Go to the documentation of this file.
00001 /* $Id: fixedsizearray.hpp 18828 2010-01-16 16:44:59Z 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 
00021 template <class T, uint C>
00022 struct FixedSizeArray {
00023 protected:
00025   struct ArrayHeader
00026   {
00027     uint items;           
00028     uint reference_count; 
00029   };
00030 
00031   /* make constants visible from outside */
00032   static const uint Tsize = sizeof(T);                // size of item
00033   static const uint HeaderSize = sizeof(ArrayHeader); // size of header
00034 
00037   T *data;
00038 
00040   FORCEINLINE ArrayHeader& Hdr() { return *(ArrayHeader*)(((byte*)data) - HeaderSize); }
00042   FORCEINLINE const ArrayHeader& Hdr() const { return *(ArrayHeader*)(((byte*)data) - HeaderSize); }
00044   FORCEINLINE uint& RefCnt() { return Hdr().reference_count; }
00046   FORCEINLINE uint& SizeRef() { return Hdr().items; }
00047 
00048 public:
00050   FixedSizeArray()
00051   {
00052     /* allocate block for header + items (don't construct items) */
00053     data = (T*)((MallocT<byte>(HeaderSize + C * Tsize)) + HeaderSize);
00054     SizeRef() = 0; // initial number of items
00055     RefCnt() = 1; // initial reference counter
00056   }
00057 
00059   FixedSizeArray(const FixedSizeArray<T, C>& src)
00060   {
00061     /* share block (header + items) with the source array */
00062     data = src.data;
00063     RefCnt()++; // now we share block with the source
00064   }
00065 
00067   ~FixedSizeArray()
00068   {
00069     /* release one reference to the shared block */
00070     if ((--RefCnt()) > 0) return; // and return if there is still some owner
00071 
00072     Clear();
00073     /* free the memory block occupied by items */
00074     free(((byte*)data) - HeaderSize);
00075     data = NULL;
00076   }
00077 
00079   FORCEINLINE void Clear()
00080   {
00081     /* Walk through all allocated items backward and destroy them
00082      * Note: this->Length() can be zero. In that case data[this->Length() - 1] is evaluated unsigned
00083      *       on some compilers with some architectures. (e.g. gcc with x86) */
00084     for (T *pItem = this->data + this->Length() - 1; pItem >= this->data; pItem--) {
00085       pItem->~T();
00086     }
00087     /* number of items become zero */
00088     SizeRef() = 0;
00089   }
00090 
00092   FORCEINLINE uint Length() const { return Hdr().items; }
00094   FORCEINLINE bool IsFull() const { return Length() >= C; };
00096   FORCEINLINE bool IsEmpty() const { return Length() <= 0; };
00098   FORCEINLINE T *Append() { assert(!IsFull()); return &data[SizeRef()++]; }
00100   FORCEINLINE T *AppendC() { T *item = Append(); new(item)T; return item; }
00102   FORCEINLINE T& operator [] (uint index) { assert(index < Length()); return data[index]; }
00104   FORCEINLINE const T& operator [] (uint index) const { assert(index < Length()); return data[index]; }
00105 };
00106 
00107 #endif /* FIXEDSIZEARRAY_HPP */

Generated on Sat Apr 17 23:24:48 2010 for OpenTTD by  doxygen 1.6.1