diff mbox series

[v3,49/53] qom: FIELD_PROP macro

Message ID 20201112214350.872250-50-ehabkost@redhat.com
State New
Headers show
Series Make qdev static property API usable by any QOM type | expand

Commit Message

Eduardo Habkost Nov. 12, 2020, 9:43 p.m. UTC
The FIELD_PROP macro can be used to define a Property struct when
initializing static variables, or as a function parameter (e.g.
as a paramter to object_class_property_add_field()).

The qdev-specific DEFINE_PROP macro can be defined using the new
macro.  The only difference is that DEFINE_PROP gets the property
name as the first argument.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
This is a new patch added in v3 of this series.
---
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: "Daniel P. Berrangé" <berrange@redhat.com>
Cc: Eduardo Habkost <ehabkost@redhat.com>
Cc: qemu-devel@nongnu.org
---
 include/qom/field-property.h | 17 +++++++++++++++++
 include/qom/property-types.h | 26 +++++++++++++++++++-------
 2 files changed, 36 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/include/qom/field-property.h b/include/qom/field-property.h
index a904f98609..0cb1fe2217 100644
--- a/include/qom/field-property.h
+++ b/include/qom/field-property.h
@@ -108,4 +108,21 @@  object_class_property_add_field(ObjectClass *oc, const char *name,
 
 void *object_field_prop_ptr(Object *obj, Property *prop);
 
+/**
+ * FIELD_PROP: Expands to a compound literal for a #Property struct
+ *
+ * @_state: name of the object state structure type
+ * @_field: name of field in @_state
+ * @_prop: name of #PropertyInfo variable with type information
+ * @_type: expected type of field @_field in struct @_state
+ * @...: additional initializers for #Property struct fields
+ */
+#define FIELD_PROP(_state, _field, _prop, _type, ...) \
+    (Property) {                                                 \
+        .info      = &(_prop),                                   \
+        .offset    = offsetof(_state, _field)                    \
+            + type_check(_type, typeof_field(_state, _field)),   \
+        __VA_ARGS__                                              \
+    }
+
 #endif
diff --git a/include/qom/property-types.h b/include/qom/property-types.h
index 46c82da4e3..62551c77e0 100644
--- a/include/qom/property-types.h
+++ b/include/qom/property-types.h
@@ -23,13 +23,25 @@  extern const PropertyInfo prop_info_size32;
 extern const PropertyInfo prop_info_arraylen;
 extern const PropertyInfo prop_info_link;
 
-#define DEFINE_PROP(_name, _state, _field, _prop, _type, ...) {  \
-        .name_template = (_name),                           \
-        .info      = &(_prop),                                   \
-        .offset    = offsetof(_state, _field)                    \
-            + type_check(_type, typeof_field(_state, _field)),   \
-        __VA_ARGS__                                              \
-        }
+/**
+ * DEFINE_PROP: Define a #Property struct, including a property name
+ *
+ * @_name: name of the property
+ * @_state: name of the object state structure type
+ * @_field: name of field in @_state
+ * @_prop: name of #PropertyInfo variable with type information
+ * @_type: expected type of field @_field in struct @_state
+ * @...: additional initializers for #Property struct fields
+ *
+ * `DEFINE_PROP` or other ``DEFINE_PROP_*`` macros are normally
+ * used when initialiing static const #Property arrays, to be
+ * used with object_class_add_field_properties() or
+ * device_class_set_props().
+ */
+#define DEFINE_PROP(_name, _state, _field, _prop, _type, ...) \
+    FIELD_PROP(_state, _field, _prop, _type,                  \
+               .name_template = (_name),                      \
+               __VA_ARGS__)
 
 #define DEFINE_PROP_SIGNED(_name, _state, _field, _defval, _prop, _type) \
     DEFINE_PROP(_name, _state, _field, _prop, _type,                     \