Tramway SDK
stack.h
Go to the documentation of this file.
1// TRAMWAY DRIFT AND DUNGEON EXPLORATION SIMULATOR 2022
2// All rights reserved.
3
4#ifndef TRAM_SDK_TEMPLATES_STACK_H
5#define TRAM_SDK_TEMPLATES_STACK_H
6
7#include <string>
8#include <iostream>
9
10namespace tram {
11 template <typename T>
12 class Stack{
13 protected:
14 // TODO: figure out what is going on in here???
15 std::string stackName; //name of queue for log messages etc.
16 uint64_t stackLength; //how many elements are in queue right now
17 uint64_t stackSize; //how many elements can be added to queue
18 T* first; //first element in queue
19 T* last; //one past last element in queue
22 public:
23 Stack(std::string name, uint64_t initialSize){
24 stackName = name;
25 stackSize = initialSize;
26 stackLength = 0;
27
28 char* newmemory = (char*)::operator new(initialSize * sizeof(T));
29 first = (T*)newmemory;
30 last = first;
32 lastend = (T*)newmemory + (initialSize * sizeof(T));
33 };
34 T* AddNew(){
36 std::cout << "Stack " << stackName << " out of space!" << std::endl;
37 return nullptr;
38 }
39
40 T* newobj = last;
41 new(newobj) T;
42 last++;
44 return newobj;
45 };
46 void Remove(){
47 if(last == firstend){
48 std::cout << "Stack " << stackName << " already empty!" << std::endl;
49 };
50 last--;
52 };
54 if (stackLength == 0)
55 return nullptr;
56 else
57 return last - 1;
58 };
59 uint64_t GetLength(){return stackLength;};
60 void Reset(){
61 stackLength = 0;
62 last = first;
63 }
64 T& top() { return *GetLastPtr(); }
65 };
66}
67
68#endif // TRAM_SDK_TEMPLATES_STACK_H
Definition: stack.h:12
T * first
Definition: stack.h:18
T & top()
Definition: stack.h:64
uint64_t stackSize
Definition: stack.h:17
void Remove()
Definition: stack.h:46
void Reset()
Definition: stack.h:60
uint64_t GetLength()
Definition: stack.h:59
std::string stackName
Definition: stack.h:15
T * last
Definition: stack.h:19
T * GetLastPtr()
Definition: stack.h:53
T * lastend
Definition: stack.h:21
uint64_t stackLength
Definition: stack.h:16
T * firstend
Definition: stack.h:20
T * AddNew()
Definition: stack.h:34
Stack(std::string name, uint64_t initialSize)
Definition: stack.h:23
Definition: api.h:9