-
Notifications
You must be signed in to change notification settings - Fork 0
/
rcgc_ptr.h
59 lines (56 loc) · 1.21 KB
/
rcgc_ptr.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
#pragma once
#include "rcgc_base.h"
template<class PTR>
class rcgc_ptr
: public rcgc_base
{
public:
rcgc_ptr(PTR* ptr = nullptr) :_ptr(ptr) {
AddRef(this->_ptr);
}
rcgc_ptr(const rcgc_ptr& src) :_ptr(src._ptr){
AddRef(this->_ptr);
}
~rcgc_ptr() {
OnCollecting<rcgc_ptr,PTR>(this);
}
public:
void disposing() {
if (this != nullptr) {
PTR* p = this->_ptr;
if (p != nullptr) {
this->_ptr = nullptr;
p->~PTR();
RelRef(p);
}
}
}
rcgc_ptr& operator = (rcgc_ptr<PTR>& src) {
if (this->_ptr == src._ptr) {
}
else if (src._ptr != nullptr) {
RelRef(this->_ptr);
AddRef(this->_ptr = src._ptr);
}
return *this;
}
operator bool() const {
return this->_ptr != nullptr;
}
PTR& operator*() const {
return *this->_ptr;
}
PTR* operator->() const {
return this->_ptr;
}
PTR* get() const {
return this->_ptr;
}
PTR* rebind(PTR* p = nullptr) {
PTR* op = this->_ptr;
this->_ptr = p;
return op;
}
protected:
PTR* _ptr;
};