/** * @brief Creates a new object: it calls the corresponding constructors * (from the constructor of the base class to the constructor of the more * derived class) and then sets the values of the given properties * * @param object_type the type of object to create * @return the created object */ rtgui_object_t *rtgui_object_create(constrtgui_type_t *object_type) { rtgui_object_t *new_object;
if (!object_type) return RT_NULL;
new_object = rtgui_malloc(object_type->size); if (new_object == RT_NULL) return RT_NULL;
voidrtgui_type_object_construct(constrtgui_type_t *type, rtgui_object_t *object) { /* construct from parent to children */ if (type->parent != RT_NULL) rtgui_type_object_construct(type->parent, object);
if (type->constructor) type->constructor(object); }
/** * @brief Destroys the object. * * The object destructors will be called in inherited type order. * * @param object the object to destroy */ voidrtgui_object_destroy(rtgui_object_t *object) { if (!object || object->flag & RTGUI_OBJECT_FLAG_STATIC) return;
voidrtgui_type_destructors_call(constrtgui_type_t *type, rtgui_object_t *object) { /* destruct from children to parent */ if (type->destructor) type->destructor(object);
if (type->parent) rtgui_type_destructors_call(type->parent, object); }
/* Destroys the object */ staticvoid _rtgui_object_destructor(rtgui_object_t *object) { /* Any valid objest should both have valid flag _and_ valid type. Only use * flag is not enough because the chunk of memory may be reallocted to other * object and thus the flag will become valid. */ object->flag = RTGUI_OBJECT_FLAG_NONE; object->type = RT_NULL; }