Skip to content

Commit bb1a48b

Browse files
author
chenglong
committed
feat(object): add realize lifecycle state
1 parent 1de357b commit bb1a48b

3 files changed

Lines changed: 67 additions & 0 deletions

File tree

include/miniqom/object.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,20 @@
99

1010
#define MINIQOM_MAX_CHILDREN 32
1111

12+
typedef enum ObjectState
13+
{
14+
OBJECT_STATE_NEW,
15+
OBJECT_STATE_REALIZED,
16+
} ObjectState;
17+
1218
struct Object
1319
{
1420
ObjectClass *klass;
1521
Object *parent;
1622
char *name;
1723
Object *children[MINIQOM_MAX_CHILDREN];
1824
size_t child_count;
25+
ObjectState state;
1926
};
2027

2128
Object *object_new(const char *type_name);
@@ -26,6 +33,10 @@ bool object_is_instance_of(const Object *obj, const char *type_name);
2633
const char *object_get_type_name(const Object *obj);
2734
bool object_is_a(const Object *obj, const char *type_name);
2835

36+
bool object_realize(Object *obj, Error *err);
37+
void object_unrealize(Object *obj);
38+
bool object_is_realized(const Object *obj);
39+
2940
bool object_add_child(Object *parent, const char *name,
3041
Object *child, Error *err);
3142

src/qom/object.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,39 @@ Object *object_new(const char *type_name)
5555
return obj;
5656
}
5757

58+
bool object_is_realized(const Object *obj)
59+
{
60+
return obj && obj->state == OBJECT_STATE_REALIZED;
61+
}
62+
63+
bool object_realize(Object *obj, Error *err)
64+
{
65+
if (!obj)
66+
{
67+
error_set(err, "cannot realize null object");
68+
return false;
69+
}
70+
71+
if (obj->state == OBJECT_STATE_REALIZED)
72+
{
73+
error_set(err, "object is already realized");
74+
return false;
75+
}
76+
77+
obj->state = OBJECT_STATE_REALIZED;
78+
return true;
79+
}
80+
81+
void object_unrealize(Object *obj)
82+
{
83+
if (!obj || obj->state != OBJECT_STATE_REALIZED)
84+
{
85+
return;
86+
}
87+
88+
obj->state = OBJECT_STATE_NEW;
89+
}
90+
5891
void object_free(Object *obj)
5992
{
6093
Type *type;

tests/test_qom.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,29 @@ int main(void)
3535
assert(object_get_type_name(NULL) == NULL);
3636
assert(!object_is_a(NULL, "object"));
3737

38+
error_clear(&err);
39+
40+
assert(!object_is_realized(mem0));
41+
42+
assert(object_realize(mem0, &err));
43+
assert(object_is_realized(mem0));
44+
45+
assert(!object_realize(mem0, &err));
46+
assert(!strcmp(err.message, "object is already realized"));
47+
assert(object_is_realized(mem0));
48+
49+
object_unrealize(mem0);
50+
assert(!object_is_realized(mem0));
51+
52+
object_unrealize(mem0);
53+
assert(!object_is_realized(mem0));
54+
55+
assert(object_realize(mem0, &err));
56+
assert(object_is_realized(mem0));
57+
58+
object_unrealize(mem0);
59+
assert(!object_is_realized(mem0));
60+
3861
backend = (HostMemoryBackendMemfd *)mem0;
3962
assert(backend->seal);
4063
assert(backend->parent_obj.share);

0 commit comments

Comments
 (0)