-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
101 lines (77 loc) · 2.09 KB
/
main.cpp
File metadata and controls
101 lines (77 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <vector>
#include "allocator/ooplloc.h"
#define objCount 3
class obj {
public:
virtual void update() = 0;
virtual void render() = 0;
virtual ~obj() = default;
};
class objOne : public obj {
public:
objOne(int x, int y) : x(x), y(y) {}
void update() override {
x++;
y++;
std::cout << "objOne changed to (" << x << ", " << y << ")" << std::endl;
}
void render() override {
std::cout << "Print objOne at (" << x << ", " << y << ")" << std::endl << std::endl;
}
private:
int x, y;
};
class objTwo : public obj {
public:
objTwo(int x, int y) : x(x), y(y) {}
void update() override {
x--;
y--;
std::cout << "objTwo changed to (" << x << ", " << y << ")" << std::endl;
}
void render() override {
std::cout << "Print objTwo at (" << x << ", " << y << ")" << std::endl << std::endl;
}
private:
int x, y;
};
int main() {
const size_t blockSize = std::max(sizeof(objOne), sizeof(objTwo));
const size_t blockCount = 6;
OOPLloc_Allocator memoryManager(blockSize * blockCount, blockSize);
std::vector<obj*> objects;
for (int i = 0; i < objCount; ++i) {
void* block = memoryManager.alloc();
int u;
std::cin >> u;
if (block) {
objOne* player = new(block) objOne(u, u);
objects.push_back(player);
}
}
for (int i = 0; i < objCount; ++i) {
void* block = memoryManager.alloc();
int u;
std::cin >> u;
if (block) {
objTwo* enemy = new(block) objTwo(u * 2, u * 2);
objects.push_back(enemy);
}
}
for (obj* i : objects) {
i->update();
i->render();
}
for (obj* obj : objects) {
if ((objOne*)(obj)) {
obj->~obj();
memoryManager.free(obj);
} else if ((objTwo*)(obj)) {
obj->~obj();
memoryManager.free(obj);
}
}
std::cout << "Used blocks at the end: " << memoryManager.getUsedBlocks() << std::endl;
return 0;
}