Tramway SDK
stackpool.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_STACKPOOL_H
5#define TRAM_SDK_TEMPLATES_STACKPOOL_H
6
7#include <string>
8#include <iostream>
9
10namespace tram {
11
12template <typename T>
13class StackPool {
14public:
15 StackPool(std::string name, size_t initialSize) {
16 poolName = name;
17 maxSize = initialSize;
18 poolSize = 0;
19
20 first = static_cast<T*>(::operator new(initialSize * sizeof(T)));
21 last = first;
22 }
23
24 StackPool(std::string name, size_t max_size, std::initializer_list<T> list) : StackPool(name, max_size) {
25 for (const auto& entry : list) {
26 *AddNew(1) = entry; // not good, but works
27 }
28 }
29
30 T* AddNew(size_t units) {
31 if (poolSize + units > maxSize) {
32 std::cout << "StackPool " << poolName << " out of space!" << std::endl;
33 return nullptr;
34 }
35
36 T* newobj;
37
38 newobj = last;
39 poolSize += units;
40 last += units;
41
42 return newobj;
43 }
44
45 void Reset() {
46 //return;
47 poolSize = 0;
48 last = first;
49 }
50
51 size_t size() { return poolSize; }
52 T* begin() { return first; }
53 T* end() { return last; }
54
55protected:
56 std::string poolName;
57 size_t poolSize;
58 size_t maxSize;
60 T* last;
61};
62
63}
64
65#endif // TRAM_SDK_TEMPLATES_STACKPOOL_H
Definition: stackpool.h:13
StackPool(std::string name, size_t initialSize)
Definition: stackpool.h:15
T * last
Definition: stackpool.h:60
size_t maxSize
Definition: stackpool.h:58
T * begin()
Definition: stackpool.h:52
void Reset()
Definition: stackpool.h:45
T * AddNew(size_t units)
Definition: stackpool.h:30
size_t poolSize
Definition: stackpool.h:57
size_t size()
Definition: stackpool.h:51
T * first
Definition: stackpool.h:59
T * end()
Definition: stackpool.h:53
StackPool(std::string name, size_t max_size, std::initializer_list< T > list)
Definition: stackpool.h:24
std::string poolName
Definition: stackpool.h:56
Definition: api.h:9