Skip to content

Commit 8ec21d1

Browse files
author
chenglong
committed
feat(object): expose object tree query APIs
1 parent 6b5b057 commit 8ec21d1

4 files changed

Lines changed: 73 additions & 3 deletions

File tree

include/miniqom/object.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ bool object_realize(Object *obj, Error *err);
3737
void object_unrealize(Object *obj);
3838
bool object_is_realized(const Object *obj);
3939

40+
Object *object_get_parent(const Object *obj);
41+
const char *object_get_name(const Object *obj);
42+
size_t object_get_child_count(const Object *obj);
43+
Object *object_get_child(const Object *obj, size_t index);
44+
Object *object_find_child(Object *parent, const char *name);
45+
4046
bool object_add_child(Object *parent, const char *name,
4147
Object *child, Error *err);
4248

src/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ static void dump_tree(const Object *obj, unsigned depth)
1515
}
1616

1717
printf("%s (%s)\n",
18-
obj->name ? obj->name : "<root>",
18+
object_get_name(obj),
1919
object_get_type_name(obj));
2020

2121
for (size_t i = 0; i < obj->child_count; i++)
2222
{
23-
dump_tree(obj->children[i], depth + 1);
23+
dump_tree(object_get_child(obj, i), depth + 1);
2424
}
2525
}
2626

src/qom/object.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,36 @@ bool object_is_instance_of(const Object *obj, const char *type_name)
213213
return object_is_a(obj, type_name);
214214
}
215215

216+
Object *object_get_parent(const Object *obj)
217+
{
218+
return obj ? obj->parent : NULL;
219+
}
220+
221+
const char *object_get_name(const Object *obj)
222+
{
223+
if (!obj || !obj->name)
224+
{
225+
return "";
226+
}
227+
228+
return obj->name;
229+
}
230+
231+
size_t object_get_child_count(const Object *obj)
232+
{
233+
return obj ? obj->child_count : 0;
234+
}
235+
236+
Object *object_get_child(const Object *obj, size_t index)
237+
{
238+
if (!obj || index >= obj->child_count)
239+
{
240+
return NULL;
241+
}
242+
243+
return obj->children[index];
244+
}
245+
216246
bool object_add_child(Object *parent, const char *name,
217247
Object *child, Error *err)
218248
{
@@ -237,8 +267,13 @@ bool object_add_child(Object *parent, const char *name,
237267
return true;
238268
}
239269

240-
static Object *object_find_child(Object *obj, const char *name)
270+
Object *object_find_child(Object *obj, const char *name)
241271
{
272+
if (!obj || !name)
273+
{
274+
return NULL;
275+
}
276+
242277
for (size_t i = 0; i < obj->child_count; i++)
243278
{
244279
if (!strcmp(obj->children[i]->name, name))

tests/test_qom.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,35 @@ static void test_object_tree(void)
307307
assert(object_add_child(objects, "mem0", mem0, &err));
308308
assert(object_resolve_path(root, "/objects/mem0") == mem0);
309309

310+
assert(object_get_parent(objects) == root);
311+
assert(object_get_parent(mem0) == objects);
312+
313+
assert(!strcmp(object_get_name(root), ""));
314+
assert(!strcmp(object_get_name(objects), "objects"));
315+
assert(!strcmp(object_get_name(mem0), "mem0"));
316+
317+
assert(object_get_child_count(root) == 1);
318+
assert(object_get_child_count(objects) == 1);
319+
assert(object_get_child_count(mem0) == 0);
320+
321+
assert(object_get_child(root, 0) == objects);
322+
assert(object_get_child(objects, 0) == mem0);
323+
assert(object_get_child(mem0, 0) == NULL);
324+
325+
assert(object_find_child(root, "objects") == objects);
326+
assert(object_find_child(objects, "mem0") == mem0);
327+
assert(object_find_child(root, "missing") == NULL);
328+
329+
assert(object_get_parent(NULL) == NULL);
330+
assert(!strcmp(object_get_name(NULL), ""));
331+
assert(object_get_child_count(NULL) == 0);
332+
assert(object_get_child(NULL, 0) == NULL);
333+
assert(object_find_child(NULL, "objects") == NULL);
334+
assert(object_find_child(root, NULL) == NULL);
335+
336+
assert(object_get_child(root, 1) == NULL);
337+
assert(object_get_child(objects, 1) == NULL);
338+
310339
object_free(root);
311340
}
312341

0 commit comments

Comments
 (0)