From 27043cac3f8c9fe1f9b26641ee37fb0f3e07e99d Mon Sep 17 00:00:00 2001 From: khamidou Date: Sun, 20 Dec 2009 19:44:45 +0100 Subject: [PATCH] defining object system --- object.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ object.h | 7 +++++-- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/object.c b/object.c index 605fff6..1b5a3b5 100644 --- a/object.c +++ b/object.c @@ -14,3 +14,59 @@ struct Method *lookup_method(struct Object *o, char *name) return NULL; } + +struct Object *clone_object(struct Object *o) +{ + if (o == NULL) + return NULL; + + struct Object *new = zalloc(sizeof(struct Object *)); + new->super = o->super; + new->type = o->type; + new->name = strndup(o->name, 255); /* FIXME : test the value of strdup */ + + struct Method *m; + struct Method *new_method; + TAILQ_INIT(new->methods_head); + TAILQ_FOREACH(m, &o->methods_head, methods) { + new_method = clone_method(m); + TAILQ_INSERT_TAIL(&new->methods_head, new_method, methods); + } + +} + +void free_object(struct Object *o) +{ + if (o == NULL) + return; + + free(o->name); + + struct Method *m; + TAILQ_FOREACH(m, &o->methods_head, methods) { + free_method(m); + } +} + +struct Method *clone_method(struct Method *m) +{ + if (m == NULL) + return NULL; + + struct Method *new = zalloc(sizeof(struct Method)); + + new->name = strndup(m->name, 255); + new->c_method = m->c_method; + new->parent = m->parent; + + return new; +} + +void free_method(struct Method *m) +{ + if (m == NULL) + return; + TAILQ_REMOVE(m->parent->methods_head, m, methods); + free(m->name); + free(m); +} diff --git a/object.h b/object.h index d5717db..3bf1b04 100644 --- a/object.h +++ b/object.h @@ -21,9 +21,12 @@ enum { */ struct Method { - struct Object *arg1, *arg2; + char *name; + struct Object *parent; + struct Object * (*c_method)(struct Object *arg1, struct Object *arg2); + TAILQ_ENTRY(Method) methods; - Object * (*c_method)(arg1, arg2); + }; struct Object { -- 2.43.0