[Spice-devel] [PATCH spice-server v2 1/2] Introduce some macros to help declaring new GObject

Frediano Ziglio fziglio at redhat.com
Wed Sep 6 14:42:29 UTC 2017


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

Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
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 | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)

diff --git a/server/red-common.h b/server/red-common.h
index 9ff1fd9b5..19bb3fb6e 100644
--- a/server/red-common.h
+++ b/server/red-common.h
@@ -93,4 +93,64 @@ typedef struct GListIter {
 #define GLIST_FOREACH_REVERSED(_list, _type, _data) \
     GLIST_FOREACH_GENERIC(_list, G_PASTE(_iter_, __LINE__), _type, _data, prev)
 
+#define SPICE_DECLARE_TYPE_GENERIC(ModuleObjType, ModuleObjName, module_obj_name, MODULE, OBJ_NAME) \
+    typedef ModuleObjType 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(MODULE,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(MODULE,OBJ_NAME),_CLASS)(void *klass) \
+    { return G_TYPE_CHECK_CLASS_CAST(klass, \
+             module_obj_name ## _get_type(), ModuleObjName ## Class); } \
+    static inline gboolean G_PASTE(G_PASTE(MODULE,IS_),OBJ_NAME)(void *obj) \
+    { return G_TYPE_CHECK_INSTANCE_TYPE(obj, module_obj_name ## _get_type()); } \
+    static inline gboolean G_PASTE(G_PASTE(G_PASTE(MODULE,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(MODULE,OBJ_NAME),_GET_CLASS)(void *obj) \
+    { return G_TYPE_INSTANCE_GET_CLASS(obj, \
+             module_obj_name ## _get_type(), ModuleObjName ## Class); }
+
+#define SPICE_DECLARE_EMPTY_MODULE
+
+/* Helper to declare a GObject type
+ *
+ * @ModuleObjType     used to specify the structure type in case is not
+ *                    the same as the @ModuleObjName. For instance can be
+ *                    "struct MyObjectDefinition"
+ * @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_STRUCT(ModuleObjType, ModuleObjName, module_obj_name, OBJ_NAME) \
+    SPICE_DECLARE_TYPE_GENERIC(ModuleObjType, ModuleObjName, module_obj_name, \
+                                 SPICE_DECLARE_EMPTY_MODULE, OBJ_NAME)
+
+/* Helper to declare a GObject type
+ *
+ * Same as SPICE_DECLARE_TYPE_STRUCT but the macro names
+ * will have the "RED_" prefix.
+ */
+#define SPICE_DECLARE_RED_TYPE_STRUCT(ModuleObjType, ModuleObjName, module_obj_name, OBJ_NAME) \
+    SPICE_DECLARE_TYPE_GENERIC(ModuleObjType, ModuleObjName, module_obj_name, RED_, OBJ_NAME)
+
+/* Helper to declare a GObject type
+ *
+ * Same as SPICE_DECLARE_TYPE_STRUCT but the structure is the same
+ * as the type, most declarations use this convention.
+ */
+#define SPICE_DECLARE_TYPE(ModuleObjName, module_obj_name, OBJ_NAME) \
+    SPICE_DECLARE_TYPE_GENERIC(struct ModuleObjName, ModuleObjName, module_obj_name, \
+                                 SPICE_DECLARE_EMPTY_MODULE, OBJ_NAME)
+
+/* Helper to declare a GObject type
+ *
+ * Same as SPICE_DECLARE_TYPE but the macro names
+ * will have the "RED_" prefix.
+ */
+#define SPICE_DECLARE_RED_TYPE(ModuleObjName, module_obj_name, OBJ_NAME) \
+    SPICE_DECLARE_TYPE_GENERIC(struct ModuleObjName, ModuleObjName, module_obj_name, RED_, OBJ_NAME)
+
 #endif /* RED_COMMON_H_ */
-- 
2.13.5



More information about the Spice-devel mailing list