1 | /* |
---|
2 | * bonobo-arg.c Bonobo argument support: |
---|
3 | * |
---|
4 | * A thin wrapper of CORBA_any's with macros |
---|
5 | * to assist in handling values safely. |
---|
6 | * |
---|
7 | * Author: |
---|
8 | * Michael Meeks (michael@helixcode.com) |
---|
9 | * |
---|
10 | * Copyright 2000, Helix Code, Inc. |
---|
11 | */ |
---|
12 | #include <config.h> |
---|
13 | #include <bonobo/bonobo-main.h> |
---|
14 | #include <bonobo/bonobo-exception.h> |
---|
15 | #include <bonobo/bonobo-arg.h> |
---|
16 | |
---|
17 | /** |
---|
18 | * bonobo_arg_new: |
---|
19 | * @t: the BonoboArgType eg. TC_long |
---|
20 | * |
---|
21 | * Create a new BonoboArg with the specified type |
---|
22 | * the value of the BonoboArg is initially empty. |
---|
23 | * |
---|
24 | * Return value: |
---|
25 | **/ |
---|
26 | BonoboArg * |
---|
27 | bonobo_arg_new (BonoboArgType t) |
---|
28 | { |
---|
29 | CORBA_any *any = NULL; |
---|
30 | DynamicAny_DynAny dyn = NULL; |
---|
31 | CORBA_Environment ev; |
---|
32 | |
---|
33 | g_return_val_if_fail (t != NULL, NULL); |
---|
34 | |
---|
35 | CORBA_exception_init (&ev); |
---|
36 | |
---|
37 | dyn = CORBA_ORB_create_basic_dyn_any (bonobo_orb (), t, &ev); |
---|
38 | |
---|
39 | if (!BONOBO_EX (&ev) && dyn != NULL) { |
---|
40 | any = DynamicAny_DynAny_to_any (dyn, &ev); |
---|
41 | DynamicAny_DynAny_destroy (dyn, &ev); |
---|
42 | CORBA_Object_release ((CORBA_Object) dyn, &ev); |
---|
43 | } |
---|
44 | |
---|
45 | CORBA_exception_free (&ev); |
---|
46 | |
---|
47 | return any; |
---|
48 | } |
---|
49 | |
---|
50 | /** |
---|
51 | * bonobo_arg_release: |
---|
52 | * @arg: the bonobo arg. |
---|
53 | * |
---|
54 | * This frees the memory associated with @arg |
---|
55 | **/ |
---|
56 | void |
---|
57 | bonobo_arg_release (BonoboArg *arg) |
---|
58 | { |
---|
59 | if (arg) |
---|
60 | CORBA_free (arg); |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * bonobo_arg_copy: |
---|
65 | * @arg: the bonobo arg |
---|
66 | * |
---|
67 | * This function duplicates @a by a deep copy |
---|
68 | * |
---|
69 | * Return value:a copy of @arg |
---|
70 | **/ |
---|
71 | BonoboArg * |
---|
72 | bonobo_arg_copy (const BonoboArg *arg) |
---|
73 | { |
---|
74 | BonoboArg *copy = CORBA_any__alloc (); |
---|
75 | |
---|
76 | if (!arg) { |
---|
77 | copy->_type = TC_null; |
---|
78 | g_warning ("Duplicating a NULL Bonobo Arg"); |
---|
79 | } else |
---|
80 | CORBA_any__copy (copy, (BonoboArg *) arg); |
---|
81 | |
---|
82 | return copy; |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | * bonobo_arg_type_from_gtk: |
---|
87 | * @id: the GtkType id. |
---|
88 | * |
---|
89 | * This maps a GtkType to a BonoboArgType |
---|
90 | * |
---|
91 | * Return value: the mapped type or NULL on failure. |
---|
92 | **/ |
---|
93 | BonoboArgType |
---|
94 | bonobo_arg_type_from_gtk (GtkType id) |
---|
95 | { |
---|
96 | switch (id) { |
---|
97 | case GTK_TYPE_CHAR: return TC_char; |
---|
98 | case GTK_TYPE_UCHAR: return TC_char; |
---|
99 | case GTK_TYPE_BOOL: return TC_boolean; |
---|
100 | case GTK_TYPE_INT: return TC_short; |
---|
101 | case GTK_TYPE_UINT: return TC_ushort; |
---|
102 | case GTK_TYPE_LONG: return TC_long; |
---|
103 | case GTK_TYPE_ULONG: return TC_ulong; |
---|
104 | case GTK_TYPE_FLOAT: return TC_float; |
---|
105 | case GTK_TYPE_DOUBLE: return TC_double; |
---|
106 | case GTK_TYPE_STRING: return TC_string; |
---|
107 | default: |
---|
108 | g_warning ("Unmapped arg type '%d'", id); |
---|
109 | break; |
---|
110 | } |
---|
111 | |
---|
112 | return NULL; |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * bonobo_arg_type_to_gtk: |
---|
117 | * @id: the BonoboArgType |
---|
118 | * |
---|
119 | * This maps a BonoboArgType to a GtkType |
---|
120 | * |
---|
121 | * Return value: the mapped type or 0 on failure |
---|
122 | **/ |
---|
123 | GtkType |
---|
124 | bonobo_arg_type_to_gtk (BonoboArgType id) |
---|
125 | { |
---|
126 | CORBA_Environment ev; |
---|
127 | GtkType gtk_type = GTK_TYPE_NONE; |
---|
128 | |
---|
129 | CORBA_exception_init (&ev); |
---|
130 | |
---|
131 | if (bonobo_arg_type_is_equal (TC_char, id, &ev)) |
---|
132 | gtk_type = GTK_TYPE_CHAR; |
---|
133 | else if (bonobo_arg_type_is_equal (TC_boolean, id, &ev)) |
---|
134 | gtk_type = GTK_TYPE_BOOL; |
---|
135 | else if (bonobo_arg_type_is_equal (TC_short, id, &ev)) |
---|
136 | gtk_type = GTK_TYPE_INT; |
---|
137 | else if (bonobo_arg_type_is_equal (TC_ushort, id, &ev)) |
---|
138 | gtk_type = GTK_TYPE_UINT; |
---|
139 | else if (bonobo_arg_type_is_equal (TC_long, id, &ev)) |
---|
140 | gtk_type = GTK_TYPE_LONG; |
---|
141 | else if (bonobo_arg_type_is_equal (TC_ulong, id, &ev)) |
---|
142 | gtk_type = GTK_TYPE_ULONG; |
---|
143 | else if (bonobo_arg_type_is_equal (TC_float, id, &ev)) |
---|
144 | gtk_type = GTK_TYPE_FLOAT; |
---|
145 | else if (bonobo_arg_type_is_equal (TC_double, id, &ev)) |
---|
146 | gtk_type = GTK_TYPE_DOUBLE; |
---|
147 | else if (bonobo_arg_type_is_equal (TC_string, id, &ev)) |
---|
148 | gtk_type = GTK_TYPE_STRING; |
---|
149 | else |
---|
150 | g_warning ("Unmapped bonobo arg type"); |
---|
151 | |
---|
152 | CORBA_exception_free (&ev); |
---|
153 | |
---|
154 | return gtk_type; |
---|
155 | } |
---|
156 | |
---|
157 | #define MAKE_FROM_GTK_CASE(gtktype,tcid,unionid,corbatype,corbakind) \ |
---|
158 | case GTK_TYPE_##gtktype: \ |
---|
159 | *((corbatype *)a->_value) = (corbatype)GTK_VALUE_##gtktype(*arg); \ |
---|
160 | break; |
---|
161 | |
---|
162 | /** |
---|
163 | * bonobo_arg_from_gtk: |
---|
164 | * @a: pointer to blank BonoboArg |
---|
165 | * @arg: GtkArg to copy |
---|
166 | * |
---|
167 | * This maps a GtkArg @arg to a BonoboArg @a; |
---|
168 | * @a must point to a freshly allocated BonoboArg |
---|
169 | * eg. such as returned by bonobo_arg_new |
---|
170 | **/ |
---|
171 | void |
---|
172 | bonobo_arg_from_gtk (BonoboArg *a, const GtkArg *arg) |
---|
173 | { |
---|
174 | int id; |
---|
175 | |
---|
176 | g_return_if_fail (a != NULL); |
---|
177 | g_return_if_fail (arg != NULL); |
---|
178 | |
---|
179 | id = arg->type; |
---|
180 | |
---|
181 | switch (id) { |
---|
182 | |
---|
183 | case GTK_TYPE_INVALID: |
---|
184 | case GTK_TYPE_NONE: |
---|
185 | g_warning ("Strange gtk arg type %d", id); |
---|
186 | break; |
---|
187 | |
---|
188 | MAKE_FROM_GTK_CASE (CHAR, TC_char, char_data, CORBA_char, CORBA_tk_char); |
---|
189 | MAKE_FROM_GTK_CASE (UCHAR, TC_char, uchar_data, CORBA_char, CORBA_tk_char); |
---|
190 | MAKE_FROM_GTK_CASE (BOOL, TC_boolean, bool_data, CORBA_boolean, CORBA_tk_boolean); |
---|
191 | MAKE_FROM_GTK_CASE (INT, TC_short, int_data, CORBA_short, CORBA_tk_short); |
---|
192 | MAKE_FROM_GTK_CASE (UINT, TC_ushort, uint_data, CORBA_unsigned_short, CORBA_tk_ushort); |
---|
193 | MAKE_FROM_GTK_CASE (LONG, TC_long, long_data, CORBA_long, CORBA_tk_long); |
---|
194 | MAKE_FROM_GTK_CASE (ULONG, TC_ulong, ulong_data, CORBA_unsigned_long, CORBA_tk_ulong); |
---|
195 | MAKE_FROM_GTK_CASE (FLOAT, TC_float, float_data, CORBA_float, CORBA_tk_float); |
---|
196 | MAKE_FROM_GTK_CASE (DOUBLE, TC_double, double_data, CORBA_double, CORBA_tk_double); |
---|
197 | |
---|
198 | case GTK_TYPE_STRING: |
---|
199 | /* Orbit really doesn't like NULL string's in anys: why ? ... */ |
---|
200 | if (GTK_VALUE_STRING (*arg)) |
---|
201 | *((CORBA_char **)a->_value) = CORBA_string_dup (GTK_VALUE_STRING (*arg)); |
---|
202 | else |
---|
203 | *((CORBA_char **)a->_value) = CORBA_string_dup (""); |
---|
204 | break; |
---|
205 | |
---|
206 | case GTK_TYPE_POINTER: |
---|
207 | g_warning ("FIXME: we can map user data callbacks locally"); |
---|
208 | break; |
---|
209 | |
---|
210 | case GTK_TYPE_OBJECT: |
---|
211 | g_warning ("All objects can be mapped to base gtk types" |
---|
212 | "in due course"); |
---|
213 | break; |
---|
214 | |
---|
215 | case GTK_TYPE_SIGNAL: |
---|
216 | case GTK_TYPE_ARGS: |
---|
217 | case GTK_TYPE_CALLBACK: |
---|
218 | case GTK_TYPE_C_CALLBACK: |
---|
219 | g_warning ("Clever things can be done for these"); |
---|
220 | break; |
---|
221 | |
---|
222 | case GTK_TYPE_ENUM: |
---|
223 | case GTK_TYPE_FLAGS: |
---|
224 | case GTK_TYPE_BOXED: |
---|
225 | case GTK_TYPE_FOREIGN: |
---|
226 | default: |
---|
227 | g_warning ("Unmapped gtk arg type %d", id); |
---|
228 | break; |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | #define MAKE_TO_GTK_CASE(gtktype,tcid,unionid,corbatype,corbakind) \ |
---|
233 | case corbakind: \ |
---|
234 | GTK_VALUE_##gtktype(*a) = *((corbatype *)arg->_value); \ |
---|
235 | break; |
---|
236 | |
---|
237 | /** |
---|
238 | * bonobo_arg_to_gtk: |
---|
239 | * @a: pointer to a blank GtkArk |
---|
240 | * @arg: the BonoboArg to copy |
---|
241 | * |
---|
242 | * Maps a BonoboArg to a GtkArg; @a must point |
---|
243 | * to a blank GtkArg. |
---|
244 | **/ |
---|
245 | void |
---|
246 | bonobo_arg_to_gtk (GtkArg *a, const BonoboArg *arg) |
---|
247 | { |
---|
248 | int id; |
---|
249 | |
---|
250 | g_return_if_fail (a != NULL); |
---|
251 | g_return_if_fail (arg != NULL); |
---|
252 | g_return_if_fail (arg->_type != NULL); |
---|
253 | |
---|
254 | id = arg->_type->kind; |
---|
255 | switch (id) { |
---|
256 | |
---|
257 | case CORBA_tk_null: |
---|
258 | case CORBA_tk_void: |
---|
259 | g_warning ("Strange corba arg type %d", id); |
---|
260 | break; |
---|
261 | |
---|
262 | MAKE_TO_GTK_CASE (CHAR, TC_char, char_data, CORBA_char, CORBA_tk_char); |
---|
263 | /* MAKE_TO_GTK_CASE (UCHAR, TC_char, uchar_data, CORBA_char, CORBA_tk_char);*/ |
---|
264 | MAKE_TO_GTK_CASE (BOOL, TC_boolean, bool_data, CORBA_boolean, CORBA_tk_boolean); |
---|
265 | MAKE_TO_GTK_CASE (INT, TC_short, int_data, CORBA_short, CORBA_tk_short); |
---|
266 | MAKE_TO_GTK_CASE (UINT, TC_ushort, uint_data, CORBA_unsigned_short, CORBA_tk_ushort); |
---|
267 | MAKE_TO_GTK_CASE (LONG, TC_long, long_data, CORBA_long, CORBA_tk_long); |
---|
268 | MAKE_TO_GTK_CASE (ULONG, TC_ulong, ulong_data, CORBA_unsigned_long, CORBA_tk_ulong); |
---|
269 | MAKE_TO_GTK_CASE (FLOAT, TC_float, float_data, CORBA_float, CORBA_tk_float); |
---|
270 | MAKE_TO_GTK_CASE (DOUBLE, TC_double, double_data, CORBA_double, CORBA_tk_double); |
---|
271 | |
---|
272 | case CORBA_tk_string: |
---|
273 | GTK_VALUE_STRING (*a) = g_strdup (BONOBO_ARG_GET_STRING (arg)); |
---|
274 | break; |
---|
275 | |
---|
276 | case CORBA_tk_objref: |
---|
277 | g_warning ("All objects can be mapped to base gtk types" |
---|
278 | "in due course"); |
---|
279 | break; |
---|
280 | |
---|
281 | case CORBA_tk_sequence: |
---|
282 | case CORBA_tk_alias: |
---|
283 | case CORBA_tk_enum: |
---|
284 | case CORBA_tk_array: |
---|
285 | case CORBA_tk_union: |
---|
286 | g_warning ("Clever things can be done for these"); |
---|
287 | break; |
---|
288 | |
---|
289 | default: |
---|
290 | g_warning ("Unmapped corba arg type %d", id); |
---|
291 | break; |
---|
292 | } |
---|
293 | } |
---|
294 | |
---|
295 | /** |
---|
296 | * bonobo_arg_type_is_equal: |
---|
297 | * @a: a type code |
---|
298 | * @b: a type code |
---|
299 | * @opt_ev: optional exception environment or NULL. |
---|
300 | * |
---|
301 | * This compares two BonoboArgType's in @a and @b. |
---|
302 | * The @opt_ev is an optional CORBA_Environment for |
---|
303 | * exceptions, or NULL. This function is commutative. |
---|
304 | * |
---|
305 | * Return value: TRUE if equal, FALSE if different |
---|
306 | **/ |
---|
307 | gboolean |
---|
308 | bonobo_arg_type_is_equal (BonoboArgType a, BonoboArgType b, CORBA_Environment *opt_ev) |
---|
309 | { |
---|
310 | CORBA_Environment ev, *my_ev; |
---|
311 | gboolean retval; |
---|
312 | |
---|
313 | if (!opt_ev) { |
---|
314 | CORBA_exception_init (&ev); |
---|
315 | my_ev = &ev; |
---|
316 | } else |
---|
317 | my_ev = opt_ev; |
---|
318 | |
---|
319 | retval = CORBA_TypeCode_equal (a, b, my_ev); |
---|
320 | |
---|
321 | if (!opt_ev) |
---|
322 | CORBA_exception_free (&ev); |
---|
323 | |
---|
324 | return retval; |
---|
325 | } |
---|
326 | |
---|
327 | /** |
---|
328 | * bonobo_arg_is_equal: |
---|
329 | * @a: a bonobo arg |
---|
330 | * @b: another bonobo arg |
---|
331 | * @opt_ev: optional exception environment or NULL. |
---|
332 | * |
---|
333 | * Compares two BonoboArgs for equivalence; will return TRUE |
---|
334 | * if equivalent for all simple cases. For Object references |
---|
335 | * CORBA sometimes denies 2 object references are equivalent |
---|
336 | * even if they are [ this is a feature_not_bug ]. |
---|
337 | * |
---|
338 | * This function is commutative. |
---|
339 | * |
---|
340 | * Return value: TRUE if @a == @b |
---|
341 | **/ |
---|
342 | gboolean |
---|
343 | bonobo_arg_is_equal (BonoboArg *a, BonoboArg *b, CORBA_Environment *opt_ev) |
---|
344 | { |
---|
345 | CORBA_Environment ev, *my_ev; |
---|
346 | gboolean retval; |
---|
347 | |
---|
348 | if (!opt_ev) { |
---|
349 | |
---|
350 | CORBA_exception_init (&ev); |
---|
351 | my_ev = &ev; |
---|
352 | } else |
---|
353 | my_ev = opt_ev; |
---|
354 | |
---|
355 | retval = ORBit_any_equivalent (a, b, my_ev); |
---|
356 | |
---|
357 | if (!opt_ev) |
---|
358 | CORBA_exception_free (&ev); |
---|
359 | |
---|
360 | return retval; |
---|
361 | } |
---|