aboutsummaryrefslogtreecommitdiff
path: root/week10/Exercise09
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2024-05-07 22:17:12 +0300
committerSyndamia <kamen@syndamia.com>2024-05-07 22:17:12 +0300
commit88f15e35713c9632216931d26443dc588238732f (patch)
tree147ff8c72009afc58d3eabb1c026a33b798f20fe /week10/Exercise09
parentf4642325f58172e85b9e41e35f69e8bea46f78c6 (diff)
downloadoop-2023-solutions-88f15e35713c9632216931d26443dc588238732f.tar
oop-2023-solutions-88f15e35713c9632216931d26443dc588238732f.tar.gz
oop-2023-solutions-88f15e35713c9632216931d26443dc588238732f.zip
[w10] Added rough solutions to ex 1-10
Diffstat (limited to 'week10/Exercise09')
-rw-r--r--week10/Exercise09/CommunicationPacket.cpp10
-rw-r--r--week10/Exercise09/CommunicationPacket.h12
-rw-r--r--week10/Exercise09/IntPacket.cpp11
-rw-r--r--week10/Exercise09/IntPacket.h10
-rw-r--r--week10/Exercise09/StringPacket.cpp62
-rw-r--r--week10/Exercise09/StringPacket.h19
6 files changed, 124 insertions, 0 deletions
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 <cstring>
+
+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);
+};