Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming / Exam 2 / ex2 / EventManager.cpp
blob: cf967560ca4bdf7f2e16cdae14fb6a23ddea9adb (plain) (blame)
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
#include "EventManager.h"

void EventManager::copyFrom(const EventManager& other) {
	events = new Event*[other.maxCount];
	for (unsigned i = 0; i < other.count; i++)
		events[i] = other.events[i]->clone();
	count = other.count;
	maxCount = other.maxCount;
}

void EventManager::free() {
	for (unsigned i = 0; i < count; i++)
		delete events[i];
	delete[] events;
}

EventManager::EventManager(unsigned maxCount) {
	events = new Event*[maxCount];
	this->maxCount = maxCount;
	count = 0;
}

bool EventManager::addEvent(const Event* event) {
	if (maxCount == count)
		return false;

	events[count++] = event->clone();
	return true;
}

unsigned EventManager::ongoingEvents(unsigned time) {
	unsigned ongoing = 0;
	for (unsigned i = 0; i < count; i++)
		ongoing += events[i]->isHappeningAt(time);
	return ongoing;
}

EventManager::EventManager() : EventManager(1) {}

EventManager::EventManager(const EventManager& other) {
	copyFrom(other);
}

EventManager& EventManager::operator=(const EventManager& other) {
	if (this != &other) {
		free();
		copyFrom(other);
	}
	return *this;
}

EventManager::~EventManager() {
	free();
}