aboutsummaryrefslogtreecommitdiff
path: root/week13/Exercise3/Thread.cpp
blob: 6cd13bad71c1ed630f868f0fbdb187d2474be3db (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
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
#include "Thread.h"
#include <cstring>

void Thread::resize() {
	this->allocated *= 2;
	char** moreMessages = new char*[this->allocated];
	for (int i = 0; i < this->size; i++) {
		moreMessages[i] = this->messages[i];
	}
	delete[] this->messages;
	this->messages = moreMessages;
}

void Thread::free() {
	for (int i = 0; i < size; i++) {
		delete[] messages[i];
	}
	delete[] messages;
}

void Thread::copyFrom(const Thread& other) {
	this->size = other.size;
	this->allocated = other.allocated;
	this->messages = new char*[allocated];
	for (int i = 0; i < size; i++) {
		this->messages[i] = new char[strlen(other.messages[i] + 1)];
		strcpy(this->messages[i], other.messages[i]);
	}
}

Thread::Thread() {
	this->messages = nullptr;
	this->size = this->allocated = 0;
}

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

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

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

Thread::Thread(Thread&& other) {
	this->size = other.size;
	this->allocated = other.allocated;
	this->messages = other.messages;
	other.messages = nullptr;
}

Thread& Thread::operator=(Thread&& other) {
	if (this != &other) {
		free();

		this->size = other.size;
		this->allocated = other.allocated;
		this->messages = other.messages;
		other.messages = nullptr;
	}
	return *this;
}

void Thread::PostMessage(const User& poster, const char* message) {
	if (poster.IsBanned()) {
		throw "User cannot post!";
	}
	if (this->size == this->allocated) {
		resize();
	}

	char* newMessage = new char[strlen(poster.GetUsername()) + 1 + strlen(message) + 1];
	strcpy(newMessage, poster.GetUsername());
	strcat(newMessage, " "); // За да знаем къде свършва потребителското име
	strcat(newMessage, message);

	this->messages[this->size++] = newMessage;
}