(file) Return to Stack.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  1 mike  1.10 //%/////////////////////////////////////////////////////////////////////////////
  2            //
  3            // Copyright (c) 2000, 2001 The Open group, BMC Software, Tivoli Systems, IBM
  4            //
  5            // Permission is hereby granted, free of charge, to any person obtaining a copy
  6            // of this software and associated documentation files (the "Software"), to 
  7            // deal in the Software without restriction, including without limitation the 
  8            // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
  9            // sell copies of the Software, and to permit persons to whom the Software is
 10            // furnished to do so, subject to the following conditions:
 11            // 
 12            // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN 
 13            // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 14            // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 15            // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
 16            // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
 17            // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
 18            // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 19            // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 20            //
 21            //==============================================================================
 22 mike  1.10 //
 23            // Author: Mike Brasher (mbrasher@bmc.com)
 24            //
 25            // Modified By:
 26            //
 27            //%/////////////////////////////////////////////////////////////////////////////
 28            
 29            #ifndef Pegasus_Stack_h
 30            #define Pegasus_Stack_h
 31            
 32            #include <Pegasus/Common/Config.h>
 33            #include <Pegasus/Common/Array.h>
 34            #include <Pegasus/Common/String.h>
 35            #include <Pegasus/Common/Exception.h>
 36            
 37            PEGASUS_NAMESPACE_BEGIN
 38            
 39            /** The Stack class provides a simple stack implementation.
 40                This class provides a stack implementation which is based on the Array<>
 41                template class.
 42            */
 43 mike  1.10 template<class T>
 44            class Stack
 45            {
 46            public:
 47            
 48 mike  1.10.2.1     /** Default constructor. */
 49 mike  1.10         Stack() { }
 50                
 51 mike  1.10.2.1     /** Copy constructor. */
 52 mike  1.10         Stack(const Stack<T>& x) : _rep(x._rep) { }
 53                
 54 mike  1.10.2.1     /** This constructor was added to provide a fast way of creating a stack
 55                	with a single element on it. This constructor is necessary to realize
 56                	the return-value compiler optimization which permits objects used in
 57                	return/constructor expressions to be initialized only once.
 58                
 59                	Notice that this constructor is explicit to avoid implicit 
 60                	initialization of a stack with the type of T.
 61                	which 
 62                    */
 63                    PEGASUS_EXPLICIT Stack(const T& x) { _rep.append(x); }
 64                
 65                    /** Destructor. */
 66 mike  1.10         ~Stack() { }
 67                
 68 mike  1.10.2.1     /** Assignment operator. */
 69 mike  1.10         Stack<T>& operator=(const Stack<T>& x) { _rep = x._rep; return *this; }
 70                
 71 mike  1.10.2.1     /** Returns size of stack. */
 72                    Uint32 size() const { return _rep.size(); }
 73                
 74                    /** Tests whether stack is empty. */
 75 mike  1.10         Boolean isEmpty() const { return _rep.size() == 0; }
 76                
 77 mike  1.10.2.1     /** Pushes entry onto the stack. */
 78 mike  1.10         void push(const T& x) { _rep.append(x); }
 79                
 80 mike  1.10.2.1     /** Returns reference to the top element on the stack.
 81                	@return reference to top element on stack.
 82                	@exception throws StackUnderflow if stack is empty.
 83 mike  1.10         */
 84                    T& top();
 85                
 86 mike  1.10.2.1     /** Const version of top() method.
 87 mike  1.10         */
 88                    const T& top() const { return ((Stack<T>*)this)->top(); }
 89                
 90 mike  1.10.2.1     /** Pops top entry from stack. */
 91 mike  1.10         void pop();
 92                
 93 mike  1.10.2.1     /** Provides indexing for stack. */
 94 mike  1.10         T& operator[](Uint32 i) { return _rep[i]; }
 95                
 96 mike  1.10.2.1     /** Const version of indxing operator. */
 97 mike  1.10         const T& operator[](Uint32 i) const { return _rep[i]; }
 98                
 99                private:
100                
101                    Array<T> _rep;
102                };
103                
104                template<class T>
105                T& Stack<T>::top()
106                {
107 mike  1.10.2.1     if (isEmpty())
108                	throw StackUnderflow();
109                
110                    return _rep[_rep.size() - 1];
111 mike  1.10     }
112                
113                template<class T>
114                void Stack<T>::pop()
115                {
116 mike  1.10.2.1     if (isEmpty())
117 mike  1.10     	throw StackUnderflow();
118                
119                    _rep.remove(_rep.size() - 1);
120                }
121                
122                PEGASUS_NAMESPACE_END
123                
124                #endif /* Pegasus_Stack_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2