[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [RFC v2 3/9] qom: QUALIFIED_CAST helper macro
From: |
Eduardo Habkost |
Subject: |
[Qemu-devel] [RFC v2 3/9] qom: QUALIFIED_CAST helper macro |
Date: |
Wed, 29 Mar 2017 16:41:42 -0300 |
The new helper will help us write macros that keep const-ness of
the input arguments, using C11's _Generic keyword.
If _Generic is not supported by the compiler, QUALIFIED_CAST
becomes a regular non-const cast.
Signed-off-by: Eduardo Habkost <address@hidden>
---
include/qom/object.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 46 insertions(+)
diff --git a/include/qom/object.h b/include/qom/object.h
index cd0f412ce9..6829735f99 100644
--- a/include/qom/object.h
+++ b/include/qom/object.h
@@ -474,6 +474,52 @@ struct TypeInfo
((Object *)(obj))
/**
+ * REFERENCED_TYPE:
+ * @t: a pointer type
+ *
+ * Returns the referenced type of a pointer type.
+ */
+#define REFERENCED_TYPE(t) \
+ typeof(*((t)0))
+
+/**
+ * QUALIFIED_CAST:
+ * @orig_type: Original type (a pointer type)
+ * @type: A pointer type to which an expression will be cast to.
+ *
+ * Helper to cast an expression to another pointer type while
+ * keeping the 'const' qualifier if it was present in
+ * @orig_type. If the compiler doesn't support _Generic, it
+ * becomes a regular non-const cast.
+ *
+ * Usage examples:
+ * QUALIFIED_CAST(AType *, BType *)(expr)
+ * equivalent to (BType *)(expr)
+ *
+ * QUALIFIED_CAST(const AType *, BType *)(expr)
+ * If supported, equivalent to: (const BType *)(expr)
+ * otherwise, equivalent to: (BType *)(expr)
+ *
+ * AType *a;
+ * QUALIFIED_CAST(typeof(a), BType *)(b)
+ * equivalent to (BType *)(b)
+ *
+ * const AType *a;
+ * QUALIFIED_CAST(typeof(a), BType *)(b)
+ * If supported, equivalent to: (const BType *)(expr)
+ * otherwise, equivalent to: (BType *)(expr)
+ */
+#ifdef HAVE_C11_GENERIC
+#define QUALIFIED_CAST(orig_type, type) \
+ (typeof(_Generic((const REFERENCED_TYPE(orig_type) *)0, \
+ orig_type: (const type)(0), \
+ default: (type)(0))))
+#else
+#define QUALIFIED_CAST(orig_type, type) \
+ (type)
+#endif
+
+/**
* OBJECT_CLASS:
* @class: A derivative of #ObjectClass.
*
--
2.11.0.259.g40922b1
- [Qemu-devel] [RFC v2 0/9] qom: Make object_get_class()/*_GET_CLASS return const pointers, Eduardo Habkost, 2017/03/29
- [Qemu-devel] [RFC v2 2/9] Simplify code using *MACHINE_GET_CLASS, Eduardo Habkost, 2017/03/29
- [Qemu-devel] [RFC v2 1/9] configure: test if _Generic works as expected, Eduardo Habkost, 2017/03/29
- [Qemu-devel] [RFC v2 3/9] qom: QUALIFIED_CAST helper macro,
Eduardo Habkost <=
- [Qemu-devel] [RFC v2 4/9] qom: Make object_class_get_parent() const-aware, Eduardo Habkost, 2017/03/29
- [Qemu-devel] [RFC v2 5/9] Make class parameter const at some functions, Eduardo Habkost, 2017/03/29
- [Qemu-devel] [RFC v2 6/9] Explicitly cast the *_GET_CLASS() value when we break the rules, Eduardo Habkost, 2017/03/29
- [Qemu-devel] [RFC v2 8/9] qom: Make class cast macros/functions const-aware, Eduardo Habkost, 2017/03/29
- [Qemu-devel] [RFC v2 9/9] qom: Make object_get_class() return const pointer, Eduardo Habkost, 2017/03/29
- Re: [Qemu-devel] [RFC v2 0/9] qom: Make object_get_class()/*_GET_CLASS return const pointers, Laszlo Ersek, 2017/03/29
- Re: [Qemu-devel] [RFC v2 0/9] qom: Make object_get_class()/*_GET_CLASS return const pointers, Eduardo Habkost, 2017/03/29
- [Qemu-devel] [RFC v2 7/9] Use const variables for *_GET_CLASS values, Eduardo Habkost, 2017/03/29
- Re: [Qemu-devel] [RFC v2 0/9] qom: Make object_get_class()/*_GET_CLASS return const pointers, Paolo Bonzini, 2017/03/30