12 #ifndef FIXEDSIZEARRAY_HPP 13 #define FIXEDSIZEARRAY_HPP 15 #include "../core/alloc_func.hpp" 23 template <
class T, u
int C>
34 static const uint
Tsize =
sizeof(T);
44 inline ArrayHeader&
Hdr()
46 return *(ArrayHeader*)(((byte*)data) -
HeaderSize);
50 inline const ArrayHeader&
Hdr()
const 52 return *(ArrayHeader*)(((byte*)data) -
HeaderSize);
72 assert_compile(C < (SIZE_MAX - HeaderSize) / Tsize);
75 data = (T*)((MallocT<byte>(HeaderSize + C * Tsize)) +
HeaderSize);
92 if ((--
RefCnt()) > 0)
return;
96 free(((byte*)data) - HeaderSize);
106 for (T *pItem = this->data + this->
Length() - 1; pItem >= this->
data; pItem--) {
bool IsEmpty() const
return true if array is empty
T * Append()
add (allocate), but don't construct item
FixedSizeArray()
Default constructor.
bool IsFull() const
return true if array is full
FixedSizeArray(const FixedSizeArray< T, C > &src)
Copy constructor.
ArrayHeader & Hdr()
return reference to the array header (non-const)
T & operator[](uint index)
return item by index (non-const version)
T * data
the only member of fixed size array is pointer to the block of C array of items.
static const uint Tsize
size of item
const ArrayHeader & Hdr() const
return reference to the array header (const)
~FixedSizeArray()
destroy remaining items and free the memory block
uint Length() const
return number of used items
fixed size array Upon construction it preallocates fixed size block of memory for all items...
void Clear()
Clear (destroy) all items.
static const uint HeaderSize
size of header
T * AppendC()
add and construct item using default constructor
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
uint & SizeRef()
return reference to number of used items
uint & RefCnt()
return reference to the block reference counter