Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5393 was 5388, checked in by bensch, 19 years ago

orxonox/trunk: implemented a t-Stack, for dynamic stacks, and integrated it into the Shell.

File size: 2.7 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<class 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    void debug() const ;
43
44  private:
45    //! One entry of the Stack
46    struct tStackEntry
47    {
48      T              value;          //!< The value of this Entry.
49      tStackEntry*   next;           //!< Pointer to the Next entry.
50    };
51
52
53    unsigned int     entryCount;      //!< The count of Entries in this Array.
54    tStackEntry*     topEntry;      //!< Pointer to the first Entry of this Array
55};
56
57/**
58 * creates and initializes a Stack
59 */
60template<class T>
61    tStack<T>::tStack()
62{
63  this->topEntry = NULL;
64  this->entryCount = 0;
65}
66
67/**
68 * delocates alocated memory from a Stack.
69 * This does not delete the entries of the Stack
70 */
71template<class T>
72    tStack<T>::~tStack()
73{
74  tStackEntry* delEntry;
75  while (this->topEntry != NULL)
76  {
77    delEntry = this->topEntry;
78    this->topEntry = topEntry->next;
79    delete delEntry;
80  }
81}
82
83
84/**
85 * pushes one Entry into the Stack.
86 * @param entry the Entry to push into the Stack
87 */
88template<class T>
89    void tStack<T>::push(T entry)
90{
91  tStackEntry* newEntry = new tStackEntry;
92  newEntry->value = entry;
93  newEntry->next = this->topEntry;
94  this->topEntry = newEntry;
95
96  this->entryCount++;
97}
98
99
100/**
101 * pops up the topmost enrty of the Stack, and moves the pointer to the next Stack-entry.
102 * @returns the top-most enrty.
103 */
104template<class T>
105    T tStack<T>::pop()
106{
107  if (this->topEntry == NULL)
108    return 0;
109
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;
116}
117
118/**
119 * @returns the topMost entry of the Stack
120 */
121template<class T>
122    T tStack<T>::getTop()
123{
124  if (this->topEntry != NULL)
125    return this->topEntry->value;
126  else
127    return NULL;
128}
129
130#endif /* _T_STACK_H */
Note: See TracBrowser for help on using the repository browser.