2003-08-30 Havoc Pennington <hp@pobox.com>
[platform/upstream/dbus.git] / glib / dbus-gidl.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-gidl.c data structure describing an interface, to be generated from IDL
3  *             or something
4  *
5  * Copyright (C) 2003  Red Hat, Inc.
6  *
7  * Licensed under the Academic Free License version 1.2
8  *
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.
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  */
24
25 #include "dbus-gidl.h"
26
27 struct InterfaceInfo
28 {
29   int refcount;
30   char *name;
31   GSList *methods;
32   GSList *signals;
33 };
34
35 struct MethodInfo
36 {
37   int refcount;
38   GSList *args;
39   char *name;
40 };
41
42 struct SignalInfo
43 {
44   int refcount;
45   GSList *args;
46   char *name;
47 };
48
49 struct ArgInfo
50 {
51   int refcount;
52   char *name;
53   int type;
54   ArgDirection direction;
55 };
56
57 static void
58 free_method_list (GSList **methods_p)
59 {
60   GSList *tmp;
61   tmp = *methods_p;
62   while (tmp != NULL)
63     {
64       method_info_unref (tmp->data);
65       tmp = tmp->next;
66     }
67   g_slist_free (*methods_p);
68   *methods_p = NULL;
69 }
70
71 static void
72 free_signal_list (GSList **signals_p)
73 {
74   GSList *tmp;
75   tmp = *signals_p;
76   while (tmp != NULL)
77     {
78       signal_info_unref (tmp->data);
79       tmp = tmp->next;
80     }
81   g_slist_free (*signals_p);
82   *signals_p = NULL;
83 }
84
85 InterfaceInfo*
86 interface_info_new (const char *name)
87 {
88   InterfaceInfo *info;
89
90   info = g_new0 (InterfaceInfo, 1);
91   info->refcount = 1;
92   info->name = g_strdup (name);
93   
94   return info;
95 }
96
97 void
98 interface_info_ref (InterfaceInfo *info)
99 {
100   info->refcount += 1;
101 }
102
103 void
104 interface_info_unref (InterfaceInfo *info)
105 {
106   info->refcount -= 1;
107   if (info->refcount == 0)
108     {
109       free_method_list (&info->methods);
110       free_signal_list (&info->signals);
111       g_free (info->name);
112       g_free (info);
113     }
114 }
115
116 GSList*
117 interface_info_get_methods (InterfaceInfo *info)
118 {
119   return info->methods;
120 }
121
122 GSList*
123 interface_info_get_signals (InterfaceInfo *info)
124 {
125   return info->signals;
126 }
127
128 void
129 interface_info_add_method (InterfaceInfo *info,
130                            MethodInfo    *method)
131 {
132   method_info_ref (method);
133   info->methods = g_slist_append (info->methods, method);
134 }
135
136 void
137 interface_info_add_signal (InterfaceInfo *info,
138                            SignalInfo    *signal)
139 {
140   signal_info_ref (signal);
141   info->signals = g_slist_append (info->signals, signal);
142 }
143
144 static void
145 free_arg_list (GSList **args_p)
146 {
147   GSList *tmp;
148   tmp = *args_p;
149   while (tmp != NULL)
150     {
151       arg_info_unref (tmp->data);
152       tmp = tmp->next;
153     }
154   g_slist_free (*args_p);
155   *args_p = NULL;
156 }
157
158 MethodInfo*
159 method_info_new (const char *name)
160 {
161   MethodInfo *info;
162
163   info = g_new0 (MethodInfo, 1);
164   info->refcount = 1;
165   info->name = g_strdup (name);
166   
167   return info;
168 }
169
170 void
171 method_info_ref (MethodInfo *info)
172 {
173   info->refcount += 1;
174 }
175
176 void
177 method_info_unref (MethodInfo *info)
178 {
179   info->refcount -= 1;
180   if (info->refcount == 0)
181     {
182       free_arg_list (&info->args);
183       g_free (info->name);
184       g_free (info);
185     }
186 }
187
188 const char*
189 method_info_get_name (MethodInfo *info)
190 {
191   return info->name;
192 }
193
194 GSList*
195 method_info_get_args (MethodInfo *info)
196 {
197   return info->args;
198 }
199
200 void
201 method_info_add_arg (MethodInfo    *info,
202                      ArgInfo       *arg)
203 {
204   arg_info_ref (arg);
205   info->args = g_slist_append (info->args, arg);
206 }
207
208 SignalInfo*
209 signal_info_new (const char *name)
210 {
211   SignalInfo *info;
212
213   info = g_new0 (SignalInfo, 1);
214   info->refcount = 1;
215   info->name = g_strdup (name);
216   
217   return info;
218 }
219
220 void
221 signal_info_ref (SignalInfo *info)
222 {
223   info->refcount += 1;
224 }
225
226 void
227 signal_info_unref (SignalInfo *info)
228 {
229   info->refcount -= 1;
230   if (info->refcount == 0)
231     {
232       free_arg_list (&info->args);
233       g_free (info->name);
234       g_free (info);
235     }
236 }
237
238 const char*
239 signal_info_get_name (SignalInfo *info)
240 {
241   return info->name;
242 }
243
244 GSList*
245 signal_info_get_args (SignalInfo *info)
246 {
247   return info->args;
248 }
249
250 void
251 signal_info_add_arg (SignalInfo    *info,
252                      ArgInfo       *arg)
253 {
254   arg_info_ref (arg);
255   info->args = g_slist_append (info->args, arg);
256 }
257
258 ArgInfo*
259 arg_info_new (const char  *name,
260               ArgDirection direction,
261               int          type)
262 {
263   ArgInfo *info;
264
265   info = g_new0 (ArgInfo, 1);
266   info->refcount = 1;
267   info->name = g_strdup (name);
268   info->direction = direction;
269   info->type = type;
270   
271   return info;
272 }
273
274 void
275 arg_info_ref (ArgInfo *info)
276 {
277   info->refcount += 1;
278 }
279
280 void
281 arg_info_unref (ArgInfo *info)
282 {
283   info->refcount -= 1;
284   if (info->refcount == 0)
285     {
286       g_free (info->name);
287       g_free (info);
288     }
289 }
290 const char*
291 arg_info_get_name (ArgInfo *info)
292 {
293   return info->name;
294 }
295
296 int
297 arg_info_get_type (ArgInfo *info)
298 {
299   return info->type;
300 }
301
302 ArgDirection
303 arg_info_get_direction (ArgInfo *info)
304 {
305   return info->direction;
306 }
307
308 #ifdef DBUS_BUILD_TESTS
309
310 /**
311  * @ingroup DBusGIDL
312  * Unit test for GLib IDL internals
313  * @returns #TRUE on success.
314  */
315 dbus_bool_t
316 _dbus_gidl_test (void)
317 {
318
319   return TRUE;
320 }
321
322 #endif /* DBUS_BUILD_TESTS */