-
Notifications
You must be signed in to change notification settings - Fork 0
/
rcgc_n_ptr.h
72 lines (66 loc) · 1.91 KB
/
rcgc_n_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
60
61
62
63
64
65
66
67
68
69
70
71
72
#pragma once
#include "rcgc_base.h"
//rcgc_n_ptr is for something like std::wstring
//which you should not call detors but just
//decrease reference count.
template<class PTR>
class rcgc_n_ptr
: public rcgc_base
{
public:
static void terminating_function(void* ptr) {
delete reinterpret_cast<PTR*>(ptr);
}
public:
rcgc_n_ptr(void* ctr) :_ptr(new PTR()), _ctr(ctr) {
AddConnection(this->_ctr, this->_ptr, terminating_function);
}
rcgc_n_ptr(PTR* ptr = nullptr, void* ctr = nullptr) :_ptr(ptr),_ctr(ctr) {
AddConnection(this->_ctr, this->_ptr, terminating_function);
}
~rcgc_n_ptr() {
this->finalize();
}
public:
void disposing() {
}
void finalize() {
PTR* p = this->_ptr;
if (p!=nullptr && IsMarkPointer(this->_ctr)) {
this->_ptr = nullptr;
terminating_function(p);
}
}
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;
void* _ctr;
};
class rcgc_w_str :public rcgc_n_ptr<std::wstring> {
public:
rcgc_w_str(void* ctr = nullptr) : rcgc_n_ptr(new std::wstring(), ctr) {}
rcgc_w_str(const wchar_t* s = L"", void* ctr = nullptr) : rcgc_n_ptr(new std::wstring(s), ctr) {}
rcgc_w_str(std::wstring* s,void* ctr = nullptr) : rcgc_n_ptr(s,ctr) {}
};
class rcgc_s_str :public rcgc_n_ptr<std::string> {
public:
rcgc_s_str(void* ctr = nullptr) : rcgc_n_ptr(new std::string(), ctr) {}
rcgc_s_str(const char* s ="", void* ctr = nullptr) : rcgc_n_ptr(new std::string(s), ctr) {}
rcgc_s_str(std::string* s,void* ctr = nullptr) : rcgc_n_ptr(s, ctr) {}
};