Skip to content

Commit 1de357b

Browse files
author
chenglong
committed
feat(object): add public type query APIs
1 parent 35eacf4 commit 1de357b

4 files changed

Lines changed: 37 additions & 3 deletions

File tree

include/miniqom/object.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ void object_free(Object *obj);
2323

2424
bool object_is_instance_of(const Object *obj, const char *type_name);
2525

26+
const char *object_get_type_name(const Object *obj);
27+
bool object_is_a(const Object *obj, const char *type_name);
28+
2629
bool object_add_child(Object *parent, const char *name,
2730
Object *child, Error *err);
2831

src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static void dump_tree(const Object *obj, unsigned depth)
1616

1717
printf("%s (%s)\n",
1818
obj->name ? obj->name : "<root>",
19-
type_get_info(obj->klass->type)->name);
19+
object_get_type_name(obj));
2020

2121
for (size_t i = 0; i < obj->child_count; i++)
2222
{

src/qom/object.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,26 @@ void object_free(Object *obj)
8383
free(obj);
8484
}
8585

86-
bool object_is_instance_of(const Object *obj, const char *type_name)
86+
const char *object_get_type_name(const Object *obj)
87+
{
88+
if (!obj || !obj->klass || !obj->klass->type)
89+
{
90+
return NULL;
91+
}
92+
93+
return type_get_info(obj->klass->type)->name;
94+
}
95+
96+
bool object_is_a(const Object *obj, const char *type_name)
8797
{
88-
for (Type *type = obj->klass->type; type; type = type_get_parent(type))
98+
Type *type;
99+
100+
if (!obj || !type_name || !obj->klass || !obj->klass->type)
101+
{
102+
return false;
103+
}
104+
105+
for (type = obj->klass->type; type; type = type_get_parent(type))
89106
{
90107
if (!strcmp(type_get_info(type)->name, type_name))
91108
{
@@ -96,6 +113,11 @@ bool object_is_instance_of(const Object *obj, const char *type_name)
96113
return false;
97114
}
98115

116+
bool object_is_instance_of(const Object *obj, const char *type_name)
117+
{
118+
return object_is_a(obj, type_name);
119+
}
120+
99121
bool object_add_child(Object *parent, const char *name,
100122
Object *child, Error *err)
101123
{

tests/test_qom.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ int main(void)
2525
assert(object_is_instance_of(mem0, TYPE_MEMORY_BACKEND_MEMFD));
2626
assert(object_is_instance_of(mem0, TYPE_MEMORY_BACKEND));
2727
assert(object_is_instance_of(mem0, "object"));
28+
assert(!strcmp(object_get_type_name(mem0),
29+
TYPE_MEMORY_BACKEND_MEMFD));
30+
31+
assert(object_is_a(mem0, TYPE_MEMORY_BACKEND_MEMFD));
32+
assert(object_is_a(mem0, TYPE_MEMORY_BACKEND));
33+
assert(object_is_a(mem0, "object"));
34+
assert(!object_is_a(mem0, "unknown-type"));
35+
assert(object_get_type_name(NULL) == NULL);
36+
assert(!object_is_a(NULL, "object"));
2837

2938
backend = (HostMemoryBackendMemfd *)mem0;
3039
assert(backend->seal);

0 commit comments

Comments
 (0)