From 9c11fababb9b6b194e646fbeb62d141aacf74c43 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 25 May 2024 16:08:17 +0300 Subject: [w13] Added solutions --- week13/Exercise3/Thread.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 week13/Exercise3/Thread.cpp (limited to 'week13/Exercise3/Thread.cpp') diff --git a/week13/Exercise3/Thread.cpp b/week13/Exercise3/Thread.cpp new file mode 100644 index 0000000..6cd13ba --- /dev/null +++ b/week13/Exercise3/Thread.cpp @@ -0,0 +1,85 @@ +#include "Thread.h" +#include + +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; +} -- cgit v1.2.3