mirror of
https://gitee.com/zlgopen/awtk.git
synced 2024-11-30 02:58:26 +08:00
add object_array_remove_value
This commit is contained in:
parent
5f0c217ae6
commit
d119ace50b
@ -554,6 +554,21 @@ tk_object_t* object_array_dup(tk_object_t* obj, uint32_t start, uint32_t end) {
|
||||
return dup;
|
||||
}
|
||||
|
||||
ret_t object_array_remove_value(tk_object_t* obj, value_t* v) {
|
||||
uint32_t i = 0;
|
||||
object_array_t* o = OBJECT_ARRAY(obj);
|
||||
return_value_if_fail(obj != NULL && v != NULL, RET_BAD_PARAMS);
|
||||
|
||||
for (i = 0; i < o->size; i++) {
|
||||
value_t* iter = o->props + i;
|
||||
if (value_equal(iter, v)) {
|
||||
return object_array_remove(obj, i);
|
||||
}
|
||||
}
|
||||
|
||||
return RET_NOT_FOUND;
|
||||
}
|
||||
|
||||
static int value_cmp_as_int(const void* a, const void* b) {
|
||||
return value_int((const value_t*)a) - value_int((const value_t*)b);
|
||||
}
|
||||
|
@ -191,6 +191,20 @@ int32_t object_array_last_index_of(tk_object_t* obj, const value_t* v);
|
||||
*/
|
||||
ret_t object_array_remove(tk_object_t* obj, uint32_t index);
|
||||
|
||||
/**
|
||||
* @method object_array_remove_value
|
||||
*
|
||||
* 删除指定的值。
|
||||
*
|
||||
* @annotation ["scriptable"]
|
||||
* @param {tk_object_t*} obj 对象。
|
||||
* @param {value_t*} v 值。
|
||||
*
|
||||
* @return {ret_t} 返回RET_OK表示成功,否则表示失败。
|
||||
*
|
||||
*/
|
||||
ret_t object_array_remove_value(tk_object_t* obj, value_t* v);
|
||||
|
||||
/**
|
||||
* @method object_array_get_and_remove
|
||||
*
|
||||
|
@ -739,3 +739,31 @@ TEST(ObjectArray, reverse) {
|
||||
str_reset(&s);
|
||||
TK_OBJECT_UNREF(obj);
|
||||
}
|
||||
|
||||
TEST(ObjectArray, push_and_remove) {
|
||||
value_t v;
|
||||
tk_object_t* obj = object_array_create();
|
||||
|
||||
value_set_int(&v, 10);
|
||||
object_array_push(obj, &v);
|
||||
value_set_int(&v, 20);
|
||||
object_array_push(obj, &v);
|
||||
value_set_int(&v, 30);
|
||||
object_array_push(obj, &v);
|
||||
value_set_int(&v, 40);
|
||||
object_array_push(obj, &v);
|
||||
|
||||
value_set_int(&v, 10);
|
||||
ASSERT_EQ(object_array_remove_value(obj, &v), RET_OK);
|
||||
ASSERT_EQ(OBJECT_ARRAY(obj)->size, 3);
|
||||
ASSERT_EQ(object_array_get(obj, 0, &v), RET_OK);
|
||||
ASSERT_EQ(value_int(&v), 20);
|
||||
|
||||
value_set_int(&v, 30);
|
||||
ASSERT_EQ(object_array_remove_value(obj, &v), RET_OK);
|
||||
ASSERT_EQ(OBJECT_ARRAY(obj)->size, 2);
|
||||
ASSERT_EQ(object_array_get(obj, 1, &v), RET_OK);
|
||||
ASSERT_EQ(value_int(&v), 40);
|
||||
|
||||
TK_OBJECT_UNREF(obj);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user