Tramway SDK
stackpool.h
Go to the documentation of this file.
1// Tramway Drifting and Dungeon Exploration Simulator SDK Runtime
2
3#ifndef TRAM_SDK_TEMPLATES_STACKPOOL_H
4#define TRAM_SDK_TEMPLATES_STACKPOOL_H
5
6#include <string>
7#include <iostream>
8
9namespace tram {
10
16template <typename T>
17class StackPool {
18public:
19 constexpr StackPool(std::string name, size_t size) {
20 this->name = name;
21 this->available_size = size;
22 this->allocated_size = 0;
23
24 first = static_cast<T*>(::operator new(size * sizeof(T)));
25 last = first;
26 }
27
28 constexpr StackPool(std::string name, size_t max_size, std::initializer_list<T> list) : StackPool(name, max_size) {
29 for (const auto& entry : list) {
30 *AddNew(1) = entry; // not good, but works
31 }
32 }
33
34 T* AddNew(size_t units) {
35 if (allocated_size + units > available_size) {
36 std::cout << "StackPool " << name << " out of space!" << std::endl;
37 return nullptr;
38 }
39
40 T* allocation = last;
41
42 allocated_size += units;
43 last += units;
44
45 return allocation;
46 }
47
48 T* allocate(size_t units) {
49 return AddNew(units);
50 }
51
52 void Reset() {
54 last = first;
55 }
56
57 void reset() {
58 Reset();
59 }
60
61 size_t size() { return allocated_size; }
62 T* begin() { return first; }
63 T* end() { return last; }
64
65protected:
66 std::string name;
70 T* last;
71};
72
73}
74
75#endif // TRAM_SDK_TEMPLATES_STACKPOOL_H
Allocator.
Definition: stackpool.h:17
T * last
Definition: stackpool.h:70
constexpr StackPool(std::string name, size_t size)
Definition: stackpool.h:19
T * begin()
Definition: stackpool.h:62
void Reset()
Definition: stackpool.h:52
T * AddNew(size_t units)
Definition: stackpool.h:34
T * allocate(size_t units)
Definition: stackpool.h:48
size_t available_size
Definition: stackpool.h:68
std::string name
Definition: stackpool.h:66
constexpr StackPool(std::string name, size_t max_size, std::initializer_list< T > list)
Definition: stackpool.h:28
size_t size()
Definition: stackpool.h:61
size_t allocated_size
Definition: stackpool.h:67
T * first
Definition: stackpool.h:69
T * end()
Definition: stackpool.h:63
void reset()
Definition: stackpool.h:57
Serialization, i.e.