True Axis Physics SDK 1.2.0.1 Beta Documentation
www.trueaxis.com

List.h

00001 //---------------------------------------------------------------------------------
00002 // File Name: List.h
00003 // Description: Singular Linked List Class.
00004 //              The Type must contain a default constructor.
00005 //              The list handles memory allocation of the items.
00006 //
00007 // Copyright (C) 2004 - 2006 True Axis Pty Ltd, Australia. 
00008 // All Rights Reserved.
00009 //
00010 // History:
00011 //      Created File.
00012 //---------------------------------------------------------------------------------
00013 
00014 #ifndef TA_LIST_H
00015 #define TA_LIST_H
00016 
00017 #ifndef TA_DEBUG_H
00018 #include "Debug.h"
00019 #endif // TA_DEBUG_H
00020 
00021 #ifndef TA_MEMORYMGR_H
00022 #include "MemoryMgr.h"
00023 #endif // TA_MEMORYMGR_H
00024 
00025 namespace TA
00026 {
00027 
00028 template <class Type>
00029 class TACOMMON_CLASS List
00030 {
00031 protected:
00032     struct Item;    
00033 
00034 public: 
00035     class Iterator
00036     {
00037     public:
00038         Iterator() { m_pItem = 0; }
00039         ~Iterator() { Finalise(); }
00040         void operator ++() { if (m_pItem) m_pItem = m_pItem->pNext; }
00041         Type& operator *() { return m_pItem->data; }
00042         bool AtEnd() { return m_pItem == 0; }
00043     private:
00044         friend class List;
00045         void Initialise(Item* pItem) { m_pItem = pItem; }
00046         void Finalise() { m_pItem = 0; }
00047         Item* m_pItem;
00048     };
00049     class ConstIterator
00050     {
00051     public:
00052         ConstIterator() { m_pItem = 0; }
00053         ~ConstIterator() { Finalise(); }
00054         void operator ++() { if (m_pItem) m_pItem = m_pItem->pNext; }
00055         const Type& operator *() { return m_pItem->data; }
00056         bool AtEnd() { return m_pItem == 0; }
00057     private:
00058         friend class List;
00059         void Initialise(Item* pItem) { m_pItem = pItem; }
00060         void Finalise() { m_pItem = 0; }
00061         const Item* m_pItem;
00062     };
00063 
00064     // todo: a const iterator
00065 
00066     List();
00067     ~List();
00068 
00069     // No need for Initialise
00070     void Finalise();
00071     void Clear();
00072 
00073     int GetSize() const; // slow as
00074     bool IsEmpty() const;
00075 
00076     // Array operator
00077     Type& operator [] (int nIndex);
00078     const Type& operator [] (int nIndex) const;
00079 
00080     Type& Append();
00081     void Append(const Type& value) { Append() = value; }
00082     void RemoveByValue(const Type& value);
00083     void Remove(const Type* pValue);
00084     Iterator GetIterator() { Iterator it; it.Initialise(m_pStart); return it; }
00085     ConstIterator GetConstIterator() const { ConstIterator it; it.Initialise(m_pStart); return it; }
00086     
00087 protected:
00088     struct Item
00089     {
00090         Type data; // Must be first in this structure.
00091         Item* pNext;
00092     };
00093     Item* m_pStart;
00094     Item* m_pEnd;
00095 };
00096 
00097 }
00098 
00099 #include "List.inl"
00100 
00101 #endif // TA_LIST_H


© Copyright 2004-2006 TRUE AXIS PTY LTD Australia. All rights reserved.