row support typecheck

This commit is contained in:
xianjimli 2019-02-26 11:21:39 +08:00
parent 4ee8a6ab57
commit a2a8ce1f11
3 changed files with 21 additions and 4 deletions

10
src/widgets/row.c Executable file → Normal file
View File

@ -22,15 +22,17 @@
#include "tkc/mem.h" #include "tkc/mem.h"
#include "widgets/row.h" #include "widgets/row.h"
static const widget_vtable_t s_row_vtable = { TK_DECL_VTABLE(row) = {.size = sizeof(row_t),
.size = sizeof(row_t), .type = WIDGET_TYPE_ROW, .create = row_create}; .type = WIDGET_TYPE_ROW,
.parent = TK_PARENT_VTABLE(widget),
.create = row_create};
widget_t* row_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) { widget_t* row_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
return widget_create(parent, &s_row_vtable, x, y, w, h); return widget_create(parent, TK_REF_VTABLE(row), x, y, w, h);
} }
widget_t* row_cast(widget_t* widget) { widget_t* row_cast(widget_t* widget) {
return_value_if_fail(widget != NULL && widget->vt == &s_row_vtable, NULL); return_value_if_fail(WIDGET_IS_INSTANCE_OF(widget, row), NULL);
return widget; return widget;
} }

View File

@ -85,6 +85,11 @@ widget_t* row_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h);
*/ */
widget_t* row_cast(widget_t* widget); widget_t* row_cast(widget_t* widget);
#define ROW(widget) ((row_t*)(row_cast(WIDGET(widget))))
/*public for subclass and runtime type check*/
TK_EXTERN_VTABLE(row);
END_C_DECLS END_C_DECLS
#endif /*TK_ROW_H*/ #endif /*TK_ROW_H*/

10
tests/row_test.cc Normal file
View File

@ -0,0 +1,10 @@
#include "widgets/row.h"
#include "gtest/gtest.h"
TEST(Row, cast) {
widget_t* w = row_create(NULL, 10, 20, 30, 40);
ASSERT_EQ(w, row_cast(w));
widget_destroy(w);
}