Skip to content

Commit

Permalink
qom: Allow properties to be registered against classes
Browse files Browse the repository at this point in the history
When there are many instances of a given class, registering
properties against the instance is wasteful of resources. The
majority of objects have a statically defined list of possible
properties, so most of the properties are easily registerable
against the class. Only those properties which are conditionally
registered at runtime need be recorded against the klass.

Registering properties against classes also makes it possible
to provide static introspection of QOM - currently introspection
is only possible after creating an instance of a class, which
severely limits its usefulness.

This impl only supports simple scalar properties. It does not
attempt to allow child object / link object properties against
the class. There are ways to support those too, but it would
make this patch more complicated, so it is left as an exercise
for the future.

There is no equivalent to object_property_del() provided, since
classes must be immutable once they are defined.

Signed-off-by: Daniel P. Berrange <[email protected]>
Signed-off-by: Andreas Färber <[email protected]>
  • Loading branch information
berrange authored and afaerber committed Jan 18, 2016
1 parent 4618834 commit 16bf7f5
Show file tree
Hide file tree
Showing 3 changed files with 287 additions and 26 deletions.
46 changes: 45 additions & 1 deletion include/qom/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ struct ObjectClass
const char *class_cast_cache[OBJECT_CLASS_CAST_CACHE];

ObjectUnparent *unparent;

GHashTable *properties;
};

/**
Expand Down Expand Up @@ -944,6 +946,13 @@ ObjectProperty *object_property_add(Object *obj, const char *name,

void object_property_del(Object *obj, const char *name, Error **errp);

ObjectProperty *object_class_property_add(ObjectClass *klass, const char *name,
const char *type,
ObjectPropertyAccessor *get,
ObjectPropertyAccessor *set,
ObjectPropertyRelease *release,
void *opaque, Error **errp);

/**
* object_property_find:
* @obj: the object
Expand All @@ -954,6 +963,8 @@ void object_property_del(Object *obj, const char *name, Error **errp);
*/
ObjectProperty *object_property_find(Object *obj, const char *name,
Error **errp);
ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name,
Error **errp);

typedef struct ObjectPropertyIterator ObjectPropertyIterator;

Expand All @@ -962,7 +973,7 @@ typedef struct ObjectPropertyIterator ObjectPropertyIterator;
* @obj: the object
*
* Initializes an iterator for traversing all properties
* registered against an object instance.
* registered against an object instance, its class and all parent classes.
*
* It is forbidden to modify the property list while iterating,
* whether removing or adding properties.
Expand Down Expand Up @@ -1371,6 +1382,12 @@ void object_property_add_str(Object *obj, const char *name,
void (*set)(Object *, const char *, Error **),
Error **errp);

void object_class_property_add_str(ObjectClass *klass, const char *name,
char *(*get)(Object *, Error **),
void (*set)(Object *, const char *,
Error **),
Error **errp);

/**
* object_property_add_bool:
* @obj: the object to add a property to
Expand All @@ -1387,6 +1404,11 @@ void object_property_add_bool(Object *obj, const char *name,
void (*set)(Object *, bool, Error **),
Error **errp);

void object_class_property_add_bool(ObjectClass *klass, const char *name,
bool (*get)(Object *, Error **),
void (*set)(Object *, bool, Error **),
Error **errp);

/**
* object_property_add_enum:
* @obj: the object to add a property to
Expand All @@ -1406,6 +1428,13 @@ void object_property_add_enum(Object *obj, const char *name,
void (*set)(Object *, int, Error **),
Error **errp);

void object_class_property_add_enum(ObjectClass *klass, const char *name,
const char *typename,
const char * const *strings,
int (*get)(Object *, Error **),
void (*set)(Object *, int, Error **),
Error **errp);

/**
* object_property_add_tm:
* @obj: the object to add a property to
Expand All @@ -1420,6 +1449,10 @@ void object_property_add_tm(Object *obj, const char *name,
void (*get)(Object *, struct tm *, Error **),
Error **errp);

void object_class_property_add_tm(ObjectClass *klass, const char *name,
void (*get)(Object *, struct tm *, Error **),
Error **errp);

/**
* object_property_add_uint8_ptr:
* @obj: the object to add a property to
Expand All @@ -1432,6 +1465,8 @@ void object_property_add_tm(Object *obj, const char *name,
*/
void object_property_add_uint8_ptr(Object *obj, const char *name,
const uint8_t *v, Error **errp);
void object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name,
const uint8_t *v, Error **errp);

/**
* object_property_add_uint16_ptr:
Expand All @@ -1445,6 +1480,8 @@ void object_property_add_uint8_ptr(Object *obj, const char *name,
*/
void object_property_add_uint16_ptr(Object *obj, const char *name,
const uint16_t *v, Error **errp);
void object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name,
const uint16_t *v, Error **errp);

/**
* object_property_add_uint32_ptr:
Expand All @@ -1458,6 +1495,8 @@ void object_property_add_uint16_ptr(Object *obj, const char *name,
*/
void object_property_add_uint32_ptr(Object *obj, const char *name,
const uint32_t *v, Error **errp);
void object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name,
const uint32_t *v, Error **errp);

/**
* object_property_add_uint64_ptr:
Expand All @@ -1471,6 +1510,8 @@ void object_property_add_uint32_ptr(Object *obj, const char *name,
*/
void object_property_add_uint64_ptr(Object *obj, const char *name,
const uint64_t *v, Error **Errp);
void object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name,
const uint64_t *v, Error **Errp);

/**
* object_property_add_alias:
Expand Down Expand Up @@ -1522,6 +1563,9 @@ void object_property_add_const_link(Object *obj, const char *name,
*/
void object_property_set_description(Object *obj, const char *name,
const char *description, Error **errp);
void object_class_property_set_description(ObjectClass *klass, const char *name,
const char *description,
Error **errp);

/**
* object_child_foreach:
Expand Down
Loading

0 comments on commit 16bf7f5

Please sign in to comment.