[Spice-devel] [PATCH spice-server v2 3/4] Introduce a macro to help declaring new GObject

Frediano Ziglio fziglio at redhat.com
Mon Sep 18 15:04:56 UTC 2017


The macro will implement most of the boilerplate needed to declare an
object.
Its usage is similar to GLib G_DECLARE_*_TYPE macros.

Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
Changes since v3:
- define just SPICE_DECLARE_TYPE.
Changes since v2:
- declare only SPICE_DECLARE_TYPE, all type should have proper types and
  namespace.
Changes since v1:
- use SPICE_ prefix instead of GOBJECT_;
- if RED_ prefix is used use this prefix for all GLib
  macros.
---
 server/red-common.h | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/server/red-common.h b/server/red-common.h
index 9ff1fd9b5..b9ab4aa5d 100644
--- a/server/red-common.h
+++ b/server/red-common.h
@@ -93,4 +93,30 @@ 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
+ *
+ * @ModuleObjName     type identifier like MyObject
+ * @module_obj_name   method prefix like my_object (no need to add the
+ *                    underscore)
+ * @OBJ_NAME          macro common part like MY_OBJECT
+ */
+#define SPICE_DECLARE_TYPE(ModuleObjName, module_obj_name, OBJ_NAME) \
+    typedef struct ModuleObjName ModuleObjName; \
+    typedef struct ModuleObjName ## Class ModuleObjName ## Class; \
+    typedef struct ModuleObjName ## Private ModuleObjName ## Private; \
+    GType module_obj_name ## _get_type(void) G_GNUC_CONST; \
+    static inline ModuleObjName *G_PASTE(RED_,OBJ_NAME)(void *obj) \
+    { return G_TYPE_CHECK_INSTANCE_CAST(obj, \
+             module_obj_name ## _get_type(), ModuleObjName); } \
+    static inline ModuleObjName ## Class *G_PASTE(G_PASTE(RED_,OBJ_NAME),_CLASS)(void *klass) \
+    { return G_TYPE_CHECK_CLASS_CAST(klass, \
+             module_obj_name ## _get_type(), ModuleObjName ## Class); } \
+    static inline gboolean G_PASTE(RED_IS_,OBJ_NAME)(void *obj) \
+    { return G_TYPE_CHECK_INSTANCE_TYPE(obj, module_obj_name ## _get_type()); } \
+    static inline gboolean G_PASTE(G_PASTE(RED_IS_,OBJ_NAME),_CLASS)(void *klass) \
+    { return G_TYPE_CHECK_CLASS_TYPE((klass), module_obj_name ## _get_type()); } \
+    static inline ModuleObjName ## Class *G_PASTE(G_PASTE(RED_,OBJ_NAME),_GET_CLASS)(void *obj) \
+    { return G_TYPE_INSTANCE_GET_CLASS(obj, \
+             module_obj_name ## _get_type(), ModuleObjName ## Class); }
+
 #endif /* RED_COMMON_H_ */
-- 
2.13.5



More information about the Spice-devel mailing list