From 5f993286a2d2e6a863b66ab961b6755fe4d81b36 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Wed, 24 Mar 2021 03:29:56 -0700 Subject: [PATCH] minor fix for obj class (#2154) * fix(class): correct the typo error in comment * fix(class): return directly if the memory allocation fail * fix(class): remove the unused code * fix(class): remove parent argument from lv_obj_construct since this argumnet is really used * fix(class): replace stddef.h with stdint.h since uint32_t is defined in stdint.h --- src/core/lv_obj_class.c | 18 +++++++----------- src/core/lv_obj_class.h | 14 +++++++------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/core/lv_obj_class.c b/src/core/lv_obj_class.c index 54497f02e..8c52b66fb 100644 --- a/src/core/lv_obj_class.c +++ b/src/core/lv_obj_class.c @@ -24,7 +24,7 @@ /********************** * STATIC PROTOTYPES **********************/ -static void lv_obj_construct(lv_obj_t * obj, lv_obj_t * parent, const lv_obj_t * copy); +static void lv_obj_construct(lv_obj_t * obj, const lv_obj_t * copy); static uint32_t get_instance_size(const lv_obj_class_t * class_p); /********************** @@ -44,14 +44,12 @@ lv_obj_t * lv_obj_create_from_class(const lv_obj_class_t * class_p, lv_obj_t * p LV_TRACE_OBJ_CREATE("Creating object with %p class on %p parent", class_p, parent); uint32_t s = get_instance_size(class_p); lv_obj_t * obj = lv_mem_alloc(s); + if(obj == NULL) return NULL; lv_memset_00(obj, s); obj->class_p = class_p; obj->parent = parent; - const lv_obj_class_t * class_start = class_p; - while(class_start && class_start->constructor_cb == NULL) class_start = class_start->base_class; - - lv_obj_construct(obj, parent, copy); + lv_obj_construct(obj, copy); if(parent) { /*Send a Call the ancestor's event handler to the parent to notify it about the new child. @@ -63,7 +61,6 @@ lv_obj_t * lv_obj_create_from_class(const lv_obj_class_t * class_p, lv_obj_t * p } if(!copy) lv_theme_apply(obj); -// else lv_style_list_copy(&checkbox->style_indic, &checkbox_copy->style_indic); LV_TRACE_OBJ_CREATE("Object created at %p address with %p class on %p parent", obj, class_p, parent); return obj; @@ -74,7 +71,7 @@ void _lv_obj_destruct(lv_obj_t * obj) if(obj->class_p->destructor_cb) obj->class_p->destructor_cb(obj); if(obj->class_p->base_class) { - /*Don't let the descendant methods run during constructing the ancestor type*/ + /*Don't let the descendant methods run during destructing the ancestor type*/ obj->class_p = obj->class_p->base_class; /*Call the base class's destructor too*/ @@ -98,7 +95,7 @@ bool lv_obj_is_editable(struct _lv_obj_t * obj) * STATIC FUNCTIONS **********************/ -static void lv_obj_construct(lv_obj_t * obj, lv_obj_t * parent, const lv_obj_t * copy) +static void lv_obj_construct(lv_obj_t * obj, const lv_obj_t * copy) { const lv_obj_class_t * original_class_p = obj->class_p; @@ -107,16 +104,15 @@ static void lv_obj_construct(lv_obj_t * obj, lv_obj_t * parent, const lv_obj_t * obj->class_p = obj->class_p->base_class; /*Construct the base first*/ - lv_obj_construct(obj, parent, copy); + lv_obj_construct(obj, copy); } /*Restore the original class*/ obj->class_p = original_class_p; if(obj->class_p->constructor_cb) obj->class_p->constructor_cb(obj, copy); - - } + static uint32_t get_instance_size(const lv_obj_class_t * class_p) { /*Find a base in which instance size is set*/ diff --git a/src/core/lv_obj_class.h b/src/core/lv_obj_class.h index 3550cb220..70ddd5cac 100644 --- a/src/core/lv_obj_class.h +++ b/src/core/lv_obj_class.h @@ -1,5 +1,5 @@ /** - * @file struct _lv_obj_tree.h + * @file lv_obj_class.h * */ @@ -13,7 +13,7 @@ extern "C" { /********************* * INCLUDES *********************/ -#include +#include #include /********************* @@ -28,7 +28,7 @@ extern "C" { struct _lv_obj_t; typedef enum { - LV_OBJ_CLASS_EDITABLE_INHERIT, /**< Check the base class. Must have 0 value to let he zero initialized class inherit*/ + LV_OBJ_CLASS_EDITABLE_INHERIT, /**< Check the base class. Must have 0 value to let zero initialized class inherit*/ LV_OBJ_CLASS_EDITABLE_TRUE, LV_OBJ_CLASS_EDITABLE_FALSE, }lv_obj_class_editable_t; @@ -37,13 +37,13 @@ typedef enum { * Describe the common methods of every object. * Similar to a C++ class. */ -typedef struct _lv_obj_class_t{ +typedef struct _lv_obj_class_t { const struct _lv_obj_class_t * base_class; void (*constructor_cb)(struct _lv_obj_t * obj, const struct _lv_obj_t * copy); void (*destructor_cb)(struct _lv_obj_t * obj); lv_event_cb_t event_cb; /**< Object type specific event function*/ - uint32_t editable :2; /**< Value from ::lv_obj_class_editable_t*/ - uint32_t instance_size :20; + uint32_t editable : 2; /**< Value from ::lv_obj_class_editable_t*/ + uint32_t instance_size : 20; }lv_obj_class_t; /********************** @@ -52,7 +52,7 @@ typedef struct _lv_obj_class_t{ /** * Create an object form a class descriptor - * @param class_p pointer to a class + * @param class_p pointer to a class * @param parent pointer to an object where the new object should be created * @param copy pointer to an other object with the same type to copy (DEPRECATED will be removed in v9) * @return pointer to the created object