-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGCP_SPointer.h
134 lines (117 loc) · 3.44 KB
/
GCP_SPointer.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#ifndef GCP_SPointerH
#define GCP_SPointerH
namespace gcp
{
template <class T>
class GCP_SPointer;
class GCP_SPointerBase
{
private:
int _nRef;
bool _isEmpty;
void AddRef() { _nRef++; }
int DelRef() { _nRef--; return _nRef; }
public:
GCP_SPointerBase() : _nRef(0), _isEmpty(false){}
template <class T>
friend class GCP_SPointer;
};
template <class T>
class GCP_SPointer
{
private:
T* _pointee;
void release(){
if (_pointee != NULL) {
assert(_pointee->_nRef > 0);
if (_pointee->DelRef() == 0) {
delete _pointee;
_pointee = NULL;
}
}
}
public:
explicit GCP_SPointer() : _pointee(NULL){}
GCP_SPointer(int n) : _pointee(NULL){}
explicit GCP_SPointer(T* pointee) : _pointee(pointee)
{
if (pointee != NULL)
{
if (!(_pointee->_isEmpty))
_pointee->AddRef();
else
_pointee = NULL;
}
};
GCP_SPointer(GCP_SPointer const& pnt) : _pointee(NULL) {
release();
_pointee = pnt.getPointer();
if (_pointee != NULL)
{
if (!(_pointee->_isEmpty))
_pointee->AddRef();
else
_pointee = NULL;
}
}
T* getPointer() const { return _pointee; } //Îïàñíî îòäàâàòü óêàçàòåëü íàðóæó!
GCP_SPointer& operator=(GCP_SPointer const &rhs) //Ïðèñâàèâàåì îäèí óêàçàòåëü äðóãîìó
{
if (_pointee != rhs.getPointer()) //Ðàçíûå óêàçàòåëè. Îòïóñêàåì íàø óêàçàòåëü. Íîâîìó äîáàâëÿåì ññûëêó.
{
release();
_pointee = rhs.getPointer();
_pointee->AddRef();
}
return *this;
}
GCP_SPointer& operator=(const int rhs) { _pointee = NULL; return *this; }
~GCP_SPointer(){ release(); };
///
void setEmpty(bool empty)
{
//Çàñòâàèòü âñå îêðóæåíèå è âñå ñìàðòïîèíòåðû äóìàòü, ÷òî êëàññ óäàëåí (ïîòîì ïîæíî îòêëþ÷èòü)
if (_pointee != NULL)
{
if (_pointee->_nRef > 0)
_pointee->_isEmpty = empty;
}
}
///
int getRefCount() const
{
if (_pointee)
return _pointee->_nRef;
return -1;
}
///
bool isEmpty()
{
if (_pointee != NULL)
return _pointee->_isEmpty;
return true;
}
T& operator*() const { return *_pointee; }//îïåðàòîð *
T* operator->() const { return _pointee; }//îïåðàòîð ->
operator T* () { return _pointee; }//ïðèâåäåíèå ê òèïó T*
//operator GCP_SPointer<T>() { return GCP_SPointer<T>(this); }
/*template <class D>
operator GCP_SPointer<D> ()
{
GCP_SPointer<D> sliced = GCP_SPointer<D>(static_cast<D>_pointee);
return sliced;
}*/
//îïåðàòîð âçÿòèÿ àäðåñà
//T** operator& () { return &_pointee; }
//îïåðàòîðû ñðàâíåíèÿ
bool operator!() const { return _pointee == 0; }
//inline friend bool operator != (const GCP_SPointer& lhs, const GCP_SPointer& rhs) { return lhs.pointee_ != rhs.pointee_;}
//inline friend bool operator != (const GCP_SPointer& lhs, int rhs) { return lhs.pointee_ != rhs;}
inline friend bool operator == (const GCP_SPointer& lhs, const GCP_SPointer& rhs) { return lhs._pointee == rhs._pointee; }
//inline friend bool operator == (const SmartPtr& lhs, const T* rhs) { return lhs.pointee_ == rhs;}
//inline friend bool operator == (const T* lhs, const SmartPtr* rhs) { return lhs == rhs.pointee_;}
//inline friend bool operator != (const SmartPtr& lhs, const T* rhs) { return lhs.pointee_ != rhs;}
//inline friend bool operator != (const T* lhs, const SmartPtr& rhs) { return lhs != rhs.pointee_;}
};
}
#endif