1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-gidl.c data structure describing an interface, to be generated from IDL
5 * Copyright (C) 2003, 2005 Red Hat, Inc.
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-gidl.h"
27 #ifndef DOXYGEN_SHOULD_SKIP_THIS
31 unsigned int refcount : 28;
32 unsigned int type : 4;
46 GHashTable *annotations;
47 /* Since we have BaseInfo now these could be one list */
56 GHashTable *annotations;
70 PropertyAccessFlags access;
77 ArgDirection direction;
81 get_hash_key (gpointer key, gpointer value, gpointer data)
84 *list = g_slist_prepend (*list, key);
88 get_hash_keys (GHashTable *table)
92 g_hash_table_foreach (table, get_hash_key, &ret);
98 base_info_ref (BaseInfo *info)
100 g_return_val_if_fail (info != NULL, NULL);
101 g_return_val_if_fail (info->refcount > 0, NULL);
109 base_info_free (void *ptr)
120 base_info_unref (BaseInfo *info)
122 g_return_if_fail (info != NULL);
123 g_return_if_fail (info->refcount > 0);
125 /* This is sort of bizarre, BaseInfo was tacked on later */
130 node_info_unref ((NodeInfo*) info);
132 case INFO_TYPE_INTERFACE:
133 interface_info_unref ((InterfaceInfo*) info);
135 case INFO_TYPE_SIGNAL:
136 signal_info_unref ((SignalInfo*) info);
138 case INFO_TYPE_METHOD:
139 method_info_unref ((MethodInfo*) info);
141 case INFO_TYPE_PROPERTY:
142 property_info_unref ((PropertyInfo*) info);
145 arg_info_unref ((ArgInfo*) info);
151 base_info_get_type (BaseInfo *info)
157 base_info_get_name (BaseInfo *info)
163 base_info_set_name (BaseInfo *info,
169 info->name = g_strdup (name);
174 base_info_get_gtype (void)
176 static GType our_type = 0;
179 our_type = g_boxed_type_register_static ("BaseInfo",
180 (GBoxedCopyFunc) base_info_ref,
181 (GBoxedFreeFunc) base_info_unref);
187 free_interface_list (GSList **interfaces_p)
193 interface_info_unref (tmp->data);
196 g_slist_free (*interfaces_p);
197 *interfaces_p = NULL;
201 free_node_list (GSList **nodes_p)
207 node_info_unref (tmp->data);
210 g_slist_free (*nodes_p);
215 free_method_list (GSList **methods_p)
221 method_info_unref (tmp->data);
224 g_slist_free (*methods_p);
229 free_signal_list (GSList **signals_p)
235 signal_info_unref (tmp->data);
238 g_slist_free (*signals_p);
243 free_property_list (GSList **props_p)
249 property_info_unref (tmp->data);
252 g_slist_free (*props_p);
257 node_info_new (const char *name)
261 /* name can be NULL */
263 info = g_new0 (NodeInfo, 1);
264 info->base.refcount = 1;
265 info->base.name = g_strdup (name);
266 info->base.type = INFO_TYPE_NODE;
272 node_info_ref (NodeInfo *info)
274 info->base.refcount += 1;
280 node_info_unref (NodeInfo *info)
282 info->base.refcount -= 1;
283 if (info->base.refcount == 0)
285 free_interface_list (&info->interfaces);
286 free_node_list (&info->nodes);
287 base_info_free (info);
292 node_info_get_name (NodeInfo *info)
294 return info->base.name;
298 node_info_get_interfaces (NodeInfo *info)
300 return info->interfaces;
304 node_info_add_interface (NodeInfo *info,
305 InterfaceInfo *interface)
307 interface_info_ref (interface);
308 info->interfaces = g_slist_append (info->interfaces, interface);
312 node_info_get_nodes (NodeInfo *info)
318 node_info_add_node (NodeInfo *info,
321 node_info_ref (node);
322 info->nodes = g_slist_append (info->nodes, node);
326 node_info_replace_node (NodeInfo *info,
332 node_info_ref (new_child); /* before unref old_child in case they are the same */
333 link = g_slist_find (info->nodes, old_child);
334 g_assert (link != NULL);
335 node_info_unref (old_child);
336 link->data = new_child;
340 interface_info_new (const char *name)
344 info = g_new0 (InterfaceInfo, 1);
345 info->base.refcount = 1;
346 info->base.name = g_strdup (name);
347 info->base.type = INFO_TYPE_INTERFACE;
348 info->annotations = g_hash_table_new_full (g_str_hash, g_str_equal,
349 (GDestroyNotify) g_free,
350 (GDestroyNotify) g_free);
356 interface_info_ref (InterfaceInfo *info)
358 info->base.refcount += 1;
364 interface_info_unref (InterfaceInfo *info)
366 info->base.refcount -= 1;
367 if (info->base.refcount == 0)
369 g_hash_table_destroy (info->annotations);
370 free_method_list (&info->methods);
371 free_signal_list (&info->signals);
372 free_property_list (&info->properties);
373 base_info_free (info);
378 interface_info_get_name (InterfaceInfo *info)
380 return info->base.name;
384 interface_info_get_annotations (InterfaceInfo *info)
386 return get_hash_keys (info->annotations);
390 interface_info_get_annotation (InterfaceInfo *info,
393 return g_hash_table_lookup (info->annotations, name);
397 interface_info_get_methods (InterfaceInfo *info)
399 return info->methods;
403 interface_info_get_signals (InterfaceInfo *info)
405 return info->signals;
409 interface_info_get_properties (InterfaceInfo *info)
411 return info->properties;
415 interface_info_add_annotation (InterfaceInfo *info,
419 g_hash_table_insert (info->annotations,
425 interface_info_add_method (InterfaceInfo *info,
428 method_info_ref (method);
429 info->methods = g_slist_append (info->methods, method);
433 interface_info_add_signal (InterfaceInfo *info,
436 signal_info_ref (signal);
437 info->signals = g_slist_append (info->signals, signal);
441 interface_info_add_property (InterfaceInfo *info,
442 PropertyInfo *property)
444 property_info_ref (property);
445 info->properties = g_slist_append (info->properties, property);
449 free_arg_list (GSList **args_p)
455 ArgInfo *ai = tmp->data;
456 g_assert (ai->base.type == INFO_TYPE_ARG);
457 arg_info_unref (tmp->data);
460 g_slist_free (*args_p);
465 method_info_new (const char *name)
469 info = g_new0 (MethodInfo, 1);
470 info->base.refcount = 1;
471 info->base.name = g_strdup (name);
472 info->base.type = INFO_TYPE_METHOD;
473 info->annotations = g_hash_table_new_full (g_str_hash, g_str_equal,
474 (GDestroyNotify) g_free,
475 (GDestroyNotify) g_free);
481 method_info_ref (MethodInfo *info)
483 info->base.refcount += 1;
489 method_info_unref (MethodInfo *info)
491 info->base.refcount -= 1;
492 if (info->base.refcount == 0)
494 g_hash_table_destroy (info->annotations);
495 free_arg_list (&info->args);
496 base_info_free (info);
501 method_info_get_name (MethodInfo *info)
503 return info->base.name;
507 method_info_get_annotations (MethodInfo *info)
509 return get_hash_keys (info->annotations);
513 method_info_get_annotation (MethodInfo *info,
516 return g_hash_table_lookup (info->annotations, name);
520 method_info_get_args (MethodInfo *info)
526 method_info_get_n_args (MethodInfo *info)
528 return g_slist_length (info->args);
532 args_sort_by_direction (const void *a,
535 const ArgInfo *arg_a = a;
536 const ArgInfo *arg_b = b;
538 if (arg_a->direction == arg_b->direction)
540 else if (arg_a->direction == ARG_IN)
541 return -1; /* in is less than out */
547 method_info_add_annotation (MethodInfo *info,
551 g_hash_table_insert (info->annotations,
557 method_info_add_arg (MethodInfo *info,
561 info->args = g_slist_append (info->args, arg);
563 /* Keep "in" args sorted before "out" and otherwise maintain
564 * stable order (g_slist_sort is stable, at least in sufficiently
567 info->args = g_slist_sort (info->args, args_sort_by_direction);
571 signal_info_new (const char *name)
575 info = g_new0 (SignalInfo, 1);
576 info->base.refcount = 1;
577 info->base.name = g_strdup (name);
578 info->base.type = INFO_TYPE_SIGNAL;
584 signal_info_ref (SignalInfo *info)
586 info->base.refcount += 1;
592 signal_info_unref (SignalInfo *info)
594 info->base.refcount -= 1;
595 if (info->base.refcount == 0)
597 free_arg_list (&info->args);
598 base_info_free (info);
603 signal_info_get_name (SignalInfo *info)
605 return info->base.name;
609 signal_info_get_args (SignalInfo *info)
615 signal_info_get_n_args (SignalInfo *info)
617 return g_slist_length (info->args);
621 signal_info_add_arg (SignalInfo *info,
624 g_assert (arg->direction == ARG_OUT);
627 info->args = g_slist_append (info->args, arg);
629 /* signal args don't need sorting since only "out" is allowed */
633 property_info_new (const char *name,
635 PropertyAccessFlags access)
639 info = g_new0 (PropertyInfo, 1);
640 info->base.refcount = 1;
641 info->base.name = g_strdup (name);
642 info->base.type = INFO_TYPE_PROPERTY;
645 info->access = access;
651 property_info_ref (PropertyInfo *info)
653 info->base.refcount += 1;
659 property_info_unref (PropertyInfo *info)
661 info->base.refcount -= 1;
662 if (info->base.refcount == 0)
664 base_info_free (info);
669 property_info_get_name (PropertyInfo *info)
671 return info->base.name;
675 property_info_get_type (PropertyInfo *info)
681 property_info_get_access (PropertyInfo *info)
687 arg_info_new (const char *name,
688 ArgDirection direction,
693 info = g_new0 (ArgInfo, 1);
694 info->base.refcount = 1;
695 info->base.type = INFO_TYPE_ARG;
697 /* name can be NULL */
698 info->base.name = g_strdup (name);
699 info->direction = direction;
706 arg_info_ref (ArgInfo *info)
708 info->base.refcount += 1;
714 arg_info_unref (ArgInfo *info)
716 info->base.refcount -= 1;
717 if (info->base.refcount == 0)
719 base_info_free (info);
723 arg_info_get_name (ArgInfo *info)
725 return info->base.name;
729 arg_info_get_type (ArgInfo *info)
735 arg_info_get_direction (ArgInfo *info)
737 return info->direction;
740 #ifdef DBUS_BUILD_TESTS
744 * Unit test for GLib IDL internals
745 * @returns #TRUE on success.
748 _dbus_gidl_test (void)
754 #endif /* DBUS_BUILD_TESTS */
756 #endif /* DOXYGEN_SHOULD_SKIP_THIS */