Game Accessibility Library logo SourceForge.net Logo
Game Accessibility Suite: CAT/CATStack.h Source File

CATStack.h

Go to the documentation of this file.
00001 /// \file CATStack.h
00002 /// \brief Defines a templated stack
00003 /// \ingroup CAT
00004 /// 
00005 /// Copyright (c) 2003-2008 by Michael Ellison.
00006 /// See COPYING.txt for the \ref gaslicense License (MIT License).
00007 ///
00008 // $Author: mikeellison $
00009 // $Date: 2008-01-21 08:33:12 -0600 (Mon, 21 Jan 2008) $
00010 // $Revision:   $
00011 // $NoKeywords: $
00012 #ifndef _CATStack_H_
00013 #define _CATStack_H_
00014 
00015 #include "CATInternal.h"
00016 
00017 /// \class CATStack CATStack.h
00018 /// \brief Defines a templated stack
00019 /// \ingroup CAT
00020 ///
00021 /// CATStack does not take any responsibility
00022 /// for object deletion.  It is just a simple object stack.
00023 ///
00024 template<class T>
00025 class CATStack
00026 {
00027    public: 
00028       /// Enumeration callback for objects
00029       /// \sa Enumerate
00030       typedef void (*CATSTACKENUMCB)(T& object, void* userParam);
00031 
00032    protected:
00033       
00034       /// \class Node
00035       /// \brief Protected node class for CATStack
00036       /// 
00037       template<class T>
00038       class Node
00039       {
00040          friend CATStack<T>;
00041          protected:              
00042             Node(T& data)
00043             {
00044                fData    = data;
00045                fNext    = 0;
00046             }
00047 
00048             // Node destructor - when a node is destroyed,
00049             // it may destroy the data it contains if the list
00050             // owns it.
00051             virtual ~Node()
00052             {
00053             }
00054 
00055          private:
00056             T              fData;   // Data ptr
00057             Node<T>* fNext;   // Ptr to next node
00058       };
00059          
00060 
00061    public:
00062       /// CATStack constructor      
00063       CATStack()
00064       {
00065          fHead = 0;
00066          fSize = 0;
00067       }
00068 
00069       /// CATStack copy constructor
00070       CATStack(const CATStack& srcStack)
00071       {
00072          CATUInt32 size = srcStack.Size();
00073          
00074          Node<T>* curNode = srcStack.fHead;
00075          while (curNode)
00076          {
00077             this->Push(curNode->fData);
00078             curNode = curNode->fNext;
00079          }
00080       }
00081       
00082       /// CATStack virtual destructor
00083       virtual ~CATStack()
00084       {
00085          Clear();
00086       }
00087 
00088       /// Clear() clears the stack.
00089       ///
00090       /// This will cause a leak if the objects
00091       /// pushed onto the stack are *'s that need deletion.
00092       void Clear()
00093       {
00094          T object;
00095          while (CATSUCCEEDED(this->Pop(object)));
00096       }
00097 
00098       /// Operator= override for copying stacks around.
00099       CATStack& operator=(const CATStack& srcStack)
00100       {
00101          // Check for self assignment
00102          if (&srcStack == this)
00103             return *this;
00104 
00105          int size = srcStack.Size();
00106          
00107          Node<T>* curNode = srcStack.fHead;
00108          while (curNode)
00109          {
00110             this->Push(curNode->fData);
00111             curNode = curNode->fNext;
00112          }
00113 
00114          return *this;
00115       }
00116             
00117       /// Push() inserts an object at the head of the stack.
00118       ///
00119       /// \param object - Object to be placed in the list
00120       ///
00121       /// \return CATResult - CAT_SUCCESS on success.
00122       /// \sa Pop()
00123       /// 
00124       CATResult Push(T& object)
00125       {
00126          Node<T>* node = 0;
00127          try
00128          {
00129             node = new Node<T>(object);
00130          }
00131          catch(...)
00132          {
00133             // We aren't throwing exceptions from within the Node
00134             // constructor, so the only exception here that we might
00135             // expect would be one from a new handler.  This is a compiler
00136             // specific issue though - it might throw, it might just return
00137             // 0.  We'll catch it either way in the next check.
00138             node = 0;
00139          }
00140 
00141          if (node == 0)
00142          {
00143             return CATRESULT(CAT_ERR_OUT_OF_MEMORY);
00144          }                  
00145          
00146          node->fNext = fHead;
00147          fHead = node;      
00148          fSize++;
00149          return CATRESULT(CAT_SUCCESS);
00150       }
00151 
00152       /// Pop() pops the next object off of the stack.
00153       ///
00154       /// \param object - pointer to receive the object pointer
00155       /// 
00156       /// \return CATResult - CAT_SUCCESS on success.
00157       /// \sa Push(), Size()
00158       /// 
00159       CATResult Pop(T& object)
00160       {
00161          if (fSize == 0)
00162          {
00163             //return CATRESULTDESC(CAT_ERR_STACK_EMPTY,"Stack is empty");
00164             return CATRESULT(CAT_ERR_STACK_EMPTY);
00165          }
00166          Node<T>* node = fHead;
00167          object = node->fData;
00168          fHead =  node->fNext;         
00169          delete node;
00170          fSize--;
00171                                        
00172          return CATRESULT(CAT_SUCCESS);
00173       }
00174 
00175       /// Size() returns the number of items in the list.
00176       CATUInt32  Size() const
00177       {
00178          return fSize;
00179       }
00180 
00181       /// Enumerate() calls the specified callback once for each
00182       /// item on the stack.
00183       ///
00184       /// The type of callback must match the specified list type.
00185       ///
00186       /// \param enumCallback - Called for each list item in order
00187       /// \param userParam - user defined parameter, passed back in callback..      
00188       void Enumerate(CATSTACKENUMCB enumCallback, void* userParam)
00189       {
00190          CATASSERT(enumCallback != 0, "Callback must be valid.");
00191          
00192          if (!enumCallback)
00193            return;
00194 
00195          Node<T>* curNode = fHead;         
00196          while (curNode != 0)
00197          {
00198             enumCallback(curNode->fData,userParam);
00199             curNode = curNode->fNext;          
00200          }
00201       }
00202 
00203 
00204    private:
00205       Node<T>*    fHead;   // Head ptr - null if empty
00206       CATUInt32     fSize;
00207 };    
00208 
00209 
00210 #endif // _CATStack_H_

Generated on Mon Feb 11 04:09:47 2008 for Game Accessibility Suite by doxygen 1.5.4