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

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2