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

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2