# fixedsizearray.hpp

Go to the documentation of this file.

    00001 /* $Id$ */
    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     /* allocate block for header + items (don't construct items) */
    00057     data = (T*)((MallocT<byte>(HeaderSize + C * Tsize)) + HeaderSize);
    00058     SizeRef() = 0; // initial number of items
    00059     RefCnt() = 1; // initial reference counter
    00060   }
    00061
    00063   FixedSizeArray(const FixedSizeArray<T, C>& src)
    00064   {
    00065     /* share block (header + items) with the source array */
    00066     data = src.data;
    00067     RefCnt()++; // now we share block with the source
    00068   }
    00069
    00071   ~FixedSizeArray()
    00072   {
    00073     /* release one reference to the shared block */
    00074     if ((--RefCnt()) > 0) return; // and return if there is still some owner
    00075
    00076     Clear();
    00077     /* free the memory block occupied by items */
    00078     free(((byte*)data) - HeaderSize);
    00079     data = NULL;
    00080   }
    00081
    00083   FORCEINLINE void Clear()
    00084   {
    00085     /* Walk through all allocated items backward and destroy them
    00086      * Note: this->Length() can be zero. In that case data[this->Length() - 1] is evaluated unsigned
    00087      *       on some compilers with some architectures. (e.g. gcc with x86) */
    00088     for (T *pItem = this->data + this->Length() - 1; pItem >= this->data; pItem--) {
    00089       pItem->~T();
    00090     }
    00091     /* number of items become zero */
    00092     SizeRef() = 0;
    00093   }
    00094
    00096   FORCEINLINE uint Length() const { return Hdr().items; }
    00098   FORCEINLINE bool IsFull() const { return Length() >= C; }
    00100   FORCEINLINE bool IsEmpty() const { return Length() <= 0; }
    00102   FORCEINLINE T *Append() { assert(!IsFull()); return &data[SizeRef()++]; }
    00104   FORCEINLINE T *AppendC() { T *item = Append(); new(item)T; return item; }
    00106   FORCEINLINE T& operator [] (uint index) { assert(index < Length()); return data[index]; }
    00108   FORCEINLINE const T& operator [] (uint index) const { assert(index < Length()); return data[index]; }
    00109 };
    00110
    00111 #endif /* FIXEDSIZEARRAY_HPP */