1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * gsf-impl-utils.h:
4  *
5  * Copyright (C) 2002-2006 Jody Goldberg (jody@gnome.org)
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of version 2.1 of the GNU Lesser General Public
9  * License as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
19  * USA
20  */
21 
22 #ifndef GSF_IMPL_UTILS_H
23 #define GSF_IMPL_UTILS_H
24 
25 #include <gsf/gsf.h>
26 #include <glib-object.h>
27 
28 G_BEGIN_DECLS
29 
30 /* kept for compatibility reasons */
31 #define GSF_PARAM_STATIC G_PARAM_STATIC_STRINGS
32 
33 /*************************************************************************/
34 
35 #define	GSF_CLASS_FULL(name, prefix, base_init, base_finalize, \
36 		       class_init, class_finalize, instance_init, parent_type, \
37 		       abstract, interface_decl) \
38 GType									\
39 prefix ## _get_type (void)						\
40 {									\
41 	static GType type = 0;						\
42 	if (G_UNLIKELY  (type == 0)) {					\
43 		static GTypeInfo const object_info = {			\
44 			sizeof (name ## Class),				\
45 			(GBaseInitFunc) base_init,			\
46 			(GBaseFinalizeFunc) base_finalize,		\
47 			(GClassInitFunc) class_init,			\
48 			(GClassFinalizeFunc) class_finalize,		\
49 			NULL,	/* class_data */			\
50 			sizeof (name),					\
51 			0,	/* n_preallocs */			\
52 			(GInstanceInitFunc) instance_init,		\
53 			NULL						\
54 		};							\
55 		type = g_type_register_static (parent_type, #name,	\
56 			&object_info, (GTypeFlags) abstract);		\
57 		interface_decl						\
58 	}								\
59 	return type;							\
60 }
61 
62 /**
63  * GSF_CLASS:
64  * @name: Name of the class.
65  * @prefix: Symbol prefix designating the namespace to be used for
66  * implementing the class.
67  * @class_init: Initialisation function of type #GClassInitFunc for the class.
68  * @instance_init: Initialisation function of type #GInstanceInitFunc
69  * for an instance of the class.
70  * @parent: Parent class to this class.
71  *
72  * Set up a GSF class.
73  *
74  */
75 #define	GSF_CLASS(name, prefix, class_init, instance_init, parent) \
76 	GSF_CLASS_FULL(name, prefix, NULL, NULL, class_init, NULL, \
77 				instance_init, parent, 0, {})
78 #define	GSF_CLASS_ABSTRACT(name, prefix, class_init, instance_init, parent) \
79 	GSF_CLASS_FULL(name, prefix, NULL, NULL, class_init, NULL, \
80 		       instance_init, parent, G_TYPE_FLAG_ABSTRACT, {})
81 
82 #define GSF_INTERFACE_FULL(type, init_func, iface_type) {	\
83 	static GInterfaceInfo const iface = {			\
84 		(GInterfaceInitFunc) init_func, NULL, NULL };	\
85 	g_type_add_interface_static (type, iface_type, &iface);	\
86 }
87 
88 #define GSF_INTERFACE(init_func, iface_type)			\
89 	GSF_INTERFACE_FULL(type, init_func, iface_type)
90 
91 /*************************************************************************/
92 
93 #define	GSF_DYNAMIC_CLASS_FULL(name, prefix, base_init, base_finalize, \
94 				   class_init,  class_finalize, instance_init, parent_type, \
95 			       abstract, interface_decl) 		\
96 static GType prefix ## _type; 						\
97 									\
98 GType prefix ## _get_type (void);					\
99 void  prefix ## _register_type (GTypeModule *module);			\
100 									\
101 GType									\
102 prefix ## _get_type (void)						\
103 {									\
104 	g_return_val_if_fail (prefix ## _type != 0, 0); 		\
105 	return prefix ## _type;						\
106 }									\
107 void									\
108 prefix ## _register_type (GTypeModule *module)				\
109 {									\
110 	GTypeInfo const type_info = {					\
111 		sizeof (name ## Class),					\
112 		(GBaseInitFunc) base_init,				\
113 		(GBaseFinalizeFunc) base_finalize,			\
114 		(GClassInitFunc) class_init,				\
115 		(GClassFinalizeFunc) class_finalize,			\
116 		NULL,	/* class_data */				\
117 		sizeof (name),						\
118 		0,	/* n_preallocs */				\
119 		(GInstanceInitFunc) instance_init,			\
120 		NULL							\
121 	};								\
122 	GType type;							\
123 									\
124 	g_return_if_fail (prefix ## _type == 0); 			\
125 									\
126 	prefix ## _type = type = g_type_module_register_type (module,	\
127 		parent_type, #name, &type_info, (GTypeFlags) abstract);	\
128 	interface_decl							\
129 }
130 
131 #define	GSF_DYNAMIC_CLASS(name, prefix, class_init, instance_init, parent)	\
132 	GSF_DYNAMIC_CLASS_FULL(name, prefix, NULL, NULL, class_init, NULL, 	\
133 				   instance_init, parent, 0, {})
134 #define	GSF_DYNAMIC_CLASS_ABSTRACT(name, prefix, class_init, instance_init, parent) \
135 	GSF_DYNAMIC_CLASS_FULL(name, prefix, NULL, NULL, class_init, NULL, 	\
136 		       instance_init, parent, G_TYPE_FLAG_ABSTRACT, {})
137 
138 #define GSF_DYNAMIC_INTERFACE_FULL(type, init_func, iface_type, module) {	\
139 	GInterfaceInfo const iface = {						\
140 		(GInterfaceInitFunc) init_func, NULL, NULL };			\
141 	g_type_module_add_interface (module, type, iface_type, &iface);		\
142 }
143 
144 #define GSF_DYNAMIC_INTERFACE(init_func, iface_type, module)			\
145 	GSF_DYNAMIC_INTERFACE_FULL(type, init_func, iface_type, module)
146 
147 G_END_DECLS
148 
149 #endif /* GSF_IMPL_UTILS_H */
150