From 88f15e35713c9632216931d26443dc588238732f Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 7 May 2024 22:17:12 +0300 Subject: [w10] Added rough solutions to ex 1-10 --- week10/Exercise09/CommunicationPacket.cpp | 10 +++++ week10/Exercise09/CommunicationPacket.h | 12 ++++++ week10/Exercise09/IntPacket.cpp | 11 ++++++ week10/Exercise09/IntPacket.h | 10 +++++ week10/Exercise09/StringPacket.cpp | 62 +++++++++++++++++++++++++++++++ week10/Exercise09/StringPacket.h | 19 ++++++++++ 6 files changed, 124 insertions(+) create mode 100644 week10/Exercise09/CommunicationPacket.cpp create mode 100644 week10/Exercise09/CommunicationPacket.h create mode 100644 week10/Exercise09/IntPacket.cpp create mode 100644 week10/Exercise09/IntPacket.h create mode 100644 week10/Exercise09/StringPacket.cpp create mode 100644 week10/Exercise09/StringPacket.h (limited to 'week10/Exercise09') diff --git a/week10/Exercise09/CommunicationPacket.cpp b/week10/Exercise09/CommunicationPacket.cpp new file mode 100644 index 0000000..4e9cb58 --- /dev/null +++ b/week10/Exercise09/CommunicationPacket.cpp @@ -0,0 +1,10 @@ +#include "CommunicationPacket.h" + +CommunicationPacket::CommunicationPacket(unsigned startAddress, unsigned endAddress) { + this->startAddress = startAddress; + this->endAddress = endAddress; + dataSize = 0; +} + +CommunicationPacket::CommunicationPacket() : CommunicationPacket(0, 0) { +} diff --git a/week10/Exercise09/CommunicationPacket.h b/week10/Exercise09/CommunicationPacket.h new file mode 100644 index 0000000..18ea7be --- /dev/null +++ b/week10/Exercise09/CommunicationPacket.h @@ -0,0 +1,12 @@ +#pragma once + +class CommunicationPacket { +protected: + unsigned startAddress; + unsigned endAddress; + unsigned dataSize; + +public: + CommunicationPacket(unsigned startAddress, unsigned endAddress); + CommunicationPacket(); +}; diff --git a/week10/Exercise09/IntPacket.cpp b/week10/Exercise09/IntPacket.cpp new file mode 100644 index 0000000..a4b4842 --- /dev/null +++ b/week10/Exercise09/IntPacket.cpp @@ -0,0 +1,11 @@ +#include "IntPacket.h" +#include "CommunicationPacket.h" + +IntPacket::IntPacket() : CommunicationPacket() { + data = 0; +} + +IntPacket::IntPacket(unsigned startAddress, unsigned endAddress, int data) : CommunicationPacket(startAddress, endAddress) { + this->data = data; + this->dataSize = sizeof(int); +} diff --git a/week10/Exercise09/IntPacket.h b/week10/Exercise09/IntPacket.h new file mode 100644 index 0000000..be88bce --- /dev/null +++ b/week10/Exercise09/IntPacket.h @@ -0,0 +1,10 @@ +#pragma once +#include "CommunicationPacket.h" + +class IntPacket : CommunicationPacket { + int data; + +public: + IntPacket(); + IntPacket(unsigned startAddress, unsigned endAddress, int data); +}; diff --git a/week10/Exercise09/StringPacket.cpp b/week10/Exercise09/StringPacket.cpp new file mode 100644 index 0000000..2b8aa72 --- /dev/null +++ b/week10/Exercise09/StringPacket.cpp @@ -0,0 +1,62 @@ +#include "StringPacket.h" +#include "CommunicationPacket.h" +#include + +void StringPacket::free() { + delete[] data; +} + +void StringPacket::copyFrom(const StringPacket& other) { + this->startAddress = other.startAddress; + this->endAddress = other.endAddress; + this->dataSize = other.dataSize; + this->data = new char[dataSize + 1]; + strcpy(this->data, other.data); +} + +StringPacket::StringPacket() : CommunicationPacket() { + data = nullptr; +} + +StringPacket::~StringPacket() { + free(); +} + +StringPacket::StringPacket(const StringPacket& other) { + copyFrom(other); +} + +StringPacket& StringPacket::operator=(const StringPacket& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +StringPacket::StringPacket(StringPacket&& other) { + this->startAddress = other.startAddress; + this->endAddress = other.endAddress; + this->dataSize = other.dataSize; + this->data = other.data; + other.data = nullptr; +} + +StringPacket& StringPacket::operator=(StringPacket&& other) { + if (this != &other) { + free(); + + this->startAddress = other.startAddress; + this->endAddress = other.endAddress; + this->dataSize = other.dataSize; + this->data = other.data; + other.data = nullptr; + } + return *this; +} + +StringPacket::StringPacket(unsigned startAddress, unsigned endAddress, const char* data) : CommunicationPacket(startAddress, endAddress) { + this->dataSize = strlen(data); + this->data = new char[dataSize + 1]; + strcpy(this->data, data); +} diff --git a/week10/Exercise09/StringPacket.h b/week10/Exercise09/StringPacket.h new file mode 100644 index 0000000..b7bbc73 --- /dev/null +++ b/week10/Exercise09/StringPacket.h @@ -0,0 +1,19 @@ +#pragma once + +#include "CommunicationPacket.h" +class StringPacket : CommunicationPacket { + char* data; + + void free(); + void copyFrom(const StringPacket& other); + +public: + StringPacket(); + ~StringPacket(); + StringPacket(const StringPacket& other); + StringPacket& operator=(const StringPacket& other); + StringPacket(StringPacket&& other); + StringPacket& operator=(StringPacket&& other); + + StringPacket(unsigned startAddress, unsigned endAddress, const char* data); +}; -- cgit v1.2.3