-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathref_object.h
221 lines (196 loc) · 5.45 KB
/
ref_object.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// The core library - copyright GarageGames. The core library is released under the MIT Open Source software license. See /license.txt in this distribution for specific license terms.
class safe_object_ref;
class ref_object
{
safe_object_ref *_first_object_ref; ///< The head of the linked list of safe object references.
uint32 _ref_count; ///< Reference counter for ref_ptr objects.
friend class safe_object_ref;
friend class ref_object_ref;
public:
ref_object()
{
_first_object_ref = 0;
_ref_count = 0;
}
virtual ~ref_object();
/// object destroy self call (from ref_ptr).
///
/// @note Override if this class has specially allocated memory.
virtual void destroy_self() { delete this; }
void inc_ref()
{
_ref_count++;
}
void dec_ref()
{
_ref_count--;
if(!_ref_count)
destroy_self();
}
/// @}
};
/// Base class for object reference counting.
class ref_object_ref
{
protected:
ref_object *_object; ///< The object this ref_object_ref references.
/// Increments the reference count on the referenced object.
void inc_ref()
{
if(_object)
_object->inc_ref();
}
/// Decrements the reference count on the referenced object.
void dec_ref()
{
if(_object)
{
_object->dec_ref();
}
}
public:
/// Constructor, assigns from the object and increments its reference count if it's not NULL.
ref_object_ref(ref_object *the_object = NULL)
{
_object = the_object;
inc_ref();
}
/// Destructor, dereferences the object, if there is one.
~ref_object_ref()
{
dec_ref();
}
/// Assigns this reference object from an existing object instance.
void set(ref_object *the_object)
{
dec_ref();
_object = the_object;
inc_ref();
}
};
/// Reference counted object template pointer class.
///
/// Instances of this template class can be used as pointers to
/// instances of object and its subclasses. The object will not
/// be deleted until all of the ref_ptr instances pointing to it
/// have been destructed.
template <class T> class ref_ptr : public ref_object_ref
{
public:
ref_ptr() : ref_object_ref() {}
ref_ptr(T *ptr) : ref_object_ref(ptr) {}
ref_ptr(const ref_ptr<T>& ref) : ref_object_ref((T *) ref._object) {}
ref_ptr<T>& operator=(const ref_ptr<T>& ref)
{
set((T *) ref._object);
return *this;
}
ref_ptr<T>& operator=(T *ptr)
{
set(ptr);
return *this;
}
bool is_null() const { return _object == 0; }
bool is_valid() const { return _object != 0; }
T* operator->() const { return static_cast<T*>(_object); }
T& operator*() const { return *static_cast<T*>(_object); }
operator T*() const { return static_cast<T*>(_object); }
operator T*() { return static_cast<T*>(_object); }
T* get_pointer() { return static_cast<T*>(_object); }
};
/// Base class for object safe pointers.
class safe_object_ref
{
friend class ref_object;
protected:
ref_object *_object; ///< The object this is a safe pointer to, or NULL if the object has been deleted.
safe_object_ref *_prev_object_ref; ///< The previous safe_object_ref for _object.
safe_object_ref *_next_object_ref; ///< The next safe_object_ref for _object.
public:
safe_object_ref(ref_object *object)
{
_object = object;
register_reference();
}
safe_object_ref()
{
_object = NULL;
}
void set(ref_object *object)
{
unregister_reference();
_object = object;
register_reference();
}
~safe_object_ref()
{
unregister_reference();
}
void register_reference() ///< Links this safe_object_ref into the doubly linked list of safe_object_ref instances for _object.
{
if(_object)
{
_next_object_ref = _object->_first_object_ref;
if(_next_object_ref)
_next_object_ref->_prev_object_ref = this;
_prev_object_ref = NULL;
_object->_first_object_ref = this;
}
}
void unregister_reference() ///< Unlinks this safe_object_ref from the doubly linked list of safe_object_ref instance for _object.
{
if(_object)
{
if(_prev_object_ref)
_prev_object_ref->_next_object_ref = _next_object_ref;
else
_object->_first_object_ref = _next_object_ref;
if(_next_object_ref)
_next_object_ref->_prev_object_ref = _prev_object_ref;
}
}
};
/// Safe object template pointer class.
///
/// Instances of this template class can be used as pointers to
/// instances of object and its subclasses.
///
/// When the object referenced by a safe_ptr instance is deleted,
/// the pointer to the object is set to NULL in the safe_ptr instance.
template <class T> class safe_ptr : public safe_object_ref
{
public:
safe_ptr() : safe_object_ref() {}
safe_ptr(T *ptr) : safe_object_ref(ptr) {}
safe_ptr(const safe_ptr<T>& ref) : safe_object_ref((T *) ref._object) {}
safe_ptr<T>& operator=(const safe_ptr<T>& ref)
{
set((T *) ref._object);
return *this;
}
safe_ptr<T>& operator=(T *ptr)
{
set(ptr);
return *this;
}
bool is_null() const { return _object == 0; }
bool is_valid() const { return _object != 0; }
T* operator->() const { return static_cast<T*>(_object); }
T& operator*() const { return *static_cast<T*>(_object); }
operator T*() const { return static_cast<T*>(_object); }
operator T*() { return reinterpret_cast<T*>(_object); }
T* get_pointer() { return static_cast<T*>(_object); }
};
inline ref_object::~ref_object()
{
assert(_ref_count == 0);
safe_object_ref *walk = _first_object_ref;
while(walk)
{
safe_object_ref *next = walk->_next_object_ref;
walk->_object = 0;
walk->_prev_object_ref = 0;
walk->_next_object_ref = 0;
walk = next;
}
}