-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyPointer.h
More file actions
114 lines (100 loc) · 1.76 KB
/
CopyPointer.h
File metadata and controls
114 lines (100 loc) · 1.76 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Created on: May 22, 2014
* Author: Mirko Myllykoski (mirko.myllykoski@gmail.com)
*/
#ifndef PSCRCL_AUTO_POINTER
#define PSCRCL_AUTO_POINTER
#include "common.h"
namespace pscrCL {
// An automatic pointer.
template <typename T>
class CopyPointer {
public:
// Null pointer exception
class NullPointerException : public Exception {
public:
NullPointerException(const char* message) : Exception(message) {}
};
CopyPointer() {
ptr = 0;
}
CopyPointer(const T* _a) {
if(_a)
ptr = new T(*_a);
else
ptr = 0;
}
CopyPointer(const CopyPointer<T>& _old) {
if(_old.ptr)
ptr = new T(_old);
else
ptr = 0;
}
CopyPointer<T>& operator=(const CopyPointer<T>& _a) {
if(this == &_a)
return *this;
if(ptr)
delete ptr;
if(_a.ptr)
ptr = new T(*_a.ptr);
else
ptr = 0;
return *this;
}
CopyPointer<T>& operator=(const T* _a) {
if(ptr)
delete ptr;
if(_a)
ptr = new T(*_a);
else
ptr = 0;
return *this;
}
~CopyPointer() {
if(ptr)
delete ptr;
}
CopyPointer<T>& operator<<=(T* _a) {
if(ptr)
delete ptr;
ptr = _a;
return *this;
}
T& operator*() {
if(ptr == 0)
throw NullPointerException(
"CopyPointer null pointer exception");
return *ptr;
}
const T& operator*() const {
if(ptr == 0)
throw NullPointerException(
"CopyPointer null pointer exception");
return *ptr;
}
T* operator->() {
if(ptr == 0)
throw NullPointerException(
"CopyPointer null pointer exception");
return ptr;
}
const T* operator->() const {
if(ptr == 0)
throw NullPointerException(
"CopyPointer null pointer exception");
return ptr;
}
T* getPtr() {
return ptr;
}
const T* getPtr() const {
return ptr;
}
bool isNull() const {
return ptr == 0;
}
private:
T *ptr;
};
}
#endif