1 |
/** |
2 |
* object.h: Object management. |
3 |
* |
4 |
* Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team |
5 |
* Rights to this code are as documented in COPYING. |
6 |
* |
7 |
* |
8 |
* Portions of this file were derived from sources bearing the following license: |
9 |
* Copyright © 2007 William Pitcock <nenolod -at- atheme.org> |
10 |
* Rights to this code are as documented in doc/pod/license.pod. |
11 |
* |
12 |
* $Id: object.h,v 1.5 2007-09-16 18:54:42 pippijn Exp $ |
13 |
*/ |
14 |
|
15 |
#ifndef OBJECT_H |
16 |
#define OBJECT_H |
17 |
|
18 |
#include <util/memory.h> |
19 |
|
20 |
/* object base class */ |
21 |
class object |
22 |
{ |
23 |
mutable int refcnt; |
24 |
|
25 |
protected: |
26 |
object () |
27 |
: refcnt (1) |
28 |
{ |
29 |
} |
30 |
|
31 |
object (const object &other) |
32 |
: refcnt (1) |
33 |
{ |
34 |
} |
35 |
|
36 |
virtual ~object () { } |
37 |
virtual void _destroy () = 0; |
38 |
|
39 |
public: |
40 |
int refcnt_inc () |
41 |
{ |
42 |
return ++refcnt; |
43 |
} |
44 |
|
45 |
int refcnt_dec () |
46 |
{ |
47 |
if (!--refcnt) |
48 |
{ |
49 |
this->_destroy (); |
50 |
return 0; |
51 |
} |
52 |
else |
53 |
return refcnt; |
54 |
} |
55 |
}; |
56 |
|
57 |
typedef void (*destructor_t) (void *); |
58 |
|
59 |
struct object_t : zero_initialised |
60 |
{ |
61 |
char *name; |
62 |
int refcount; |
63 |
destructor_t destructor; |
64 |
}; |
65 |
|
66 |
E void object_init (object_t *, const char *name, destructor_t destructor); |
67 |
E void *object_ref (void *); |
68 |
E void object_unref (void *); |
69 |
|
70 |
#define asobject(x) ((object_t *) x) |
71 |
|
72 |
#endif |