Fix #10490: Allow ships to exit depots if another is not moving at the exit point...
[openttd-github.git] / src / misc / fixedsizearray.hpp
blob7a2c97b58b0b587247dd0382e4118400b9ca5cea
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file fixedsizearray.hpp A fixed size array that doesn't create items until needed. */
10 #ifndef FIXEDSIZEARRAY_HPP
11 #define FIXEDSIZEARRAY_HPP
13 #include "../core/alloc_func.hpp"
15 /**
16 * fixed size array
17 * Upon construction it preallocates fixed size block of memory
18 * for all items, but doesn't construct them. Item's construction
19 * is delayed.
21 template <class T, uint C>
22 struct FixedSizeArray {
23 protected:
24 /** header for fixed size array */
25 struct ArrayHeader
27 uint items; ///< number of items in the array
28 uint reference_count; ///< block reference counter (used by copy constructor and by destructor)
31 /* make constants visible from outside */
32 static const uint Tsize = sizeof(T); ///< size of item
33 static const uint HeaderSize = sizeof(ArrayHeader); ///< size of header
35 /**
36 * the only member of fixed size array is pointer to the block
37 * of C array of items. Header can be found on the offset -sizeof(ArrayHeader).
39 T *data;
41 /** return reference to the array header (non-const) */
42 inline ArrayHeader &Hdr()
44 return *(ArrayHeader*)(((byte*)data) - HeaderSize);
47 /** return reference to the array header (const) */
48 inline const ArrayHeader &Hdr() const
50 return *(ArrayHeader*)(((byte*)data) - HeaderSize);
53 /** return reference to the block reference counter */
54 inline uint &RefCnt()
56 return Hdr().reference_count;
59 /** return reference to number of used items */
60 inline uint &SizeRef()
62 return Hdr().items;
65 public:
66 /** Default constructor. Preallocate space for items and header, then initialize header. */
67 FixedSizeArray()
69 /* Ensure the size won't overflow. */
70 static_assert(C < (SIZE_MAX - HeaderSize) / Tsize);
72 /* allocate block for header + items (don't construct items) */
73 data = (T*)((MallocT<byte>(HeaderSize + C * Tsize)) + HeaderSize);
74 SizeRef() = 0; // initial number of items
75 RefCnt() = 1; // initial reference counter
78 /** Copy constructor. Preallocate space for items and header, then initialize header. */
79 FixedSizeArray(const FixedSizeArray<T, C> &src)
81 /* share block (header + items) with the source array */
82 data = src.data;
83 RefCnt()++; // now we share block with the source
86 /** destroy remaining items and free the memory block */
87 ~FixedSizeArray()
89 /* release one reference to the shared block */
90 if ((--RefCnt()) > 0) return; // and return if there is still some owner
92 Clear();
93 /* free the memory block occupied by items */
94 free(((byte*)data) - HeaderSize);
95 data = nullptr;
98 /** Clear (destroy) all items */
99 inline void Clear()
101 /* Walk through all allocated items backward and destroy them
102 * Note: this->Length() can be zero. In that case data[this->Length() - 1] is evaluated unsigned
103 * on some compilers with some architectures. (e.g. gcc with x86) */
104 for (T *pItem = this->data + this->Length() - 1; pItem >= this->data; pItem--) {
105 pItem->~T();
107 /* number of items become zero */
108 SizeRef() = 0;
111 /** return number of used items */
112 inline uint Length() const
114 return Hdr().items;
117 /** return true if array is full */
118 inline bool IsFull() const
120 return Length() >= C;
123 /** return true if array is empty */
124 inline bool IsEmpty() const
126 return Length() <= 0;
129 /** add (allocate), but don't construct item */
130 inline T *Append()
132 assert(!IsFull());
133 return &data[SizeRef()++];
136 /** add and construct item using default constructor */
137 inline T *AppendC()
139 T *item = Append();
140 new(item)T;
141 return item;
143 /** return item by index (non-const version) */
144 inline T& operator[](uint index)
146 assert(index < Length());
147 return data[index];
150 /** return item by index (const version) */
151 inline const T& operator[](uint index) const
153 assert(index < Length());
154 return data[index];
158 #endif /* FIXEDSIZEARRAY_HPP */