-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeepptr.h
76 lines (63 loc) · 1.46 KB
/
deepptr.h
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
/*
* DeepPtr<T> implementa una gestione automatica della memoria “profonda” e polimorfa di
* puntatori a T richiedendo che T si un tipo che supporti la clonazione e la distruziona
* polimorfa
*
* Simile ad unique_ptr della stl
*
* PRECONDIZIONE: T* ptr non deve essere un puntatore nullo
*/
#ifndef DEEPPTR_H
#define DEEPPTR_H
//https://www.codeproject.com/Articles/15351/Implementing-a-simple-smart-pointer-in-c
#include <exception>
template <typename T>
class DeepPtr {
T* ptr;
public:
DeepPtr(T*);
DeepPtr(const DeepPtr&);
~DeepPtr();
DeepPtr& operator=(const DeepPtr&);
bool operator==(const DeepPtr&);
T* operator->() const;
T& operator*() const;
T* get() const;
};
template <typename T>
DeepPtr<T>::DeepPtr(T* p) {
ptr = p->clone();
}
template <typename T>
DeepPtr<T>::DeepPtr(const DeepPtr& dptr) {
ptr = dptr.ptr->clone();
}
template <typename T>
DeepPtr<T>::~DeepPtr() {
delete ptr;
}
template <typename T>
DeepPtr<T>& DeepPtr<T>::operator=(const DeepPtr<T>& dptr) {
if(this != &dptr) {
delete ptr;
ptr = dptr.pt->clone();
}
return *this;
}
template <typename T>
bool DeepPtr<T>::operator==(const DeepPtr& dptr) {
return *ptr == *dptr.ptr;
}
template <typename T>
T* DeepPtr<T>::operator->() const {
return ptr;
}
template <typename T>
T& DeepPtr<T>::operator*() const {
return *ptr;
}
template <typename T>
T* DeepPtr<T>::get() const {
return ptr;
}
#endif // DEEPPTR_H