[RFC PATCH 2/5] qdev-properties: Add OptionalBool QAPI type

Philippe Mathieu-Daudé posted 5 patches 10 months, 4 weeks ago
Maintainers: Peter Maydell <peter.maydell@linaro.org>, Paolo Bonzini <pbonzini@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, Eduardo Habkost <eduardo@habkost.net>, Eric Blake <eblake@redhat.com>, Markus Armbruster <armbru@redhat.com>
[RFC PATCH 2/5] qdev-properties: Add OptionalBool QAPI type
Posted by Philippe Mathieu-Daudé 10 months, 4 weeks ago
To be able to distinct whether a boolean qdev property
has been set or not, add the DEFINE_PROP_BOOL_NODEFAULT()
qdev macro based on the tri-state OptionalBool QAPI type.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 qapi/common.json             | 16 ++++++++++++++++
 include/hw/qdev-properties.h |  5 +++++
 hw/core/qdev-properties.c    | 10 ++++++++++
 3 files changed, 31 insertions(+)

diff --git a/qapi/common.json b/qapi/common.json
index 6fed9cde1a..884c143e2a 100644
--- a/qapi/common.json
+++ b/qapi/common.json
@@ -207,3 +207,19 @@
 ##
 { 'struct': 'HumanReadableText',
   'data': { 'human-readable-text': 'str' } }
+
+##
+# @OptionalBool:
+#
+# An enumeration of three options: true, false, and unset
+#
+# @unset: Unset (default)
+#
+# @false: False
+#
+# @true: True
+#
+# Since: 9.0
+##
+{ 'enum': 'OptionalBool',
+  'data': [ 'false', 'true', 'unset' ] }
diff --git a/include/hw/qdev-properties.h b/include/hw/qdev-properties.h
index 0e1930177e..8cf95da2c3 100644
--- a/include/hw/qdev-properties.h
+++ b/include/hw/qdev-properties.h
@@ -49,6 +49,7 @@ struct PropertyInfo {
 extern const PropertyInfo qdev_prop_bit;
 extern const PropertyInfo qdev_prop_bit64;
 extern const PropertyInfo qdev_prop_bool;
+extern const PropertyInfo qdev_prop_bool_unset;
 extern const PropertyInfo qdev_prop_enum;
 extern const PropertyInfo qdev_prop_uint8;
 extern const PropertyInfo qdev_prop_uint16;
@@ -105,6 +106,10 @@ extern const PropertyInfo qdev_prop_link;
                 .set_default = true,                         \
                 .defval.u    = (bool)_defval)
 
+#define DEFINE_PROP_BOOL_NODEFAULT(_name, _state, _field) \
+    DEFINE_PROP_SIGNED(_name, _state, _field, OPTIONAL_BOOL_UNSET, \
+                        qdev_prop_bool_unset, OptionalBool)
+
 /**
  * The DEFINE_PROP_UINT64_CHECKMASK macro checks a user-supplied value
  * against corresponding bitmask, rejects the value if it violates.
diff --git a/hw/core/qdev-properties.c b/hw/core/qdev-properties.c
index 0c17a5de82..1bec8ee679 100644
--- a/hw/core/qdev-properties.c
+++ b/hw/core/qdev-properties.c
@@ -260,6 +260,16 @@ const PropertyInfo qdev_prop_bool = {
     .set_default_value = set_default_value_bool,
 };
 
+/* --- optional bool --- */
+
+const PropertyInfo qdev_prop_bool_unset = {
+    .name  = "OptionalBool",
+    .enum_table  = &OptionalBool_lookup,
+    .get   = qdev_propinfo_get_enum,
+    .set   = qdev_propinfo_set_enum,
+    .set_default_value = qdev_propinfo_set_default_value_enum,
+};
+
 /* --- 8bit integer --- */
 
 static void get_uint8(Object *obj, Visitor *v, const char *name, void *opaque,
-- 
2.41.0


Re: [RFC PATCH 2/5] qdev-properties: Add OptionalBool QAPI type
Posted by Richard Henderson 10 months, 4 weeks ago
On 1/3/24 03:04, Philippe Mathieu-Daudé wrote:
> To be able to distinct whether a boolean qdev property
> has been set or not, add the DEFINE_PROP_BOOL_NODEFAULT()
> qdev macro based on the tri-state OptionalBool QAPI type.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   qapi/common.json             | 16 ++++++++++++++++
>   include/hw/qdev-properties.h |  5 +++++
>   hw/core/qdev-properties.c    | 10 ++++++++++
>   3 files changed, 31 insertions(+)

How is this different from OnOffAuto?


r~

Re: [RFC PATCH 2/5] qdev-properties: Add OptionalBool QAPI type
Posted by Philippe Mathieu-Daudé 10 months, 4 weeks ago
On 2/1/24 23:44, Richard Henderson wrote:
> On 1/3/24 03:04, Philippe Mathieu-Daudé wrote:
>> To be able to distinct whether a boolean qdev property
>> has been set or not, add the DEFINE_PROP_BOOL_NODEFAULT()
>> qdev macro based on the tri-state OptionalBool QAPI type.
>>
>> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
>> ---
>>   qapi/common.json             | 16 ++++++++++++++++
>>   include/hw/qdev-properties.h |  5 +++++
>>   hw/core/qdev-properties.c    | 10 ++++++++++
>>   3 files changed, 31 insertions(+)
> 
> How is this different from OnOffAuto?

I am trying to not break CLI which expects true/false and not on/off.

We could extend OnOffAuto to parse true/false if preferred.

(The particular device used as example in this series is not - yet -
user-creatable, so this doesn't matter there).