1 |
pippijn |
1.1 |
/* |
2 |
|
|
* Copyright © 2007 William Pitcock <nenolod -at- atheme.org> |
3 |
|
|
* Rights to this code are as documented in doc/LICENSE. |
4 |
|
|
* |
5 |
|
|
* Object management. |
6 |
|
|
* |
7 |
|
|
* $Id: object.h 7779 2007-03-03 13:55:42Z pippijn $ |
8 |
|
|
*/ |
9 |
|
|
|
10 |
|
|
#ifndef OBJECT_H |
11 |
|
|
#define OBJECT_H |
12 |
|
|
|
13 |
|
|
/* object base class */ |
14 |
|
|
class object |
15 |
|
|
{ |
16 |
|
|
mutable int refcnt; |
17 |
|
|
|
18 |
|
|
protected: |
19 |
|
|
object () |
20 |
|
|
: refcnt (1) |
21 |
|
|
{ |
22 |
|
|
} |
23 |
|
|
|
24 |
|
|
object (const object &other) |
25 |
|
|
: refcnt (1) |
26 |
|
|
{ |
27 |
|
|
} |
28 |
|
|
|
29 |
|
|
virtual ~object () { } |
30 |
|
|
virtual void _destroy () = 0; |
31 |
|
|
|
32 |
|
|
public: |
33 |
|
|
int refcnt_inc () |
34 |
|
|
{ |
35 |
|
|
return ++refcnt; |
36 |
|
|
} |
37 |
|
|
|
38 |
|
|
int refcnt_dec () |
39 |
|
|
{ |
40 |
|
|
if (!--refcnt) |
41 |
|
|
{ |
42 |
|
|
this->_destroy (); |
43 |
|
|
return 0; |
44 |
|
|
} |
45 |
|
|
else |
46 |
|
|
return refcnt; |
47 |
|
|
} |
48 |
|
|
}; |
49 |
|
|
|
50 |
|
|
typedef void (*destructor_t) (void *); |
51 |
|
|
|
52 |
|
|
struct object_t |
53 |
|
|
{ |
54 |
|
|
char *name; |
55 |
|
|
int refcount; |
56 |
|
|
destructor_t destructor; |
57 |
|
|
}; |
58 |
|
|
|
59 |
|
|
E void object_init (object_t *, const char *name, destructor_t destructor); |
60 |
|
|
E void *object_ref (void *); |
61 |
|
|
E void object_unref (void *); |
62 |
|
|
|
63 |
|
|
#define asobject(x) ((object_t *) x) |
64 |
|
|
|
65 |
|
|
#endif |