image_animation support typecheck

This commit is contained in:
xianjimli 2019-02-26 14:03:02 +08:00
parent 3fa3353eb9
commit 1945fca149
3 changed files with 23 additions and 12 deletions

View File

@ -169,15 +169,15 @@ static const char* s_image_animation_clone_properties[] = {IMAGE_ANIMATION_PROP_
IMAGE_ANIMATION_PROP_AUTO_PLAY,
NULL};
static const widget_vtable_t s_image_animation_vtable = {
.size = sizeof(image_animation_t),
.type = WIDGET_TYPE_IMAGE_ANIMATION,
.clone_properties = s_image_animation_clone_properties,
.create = image_animation_create,
.on_destroy = image_animation_on_destroy,
.get_prop = image_animation_get_prop,
.set_prop = image_animation_set_prop,
.on_paint_self = image_animation_on_paint_self};
TK_DECL_VTABLE(image_animation) = {.size = sizeof(image_animation_t),
.type = WIDGET_TYPE_IMAGE_ANIMATION,
.clone_properties = s_image_animation_clone_properties,
.parent = TK_PARENT_VTABLE(widget),
.create = image_animation_create,
.on_destroy = image_animation_on_destroy,
.get_prop = image_animation_get_prop,
.set_prop = image_animation_set_prop,
.on_paint_self = image_animation_on_paint_self};
static ret_t image_animation_delay_play(const timer_info_t* info) {
widget_t* widget = WIDGET(info->ctx);
@ -207,7 +207,7 @@ static ret_t image_animation_on_open(void* ctx, event_t* e) {
widget_t* image_animation_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
widget_t* win = widget_get_window(parent);
widget_t* widget = widget_create(parent, &s_image_animation_vtable, x, y, w, h);
widget_t* widget = widget_create(parent, TK_REF_VTABLE(image_animation), x, y, w, h);
image_animation_t* image_animation = IMAGE_ANIMATION(widget);
return_value_if_fail(image_animation != NULL, NULL);
@ -405,7 +405,7 @@ ret_t image_animation_set_format(widget_t* widget, const char* format) {
}
widget_t* image_animation_cast(widget_t* widget) {
return_value_if_fail(widget != NULL && widget->vt == &s_image_animation_vtable, NULL);
return_value_if_fail(WIDGET_IS_INSTANCE_OF(widget, image_animation), NULL);
return widget;
}

View File

@ -308,7 +308,10 @@ widget_t* image_animation_cast(widget_t* widget);
#define IMAGE_ANIMATION_PROP_UNLOAD_AFTER_PAINT "unload_after_paint"
#define WIDGET_TYPE_IMAGE_ANIMATION "image_animation"
#define IMAGE_ANIMATION(widget) ((image_animation_t*)(widget))
#define IMAGE_ANIMATION(widget) ((image_animation_t*)(image_animation_cast(WIDGET(widget))))
/*public for subclass and runtime type check*/
TK_EXTERN_VTABLE(image_animation);
/*public for test*/
ret_t image_animation_update(widget_t* widget);

View File

@ -124,3 +124,11 @@ TEST(ImageAnimation, format) {
widget_destroy(w);
}
TEST(ImageAnimation, cast) {
widget_t* w = image_animation_create(NULL, 10, 20, 30, 40);
ASSERT_EQ(w, image_animation_cast(w));
widget_destroy(w);
}