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

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2