Files
@ r24118:35dc08a3586b
Branch filter:
Location: cpp/openttd-patchpack/source/src/misc/fixedsizearray.hpp - annotation
r24118:35dc08a3586b
4.4 KiB
text/x-c++hdr
Fix f5381798: Station::GetTileArea reduced docks to a single tile (#8014)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r12768:980ae0491352 r9111:983de9c5a848 r6481:534c9f0317ec r16850:82c7acc31833 r16850:82c7acc31833 r5633:33223f089b79 r14248:a9050881acd7 r5633:33223f089b79 r15610:623a23fb6560 r15610:623a23fb6560 r5633:33223f089b79 r5633:33223f089b79 r15613:193c12018337 r15613:193c12018337 r14261:734469f01cfe r14260:f3bc3a5d8acc r14262:62859bc0fbdc r5633:33223f089b79 r14260:f3bc3a5d8acc r5633:33223f089b79 r14261:734469f01cfe r14261:734469f01cfe r5633:33223f089b79 r5633:33223f089b79 r14261:734469f01cfe r17630:7d818445376d r17630:7d818445376d r5633:33223f089b79 r15610:623a23fb6560 r15610:623a23fb6560 r15613:193c12018337 r15613:193c12018337 r14262:62859bc0fbdc r14262:62859bc0fbdc r14262:62859bc0fbdc r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r14262:62859bc0fbdc r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r14262:62859bc0fbdc r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r14262:62859bc0fbdc r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r14262:62859bc0fbdc r14262:62859bc0fbdc r5633:33223f089b79 r14260:f3bc3a5d8acc r5633:33223f089b79 r18050:a6651f064e89 r18050:a6651f064e89 r18050:a6651f064e89 r11363:6906c490a00e r14261:734469f01cfe r5633:33223f089b79 r5633:33223f089b79 r5633:33223f089b79 r5633:33223f089b79 r5633:33223f089b79 r22182:16fb91fde98a r5633:33223f089b79 r11363:6906c490a00e r14260:f3bc3a5d8acc r5633:33223f089b79 r5633:33223f089b79 r5633:33223f089b79 r5633:33223f089b79 r14260:f3bc3a5d8acc r5633:33223f089b79 r11363:6906c490a00e r5633:33223f089b79 r5633:33223f089b79 r5633:33223f089b79 r11363:6906c490a00e r14261:734469f01cfe r23607:36c15679007d r5633:33223f089b79 r5633:33223f089b79 r5633:33223f089b79 r18782:6453522c2154 r5633:33223f089b79 r14266:f9d8a18ef0cc r14266:f9d8a18ef0cc r14266:f9d8a18ef0cc r14266:f9d8a18ef0cc r14260:f3bc3a5d8acc r5633:33223f089b79 r11363:6906c490a00e r5633:33223f089b79 r5633:33223f089b79 r5633:33223f089b79 r5633:33223f089b79 r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r5633:33223f089b79 r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r5633:33223f089b79 r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r5633:33223f089b79 r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r5633:33223f089b79 r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r5633:33223f089b79 r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r5633:33223f089b79 r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r22183:ce9ab6d4ab0f r5633:33223f089b79 r5633:33223f089b79 r5633:33223f089b79 | /*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file fixedsizearray.hpp A fixed size array that doesn't create items until needed. */
#ifndef FIXEDSIZEARRAY_HPP
#define FIXEDSIZEARRAY_HPP
#include "../core/alloc_func.hpp"
/**
* fixed size array
* Upon construction it preallocates fixed size block of memory
* for all items, but doesn't construct them. Item's construction
* is delayed.
*/
template <class T, uint C>
struct FixedSizeArray {
protected:
/** header for fixed size array */
struct ArrayHeader
{
uint items; ///< number of items in the array
uint reference_count; ///< block reference counter (used by copy constructor and by destructor)
};
/* make constants visible from outside */
static const uint Tsize = sizeof(T); ///< size of item
static const uint HeaderSize = sizeof(ArrayHeader); ///< size of header
/**
* the only member of fixed size array is pointer to the block
* of C array of items. Header can be found on the offset -sizeof(ArrayHeader).
*/
T *data;
/** return reference to the array header (non-const) */
inline ArrayHeader& Hdr()
{
return *(ArrayHeader*)(((byte*)data) - HeaderSize);
}
/** return reference to the array header (const) */
inline const ArrayHeader& Hdr() const
{
return *(ArrayHeader*)(((byte*)data) - HeaderSize);
}
/** return reference to the block reference counter */
inline uint& RefCnt()
{
return Hdr().reference_count;
}
/** return reference to number of used items */
inline uint& SizeRef()
{
return Hdr().items;
}
public:
/** Default constructor. Preallocate space for items and header, then initialize header. */
FixedSizeArray()
{
/* Ensure the size won't overflow. */
assert_compile(C < (SIZE_MAX - HeaderSize) / Tsize);
/* allocate block for header + items (don't construct items) */
data = (T*)((MallocT<byte>(HeaderSize + C * Tsize)) + HeaderSize);
SizeRef() = 0; // initial number of items
RefCnt() = 1; // initial reference counter
}
/** Copy constructor. Preallocate space for items and header, then initialize header. */
FixedSizeArray(const FixedSizeArray<T, C> &src)
{
/* share block (header + items) with the source array */
data = src.data;
RefCnt()++; // now we share block with the source
}
/** destroy remaining items and free the memory block */
~FixedSizeArray()
{
/* release one reference to the shared block */
if ((--RefCnt()) > 0) return; // and return if there is still some owner
Clear();
/* free the memory block occupied by items */
free(((byte*)data) - HeaderSize);
data = nullptr;
}
/** Clear (destroy) all items */
inline void Clear()
{
/* Walk through all allocated items backward and destroy them
* Note: this->Length() can be zero. In that case data[this->Length() - 1] is evaluated unsigned
* on some compilers with some architectures. (e.g. gcc with x86) */
for (T *pItem = this->data + this->Length() - 1; pItem >= this->data; pItem--) {
pItem->~T();
}
/* number of items become zero */
SizeRef() = 0;
}
/** return number of used items */
inline uint Length() const
{
return Hdr().items;
}
/** return true if array is full */
inline bool IsFull() const
{
return Length() >= C;
}
/** return true if array is empty */
inline bool IsEmpty() const
{
return Length() <= 0;
}
/** add (allocate), but don't construct item */
inline T *Append()
{
assert(!IsFull());
return &data[SizeRef()++];
}
/** add and construct item using default constructor */
inline T *AppendC()
{
T *item = Append();
new(item)T;
return item;
}
/** return item by index (non-const version) */
inline T& operator[](uint index)
{
assert(index < Length());
return data[index];
}
/** return item by index (const version) */
inline const T& operator[](uint index) const
{
assert(index < Length());
return data[index];
}
};
#endif /* FIXEDSIZEARRAY_HPP */
|