G
guillemort
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.
Any ideas for converting that macro to an inline function?
my_error alloc_text(object_master *om, params* pms)
{
my_errcode err = 0;
text_object *to;
ALLOC_OBJ(om, to, textobj, TEXT_OBJECT_TYPE, pms);
return err;
}
my_error alloc_text(object_master *om, params* pms)
{
my_errcode err = 0;
media_object *mo;
ALLOC_OBJ(om, mo, mediaobj, MEDIA_OBJECT_TYPE, pms);
return err;
}
#define ALLOC_OBJ(objmaster, gen_obj, gen_obj_field, gen_obj_type,
params)
{
my_ASSERT(obj != NULL);
my_ASSERT(params != NULL);
gen_obj = my_malloc(sizeof(*gen_obj));
if (! gen_obj)
return ERR_OOM;
my_memset(gen_obj, 0, sizeof(*gen_obj));
gen_obj->module = my_module_find(params);
if (! gen_obj->module) {
err = ERR_MISSING_MODULE;
goto out_free_gen_obj;
}
if (my_typeof(gen_obj->module) != gen_obj_type) {
err = ERR_WRONG_MODULE;
goto out_free_gen_obj;
}
err = gen_obj->module->mod_init(gen_obj, params);
if (err)
goto out_free_gen_obj;
goto out;
out_free_gen_obj:
my_memset(gen_obj, 0, sizeof(*gen_obj));
my_free(gen_obj);
gen_obj = NULL;
out:
/* especially rigorous w/o a macro */
objmaster->gen_obj_field = gen_obj;
}
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.
Any ideas for converting that macro to an inline function?
my_error alloc_text(object_master *om, params* pms)
{
my_errcode err = 0;
text_object *to;
ALLOC_OBJ(om, to, textobj, TEXT_OBJECT_TYPE, pms);
return err;
}
my_error alloc_text(object_master *om, params* pms)
{
my_errcode err = 0;
media_object *mo;
ALLOC_OBJ(om, mo, mediaobj, MEDIA_OBJECT_TYPE, pms);
return err;
}
#define ALLOC_OBJ(objmaster, gen_obj, gen_obj_field, gen_obj_type,
params)
{
my_ASSERT(obj != NULL);
my_ASSERT(params != NULL);
gen_obj = my_malloc(sizeof(*gen_obj));
if (! gen_obj)
return ERR_OOM;
my_memset(gen_obj, 0, sizeof(*gen_obj));
gen_obj->module = my_module_find(params);
if (! gen_obj->module) {
err = ERR_MISSING_MODULE;
goto out_free_gen_obj;
}
if (my_typeof(gen_obj->module) != gen_obj_type) {
err = ERR_WRONG_MODULE;
goto out_free_gen_obj;
}
err = gen_obj->module->mod_init(gen_obj, params);
if (err)
goto out_free_gen_obj;
goto out;
out_free_gen_obj:
my_memset(gen_obj, 0, sizeof(*gen_obj));
my_free(gen_obj);
gen_obj = NULL;
out:
/* especially rigorous w/o a macro */
objmaster->gen_obj_field = gen_obj;
}