Tramway SDK
painis
tram-sdk
src
templates
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
10
namespace
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
20
T*
firstend
;
21
T*
lastend
;
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
;
31
firstend
=
first
;
32
lastend
= (T*)newmemory + (initialSize *
sizeof
(T));
33
};
34
T*
AddNew
(){
35
if
(
stackLength
==
stackSize
){
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
++;
43
stackLength
++;
44
return
newobj;
45
};
46
void
Remove
(){
47
if
(
last
==
firstend
){
48
std::cout <<
"Stack "
<<
stackName
<<
" already empty!"
<< std::endl;
49
};
50
last
--;
51
stackLength
--;
52
};
53
T*
GetLastPtr
(){
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
tram::Stack
Definition:
stack.h:12
tram::Stack::first
T * first
Definition:
stack.h:18
tram::Stack::top
T & top()
Definition:
stack.h:64
tram::Stack::stackSize
uint64_t stackSize
Definition:
stack.h:17
tram::Stack::Remove
void Remove()
Definition:
stack.h:46
tram::Stack::Reset
void Reset()
Definition:
stack.h:60
tram::Stack::GetLength
uint64_t GetLength()
Definition:
stack.h:59
tram::Stack::stackName
std::string stackName
Definition:
stack.h:15
tram::Stack::last
T * last
Definition:
stack.h:19
tram::Stack::GetLastPtr
T * GetLastPtr()
Definition:
stack.h:53
tram::Stack::lastend
T * lastend
Definition:
stack.h:21
tram::Stack::stackLength
uint64_t stackLength
Definition:
stack.h:16
tram::Stack::firstend
T * firstend
Definition:
stack.h:20
tram::Stack::AddNew
T * AddNew()
Definition:
stack.h:34
tram::Stack::Stack
Stack(std::string name, uint64_t initialSize)
Definition:
stack.h:23
tram
Definition:
api.h:9
Generated by
1.9.2