-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrcgc_c_ptr.h
More file actions
89 lines (83 loc) · 2.42 KB
/
rcgc_c_ptr.h
File metadata and controls
89 lines (83 loc) · 2.42 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
#pragma once
#include "rcgc_d_ptr.h"
#include "rcgc_f_ptr.h"
//Container version of rcgc_ptr (wraps containers)
template<class PTR>
class rcgc_c_ptr
: public rcgc_base
{
public:
static void terminating_function(void* ptr) {
delete reinterpret_cast<PTR*>(ptr);
}
public:
rcgc_c_ptr(PTR* ptr = nullptr, void* ctr = nullptr) :_ptr(ptr), _ctr(ctr) {
AddConnection(this->_ctr, this->_ptr, terminating_function);
}
~rcgc_c_ptr() {
this->disposing();
//if the container is wild, we terminate it instantly
PTR* p = this->_ptr;
if (p != nullptr && IsMarkPointer(this->_ctr)) {
this->_ptr = nullptr;
terminating_function(p);
}
}
public:
void disposing() {
PTR* p = this->_ptr;
if (p != nullptr) {
for (auto& q : *p) {
q.disposing();
}
}
}
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;
}
void* get_ctr() const {
return this->_ctr;
}
protected:
PTR* _ptr;
void* _ctr;
};
template<class PTR>
class rcgc_vector : public rcgc_c_ptr<std::vector<PTR>> {
public:
rcgc_vector(void* ctr = nullptr) : rcgc_c_ptr<std::vector<PTR>>(new std::vector<PTR>(),ctr) {}
rcgc_vector(std::vector<PTR>* v, void* ctr = nullptr) : rcgc_c_ptr<std::vector<PTR>>(v, ctr) {}
};
template<class PTR>
class rcgc_ptr_vector : public rcgc_vector<rcgc_ptr<PTR>> {
public:
rcgc_ptr_vector(void* ctr = nullptr)
: rcgc_vector<rcgc_ptr<PTR>>(ctr) {}
rcgc_ptr_vector(std::vector<rcgc_ptr<PTR>>* v, void* ctr = nullptr)
: rcgc_vector<rcgc_ptr<PTR>>(v, ctr) {}
};
template<class PTR>
class rcgc_d_ptr_vector : public rcgc_vector<rcgc_d_ptr<PTR>> {
public:
rcgc_d_ptr_vector(void* ctr = nullptr)
: rcgc_vector<rcgc_d_ptr<PTR>>(ctr) {}
rcgc_d_ptr_vector(std::vector<rcgc_d_ptr<PTR>>*v, void* ctr = nullptr)
: rcgc_vector<rcgc_d_ptr<PTR>>(v,ctr) {}
};
template<class PTR>
class rcgc_f_ptr_vector : public rcgc_vector<rcgc_f_ptr<PTR>> {
public:
rcgc_f_ptr_vector(void* ctr = nullptr)
: rcgc_vector<rcgc_f_ptr<PTR>>(ctr) {}
rcgc_f_ptr_vector(std::vector<rcgc_f_ptr<PTR>>* v,void* ctr = nullptr)
: rcgc_vector<rcgc_f_ptr<PTR>>(v,ctr) {}
};