yapf/fixedsizearray.hpp
author bjarni
Thu, 05 Oct 2006 12:59:28 +0000
changeset 4739 e626ef1b522e
parent 4549 106ed18a7675
child 4988 879ba90441d1
permissions -rw-r--r--
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
This gives the ability to invalidate some window data and recalculate as needed instead of doing it for each WE_PAINT

This event is called right away when using InvalidateWindowData(), so it may be a good idea to set a bool or similar in the window
or similar and then act on that bool in WE_PAINT instead of doing a lot of stuff in WE_INVALIDATE_DATA as it might be called more than once before WE_PAINT is called

InvalidateWindowData() will not automatically repaint the window, so if you want to repaint it as well, you need to mark it dirty as well.

Made the depot windows use WE_INVALIDATE_DATA to set when to generate the engine and wagon lists instead of at each redraw
It makes no sense to regenerate the list when say using the scrollbar if we know that no vehicle have entered or left the list

NOTE: currently there is a piece of code to generate the list when it's not needed and compare it to the stored list and assert if they mismatch
This check is somewhat slow and kills the whole idea of WE_INVALIDATE_DATA, so it's a short lived one to verify that InvalidateWindowData() is used everywhere where it's needed
/* $Id$ */

#ifndef  FIXEDSIZEARRAY_HPP
#define  FIXEDSIZEARRAY_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 Titem_, int Tcapacity_>
struct CFixedSizeArrayT {
	/** 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(CHdr). */
	Titem_ *m_items;

	/** header for fixed size array */
	struct CHdr
	{
		int    m_num_items; ///< number of items in the array
		int    m_ref_cnt;   ///< block reference counter (used by copy constructor and by destructor)
	};

	// make types and constants visible from outside
	typedef Titem_ Titem; // type of array item

	ST_CONST(int, Tcapacity = Tcapacity_); // the array capacity (maximum size)
	ST_CONST(int, TitemSize = sizeof(Titem_)); // size of item
	ST_CONST(int, ThdrSize  = sizeof(CHdr)); // size of header

	/** Default constructor. Preallocate space for items and header, then initialize header. */
	CFixedSizeArrayT()
	{
		// allocate block for header + items (don't construct items)
		m_items = (Titem*)(((int8*)malloc(ThdrSize + Tcapacity * sizeof(Titem))) + ThdrSize);
		SizeRef() = 0; // initial number of items
		RefCnt() = 1; // initial reference counter
	}

	/** Copy constructor. Preallocate space for items and header, then initialize header. */
	CFixedSizeArrayT(const CFixedSizeArrayT<Titem_, Tcapacity_>& src)
	{
		// share block (header + items) with the source array
		m_items = const_cast<Titem*>(src.m_items); // here we break the 'const' modifier
		RefCnt()++; // now we share block with the source
	}

	/** destroy remaining items and free the memory block */
	~CFixedSizeArrayT()
	{
		// release one reference to the shared block
		if ((--RefCnt()) > 0) return; // and return if there is still some owner

		// walk through all allocated items backward and destroy them
		for (Titem* pItem = &m_items[Size() - 1]; pItem >= m_items; pItem--) {
			pItem->~Titem_();
		}
		free(((int8*)m_items) - ThdrSize);
		m_items = NULL;
	}

protected:
	/** return reference to the array header (non-const) */
	FORCEINLINE CHdr& Hdr() { return *(CHdr*)(((int8*)m_items) - ThdrSize); }
	/** return reference to the array header (const) */
	FORCEINLINE const CHdr& Hdr() const { return *(CHdr*)(((int8*)m_items) - ThdrSize); }
	/** return reference to the block reference counter */
	FORCEINLINE int& RefCnt() { return Hdr().m_ref_cnt; }
	/** return reference to number of used items */
	FORCEINLINE int& SizeRef() { return Hdr().m_num_items; }
public:
	/** return number of used items */
	FORCEINLINE int Size() const { return Hdr().m_num_items; }
	/** return true if array is full */
	FORCEINLINE bool IsFull() const { return Size() >= Tcapacity; };
	/** return true if array is empty */
	FORCEINLINE bool IsEmpty() const { return Size() <= 0; };
	/** index validation */
	FORCEINLINE void CheckIdx(int idx) const { assert(idx >= 0); assert(idx < Size()); }
	/** add (allocate), but don't construct item */
	FORCEINLINE Titem& AddNC() { assert(!IsFull()); return m_items[SizeRef()++]; }
	/** add and construct item using default constructor */
	FORCEINLINE Titem& Add() { Titem& item = AddNC(); new(&item)Titem; return item; }
	/** return item by index (non-const version) */
	FORCEINLINE Titem& operator [] (int idx) { CheckIdx(idx); return m_items[idx]; }
	/** return item by index (const version) */
	FORCEINLINE const Titem& operator [] (int idx) const { CheckIdx(idx); return m_items[idx]; }
};

#endif /* FIXEDSIZEARRAY_HPP */