1 /* $Id: fixedsizearray.hpp 23640 2011-12-20 17:57:56Z truebrain $ */
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file fixedsizearray.hpp A fixed size array that doesn't create items until needed. */
12 #ifndef FIXEDSIZEARRAY_HPP
13 #define FIXEDSIZEARRAY_HPP
15 #include "../core/alloc_func.hpp"
19 * Upon construction it preallocates fixed size block of memory
20 * for all items, but doesn't construct them. Item's construction
23 template <class T
, uint C
>
24 struct FixedSizeArray
{
26 /** header for fixed size array */
29 uint items
; ///< number of items in the array
30 uint reference_count
; ///< block reference counter (used by copy constructor and by destructor)
33 /* make constants visible from outside */
34 static const uint Tsize
= sizeof(T
); ///< size of item
35 static const uint HeaderSize
= sizeof(ArrayHeader
); ///< size of header
38 * the only member of fixed size array is pointer to the block
39 * of C array of items. Header can be found on the offset -sizeof(ArrayHeader).
43 /** return reference to the array header (non-const) */
44 inline ArrayHeader
& Hdr()
46 return *(ArrayHeader
*)(((byte
*)data
) - HeaderSize
);
49 /** return reference to the array header (const) */
50 inline const ArrayHeader
& Hdr() const
52 return *(ArrayHeader
*)(((byte
*)data
) - HeaderSize
);
55 /** return reference to the block reference counter */
58 return Hdr().reference_count
;
61 /** return reference to number of used items */
62 inline uint
& SizeRef()
68 /** Default constructor. Preallocate space for items and header, then initialize header. */
71 /* Ensure the size won't overflow. */
72 assert_compile(C
< (SIZE_MAX
- HeaderSize
) / Tsize
);
74 /* allocate block for header + items (don't construct items) */
75 data
= (T
*)((MallocT
<byte
>(HeaderSize
+ C
* Tsize
)) + HeaderSize
);
76 SizeRef() = 0; // initial number of items
77 RefCnt() = 1; // initial reference counter
80 /** Copy constructor. Preallocate space for items and header, then initialize header. */
81 FixedSizeArray(const FixedSizeArray
<T
, C
> &src
)
83 /* share block (header + items) with the source array */
85 RefCnt()++; // now we share block with the source
88 /** destroy remaining items and free the memory block */
91 /* release one reference to the shared block */
92 if ((--RefCnt()) > 0) return; // and return if there is still some owner
95 /* free the memory block occupied by items */
96 free(((byte
*)data
) - HeaderSize
);
100 /** Clear (destroy) all items */
103 /* Walk through all allocated items backward and destroy them
104 * Note: this->Length() can be zero. In that case data[this->Length() - 1] is evaluated unsigned
105 * on some compilers with some architectures. (e.g. gcc with x86) */
106 for (T
*pItem
= this->data
+ this->Length() - 1; pItem
>= this->data
; pItem
--) {
109 /* number of items become zero */
113 /** return number of used items */
114 inline uint
Length() const
119 /** return true if array is full */
120 inline bool IsFull() const
122 return Length() >= C
;
125 /** return true if array is empty */
126 inline bool IsEmpty() const
128 return Length() <= 0;
131 /** add (allocate), but don't construct item */
135 return &data
[SizeRef()++];
138 /** add and construct item using default constructor */
145 /** return item by index (non-const version) */
146 inline T
& operator[](uint index
)
148 assert(index
< Length());
152 /** return item by index (const version) */
153 inline const T
& operator[](uint index
) const
155 assert(index
< Length());
160 #endif /* FIXEDSIZEARRAY_HPP */