guage support typecheck

This commit is contained in:
xianjimli 2019-02-26 14:00:56 +08:00
parent 492d2c2751
commit 3fa3353eb9
6 changed files with 55 additions and 26 deletions

View File

@ -102,18 +102,19 @@ static ret_t guage_on_paint_self(widget_t* widget, canvas_t* c) {
return RET_OK;
}
static const widget_vtable_t s_guage_vtable = {.size = sizeof(guage_t),
.type = WIDGET_TYPE_GUAGE,
.clone_properties = s_guage_properties,
.persistent_properties = s_guage_properties,
.create = guage_create,
.on_paint_self = guage_on_paint_self,
.set_prop = guage_set_prop,
.get_prop = guage_get_prop,
.on_destroy = guage_on_destroy};
TK_DECL_VTABLE(guage) = {.size = sizeof(guage_t),
.type = WIDGET_TYPE_GUAGE,
.clone_properties = s_guage_properties,
.persistent_properties = s_guage_properties,
.parent = TK_PARENT_VTABLE(widget),
.create = guage_create,
.on_paint_self = guage_on_paint_self,
.set_prop = guage_set_prop,
.get_prop = guage_get_prop,
.on_destroy = guage_on_destroy};
widget_t* guage_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
widget_t* widget = widget_create(parent, &s_guage_vtable, x, y, w, h);
widget_t* widget = widget_create(parent, TK_REF_VTABLE(guage), x, y, w, h);
guage_t* guage = GUAGE(widget);
return_value_if_fail(guage != NULL, NULL);
@ -124,7 +125,7 @@ widget_t* guage_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
}
widget_t* guage_cast(widget_t* widget) {
return_value_if_fail(widget != NULL && widget->vt == &s_guage_vtable, NULL);
return_value_if_fail(WIDGET_IS_INSTANCE_OF(widget, guage), NULL);
return widget;
}

View File

@ -135,7 +135,10 @@ ret_t guage_set_image(widget_t* widget, const char* name);
ret_t guage_set_draw_type(widget_t* widget, image_draw_type_t draw_type);
#define WIDGET_TYPE_GUAGE "guage"
#define GUAGE(widget) ((guage_t*)(widget))
#define GUAGE(widget) ((guage_t*)(guage_cast(WIDGET(widget))))
/*public for subclass and runtime type check*/
TK_EXTERN_VTABLE(guage);
END_C_DECLS

26
src/ext_widgets/guage/guage_pointer.c Executable file → Normal file
View File

@ -174,24 +174,24 @@ static ret_t guage_pointer_on_paint_self(widget_t* widget, canvas_t* c) {
static const char* s_guage_pointer_properties[] = {GUAGE_POINTER_PROP_ANGLE, WIDGET_PROP_IMAGE,
NULL};
static const widget_vtable_t s_guage_pointer_vtable = {
.size = sizeof(guage_pointer_t),
.type = WIDGET_TYPE_GUAGE_POINTER,
.clone_properties = s_guage_pointer_properties,
.persistent_properties = s_guage_pointer_properties,
.create = guage_pointer_create,
.on_paint_self = guage_pointer_on_paint_self,
.on_paint_background = widget_on_paint_null,
.set_prop = guage_pointer_set_prop,
.get_prop = guage_pointer_get_prop,
.on_destroy = guage_pointer_on_destroy};
TK_DECL_VTABLE(guage_pointer) = {.size = sizeof(guage_pointer_t),
.type = WIDGET_TYPE_GUAGE_POINTER,
.clone_properties = s_guage_pointer_properties,
.persistent_properties = s_guage_pointer_properties,
.parent = TK_PARENT_VTABLE(widget),
.create = guage_pointer_create,
.on_paint_self = guage_pointer_on_paint_self,
.on_paint_background = widget_on_paint_null,
.set_prop = guage_pointer_set_prop,
.get_prop = guage_pointer_get_prop,
.on_destroy = guage_pointer_on_destroy};
widget_t* guage_pointer_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
return widget_create(parent, &s_guage_pointer_vtable, x, y, w, h);
return widget_create(parent, TK_REF_VTABLE(guage_pointer), x, y, w, h);
}
widget_t* guage_pointer_cast(widget_t* widget) {
return_value_if_fail(widget != NULL && widget->vt == &s_guage_pointer_vtable, NULL);
return_value_if_fail(WIDGET_IS_INSTANCE_OF(widget, guage_pointer), NULL);
return widget;
}

View File

@ -130,7 +130,10 @@ ret_t guage_pointer_set_image(widget_t* widget, const char* image);
#define GUAGE_POINTER_PROP_ANGLE "angle"
#define WIDGET_TYPE_GUAGE_POINTER "guage_pointer"
#define GUAGE_POINTER(widget) ((guage_pointer_t*)(widget))
#define GUAGE_POINTER(widget) ((guage_pointer_t*)(guage_pointer_cast(WIDGET(widget))))
/*public for subclass and runtime type check*/
TK_EXTERN_VTABLE(guage_pointer);
END_C_DECLS

View File

@ -0,0 +1,10 @@
#include "guage/guage_pointer.h"
#include "gtest/gtest.h"
TEST(GuagePointer, cast) {
widget_t* w = guage_pointer_create(NULL, 0, 0, 100, 100);
ASSERT_EQ(w, guage_pointer_cast(w));
widget_destroy(w);
}

12
tests/guage_test.cc Normal file
View File

@ -0,0 +1,12 @@
#include "widgets/window.h"
#include "guage/guage.h"
#include "gtest/gtest.h"
TEST(Guage, cast) {
widget_t* w = window_create(NULL, 0, 0, 0, 0);
widget_t* guage = guage_create(w, 0, 0, 100, 100);
ASSERT_EQ(guage, guage_cast(guage));
widget_destroy(w);
}