Index: cfwarray.c ================================================================== --- cfwarray.c +++ cfwarray.c @@ -33,17 +33,24 @@ CFWObject obj; void **data; size_t size; }; -static void -ctor(void *ptr) +static bool +ctor(void *ptr, va_list args) { CFWArray *array = ptr; + void *obj; array->data = NULL; array->size = 0; + + while ((obj = va_arg(args, void*)) != NULL) + if (!cfw_array_push(array, obj)) + return false; + + return true; } static void dtor(void *ptr) { Index: cfwclass.h ================================================================== --- cfwclass.h +++ cfwclass.h @@ -27,15 +27,16 @@ #ifndef __CFWCLASS_H__ #define __CFWCLASS_H__ #include #include +#include typedef struct CFWClass { const char *name; size_t size; - void (*ctor)(void*); + bool (*ctor)(void*, va_list args); void (*dtor)(void*); bool (*equal)(void*, void*); void* (*copy)(void*); } CFWClass; Index: cfwobject.c ================================================================== --- cfwobject.c +++ cfwobject.c @@ -27,22 +27,31 @@ #include #include "cfwobject.h" void* -cfw_new(CFWClass *class) +cfw_new(CFWClass *class, ...) { CFWObject *obj; if ((obj = malloc(class->size)) == NULL) return NULL; obj->clsptr = class; obj->ref_cnt = 1; - if (class->ctor != NULL) - class->ctor(obj); + if (class->ctor != NULL) { + va_list args; + va_start(args, class); + + if (!class->ctor(obj, args)) { + cfw_unref(obj); + return NULL; + } + + va_end(args); + } return obj; } void* Index: cfwobject.h ================================================================== --- cfwobject.h +++ cfwobject.h @@ -33,11 +33,11 @@ CFWClass *clsptr; int ref_cnt; } CFWObject; extern CFWClass *cfw_object; -extern void* cfw_new(CFWClass*); +extern void* cfw_new(CFWClass*, ...); extern void* cfw_ref(void*); extern void cfw_unref(void*); extern void cfw_free(void*); extern bool cfw_equal(void*, void*); extern void* cfw_copy(void*); Index: cfwstring.c ================================================================== --- cfwstring.c +++ cfwstring.c @@ -34,17 +34,27 @@ CFWObject obj; char *cstr; size_t len; }; -static void -ctor(void *ptr) +static bool +ctor(void *ptr, va_list args) { CFWString *str = ptr; + const char *cstr = va_arg(args, const char*); + + if (cstr != NULL) { + if ((str->cstr = strdup(cstr)) == NULL) + return false; + + str->len = strlen(cstr); + } else { + str->cstr = NULL; + str->len = 0; + } - str->cstr = NULL; - str->len = 0; + return true; } static void dtor(void *ptr) { Index: test.c ================================================================== --- test.c +++ test.c @@ -31,34 +31,19 @@ #include "cfwarray.h" int main() { + CFWString *s[3]; CFWArray *a; - CFWString *s; size_t i; - a = cfw_new(cfw_array); - - for (i = 0; i < 3; i++) { - s = cfw_new(cfw_string); - - switch (i) { - case 0: - cfw_string_set(s, "Hello"); - break; - case 1: - cfw_string_set(s, "World"); - break; - case 2: - cfw_string_set(s, "!"); - break; - } - - cfw_array_push(a, s); - cfw_unref(s); - } + s[0] = cfw_new(cfw_string, "Hallo"); + s[1] = cfw_new(cfw_string, "Welt"); + s[2] = cfw_new(cfw_string, "!"); + + a = cfw_new(cfw_array, s[0], s[1], s[2], NULL); for (i = 0; i < cfw_array_size(a); i++) puts(cfw_string_c(cfw_array_get(a, i))); return 0;