diff options
author | Luo Jinghua <sunmoon1997@gmail.com> | 2009-10-29 16:56:11 +0800 |
---|---|---|
committer | Luo Jinghua <sunmoon1997@gmail.com> | 2009-10-29 16:56:11 +0800 |
commit | 75db46f13c711919a0ee03d522016fe22fefa419 (patch) | |
tree | a8cbf653e29983ad93001c97d5ad994e9834103b | |
parent | 9794cbd87fe6d8292710a53de3cfdd6d19b5839b (diff) |
milkway: test mw-object in details
-rw-r--r-- | milkway/mw-object.c | 20 | ||||
-rw-r--r-- | milkway/mw-object.h | 5 | ||||
-rw-r--r-- | milkway/test/object-test.c | 138 |
3 files changed, 157 insertions, 6 deletions
diff --git a/milkway/mw-object.c b/milkway/mw-object.c index 7f19014..1b7d978 100644 --- a/milkway/mw-object.c +++ b/milkway/mw-object.c @@ -184,3 +184,23 @@ mw_object_check(const void *obj, const void * typep) return MW_FALSE; } + +void* +mw_object_get_super_type(const void *obj, const void * typep) +{ + const mw_object_type_t * type; + const mw_object_t * self = obj; + + if (!self) + return NULL; + +#ifndef NDEBUG + if (self->magic != MW_OBJECT_MAGIC) + return NULL; +#endif + for (type = self->type; type; type = type->parent) + if (type == typep) + return type->parent; + + return NULL; +} diff --git a/milkway/mw-object.h b/milkway/mw-object.h index b1fd1ce..320cba9 100644 --- a/milkway/mw-object.h +++ b/milkway/mw-object.h @@ -52,6 +52,8 @@ struct mw_object { #define MW_TYPE(self) (((mw_object_t*)(self))->type) #define MW_SUPER_TYPE(self) (((mw_object_t*)(self))->type->parent) #define MW_SUPER_TYPE_CAST(self, type) ((type*)MW_SUPER_TYPE(self)) +#define MW_SUPER_TYPE_BASE(self, base) ((mw_object_type_t*)mw_object_get_super_type(self, base)) +#define MW_TYPE_SUPER(type) ((mw_object_type_t*)(((mw_object_type_t*)(type))->parent)) #define MW_PRIV_ALIGN(size) ((size + 15) & ~15) #define MW_GET_PRIV(self, type) ((char*)self + NW_TYPE(self)->size + \ (mw_object_type_t*)(type)->priv_offset) @@ -114,4 +116,7 @@ mw_object_isa(const void *self, const char * type_name); mw_public mw_bool_t mw_object_check(const void *self, const void * typep); +mw_public void* +mw_object_get_super_type(const void *self, const void *base); + #endif diff --git a/milkway/test/object-test.c b/milkway/test/object-test.c index 59b9eaa..d418bf1 100644 --- a/milkway/test/object-test.c +++ b/milkway/test/object-test.c @@ -24,14 +24,140 @@ #include <string.h> #include <stdio.h> -int main(int argc, char **argv) +typedef struct test_a_type test_a_type_t; +typedef struct test_a test_a_t; +#define TEST_A_TYPE test_a_get_type() +struct test_a_type { + mw_object_type_t base; +}; + +struct test_a { + mw_object_t base; + + int a; + char b; +}; + +static test_a_type_t* +test_a_get_type(void); + +static test_a_t* +test_a_init(test_a_t *self, + test_a_type_t *type) +{ + if (!mw_object_init(&self->base, &type->base)) + return NULL; + return self; +} + +static test_a_t* +test_a_new(void) +{ + test_a_t *self = mw_object_alloc(TEST_A_TYPE); + if (!self) + return NULL; + return test_a_init(self, TEST_A_TYPE); +} + +static void +test_a_finalize(mw_object_t *self) +{ + MW_TYPE_SUPER(TEST_A_TYPE)->finalize(self); +} + +static void +test_a_type_init(test_a_type_t *type, + mw_object_type_t *parent) +{ + type->base.finalize = test_a_finalize; +} + +static MW_DEFINE_GET_TYPE(test_a, test_a_type_t, + MW_OBJECT_TYPE, "testa", 0); + +typedef struct test_b_type test_b_type_t; +typedef struct test_b test_b_t; +#define TEST_B_TYPE test_b_get_type() +struct test_b_type { + mw_object_type_t base; +}; + +struct test_b { + mw_object_t base; + + int a; + char b; +}; + +static test_b_type_t* +test_b_get_type(void); + +static test_b_t* +test_b_init(test_b_t *self, + test_b_type_t *type) +{ + if (!mw_object_init(&self->base, &type->base)) + return NULL; + return self; +} + +static test_b_t* +test_b_new(void) +{ + test_b_t *self = mw_object_alloc(TEST_B_TYPE); + if (!self) + return NULL; + return test_b_init(self, TEST_B_TYPE); +} + +static void +test_b_finalize(mw_object_t *self) +{ + MW_SUPER_TYPE_BASE(self, TEST_B_TYPE)->finalize(self); +} + +static void +test_b_type_init(test_b_type_t *type, + mw_object_type_t *parent) +{ + type->base.finalize = test_b_finalize; +} + +static MW_DEFINE_GET_TYPE(test_b, test_b_type_t, + TEST_A_TYPE, "testa", 0); + +static void +test_ref(void) { - mw_connection_mgr_t *mgr; + test_a_t *a; - mgr = mw_connection_mgr_new(); - mw_object_ref(mgr); - mw_object_unref(mgr); - mw_object_unref(mgr); + a = test_a_new(); + mw_object_unref(a); + + a = test_a_new(); + mw_object_ref(a); + mw_object_unref(a); + mw_object_unref(a); +} + +static void +test_check(void) +{ + test_b_t *b; + + b = test_b_new(); + + mw_unless(mw_object_check(b, TEST_A_TYPE)); + mw_unless(mw_object_check(b, TEST_B_TYPE)); + mw_unless(mw_object_check(b, MW_OBJECT_TYPE)); + + mw_object_unref(b); +} + +int main(int argc, char **argv) +{ + test_ref(); + test_check(); printf ("object test: PASS\n"); |