-
Notifications
You must be signed in to change notification settings - Fork 59
/
RefCounter.h
54 lines (44 loc) · 1.09 KB
/
RefCounter.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
/*
* RefCounter.h
* OpenLieroX
*
* Created by Albert Zeyer on 01.06.09.
* code under LGPL
*
*/
#ifndef __OLX__REFCOUNTER_H__
#define __OLX__REFCOUNTER_H__
#include <cstring> // for size_t
/*
* This class is to be added as an additional baseclass and overload the onLastRefRemoved method.
*/
class RefCounter {
private:
size_t* m_refCount;
// asumes that already cleared
void copy(const RefCounter& r) {
m_refCount = r.m_refCount;
(*m_refCount)++;
}
protected:
// call this manually if you aren't sure if your virtual function is already unset
void uninit() {
if(m_refCount) {
(*m_refCount)--;
size_t* ref = m_refCount;
m_refCount = NULL;
if(*ref == 0) {
delete ref;
onLastRefRemoved();
}
}
}
public:
RefCounter() : m_refCount(NULL) { m_refCount = new size_t(1); }
RefCounter(const RefCounter& r) : m_refCount(NULL) { copy(r); }
RefCounter& operator=(const RefCounter& r) { uninit(); copy(r); return *this; }
virtual ~RefCounter() { uninit(); }
size_t refCount() const { return *m_refCount; }
virtual void onLastRefRemoved() = 0;
};
#endif