unify array return value

This commit is contained in:
xianjimli 2018-05-04 17:23:21 +08:00
parent f5375677a8
commit 02449e1979
10 changed files with 45 additions and 52 deletions

View File

@ -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) {

View File

@ -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} TRUEFALSE
* @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} TRUEFALSE
* @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} TRUEFALSE
* @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

View File

@ -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) {

View File

@ -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*/
}

View File

@ -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) {

View File

@ -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) {

View File

@ -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*/
}

View File

@ -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) {

View File

@ -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) {

View File

@ -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);