From 02449e197904ac9b95aa3def171e6bf2a956b799 Mon Sep 17 00:00:00 2001 From: xianjimli Date: Fri, 4 May 2018 17:23:21 +0800 Subject: [PATCH] unify array return value --- src/base/array.c | 22 +++++++++++----------- src/base/array.h | 12 ++++++------ src/base/font_manager.c | 5 ++--- src/base/idle.c | 4 ++-- src/base/image_manager.c | 4 +--- src/base/resource_manager.c | 6 ++---- src/base/timer.c | 4 ++-- src/base/widget.c | 4 ++-- src/base/window_manager.c | 8 +++----- tests/array_test.cc | 28 ++++++++++++++-------------- 10 files changed, 45 insertions(+), 52 deletions(-) diff --git a/src/base/array.c b/src/base/array.c index 1631c292c..56e1948ae 100644 --- a/src/base/array.c +++ b/src/base/array.c @@ -95,16 +95,16 @@ int array_find_index(array_t* array, compare_t cmp, void* data) { return -1; } -bool_t array_remove(array_t* array, compare_t cmp, void* data) { +ret_t array_remove(array_t* array, compare_t cmp, void* data) { int32_t i = 0; int32_t pos = 0; int32_t size = 0; void** elms = NULL; - return_value_if_fail(array != NULL, FALSE); + return_value_if_fail(array != NULL, RET_BAD_PARAMS); pos = array_find_index(array, cmp, data); if (pos < 0) { - return FALSE; + return RET_NOT_FOUND; } elms = array->elms; @@ -115,16 +115,16 @@ bool_t array_remove(array_t* array, compare_t cmp, void* data) { elms[i] = NULL; array->size--; - return TRUE; + return RET_OK; } -bool_t array_remove_all(array_t* array, compare_t cmp, void* data, destroy_t destroy) { +ret_t array_remove_all(array_t* array, compare_t cmp, void* data, destroy_t destroy) { int32_t i = 0; int32_t k = 0; int32_t size = 0; void** elms = NULL; - return_value_if_fail(array != NULL, FALSE); + return_value_if_fail(array != NULL, RET_BAD_PARAMS); elms = array->elms; size = array->size; @@ -145,7 +145,7 @@ bool_t array_remove_all(array_t* array, compare_t cmp, void* data, destroy_t des } array->size = k; - return TRUE; + return RET_OK; } void* array_find(array_t* array, compare_t cmp, void* data) { @@ -163,13 +163,13 @@ void* array_pop(array_t* array) { return array->elms[--array->size]; } -bool_t array_push(array_t* array, void* data) { - return_value_if_fail(array != NULL, FALSE); - return_value_if_fail(array_extend(array), FALSE); +ret_t array_push(array_t* array, void* data) { + return_value_if_fail(array != NULL, RET_BAD_PARAMS); + return_value_if_fail(array_extend(array), RET_OOM); array->elms[array->size++] = data; - return TRUE; + return RET_OK; } void array_deinit(array_t* array) { diff --git a/src/base/array.h b/src/base/array.h index 87043f4c0..d5e38aa13 100644 --- a/src/base/array.h +++ b/src/base/array.h @@ -85,9 +85,9 @@ int array_find_index(array_t* array, compare_t cmp, void* ctx); * @param {compare_t} cmp 比较函数,为NULL时直接比较指针。 * @param {void*} ctx 比较函数的上下文。 * - * @return {bool_t} 成功返回TRUE,失败返回FALSE。 + * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 */ -bool_t array_remove(array_t* array, compare_t cmp, void* ctx); +ret_t array_remove(array_t* array, compare_t cmp, void* ctx); /** * @method array_remove_all @@ -97,9 +97,9 @@ bool_t array_remove(array_t* array, compare_t cmp, void* ctx); * @param {void*} ctx 比较函数的上下文。 * @param {destroy_t} destroy 销毁元素的回调函数。 * - * @return {bool_t} 成功返回TRUE,失败返回FALSE。 + * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 */ -bool_t array_remove_all(array_t* array, compare_t cmp, void* ctx, destroy_t destroy); +ret_t array_remove_all(array_t* array, compare_t cmp, void* ctx, destroy_t destroy); /** * @method array_pop @@ -116,9 +116,9 @@ void* array_pop(array_t* array); * @param {array_t*} array 数组对象。 * @param {void*} data 待追加的元素。 * - * @return {bool_t} 成功返回TRUE,失败返回FALSE。 + * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 */ -bool_t array_push(array_t* array, void* data); +ret_t array_push(array_t* array, void* data); /** * @method array_deinit diff --git a/src/base/font_manager.c b/src/base/font_manager.c index ca3fe711f..8a1549eb3 100644 --- a/src/base/font_manager.c +++ b/src/base/font_manager.c @@ -49,9 +49,8 @@ font_manager_t* font_manager_init(font_manager_t* fm) { ret_t font_manager_add(font_manager_t* fm, font_t* font) { return_value_if_fail(fm != NULL && font != NULL, RET_BAD_PARAMS); - array_push(&(fm->fonts), font); - - return RET_OK; + + return array_push(&(fm->fonts), font); } font_t* font_manager_find(font_manager_t* fm, const char* name, uint16_t size) { diff --git a/src/base/idle.c b/src/base/idle.c index 39f35293b..acd43c33b 100644 --- a/src/base/idle.c +++ b/src/base/idle.c @@ -48,7 +48,7 @@ uint32_t idle_add(idle_func_t on_idle, void* ctx) { idle->id = s_idle_id++; idle->on_idle = on_idle; - return array_push(s_idle_manager, idle) ? idle->id : 0; + return array_push(s_idle_manager, idle) == RET_OK ? idle->id : 0; } static int compare_idle(const void* a, const void* b) { @@ -72,7 +72,7 @@ ret_t idle_remove(uint32_t idle_id) { ret = (idle_info_t*)array_find(s_idle_manager, compare_idle, &idle); return_value_if_fail(ret != NULL, RET_NOT_FOUND); - if (array_remove(s_idle_manager, compare_idle, &idle)) { + if (array_remove(s_idle_manager, compare_idle, &idle) == RET_OK) { memset(ret, 0x00, sizeof(idle_info_t)); /*remove it when dispatch*/ } diff --git a/src/base/image_manager.c b/src/base/image_manager.c index 79e17bb82..5843c948b 100644 --- a/src/base/image_manager.c +++ b/src/base/image_manager.c @@ -71,9 +71,7 @@ ret_t image_manager_add(image_manager_t* imm, const char* name, const bitmap_t* cache->last_access_time = cache->created_time; cache->image.name = cache->name; - array_push(&(imm->images), cache); - - return RET_OK; + return array_push(&(imm->images), cache); } ret_t image_manager_lookup(image_manager_t* imm, const char* name, bitmap_t* image) { diff --git a/src/base/resource_manager.c b/src/base/resource_manager.c index 192a0fe67..8d1fedc4c 100644 --- a/src/base/resource_manager.c +++ b/src/base/resource_manager.c @@ -232,7 +232,7 @@ ret_t resource_manager_add(resource_manager_t* rm, const void* info) { resource_info_ref((resource_info_t*)r); - return array_push(&(rm->resources), (void*)r) ? RET_OK : RET_FAIL; + return array_push(&(rm->resources), (void*)r); } const resource_info_t* resource_manager_find_in_cache(resource_manager_t* rm, resource_type_t type, @@ -290,9 +290,7 @@ ret_t resource_manager_clear_cache(resource_manager_t* rm, resource_type_t type) resource_type_t res = {type}; return_value_if_fail(rm != NULL, RET_BAD_PARAMS); - return array_remove_all(&(rm->resources), res_cmp_type, &res, (destroy_t)resource_info_unref) - ? RET_OK - : RET_FAIL; + return array_remove_all(&(rm->resources), res_cmp_type, &res, (destroy_t)resource_info_unref); } ret_t resource_manager_deinit(resource_manager_t* rm) { diff --git a/src/base/timer.c b/src/base/timer.c index 3c595dc65..37cc41061 100644 --- a/src/base/timer.c +++ b/src/base/timer.c @@ -58,7 +58,7 @@ uint32_t timer_add(timer_func_t on_timer, void* ctx, uint32_t duration_ms) { timer->on_timer = on_timer; timer->duration_ms = duration_ms; - return array_push(s_timer_manager, timer) ? timer->id : 0; + return array_push(s_timer_manager, timer) == RET_OK ? timer->id : 0; } static int compare_timer(const void* a, const void* b) { @@ -81,7 +81,7 @@ ret_t timer_remove(uint32_t timer_id) { timer.id = timer_id; ret = (timer_info_t*)array_find(s_timer_manager, compare_timer, &timer); return_value_if_fail(ret != NULL, RET_NOT_FOUND); - if (array_remove(s_timer_manager, compare_timer, &timer)) { + if (array_remove(s_timer_manager, compare_timer, &timer) == RET_OK) { memset(ret, 0x00, sizeof(timer_info_t)); /*will be removed on next check*/ } diff --git a/src/base/widget.c b/src/base/widget.c index f681a9b82..a8721a7ba 100644 --- a/src/base/widget.c +++ b/src/base/widget.c @@ -207,7 +207,7 @@ ret_t widget_add_child(widget_t* widget, widget_t* child) { widget->children = array_create(4); } - return array_push(widget->children, child) ? RET_OK : RET_FAIL; + return array_push(widget->children, child); } ret_t widget_remove_child(widget_t* widget, widget_t* child) { @@ -221,7 +221,7 @@ ret_t widget_remove_child(widget_t* widget, widget_t* child) { widget->key_target = NULL; } - return array_remove(widget->children, NULL, child) ? RET_OK : RET_NOT_FOUND; + return array_remove(widget->children, NULL, child); } static widget_t* widget_lookup_child(widget_t* widget, const char* name) { diff --git a/src/base/window_manager.c b/src/base/window_manager.c index 1043cd480..4905f9185 100644 --- a/src/base/window_manager.c +++ b/src/base/window_manager.c @@ -292,17 +292,15 @@ widget_t* window_manager_create(void) { static ret_t window_manager_grab(widget_t* widget, widget_t* child) { window_manager_t* wm = WINDOW_MANAGER(widget); return_value_if_fail(widget != NULL && child != NULL, RET_BAD_PARAMS); - array_push(&(wm->graps), child); - - return RET_OK; + + return array_push(&(wm->graps), child); } static ret_t window_manager_ungrab(widget_t* widget, widget_t* child) { window_manager_t* wm = WINDOW_MANAGER(widget); return_value_if_fail(widget != NULL && child != NULL, RET_BAD_PARAMS); - array_remove(&(wm->graps), NULL, child); - return RET_OK; + return array_remove(&(wm->graps), NULL, child); } static ret_t window_manager_invalidate(widget_t* widget, rect_t* r) { diff --git a/tests/array_test.cc b/tests/array_test.cc index 5fd145080..fbfcecc2e 100644 --- a/tests/array_test.cc +++ b/tests/array_test.cc @@ -19,7 +19,7 @@ static int pointer_cmp(const void* p1, const void* p2) { } static void test_add(array_t* array, void* pv) { - ASSERT_EQ(TRUE, array_push(array, pv)); + ASSERT_EQ(RET_OK, array_push(array, pv)); ASSERT_EQ(pv, array_find(array, pointer_cmp, pv)); } @@ -31,7 +31,7 @@ TEST(OArrayTest, popPush) { array_init(&array, 10); for (i = 0; i < ARRAY_SIZE(cases); i++) { - ASSERT_EQ(TRUE, array_push(&array, p + cases[i])); + ASSERT_EQ(RET_OK, array_push(&array, p + cases[i])); ASSERT_EQ(i + 1, array.size); } @@ -63,7 +63,7 @@ TEST(OArrayTest, random) { long k = random() % array.size; void* pv = array.elms[k]; - ASSERT_EQ(TRUE, array_remove(&array, pointer_cmp, pv)); + ASSERT_EQ(RET_OK, array_remove(&array, pointer_cmp, pv)); } ASSERT_EQ(array.size, 0); @@ -87,7 +87,7 @@ TEST(OArrayTest, inc) { long k = random() % array.size; void* pv = array.elms[k]; - ASSERT_EQ(TRUE, array_remove(&array, pointer_cmp, pv)); + ASSERT_EQ(RET_OK, array_remove(&array, pointer_cmp, pv)); } ASSERT_EQ(array.size, 0); @@ -112,7 +112,7 @@ TEST(OArrayTest, dec) { void* pv = array.elms[k]; ASSERT_EQ(pv, array_find(&array, pointer_cmp, pv)); - ASSERT_EQ(TRUE, array_remove(&array, pointer_cmp, pv)); + ASSERT_EQ(RET_OK, array_remove(&array, pointer_cmp, pv)); ASSERT_EQ(NULL, array_find(&array, pointer_cmp, pv)); } ASSERT_EQ(array.size, 0); @@ -128,33 +128,33 @@ TEST(OArrayTest, removeAll) { array_init(&array, 10); for (i = 0; i < ARRAY_SIZE(cases); i++) { - ASSERT_EQ(TRUE, array_push(&array, p + cases[i])); + ASSERT_EQ(RET_OK, array_push(&array, p + cases[i])); ASSERT_EQ(i + 1, array.size); } ASSERT_EQ(array.size, ARRAY_SIZE(cases)); - ASSERT_EQ(array_remove_all(&array, NULL, p + 2, NULL), TRUE); + ASSERT_EQ(array_remove_all(&array, NULL, p + 2, NULL), RET_OK); ASSERT_EQ(array.size, ARRAY_SIZE(cases) - 4); - ASSERT_EQ(array_remove_all(&array, NULL, p + 1, NULL), TRUE); + ASSERT_EQ(array_remove_all(&array, NULL, p + 1, NULL), RET_OK); ASSERT_EQ(array.size, ARRAY_SIZE(cases) - 4 - 2); - ASSERT_EQ(array_remove_all(&array, NULL, p + 12, NULL), TRUE); + ASSERT_EQ(array_remove_all(&array, NULL, p + 12, NULL), RET_OK); ASSERT_EQ(array.size, ARRAY_SIZE(cases) - 4 - 2 - 2); - ASSERT_EQ(array_remove_all(&array, NULL, p + 13, NULL), TRUE); + ASSERT_EQ(array_remove_all(&array, NULL, p + 13, NULL), RET_OK); ASSERT_EQ(array.size, ARRAY_SIZE(cases) - 4 - 2 - 2 - 1); - ASSERT_EQ(array_remove_all(&array, NULL, p + 14, NULL), TRUE); + ASSERT_EQ(array_remove_all(&array, NULL, p + 14, NULL), RET_OK); ASSERT_EQ(array.size, ARRAY_SIZE(cases) - 4 - 2 - 2 - 1 - 1); - ASSERT_EQ(array_remove_all(&array, NULL, p + 33, NULL), TRUE); + ASSERT_EQ(array_remove_all(&array, NULL, p + 33, NULL), RET_OK); ASSERT_EQ(array.size, ARRAY_SIZE(cases) - 4 - 2 - 2 - 1 - 1 - 1); - ASSERT_EQ(array_remove_all(&array, NULL, p + 34, NULL), TRUE); + ASSERT_EQ(array_remove_all(&array, NULL, p + 34, NULL), RET_OK); ASSERT_EQ(array.size, ARRAY_SIZE(cases) - 4 - 2 - 2 - 1 - 1 - 1 - 2); - ASSERT_EQ(array_remove_all(&array, NULL, p + 0, NULL), TRUE); + ASSERT_EQ(array_remove_all(&array, NULL, p + 0, NULL), RET_OK); ASSERT_EQ(array.size, ARRAY_SIZE(cases) - 4 - 2 - 2 - 1 - 1 - 1 - 2); array_deinit(&array);