(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            //%/////////////////////////////////////////////////////////////////////////////
 33            
 34            #ifndef Pegasus_Stack_h
 35            #define Pegasus_Stack_h
 36            
 37            #include <Pegasus/Common/Config.h>
 38 kumpf 1.15 #include <Pegasus/Common/ArrayInternal.h>
 39 mike  1.10 #include <Pegasus/Common/String.h>
 40 kumpf 1.14 #include <Pegasus/Common/InternalException.h>
 41 mike  1.10 
 42            PEGASUS_NAMESPACE_BEGIN
 43            
 44            /** The Stack class provides a simple stack implementation.
 45                This class provides a stack implementation which is based on the Array<>
 46                template class.
 47            */
 48            template<class T>
 49            class Stack
 50            {
 51            public:
 52            
 53 mike  1.11     /** Default constructor. */
 54 mike  1.10     Stack() { }
 55            
 56 mike  1.11     /** Copy constructor. */
 57 mike  1.10     Stack(const Stack<T>& x) : _rep(x._rep) { }
 58            
 59 mike  1.11     /** This constructor was added to provide a fast way of creating a stack
 60 kumpf 1.20         with a single element on it. This constructor is necessary to realize
 61                    the return-value compiler optimization which permits objects used in
 62                    return/constructor expressions to be initialized only once.
 63            
 64                    Notice that this constructor is explicit to avoid implicit
 65                    initialization of a stack with the type of T.
 66                    which
 67 mike  1.11     */
 68                PEGASUS_EXPLICIT Stack(const T& x) { _rep.append(x); }
 69            
 70                /** Destructor. */
 71 mike  1.10     ~Stack() { }
 72            
 73 mike  1.11     /** Assignment operator. */
 74 mike  1.10     Stack<T>& operator=(const Stack<T>& x) { _rep = x._rep; return *this; }
 75            
 76 mike  1.11     /** Returns size of stack. */
 77                Uint32 size() const { return _rep.size(); }
 78            
 79                /** Tests whether stack is empty. */
 80 mike  1.10     Boolean isEmpty() const { return _rep.size() == 0; }
 81            
 82 mike  1.11     /** Pushes entry onto the stack. */
 83 mike  1.10     void push(const T& x) { _rep.append(x); }
 84            
 85 mike  1.11     /** Returns reference to the top element on the stack.
 86 kumpf 1.20         @return reference to top element on stack.
 87                    @exception throws StackUnderflow if stack is empty.
 88 mike  1.10     */
 89                T& top();
 90            
 91 mike  1.11     /** Const version of top() method.
 92 mike  1.10     */
 93                const T& top() const { return ((Stack<T>*)this)->top(); }
 94            
 95 mike  1.11     /** Pops top entry from stack. */
 96 mike  1.10     void pop();
 97            
 98 mike  1.11     /** Provides indexing for stack. */
 99 mike  1.10     T& operator[](Uint32 i) { return _rep[i]; }
100            
101 mike  1.11     /** Const version of indxing operator. */
102 mike  1.10     const T& operator[](Uint32 i) const { return _rep[i]; }
103 kumpf 1.20 
104 kumpf 1.13     void reserveCapacity(Uint32 capacity) { _rep.reserveCapacity(capacity); }
105 mike  1.10 
106            private:
107            
108                Array<T> _rep;
109            };
110            
111            template<class T>
112            T& Stack<T>::top()
113            {
114 mike  1.11     if (isEmpty())
115 kumpf 1.20         throw StackUnderflow();
116 mike  1.11 
117                return _rep[_rep.size() - 1];
118 mike  1.10 }
119            
120            template<class T>
121            void Stack<T>::pop()
122            {
123 mike  1.11     if (isEmpty())
124 kumpf 1.20         throw StackUnderflow();
125 mike  1.10 
126                _rep.remove(_rep.size() - 1);
127            }
128            
129            PEGASUS_NAMESPACE_END
130            
131            #endif /* Pegasus_Stack_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2