Website Summary Files Commits Branches @commit @diff @stats
path: root / Week11 / Exercise06 / UniquePointer.hpp
blob: fe95234dd75e07441215c276c232dd34e86cc3fc (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
63
64
65
66
67
68
69
70
71
72
73
74
75
#ifndef _UNIQUEPOINTER
#define _UNIQUEPOINTER

template<class T>
class UniquePointer {
	T* value;

public:
	UniquePointer(T* value);

	UniquePointer();
	~UniquePointer();

	UniquePointer(UniquePointer&& other);
	UniquePointer& operator=(UniquePointer&& other);

	T& operator*();
	const T& operator*() const;
	T* operator->();
	const T* operator->() const;
};

template<class T>
UniquePointer<T>::UniquePointer(T* value) : value(value)
{ }

template<class T>
UniquePointer<T>::UniquePointer() : value()
{ }

template<class T>
UniquePointer<T>::~UniquePointer() {
	delete value;
}

template<class T>
UniquePointer<T>::UniquePointer(UniquePointer&& other) {
	value = other.value;

	other.value = nullptr;
}

template<class T>
UniquePointer<T>& UniquePointer<T>::operator=(UniquePointer&& other) {
	if (this != &other) {
		delete value;

		value = other.value;

		other.value = nullptr;
	}
	return *this;
}

template<class T>
T& UniquePointer<T>::operator*() {
	return *value;
}

template<class T>
const T& UniquePointer<T>::operator*() const {
	return *value;
}

template<class T>
T* UniquePointer<T>::operator->() {
	return value;
}

template<class T>
const T* UniquePointer<T>::operator->() const {
	return value;
}

#endif /* _UNIQUEPOINTER */