KUDr@3900: /* $Id$ */ KUDr@3900: KUDr@3900: #ifndef FIXEDSIZEARRAY_HPP KUDr@3900: #define FIXEDSIZEARRAY_HPP KUDr@3900: KUDr@3900: KUDr@3900: /** fixed size array rubidium@4549: * Upon construction it preallocates fixed size block of memory rubidium@4549: * for all items, but doesn't construct them. Item's construction rubidium@4549: * is delayed. */ KUDr@3900: template KUDr@3900: struct CFixedSizeArrayT { KUDr@3900: /** the only member of fixed size array is pointer to the block rubidium@4549: * of C array of items. Header can be found on the offset -sizeof(CHdr). */ KUDr@3900: Titem_ *m_items; KUDr@3900: KUDr@3900: /** header for fixed size array */ KUDr@3900: struct CHdr KUDr@3900: { KUDr@3900: int m_num_items; ///< number of items in the array KUDr@3900: int m_ref_cnt; ///< block reference counter (used by copy constructor and by destructor) KUDr@3900: }; KUDr@3900: KUDr@3900: // make types and constants visible from outside KUDr@3900: typedef Titem_ Titem; // type of array item KUDr@3900: KUDr@3900: ST_CONST(int, Tcapacity = Tcapacity_); // the array capacity (maximum size) KUDr@3900: ST_CONST(int, TitemSize = sizeof(Titem_)); // size of item KUDr@3900: ST_CONST(int, ThdrSize = sizeof(CHdr)); // size of header KUDr@3900: KUDr@3900: /** Default constructor. Preallocate space for items and header, then initialize header. */ KUDr@3900: CFixedSizeArrayT() KUDr@3900: { KUDr@3900: // allocate block for header + items (don't construct items) KUDr@3900: m_items = (Titem*)(((int8*)malloc(ThdrSize + Tcapacity * sizeof(Titem))) + ThdrSize); KUDr@3900: SizeRef() = 0; // initial number of items KUDr@3900: RefCnt() = 1; // initial reference counter KUDr@3900: } KUDr@3900: KUDr@3900: /** Copy constructor. Preallocate space for items and header, then initialize header. */ KUDr@3900: CFixedSizeArrayT(const CFixedSizeArrayT& src) KUDr@3900: { KUDr@3900: // share block (header + items) with the source array KUDr@3900: m_items = const_cast(src.m_items); // here we break the 'const' modifier KUDr@3900: RefCnt()++; // now we share block with the source KUDr@3900: } KUDr@3900: KUDr@3900: /** destroy remaining items and free the memory block */ KUDr@3900: ~CFixedSizeArrayT() KUDr@3900: { KUDr@3900: // release one reference to the shared block KUDr@3900: if ((--RefCnt()) > 0) return; // and return if there is still some owner KUDr@3900: KUDr@3900: // walk through all allocated items backward and destroy them KUDr@3900: for (Titem* pItem = &m_items[Size() - 1]; pItem >= m_items; pItem--) { KUDr@3900: pItem->~Titem_(); KUDr@3900: } KUDr@3900: free(((int8*)m_items) - ThdrSize); KUDr@3900: m_items = NULL; KUDr@3900: } KUDr@3900: KUDr@3900: protected: KUDr@3900: /** return reference to the array header (non-const) */ KUDr@3900: FORCEINLINE CHdr& Hdr() { return *(CHdr*)(((int8*)m_items) - ThdrSize); } KUDr@3900: /** return reference to the array header (const) */ KUDr@3900: FORCEINLINE const CHdr& Hdr() const { return *(CHdr*)(((int8*)m_items) - ThdrSize); } KUDr@3900: /** return reference to the block reference counter */ KUDr@3900: FORCEINLINE int& RefCnt() { return Hdr().m_ref_cnt; } KUDr@3900: /** return reference to number of used items */ KUDr@3900: FORCEINLINE int& SizeRef() { return Hdr().m_num_items; } KUDr@3900: public: KUDr@3900: /** return number of used items */ KUDr@3900: FORCEINLINE int Size() const { return Hdr().m_num_items; } KUDr@3900: /** return true if array is full */ KUDr@3900: FORCEINLINE bool IsFull() const { return Size() >= Tcapacity; }; KUDr@3900: /** return true if array is empty */ KUDr@3900: FORCEINLINE bool IsEmpty() const { return Size() <= 0; }; KUDr@3900: /** index validation */ KUDr@3900: FORCEINLINE void CheckIdx(int idx) const { assert(idx >= 0); assert(idx < Size()); } KUDr@3900: /** add (allocate), but don't construct item */ KUDr@3900: FORCEINLINE Titem& AddNC() { assert(!IsFull()); return m_items[SizeRef()++]; } KUDr@3900: /** add and construct item using default constructor */ KUDr@3900: FORCEINLINE Titem& Add() { Titem& item = AddNC(); new(&item)Titem; return item; } KUDr@3900: /** return item by index (non-const version) */ KUDr@3900: FORCEINLINE Titem& operator [] (int idx) { CheckIdx(idx); return m_items[idx]; } KUDr@3900: /** return item by index (const version) */ KUDr@3900: FORCEINLINE const Titem& operator [] (int idx) const { CheckIdx(idx); return m_items[idx]; } KUDr@3900: }; KUDr@3900: KUDr@3900: #endif /* FIXEDSIZEARRAY_HPP */