1 karl 1.17 //%2004////////////////////////////////////////////////////////////////////////
|
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 mike 1.10 //
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
11 kumpf 1.12 // of this software and associated documentation files (the "Software"), to
12 // deal in the Software without restriction, including without limitation the
13 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
14 mike 1.10 // sell copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
16 //
|
17 kumpf 1.12 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
|
18 mike 1.10 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
19 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
20 kumpf 1.12 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
21 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
23 mike 1.10 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 //
26 //==============================================================================
27 //
28 // Author: Mike Brasher (mbrasher@bmc.com)
29 //
30 // Modified By:
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 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 */
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 @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 mike 1.11
|
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 throw StackUnderflow();
116
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 mike 1.10 throw StackUnderflow();
125
126 _rep.remove(_rep.size() - 1);
127 }
128
129 PEGASUS_NAMESPACE_END
130
131 #endif /* Pegasus_Stack_h */
|