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

CATQueue.h

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

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