[Spice-devel] [PATCH spice-server 1/2] Introduce a macro to help declaring new GObject

Frediano Ziglio fziglio at redhat.com
Mon Sep 4 16:09:22 UTC 2017


The macro will implement most of the boilerplate needed
to declare an object.

Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 server/red-common.h | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/server/red-common.h b/server/red-common.h
index 9ff1fd9b..7f60e228 100644
--- a/server/red-common.h
+++ b/server/red-common.h
@@ -93,4 +93,38 @@ typedef struct GListIter {
 #define GLIST_FOREACH_REVERSED(_list, _type, _data) \
     GLIST_FOREACH_GENERIC(_list, G_PASTE(_iter_, __LINE__), _type, _data, prev)
 
+/* Helper to declare a GObject type
+ *
+ * @_type     type identifier like MyObject
+ * @_prefix   method prefix like my_object (no need to add the
+ *            underscore)
+ * @_macro    macro common part like MY_OBJECT
+ * @_struct   used to specify the structure type in case is not
+ *            the same as the @_type. For instance can be
+ *            "struct MyOjectDefinition"
+ */
+#define GOBJECT_DECLARE_TYPE_STRUCT(_type, _prefix, _macro, _struct) \
+    typedef _struct _type; \
+    typedef struct _type ## Class _type ## Class; \
+    typedef struct _type ## Private _type ## Private; \
+    GType _prefix ## _get_type(void) G_GNUC_CONST; \
+    static inline _type *_macro(void *obj) \
+    { return G_TYPE_CHECK_INSTANCE_CAST(obj, _prefix ## _get_type(), _type); } \
+    static inline _type ## Class *_macro ## _CLASS(void *klass) \
+    { return G_TYPE_CHECK_CLASS_CAST(klass, _prefix ## _get_type(), _type ## Class); } \
+    static inline gboolean IS_ ## _macro(void *obj) \
+    { return G_TYPE_CHECK_INSTANCE_TYPE(obj, _prefix ## _get_type()); } \
+    static inline gboolean IS_ ## _macro ## _CLASS(void *klass) \
+    { return G_TYPE_CHECK_CLASS_TYPE((klass), _prefix ## _get_type()); } \
+    static inline _type ## Class *_macro ## _GET_CLASS(void *obj) \
+    { return G_TYPE_INSTANCE_GET_CLASS(obj, _prefix ## _get_type(), _type ## Class); }
+
+/* Helper to declare a GObject type
+ *
+ * Same as GOBJECT_DECLARE_TYPE_STRUCT but the structure is the same
+ * as the type, most declarations use this convention.
+ */
+#define GOBJECT_DECLARE_TYPE(_type, _prefix, _macro) \
+    GOBJECT_DECLARE_TYPE_STRUCT(_type, _prefix, _macro, struct _type)
+
 #endif /* RED_COMMON_H_ */
-- 
2.13.5



More information about the Spice-devel mailing list