1 // -*- C++ -*-
2 
3 /*
4  * Gnome Chemisty Utils
5  * macros.h
6  *
7  * Copyright (C) 2001-2011 Jean Bréfort <jean.brefort@normalesup.org>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 3 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
22  * USA
23  */
24 
25 #ifndef GCU_MACROS_H
26 #define GCU_MACROS_H
27 
28 #include <goffice/goffice.h>
29 
30 /*!\file */
31 /*!\def GCU_PROP()
32 Defines a private member with appropriate get/set methods.
33 GCU_PROP((Type,Foo) expands to one private member:
34 \code
35 	Type m_Foo;
36 \endcode
37 
38 and three public methods:
39 \code
40 	void SetFoo(Type val);
41 	Type GetFoo();
42 	Type& GetRefFoo();
43 \endcode
44 
45 The last one allows code as:
46 \code
47 	obj.GetRefFoo() = val;
48 \endcode
49 */
50 #define GCU_PROP(type,member) \
51 public:	\
52 	void Set##member (type val) {m_##member = val;}	\
53 	type Get##member (void) const {return m_##member;}	\
54 	type &GetRef##member (void) {return m_##member;}	\
55 private:	\
56 	type m_##member;
57 
58 /*!\def GCU_PROP_EX()
59 Defines a private member with appropriate get/set methods.
60 GCU_PROP_EX((Type,Foo) expands to one private member:
61 \code
62 	Type m_Foo;
63 \endcode
64 
65 and two public methods:
66 \code
67 	void SetFoo(Type val);
68 	Type GetFoo();
69 \endcode
70 
71 SetFoo() calls private method (void ChangedFoo()) which must be implemented by
72 the class.
73 */
74 #define GCU_PROP_EX(type,member) \
75 public:	\
76 	void Set##member (type val) {m_##member = val; Changed##member ();}	\
77 	type Get##member (void) const {return m_##member;}	\
78 private:	\
79 	void Changed##member (void);	\
80 	type m_##member;
81 
82 /*!\def GCU_POINTER_PROP()
83 Defines a private pointer member with appropriate get/set methods.
84 GCU_POINTER_PROP((Type,Foo) expands to one private member:
85 \code
86 	Type *m_Foo;
87 \endcode
88 
89 and three public methods:
90 \code
91 	void SetFoo(Type *val);
92 	Type *GetFoo();
93 	Type const *GetFoo() const;
94 \endcode
95 */
96 #define GCU_POINTER_PROP(type,member) \
97 public:	\
98 	void Set##member (type *val) {m_##member = val;}	\
99 	type *Get##member (void) {return m_##member;}	\
100 	type const *Get##member (void) const {return m_##member;}	\
101 private:	\
102 	type *m_##member;
103 
104 /*!\def GCU_RO_PROP()
105 Defines a private member with an appropriate get method. RO stands for Read Only. The member
106 can't be modified from outside the class it belongs to or a friend class.
107 GCU_RO_PROP(Type,Foo) expands to one private member:
108 \code
109 	Type m_Foo;
110 \endcode
111 
112 and one public method:
113 \code
114 	Type GetFoo() const;
115 \endcode
116 */
117 #define GCU_RO_PROP(type,member) \
118 public:	\
119 	type Get##member (void) const {return m_##member;}	\
120 private:	\
121 	type m_##member;
122 
123 /*!\def GCU_RO_STATIC_PROP()
124 Defines a static private member with an appropriate get method. RO stands for Read Only. The member
125 can't be modified from outside the class it belongs to or a friend class.
126 GCU_RO_STATIC_PROP(Type,Foo) expands to one private member:
127 \code
128 	static Type m_Foo;
129 \endcode
130 
131 and one public method:
132 \code
133 	Type GetFoo() const;
134 \endcode
135 */
136 #define GCU_RO_STATIC_PROP(type,member) \
137 public:	\
138 	type Get##member (void) const {return m_##member;}	\
139 private:	\
140 	static type m_##member;
141 
142 /*!\def GCU_RO_POINTER_PROP()
143 Defines a private pointer member an with appropriate get method. RO stands for Read Only. The member
144 can't be modified from outside the class it belongs to or a friend class.
145 GCU_RO_POINTER_PROP((Type,Foo) expands to one private member:
146 \code
147 	Type *m_Foo;
148 \endcode
149 
150 and one public methods:
151 \code
152 	Type const *GetFoo() const;
153 \endcode
154 */
155 #define GCU_RO_POINTER_PROP(type,member) \
156 public:	\
157 	type const *Get##member (void) const {return m_##member;}	\
158 private:	\
159 	type *m_##member;
160 
161 /*!\def GCU_PROT_PROP()
162 Defines a protected member with an appropriate get method. The member can be modified
163 the class it belongs too or a friend class or a derived class.
164 GCU_PROT_PROP(Type,Foo) expands to one protected member:
165 \code
166 	Type m_Foo;
167 \endcode
168 
169 and one public method:
170 \code
171 	Type GetFoo();
172 \endcode
173 */
174 #define GCU_PROT_PROP(type,member) \
175 public:	\
176 	type Get##member (void) {return m_##member;}	\
177 protected:	\
178 	type m_##member;
179 
180 /*!\def GCU_PROT_POINTER_PROP()
181 Defines a protected pointer member with an appropriate get method. The member can be modified
182 the class it belongs too or a friend class or a derived class. The data referenced
183 by the pointer can be modified if the class instance is not const.
184 GCU_PROT_POINTER_PROP((Type,Foo) expands to one private member:
185 \code
186 	Type *m_Foo;
187 \endcode
188 
189 and two public methods:
190 \code
191 	Type *GetFoo();
192 	Type const *GetFoo() const;
193 \endcode
194 */
195 #define GCU_PROT_POINTER_PROP(type,member) \
196 public:	\
197 	type *Get##member (void) {return m_##member;}	\
198 	type const *Get##member (void) const {return m_##member;}	\
199 protected:	\
200 	type *m_##member;
201 
202 /*!\def GCU_GCONF_GET()
203 This macro gets the numerical value of type \a type associated to \a key, and
204 copies it to \a target. If an error occurs or if the value is 0,
205 \a defaultval is used instead.\n
206 If the GOConf mechanism is available in goffice (>= 0.7.0), calling class must
207 have a GOConfNode called m_ConfNode, and the code must provide a GError *error initially
208 set to NULL (GConf version only).
209 The real key is obtained by appending the value of ROOTDIR to \a key.
210 */
211 #define go_conf_get_float go_conf_get_double
212 #define GCU_GCONF_GET(key,type,target,defaultval) \
213 	target = go_conf_get_##type (m_ConfNode, key); \
214 	if (target == (type) 0)	\
215 		target = defaultval;
216 /*!\def GCU_GCONF_GET_NO_CHECK()
217 This macro gets the numerical value of type \a type associated to \a key, and
218 copies it to \a target. If an error occurs, \a defaultval is used instead.\n
219 If the GOConf mechanism is available in goffice (>= 0.7.0), calling class must
220 have a GOConfNode called m_ConfNode, and the code must provide a GError *error initially
221 set to NULL (GConf version only).
222 The real key is obtained by appending the value of ROOTDIR to \a key.
223 */
224 #define GCU_GCONF_GET_NO_CHECK(key,type,target,defaultval) \
225 	target = go_conf_get_##type (m_ConfNode, key);
226 
227 /*!\def GCU_GCONF_GET_N_TRANSFORM()
228 This macro gets the numerical value of type \a type associated to \a key. If an error
229 occurs or if the value is 0, \a defaultval is used instead.\n
230 The resuting value (which might be the default value) is then passed
231 to \a func and the result is copied to \a target. \n
232 If the GOConf mechanism is available in goffice (>= 0.7.0), calling class must
233 have a GOConfNode called m_ConfNode, and the code must provide a GError *error initially
234 set to NULL (GConf version only).
235 The real key is obtained by appending the value of ROOTDIR to \a key.
236 */
237 #define GCU_GCONF_GET_N_TRANSFORM(key,type,target,defaultval,func) \
238 	{	\
239 		type val = go_conf_get_##type (m_ConfNode, key); \
240 		if (val == (type) 0)	\
241 			val = defaultval; \
242 		target = func (val);	\
243 	}
244 
245 /*!\def GCU_GCONF_GET_STRING()
246 This macro gets the string value associated to \a key, and
247 copies it to \a target. If an error occurs, \a defaultval is used instead.\n
248 If \a target is not NULL when entering the macro, it is deallocated using g_free
249 and set to NULL before calling gconf_client_get_string.\n
250 Calling class must have a GOConfNode called m_ConfNode, and the code
251 must provide a GError *error initially set to NULL.
252 The real key is obtained by appending the value of ROOTDIR to \a key.
253 */
254 #define GCU_GCONF_GET_STRING(key,target,defaultval) \
255 	if (target) {	\
256 		g_free (target);	\
257 		target = NULL;	\
258 	}	\
259 	target = go_conf_get_string (m_ConfNode, key); \
260 	if (target == NULL && defaultval != NULL)	\
261 		target = g_strdup (defaultval);
262 
263 /*!\def GCU_UPDATE_KEY()
264 This macro updates a value of type \a type associated to \a key, and
265 copies it to \a target. \a action is called after setting the target?
266 It also needs a GOConfNode* called node.
267 The real key is obtained by appending the value of ROOTDIR to \a key.
268 */
269 #define GCU_UPDATE_KEY(key,type,target,action) \
270 	if (!strcmp (name, ROOTDIR key)) { \
271 		target = go_conf_get_##type (node, ((node)? key: ROOTDIR key)); \
272 		action \
273 		return; \
274 	}
275 
276 /*!\def GCU_UPDATE_STRING_KEY()
277 This macro updates a string value associated to \a key, and
278 copies it to \a target. \a action is called after setting the target?
279 It also needs a GOConfNode* called node.
280 The real key is obtained by appending the value of ROOTDIR to \a key.
281 */
282 #define GCU_UPDATE_STRING_KEY(key,target,action) \
283 	if (!strcmp (name, ROOTDIR key)) { \
284 		target = go_conf_get_string (node, ((node)? key: ROOTDIR key)); \
285 		action \
286 		return; \
287 	}
288 
289 #define CC2XML(x) reinterpret_cast <xmlChar const *> (x)
290 #define C2XML(x) reinterpret_cast <xmlChar *> (x)
291 #define XML2CC(x) reinterpret_cast <char const *> (x)
292 #define XML2C(x) reinterpret_cast <char *> (x)
293 
294 #endif	//	GCU_MACROS_H
295