File: refcount.h

package info (click to toggle)
mrd6 0.9.5-release-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,308 kB
  • ctags: 3,956
  • sloc: cpp: 25,728; perl: 462; makefile: 281; ansic: 142; sh: 67
file content (58 lines) | stat: -rw-r--r-- 828 bytes parent folder | download
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
#ifndef _support_refcount_h_
#define _support_refcount_h_

class refcountable {
public:
	refcountable();
	virtual ~refcountable();

	void grab();
	void release();

	int get_refcount() const;

protected:
	virtual void destructor();

private:
	int _refcount;
};

class auto_grab {
public:
	auto_grab(refcountable *_t) : t(_t) {
		t->grab();
	}

	~auto_grab() {
		if (t)
			t->release();
	}

private:
	refcountable *t;
};

inline refcountable::refcountable() : _refcount(0) {}
inline refcountable::~refcountable() { /* assert(_refcount == 0); */ }

inline void refcountable::grab() {
	_refcount ++;
}

inline void refcountable::release() {
	_refcount --;
	if (_refcount == 0)
		destructor();
}

inline int refcountable::get_refcount() const {
	return _refcount;
}

inline void refcountable::destructor() {
	delete this;
}

#endif