gml_Stack.h

Go to the documentation of this file.
00001 /** @file gml_Stack.h
00002  * 
00003  *    Define the gml_TStack template and the gml_TPointerStack instance.
00004  * 
00005  *   Copyright (c) 2004 CLIPS-IMAG
00006  * 
00007  *   See the file "gml_LicenseTerms.txt" for information on usage and redistribution
00008  *   of this file, and for a DISCLAIMER OF ALL WARRANTIES.
00009  * 
00010  *   Created in July 2004 (JL).
00011  */
00012 
00013 #ifndef __GML_STACK__
00014 #define __GML_STACK__
00015 
00016 #include "gml/base/gml_Array.h"
00017 #include "gml/base/gml_Types.h"
00018 #include "gml/base/gml_Errors.h"
00019 
00020 
00021 /**
00022  *  gml_TStack_Base --
00023  *    Common data to all gml_TStack instances.
00024  * 
00025  */
00026 class gml_TStack_Base
00027 {
00028 public:
00029 
00030   // errors
00031   static gml_TError sErrorStackFull;    ///< stack is full (occurs on Push)
00032   static gml_TError sErrorStackEmpty;   ///< stack is empty (occurs on Pop)
00033   
00034   // constants
00035   static UInt32 const sDefaultStackSize = 64;
00036 
00037 protected:
00038 
00039   // setup
00040   static void ClassInit ();     ///< class setup
00041   static bool sModuleInited;    ///< was ClassInit() called ? 
00042 };
00043 
00044 
00045 
00046 /**
00047  *  gml_TStack --
00048  *    A template for fixed-length, typed stack (last in, first out) containers.
00049  * 
00050  */
00051 template <typename T>
00052 class gml_TStack : public gml_TStack_Base, public gml_TArray<T>
00053 {
00054   public:  
00055 
00056 
00057     /// Init --
00058     ///
00059     ///   Allocate resources for a stack of size <stackSize>.
00060 
00061     gml_TError Init (UInt32 stackSize = sDefaultStackSize);
00062 
00063     
00064     /// Dispose --
00065     ///
00066     ///   Release resources
00067 
00068     void Dispose ();
00069 
00070     
00071     /// Push -- 
00072     ///
00073     ///   Push an object into the stack.
00074     ///   Will fail on memory exhaustion.
00075 
00076     gml_TError Push (const T object);
00077 
00078     
00079     /// Pop --
00080     ///
00081     ///   Remove the oldest object from the stack and return it in <object>.
00082     ///   Will fail if the stack is empty.
00083 
00084     gml_TError Pop  (T & object);
00085     
00086 
00087     /// Height --
00088     ///
00089     ///   Return the current stack length (number of items).
00090 
00091     unsigned Height () { return fStackHeight; }
00092     
00093   private:
00094   
00095     UInt32 fStackHeight;   ///< current number of items in stack
00096 };
00097 
00098 
00099 /**
00100  *  gml_TPointerStack --
00101  *    Stacks of pointers, to be used for untyped / polymorphic stacks.
00102  * 
00103  */
00104 typedef gml_TStack <gml_TPointer> gml_TPointerStack;
00105 
00106 
00107 
00108 
00109 template <typename T>
00110 gml_TError gml_TStack<T>::Init (UInt32 stackSize)
00111 {
00112   gml_TError err;
00113   
00114   ClassInit ();
00115   
00116   err = gml_TArray<T>::Init (stackSize);
00117   if (err != gml_cNoError) return err;
00118   
00119   fStackHeight = 0;
00120   return gml_cNoError;
00121 }
00122 
00123 template <typename T>
00124 void gml_TStack<T>::Dispose ()
00125 {
00126   fStackHeight = 0;
00127   gml_TArray<T>::Dispose ();
00128 }
00129 
00130 template <typename T>
00131 gml_TError gml_TStack<T>::Push (const T object)
00132 {
00133   if (fStackHeight > this->Size ())
00134     return sErrorStackFull;
00135     
00136   (*this)[fStackHeight] = object;
00137   fStackHeight += 1;
00138   return gml_cNoError;
00139 }
00140 
00141 template <typename T>
00142 gml_TError gml_TStack<T>::Pop (T & object)
00143 {
00144   if (fStackHeight == 0)
00145     return sErrorStackEmpty;
00146     
00147   object = (*this)[fStackHeight-1];
00148   fStackHeight -= 1;
00149   return gml_cNoError;
00150 }
00151 
00152 #endif /* __GML_STACK__ */
00153 
Generated on Tue Jun 12 14:03:27 2007 for gml by Doxygen 1.5.2.