Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/t_stack.h @ 6612

Last change on this file since 6612 was 5466, checked in by bensch, 19 years ago

orxonox/trunk: some changes from class to typename in Templates

File size: 2.8 KB
Line 
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
16/*!
17  @file t_stack.h
18  @brief Contains the tStack Class that handles stacks of classes.
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
23#ifndef _T_STACK_H
24#define _T_STACK_H
25
26using namespace std;
27
28//! Stack Class that handles dynamic-type Stacks.
29template<typename T>
30    class tStack
31{
32  public:
33    tStack();
34    ~tStack();
35
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; };
41
42    // stack copying.
43    // tStack<T>& operator= (const tStack<T>& stack);
44
45    void debug() const ;
46
47  private:
48    //! One entry of the Stack
49    struct tStackEntry
50    {
51      T              value;          //!< The value of this Entry.
52      tStackEntry*   next;           //!< Pointer to the Next entry.
53    };
54
55
56    unsigned int     entryCount;      //!< The count of Entries in this Array.
57    tStackEntry*     topEntry;        //!< Pointer to the first Entry of this Array
58};
59
60/**
61 * creates and initializes a Stack
62 */
63template<typename T>
64    tStack<T>::tStack()
65{
66  this->topEntry = NULL;
67  this->entryCount = 0;
68}
69
70/**
71 * delocates alocated memory from a Stack.
72 * This does not delete the entries of the Stack
73 */
74template<typename T>
75    tStack<T>::~tStack()
76{
77  tStackEntry* delEntry;
78  while (this->topEntry != NULL)
79  {
80    delEntry = this->topEntry;
81    this->topEntry = topEntry->next;
82    delete delEntry;
83  }
84}
85
86
87/**
88 * pushes one Entry into the Stack.
89 * @param entry the Entry to push into the Stack
90 */
91template<typename T>
92    void tStack<T>::push(T entry)
93{
94  tStackEntry* newEntry = new tStackEntry;
95  newEntry->value = entry;
96  newEntry->next = this->topEntry;
97  this->topEntry = newEntry;
98
99  this->entryCount++;
100}
101
102
103/**
104 * pops up the topmost enrty of the Stack, and moves the pointer to the next Stack-entry.
105 * @returns the top-most enrty.
106 */
107template<typename T>
108    T tStack<T>::pop()
109{
110  if (this->topEntry == NULL)
111    return 0;
112
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;
119}
120
121/**
122 * @returns the topMost entry of the Stack without removing it.
123 */
124template<typename T>
125    T tStack<T>::getTop()
126{
127  if (this->topEntry != NULL)
128    return this->topEntry->value;
129  else
130    return NULL;
131}
132
133#endif /* _T_STACK_H */
Note: See TracBrowser for help on using the repository browser.