Tramway SDK
stack.h
Go to the documentation of this file.
1// Tramway Drifting and Dungeon Exploration Simulator SDK Runtime
2
3#ifndef TRAM_SDK_TEMPLATES_STACK_H
4#define TRAM_SDK_TEMPLATES_STACK_H
5
6#include <string>
7#include <iostream>
8
9/* copy-pasting queue code might have not been the best idea. anyway, it would
10 * be nice to yeet all of the old names of methods an keep only the new aliases.
11 *
12 * the internal stuff should also be rewritten to be actually understandable
13 */
14
15namespace tram {
16 template <typename T>
17 class Stack{
18 protected:
19 // TODO: figure out what is going on in here???
20 std::string stackName; //name of queue for log messages etc.
21 uint64_t stackLength; //how many elements are in queue right now
22 uint64_t stackSize; //how many elements can be added to queue
23 T* first; //first element in queue
24 T* last; //one past last element in queue
27 public:
28 Stack(std::string name, uint64_t initialSize){
29 stackName = name;
30 stackSize = initialSize;
31 stackLength = 0;
32
33 char* newmemory = (char*)::operator new(initialSize * sizeof(T));
34 first = (T*)newmemory;
35 last = first;
37 lastend = (T*)newmemory + (initialSize * sizeof(T));
38 };
39 T* AddNew(){
41 std::cout << "Stack " << stackName << " out of space!" << std::endl;
42 return nullptr;
43 }
44
45 T* newobj = last;
46 new(newobj) T;
47 last++;
49 return newobj;
50 };
51 void Remove(){
52 if(last == firstend){
53 std::cout << "Stack " << stackName << " already empty!" << std::endl;
54 };
55 last--;
57 };
59 if (stackLength == 0)
60 return nullptr;
61 else
62 return last - 1;
63 };
64 uint64_t GetLength(){return stackLength;};
65 void Reset(){
66 stackLength = 0;
67 last = first;
68 }
69
70 T& top() { return *GetLastPtr(); }
71 void push(const T& value) { *AddNew() = value; }
72 T pop() { T value = top(); Remove(); return value; }
73 void reset() { Reset(); }
74 size_t size() {return GetLength(); }
75 };
76}
77
78#endif // TRAM_SDK_TEMPLATES_STACK_H
Definition: stack.h:17
T * first
Definition: stack.h:23
size_t size()
Definition: stack.h:74
T & top()
Definition: stack.h:70
uint64_t stackSize
Definition: stack.h:22
void Remove()
Definition: stack.h:51
void reset()
Definition: stack.h:73
void Reset()
Definition: stack.h:65
uint64_t GetLength()
Definition: stack.h:64
std::string stackName
Definition: stack.h:20
T * last
Definition: stack.h:24
T pop()
Definition: stack.h:72
T * GetLastPtr()
Definition: stack.h:58
T * lastend
Definition: stack.h:26
uint64_t stackLength
Definition: stack.h:21
T * firstend
Definition: stack.h:25
T * AddNew()
Definition: stack.h:39
Stack(std::string name, uint64_t initialSize)
Definition: stack.h:28
void push(const T &value)
Definition: stack.h:71
Serialization, i.e.