aboutsummaryrefslogtreecommitdiff
path: root/week10/Exercise09/StringPacket.cpp
blob: 2b8aa7247fc5b4ed002aeae370611bce0dcfbd46 (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
#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);
}