schrieb:
> We have a system of small objects in the lines of
>
> typedef struct {
> ...
> } text_object;
>
> And we have a "object master" that serves as a container for
> several of these small objects, in the lines of
>
> typedef struct {
> ...
> text_object *text;
> media_object *media;
> ...
> } object_master;
>
> Now, we need a generic/polymorphic way to initialise the small objects
> as the initialisation is similar for all of them. I would like to use
> inlined functions instead of macros for that, but currently don't see
> any natural, clean way for it. A pseudo-implementation with a macro
> (ALLOC_OBJ) is provided below.
You want templates for this, don't you? In C, you could use a structure,
that contains the common parts:
/* "common" or "base" object */
typedef struct
{
void* module;
} gen_object;
/* specific object */
typedef struct
{
void* module;
void* media_data;
} media_object;
my_error alloc_media(object_master *om, params* pms)
{
my_errcode err = 0;
media_object *mo;
/* ALLOC_OBJ(om, to, textobj, MEDIA_OBJECT_TYPE, pms); */
alloc_obj((gen_object**)&mo, sizeof *mo, MEDIA_OBJECT_TYPE, pms);
om->media = mo;
return err;
}
void alloc_obj(gen_object** obj, size_t size, int type, params* pms)
{
/* no error checking done */
*obj = my_malloc(size);
my_memset(*obj, 0, size);
(*obj)->module = my_module_find(params);
/* ... */
}
--
Thomas