| [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. | 
|---|
| [5466] | 29 | template<typename 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 |  | 
|---|
| [5466] | 42 |     // stack copying. | 
|---|
 | 43 |     // tStack<T>& operator= (const tStack<T>& stack); | 
|---|
 | 44 |  | 
|---|
| [4746] | 45 |     void debug() const ; | 
|---|
| [4579] | 46 |  | 
|---|
 | 47 |   private: | 
|---|
| [5388] | 48 |     //! One entry of the Stack | 
|---|
 | 49 |     struct tStackEntry | 
|---|
| [4579] | 50 |     { | 
|---|
| [5388] | 51 |       T              value;          //!< The value of this Entry. | 
|---|
 | 52 |       tStackEntry*   next;           //!< Pointer to the Next entry. | 
|---|
| [4579] | 53 |     }; | 
|---|
 | 54 |  | 
|---|
| [5388] | 55 |  | 
|---|
 | 56 |     unsigned int     entryCount;      //!< The count of Entries in this Array. | 
|---|
| [5466] | 57 |     tStackEntry*     topEntry;        //!< Pointer to the first Entry of this Array | 
|---|
| [4579] | 58 | }; | 
|---|
 | 59 |  | 
|---|
 | 60 | /** | 
|---|
| [5388] | 61 |  * creates and initializes a Stack | 
|---|
 | 62 |  */ | 
|---|
| [5466] | 63 | template<typename T> | 
|---|
| [5388] | 64 |     tStack<T>::tStack() | 
|---|
| [4579] | 65 | { | 
|---|
| [5388] | 66 |   this->topEntry = NULL; | 
|---|
 | 67 |   this->entryCount = 0; | 
|---|
| [4579] | 68 | } | 
|---|
 | 69 |  | 
|---|
 | 70 | /** | 
|---|
| [5388] | 71 |  * delocates alocated memory from a Stack. | 
|---|
 | 72 |  * This does not delete the entries of the Stack | 
|---|
 | 73 |  */ | 
|---|
| [5466] | 74 | template<typename T> | 
|---|
| [5388] | 75 |     tStack<T>::~tStack() | 
|---|
| [4579] | 76 | { | 
|---|
| [5388] | 77 |   tStackEntry* delEntry; | 
|---|
 | 78 |   while (this->topEntry != NULL) | 
|---|
| [2807] | 79 |   { | 
|---|
| [5388] | 80 |     delEntry = this->topEntry; | 
|---|
 | 81 |     this->topEntry = topEntry->next; | 
|---|
 | 82 |     delete delEntry; | 
|---|
| [4579] | 83 |   } | 
|---|
 | 84 | } | 
|---|
| [2807] | 85 |  | 
|---|
| [4577] | 86 |  | 
|---|
| [4579] | 87 | /** | 
|---|
| [5388] | 88 |  * pushes one Entry into the Stack. | 
|---|
 | 89 |  * @param entry the Entry to push into the Stack | 
|---|
 | 90 |  */ | 
|---|
| [5466] | 91 | template<typename T> | 
|---|
| [5388] | 92 |     void tStack<T>::push(T entry) | 
|---|
| [4579] | 93 | { | 
|---|
| [5388] | 94 |   tStackEntry* newEntry = new tStackEntry; | 
|---|
 | 95 |   newEntry->value = entry; | 
|---|
 | 96 |   newEntry->next = this->topEntry; | 
|---|
 | 97 |   this->topEntry = newEntry; | 
|---|
| [4577] | 98 |  | 
|---|
| [5388] | 99 |   this->entryCount++; | 
|---|
| [4579] | 100 | } | 
|---|
| [2776] | 101 |  | 
|---|
| [4579] | 102 |  | 
|---|
| [4793] | 103 | /** | 
|---|
| [5388] | 104 |  * pops up the topmost enrty of the Stack, and moves the pointer to the next Stack-entry. | 
|---|
 | 105 |  * @returns the top-most enrty. | 
|---|
| [4793] | 106 |  */ | 
|---|
| [5466] | 107 | template<typename T> | 
|---|
| [5388] | 108 |     T tStack<T>::pop() | 
|---|
| [4791] | 109 | { | 
|---|
| [5388] | 110 |   if (this->topEntry == NULL) | 
|---|
 | 111 |     return 0; | 
|---|
| [4791] | 112 |  | 
|---|
| [5388] | 113 |   tStackEntry* retEntry = this->topEntry; | 
|---|
 | 114 |   T retVal = retEntry->value; | 
|---|
 | 115 |   this->topEntry = this->topEntry->next; | 
|---|
 | 116 |   delete retEntry; | 
|---|
 | 117 |   this->entryCount--; | 
|---|
 | 118 |   return retVal; | 
|---|
| [4791] | 119 | } | 
|---|
 | 120 |  | 
|---|
| [4579] | 121 | /** | 
|---|
| [5466] | 122 |  * @returns the topMost entry of the Stack without removing it. | 
|---|
| [5388] | 123 |  */ | 
|---|
| [5466] | 124 | template<typename T> | 
|---|
| [5388] | 125 |     T tStack<T>::getTop() | 
|---|
| [4579] | 126 | { | 
|---|
| [5388] | 127 |   if (this->topEntry != NULL) | 
|---|
 | 128 |     return this->topEntry->value; | 
|---|
 | 129 |   else | 
|---|
 | 130 |     return NULL; | 
|---|
| [4579] | 131 | } | 
|---|
| [4791] | 132 |  | 
|---|
| [5388] | 133 | #endif /* _T_STACK_H */ | 
|---|