-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobject.c
83 lines (67 loc) · 2.08 KB
/
object.c
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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "object.h"
DEFINE_TYPE (Object, object, TYPE_ANY);
static void object_init (Object *self) {
self->refcount = 1;
}
static Object *object_ref_real (Object *self) {
++self->refcount;
return self;
}
static void object_unref_real (Object *self) {
if (self->refcount == 0)
return;
--self->refcount;
if (self->refcount == 0)
type_instance_instance_dispose (self);
}
static char *object_to_string_real (Object *self) {
return strdup((*(TypeInstance **)self)->name);
}
static void object_dispose (Object *self) {
#ifdef OBJECT_DEBUG
printf("%s: disposing (%s *) %p\n", __func__,
(*(TypeInstance **)self)->name, self);
#endif
assert (self->refcount == 0);
}
static void object_class_init (ObjectClass *klass) {
/* note: we need not do object_class_cast here,
* but future objects inheriting may need to
* ex: we have type Derived, so we'll get a (DerivedClass *)
* passed to us. To override the ObjectClass virtual
* methods, we call object_class_cast (DerivedClass *)
* to get a (ObjectClass *)
* note: the class's TypeInstance is already defined with
* the proper methods and information
*/
/* virtual methods */
klass->ref = object_ref_real;
klass->unref = object_unref_real;
klass->to_string = object_to_string_real;
}
static void object_class_dispose (ObjectClass *klass) { /* ... */ }
void *object_new(Type type, ...) {
void *instance;
instance = type_instance_instance_new (type);
assert (instance != NULL);
// TODO: properties / varargs
return instance;
}
Object *object_ref(Object *self) {
return object_get_class (self)->ref (self);
}
void object_unref (Object *self) {
object_get_class (self)->unref (self);
}
void object_clear_ref(Object **selfptr) {
assert (selfptr != NULL && *selfptr != NULL);
object_unref (*selfptr);
*selfptr = NULL;
}
char *object_to_string(Object *self) {
return object_get_class (self)->to_string (self);
}