aboutsummaryrefslogtreecommitdiff
path: root/week12/Exercise1/TextDocument.cpp
blob: f5abf2ce3f64b457c891b817d8591fce4186c93a (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
#include "TextDocument.h"
#include <cstring>

void TextDocument::free() {
	delete[] text;
}

void TextDocument::copyFrom(const TextDocument& other) {
	this->len = other.len;
	this->text = new char[len + 1];
	strcpy(this->text, other.text);
}

TextDocument::TextDocument() {
	this->text = nullptr;
}

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

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

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

TextDocument::TextDocument(TextDocument&& other) {
	this->len = other.len;
	this->text = other.text;
	other.text = nullptr;
}

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

		this->len = other.len;
		this->text = other.text;
		other.text = nullptr;
	}
	return *this;
}

std::ostream& operator<<(std::ostream& ostr, const TextDocument& other) {
	return ostr << other.text;
}