[4579] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Benjamin Grauer |
---|
| 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
[2842] | 16 | /*! |
---|
[5388] | 17 | @file t_stack.h |
---|
| 18 | @brief Contains the tStack Class that handles stacks of classes. |
---|
[2842] | 19 | this class creates a Array of a semi-Dynamic length. |
---|
| 20 | beware, that after finalizing the array may not be resized again. |
---|
| 21 | */ |
---|
| 22 | |
---|
[5388] | 23 | #ifndef _T_STACK_H |
---|
| 24 | #define _T_STACK_H |
---|
[2776] | 25 | |
---|
[5388] | 26 | using namespace std; |
---|
| 27 | |
---|
| 28 | //! Stack Class that handles dynamic-type Stacks. |
---|
| 29 | template<class T> |
---|
| 30 | class tStack |
---|
[2754] | 31 | { |
---|
[4579] | 32 | public: |
---|
[5388] | 33 | tStack(); |
---|
| 34 | ~tStack(); |
---|
[2754] | 35 | |
---|
[5388] | 36 | void push(T entry); |
---|
| 37 | T pop(); |
---|
| 38 | T getTop(); |
---|
| 39 | /** @returns the Size of the Stack (0 if empty) */ |
---|
| 40 | unsigned int getSize() { return this->entryCount; }; |
---|
[4577] | 41 | |
---|
[4746] | 42 | void debug() const ; |
---|
[4579] | 43 | |
---|
| 44 | private: |
---|
[5388] | 45 | //! One entry of the Stack |
---|
| 46 | struct tStackEntry |
---|
[4579] | 47 | { |
---|
[5388] | 48 | T value; //!< The value of this Entry. |
---|
| 49 | tStackEntry* next; //!< Pointer to the Next entry. |
---|
[4579] | 50 | }; |
---|
| 51 | |
---|
[5388] | 52 | |
---|
| 53 | unsigned int entryCount; //!< The count of Entries in this Array. |
---|
| 54 | tStackEntry* topEntry; //!< Pointer to the first Entry of this Array |
---|
[4579] | 55 | }; |
---|
| 56 | |
---|
| 57 | /** |
---|
[5388] | 58 | * creates and initializes a Stack |
---|
| 59 | */ |
---|
[4579] | 60 | template<class T> |
---|
[5388] | 61 | tStack<T>::tStack() |
---|
[4579] | 62 | { |
---|
[5388] | 63 | this->topEntry = NULL; |
---|
| 64 | this->entryCount = 0; |
---|
[4579] | 65 | } |
---|
| 66 | |
---|
| 67 | /** |
---|
[5388] | 68 | * delocates alocated memory from a Stack. |
---|
| 69 | * This does not delete the entries of the Stack |
---|
| 70 | */ |
---|
[4579] | 71 | template<class T> |
---|
[5388] | 72 | tStack<T>::~tStack() |
---|
[4579] | 73 | { |
---|
[5388] | 74 | tStackEntry* delEntry; |
---|
| 75 | while (this->topEntry != NULL) |
---|
[2807] | 76 | { |
---|
[5388] | 77 | delEntry = this->topEntry; |
---|
| 78 | this->topEntry = topEntry->next; |
---|
| 79 | delete delEntry; |
---|
[4579] | 80 | } |
---|
| 81 | } |
---|
[2807] | 82 | |
---|
[4577] | 83 | |
---|
[4579] | 84 | /** |
---|
[5388] | 85 | * pushes one Entry into the Stack. |
---|
| 86 | * @param entry the Entry to push into the Stack |
---|
| 87 | */ |
---|
[4579] | 88 | template<class T> |
---|
[5388] | 89 | void tStack<T>::push(T entry) |
---|
[4579] | 90 | { |
---|
[5388] | 91 | tStackEntry* newEntry = new tStackEntry; |
---|
| 92 | newEntry->value = entry; |
---|
| 93 | newEntry->next = this->topEntry; |
---|
| 94 | this->topEntry = newEntry; |
---|
[4577] | 95 | |
---|
[5388] | 96 | this->entryCount++; |
---|
[4579] | 97 | } |
---|
[2776] | 98 | |
---|
[4579] | 99 | |
---|
[4793] | 100 | /** |
---|
[5388] | 101 | * pops up the topmost enrty of the Stack, and moves the pointer to the next Stack-entry. |
---|
| 102 | * @returns the top-most enrty. |
---|
[4793] | 103 | */ |
---|
[4791] | 104 | template<class T> |
---|
[5388] | 105 | T tStack<T>::pop() |
---|
[4791] | 106 | { |
---|
[5388] | 107 | if (this->topEntry == NULL) |
---|
| 108 | return 0; |
---|
[4791] | 109 | |
---|
[5388] | 110 | tStackEntry* retEntry = this->topEntry; |
---|
| 111 | T retVal = retEntry->value; |
---|
| 112 | this->topEntry = this->topEntry->next; |
---|
| 113 | delete retEntry; |
---|
| 114 | this->entryCount--; |
---|
| 115 | return retVal; |
---|
[4791] | 116 | } |
---|
| 117 | |
---|
[4579] | 118 | /** |
---|
[5388] | 119 | * @returns the topMost entry of the Stack |
---|
| 120 | */ |
---|
[4579] | 121 | template<class T> |
---|
[5388] | 122 | T tStack<T>::getTop() |
---|
[4579] | 123 | { |
---|
[5388] | 124 | if (this->topEntry != NULL) |
---|
| 125 | return this->topEntry->value; |
---|
| 126 | else |
---|
| 127 | return NULL; |
---|
[4579] | 128 | } |
---|
[4791] | 129 | |
---|
[5388] | 130 | #endif /* _T_STACK_H */ |
---|