Branch and submit for IVI panda
[profile/ivi/gobject-introspection.git] / girepository / giinterfaceinfo.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2  * GObject introspection: Interface implementation
3  *
4  * Copyright (C) 2005 Matthias Clasen
5  * Copyright (C) 2008,2009 Red Hat, Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <glib.h>
24
25 #include <girepository.h>
26 #include "girepository-private.h"
27 #include "gitypelib-internal.h"
28
29 /**
30  * SECTION:giinterfaceinfo
31  * @Short_description: Struct representing a GInterface
32  * @Title: GIInterfaceInfo
33  *
34  * GIInterfaceInfo represents a #GInterface type.
35  *
36  * A GInterface has methods, fields, properties, signals, interfaces, constants,
37  * virtual functions and prerequisites.
38  *
39  * <refsect1 id="gi-giinterfaceinfo.struct-hierarchy" role="struct_hierarchy">
40  * <title role="struct_hierarchy.title">Struct hierarchy</title>
41  * <synopsis>
42  *   <link linkend="gi-GIBaseInfo">GIBaseInfo</link>
43  *    +----<link linkend="gi-GIRegisteredTypeInfo">GIRegisteredTypeInfo</link>
44  *          +----GIInterfaceInfo
45  * </synopsis>
46  * </refsect1>
47  */
48
49 /**
50  * g_interface_info_get_n_prerequisites:
51  * @info: a #GIInterfaceInfo
52  *
53  * Obtain the number of prerequisites for this interface type.
54  * A prerequisites is another interface that needs to be implemented for
55  * interface, similar to an base class for GObjects.
56  *
57  * Returns: number of prerequisites
58  */
59 gint
60 g_interface_info_get_n_prerequisites (GIInterfaceInfo *info)
61 {
62   GIRealInfo *rinfo = (GIRealInfo *)info;
63   InterfaceBlob *blob;
64
65   g_return_val_if_fail (info != NULL, 0);
66   g_return_val_if_fail (GI_IS_INTERFACE_INFO (info), 0);
67
68   blob = (InterfaceBlob *)&rinfo->typelib->data[rinfo->offset];
69
70   return blob->n_prerequisites;
71 }
72
73 /**
74  * g_interface_info_get_prerequisite:
75  * @info: a #GIInterfaceInfo
76  * @n: index of prerequisites to get
77  *
78  * Obtain an interface type prerequisites index @n.
79  *
80  * Returns: (transfer full): the prerequisites as a #GIBaseInfo. Free the struct by calling
81  * g_base_info_unref() when done.
82  */
83 GIBaseInfo *
84 g_interface_info_get_prerequisite (GIInterfaceInfo *info,
85                                    gint            n)
86 {
87   GIRealInfo *rinfo = (GIRealInfo *)info;
88   InterfaceBlob *blob;
89
90   g_return_val_if_fail (info != NULL, NULL);
91   g_return_val_if_fail (GI_IS_INTERFACE_INFO (info), NULL);
92
93   blob = (InterfaceBlob *)&rinfo->typelib->data[rinfo->offset];
94
95   return _g_info_from_entry (rinfo->repository,
96                              rinfo->typelib, blob->prerequisites[n]);
97 }
98
99
100 /**
101  * g_interface_info_get_n_properties:
102  * @info: a #GIInterfaceInfo
103  *
104  * Obtain the number of properties that this interface type has.
105  *
106  * Returns: number of properties
107  */
108 gint
109 g_interface_info_get_n_properties (GIInterfaceInfo *info)
110 {
111   GIRealInfo *rinfo = (GIRealInfo *)info;
112   InterfaceBlob *blob;
113
114   g_return_val_if_fail (info != NULL, 0);
115   g_return_val_if_fail (GI_IS_INTERFACE_INFO (info), 0);
116
117   blob = (InterfaceBlob *)&rinfo->typelib->data[rinfo->offset];
118
119   return blob->n_properties;
120 }
121
122 /**
123  * g_interface_info_get_property:
124  * @info: a #GIInterfaceInfo
125  * @n: index of property to get
126  *
127  * Obtain an interface type property at index @n.
128  *
129  * Returns: (transfer full): the #GIPropertyInfo. Free the struct by calling
130  * g_base_info_unref() when done.
131  */
132 GIPropertyInfo *
133 g_interface_info_get_property (GIInterfaceInfo *info,
134                                gint            n)
135 {
136   gint offset;
137   GIRealInfo *rinfo = (GIRealInfo *)info;
138   Header *header;
139   InterfaceBlob *blob;
140
141   g_return_val_if_fail (info != NULL, NULL);
142   g_return_val_if_fail (GI_IS_INTERFACE_INFO (info), NULL);
143
144   header = (Header *)rinfo->typelib->data;
145   blob = (InterfaceBlob *)&rinfo->typelib->data[rinfo->offset];
146
147   offset = rinfo->offset + header->interface_blob_size
148     + (blob->n_prerequisites + (blob->n_prerequisites % 2)) * 2
149     + n * header->property_blob_size;
150
151   return (GIPropertyInfo *) g_info_new (GI_INFO_TYPE_PROPERTY, (GIBaseInfo*)info,
152                                         rinfo->typelib, offset);
153 }
154
155 /**
156  * g_interface_info_get_n_methods:
157  * @info: a #GIInterfaceInfo
158  *
159  * Obtain the number of methods that this interface type has.
160  *
161  * Returns: number of methods
162  */
163 gint
164 g_interface_info_get_n_methods (GIInterfaceInfo *info)
165 {
166   GIRealInfo *rinfo = (GIRealInfo *)info;
167   InterfaceBlob *blob;
168
169   g_return_val_if_fail (info != NULL, 0);
170   g_return_val_if_fail (GI_IS_INTERFACE_INFO (info), 0);
171
172   blob = (InterfaceBlob *)&rinfo->typelib->data[rinfo->offset];
173
174   return blob->n_methods;
175 }
176
177 /**
178  * g_interface_info_get_method:
179  * @info: a #GIInterfaceInfo
180  * @n: index of method to get
181  *
182  * Obtain an interface type method at index @n.
183  *
184  * Returns: (transfer full): the #GIFunctionInfo. Free the struct by calling
185  * g_base_info_unref() when done.
186  */
187 GIFunctionInfo *
188 g_interface_info_get_method (GIInterfaceInfo *info,
189                              gint            n)
190 {
191   gint offset;
192   GIRealInfo *rinfo = (GIRealInfo *)info;
193   Header *header;
194   InterfaceBlob *blob;
195
196   g_return_val_if_fail (info != NULL, NULL);
197   g_return_val_if_fail (GI_IS_INTERFACE_INFO (info), NULL);
198
199   header = (Header *)rinfo->typelib->data;
200   blob = (InterfaceBlob *)&rinfo->typelib->data[rinfo->offset];
201
202   offset = rinfo->offset + header->interface_blob_size
203     + (blob->n_prerequisites + (blob->n_prerequisites % 2)) * 2
204     + blob->n_properties * header->property_blob_size
205     + n * header->function_blob_size;
206
207   return (GIFunctionInfo *) g_info_new (GI_INFO_TYPE_FUNCTION, (GIBaseInfo*)info,
208                                         rinfo->typelib, offset);
209 }
210
211 /**
212  * g_interface_info_find_method:
213  * @info: a #GIInterfaceInfo
214  * @name: name of method to obtain
215  *
216  * Obtain a method of the interface type given a @name. %NULL will be
217  * returned if there's no method available with that name.
218  *
219  * Returns: (transfer full): the #GIFunctionInfo or %NULL if none found.
220  * Free the struct by calling g_base_info_unref() when done.
221  */
222 GIFunctionInfo *
223 g_interface_info_find_method (GIInterfaceInfo *info,
224                               const gchar     *name)
225 {
226   gint offset;
227   GIRealInfo *rinfo = (GIRealInfo *)info;
228   Header *header = (Header *)rinfo->typelib->data;
229   InterfaceBlob *blob = (InterfaceBlob *)&rinfo->typelib->data[rinfo->offset];
230
231   offset = rinfo->offset + header->interface_blob_size
232     + (blob->n_prerequisites + (blob->n_prerequisites % 2)) * 2
233     + blob->n_properties * header->property_blob_size;
234
235   return _g_base_info_find_method ((GIBaseInfo*)info, offset, blob->n_methods, name);
236 }
237
238 /**
239  * g_interface_info_get_n_signals:
240  * @info: a #GIInterfaceInfo
241  *
242  * Obtain the number of signals that this interface type has.
243  *
244  * Returns: number of signals
245  */
246 gint
247 g_interface_info_get_n_signals (GIInterfaceInfo *info)
248 {
249   GIRealInfo *rinfo = (GIRealInfo *)info;
250   InterfaceBlob *blob;
251
252   g_return_val_if_fail (info != NULL, 0);
253   g_return_val_if_fail (GI_IS_INTERFACE_INFO (info), 0);
254
255   blob = (InterfaceBlob *)&rinfo->typelib->data[rinfo->offset];
256
257   return blob->n_signals;
258 }
259
260 /**
261  * g_interface_info_get_signal:
262  * @info: a #GIInterfaceInfo
263  * @n: index of signal to get
264  *
265  * Obtain an interface type signal at index @n.
266  *
267  * Returns: (transfer full): the #GISignalInfo. Free the struct by calling
268  * g_base_info_unref() when done.
269  */
270 GISignalInfo *
271 g_interface_info_get_signal (GIInterfaceInfo *info,
272                              gint            n)
273 {
274   gint offset;
275   GIRealInfo *rinfo = (GIRealInfo *)info;
276   Header *header;
277   InterfaceBlob *blob;
278
279   g_return_val_if_fail (info != NULL, NULL);
280   g_return_val_if_fail (GI_IS_INTERFACE_INFO (info), NULL);
281
282   header = (Header *)rinfo->typelib->data;
283   blob = (InterfaceBlob *)&rinfo->typelib->data[rinfo->offset];
284
285   offset = rinfo->offset + header->interface_blob_size
286     + (blob->n_prerequisites + (blob->n_prerequisites % 2)) * 2
287     + blob->n_properties * header->property_blob_size
288     + blob->n_methods * header->function_blob_size
289     + n * header->signal_blob_size;
290
291   return (GISignalInfo *) g_info_new (GI_INFO_TYPE_SIGNAL, (GIBaseInfo*)info,
292                                       rinfo->typelib, offset);
293 }
294
295 /**
296  * g_interface_info_get_n_vfuncs:
297  * @info: a #GIInterfaceInfo
298  *
299  * Obtain the number of virtual functions that this interface type has.
300  *
301  * Returns: number of virtual functions
302  */
303 gint
304 g_interface_info_get_n_vfuncs (GIInterfaceInfo *info)
305 {
306   GIRealInfo *rinfo = (GIRealInfo *)info;
307   InterfaceBlob *blob;
308
309   g_return_val_if_fail (info != NULL, 0);
310   g_return_val_if_fail (GI_IS_INTERFACE_INFO (info), 0);
311
312   blob = (InterfaceBlob *)&rinfo->typelib->data[rinfo->offset];
313
314   return blob->n_vfuncs;
315 }
316
317 /**
318  * g_interface_info_get_vfunc:
319  * @info: a #GIInterfaceInfo
320  * @n: index of virtual function to get
321  *
322  * Obtain an interface type virtual function at index @n.
323  *
324  * Returns: (transfer full): the #GIVFuncInfo. Free the struct by calling
325  * g_base_info_unref() when done.
326  */
327 GIVFuncInfo *
328 g_interface_info_get_vfunc (GIInterfaceInfo *info,
329                             gint            n)
330 {
331   gint offset;
332   GIRealInfo *rinfo = (GIRealInfo *)info;
333   Header *header;
334   InterfaceBlob *blob;
335
336   g_return_val_if_fail (info != NULL, NULL);
337   g_return_val_if_fail (GI_IS_INTERFACE_INFO (info), NULL);
338
339   header = (Header *)rinfo->typelib->data;
340   blob = (InterfaceBlob *)&rinfo->typelib->data[rinfo->offset];
341
342   offset = rinfo->offset + header->interface_blob_size
343     + (blob->n_prerequisites + (blob->n_prerequisites % 2)) * 2
344     + blob->n_properties * header->property_blob_size
345     + blob->n_methods * header->function_blob_size
346     + blob->n_signals * header->signal_blob_size
347     + n * header->vfunc_blob_size;
348
349   return (GIVFuncInfo *) g_info_new (GI_INFO_TYPE_VFUNC, (GIBaseInfo*)info,
350                                      rinfo->typelib, offset);
351 }
352
353 /**
354  * g_interface_info_find_vfunc:
355  * @info: a #GIInterfaceInfo
356  * @name: The name of a virtual function to find.
357  *
358  * Locate a virtual function slot with name @name. See the documentation
359  * for g_object_info_find_vfunc() for more information on virtuals.
360  *
361  * Returns: (transfer full): the #GIVFuncInfo, or %NULL. Free it with
362  * g_base_info_unref() when done.
363  */
364 GIVFuncInfo *
365 g_interface_info_find_vfunc (GIInterfaceInfo *info,
366                              const gchar  *name)
367 {
368   gint offset;
369   GIRealInfo *rinfo = (GIRealInfo *)info;
370   Header *header;
371   InterfaceBlob *blob;
372
373   g_return_val_if_fail (info != NULL, NULL);
374   g_return_val_if_fail (GI_IS_INTERFACE_INFO (info), NULL);
375
376   header = (Header *)rinfo->typelib->data;
377   blob = (InterfaceBlob *)&rinfo->typelib->data[rinfo->offset];
378
379   offset = rinfo->offset + header->interface_blob_size
380     + (blob->n_prerequisites + blob->n_prerequisites % 2) * 2
381     + blob->n_properties * header->property_blob_size
382     + blob->n_methods * header->function_blob_size
383     + blob->n_signals * header->signal_blob_size;
384
385   return _g_base_info_find_vfunc (rinfo, offset, blob->n_vfuncs, name);
386 }
387
388 /**
389  * g_interface_info_get_n_constants:
390  * @info: a #GIInterfaceInfo
391  *
392  * Obtain the number of constants that this interface type has.
393  *
394  * Returns: number of constants
395  */
396 gint
397 g_interface_info_get_n_constants (GIInterfaceInfo *info)
398 {
399   GIRealInfo *rinfo = (GIRealInfo *)info;
400   InterfaceBlob *blob;
401
402   g_return_val_if_fail (info != NULL, 0);
403   g_return_val_if_fail (GI_IS_INTERFACE_INFO (info), 0);
404
405   blob = (InterfaceBlob *)&rinfo->typelib->data[rinfo->offset];
406
407   return blob->n_constants;
408 }
409
410 /**
411  * g_interface_info_get_constant:
412  * @info: a #GIInterfaceInfo
413  * @n: index of constant to get
414  *
415  * Obtain an interface type constant at index @n.
416  *
417  * Returns: (transfer full): the #GIConstantInfo. Free the struct by calling
418  * g_base_info_unref() when done.
419  */
420 GIConstantInfo *
421 g_interface_info_get_constant (GIInterfaceInfo *info,
422                                gint             n)
423 {
424   gint offset;
425   GIRealInfo *rinfo = (GIRealInfo *)info;
426   Header *header;
427   InterfaceBlob *blob;
428
429   g_return_val_if_fail (info != NULL, NULL);
430   g_return_val_if_fail (GI_IS_INTERFACE_INFO (info), NULL);
431
432   header = (Header *)rinfo->typelib->data;
433   blob = (InterfaceBlob *)&rinfo->typelib->data[rinfo->offset];
434
435   offset = rinfo->offset + header->interface_blob_size
436     + (blob->n_prerequisites + (blob->n_prerequisites % 2)) * 2
437     + blob->n_properties * header->property_blob_size
438     + blob->n_methods * header->function_blob_size
439     + blob->n_signals * header->signal_blob_size
440     + blob->n_vfuncs * header->vfunc_blob_size
441     + n * header->constant_blob_size;
442
443   return (GIConstantInfo *) g_info_new (GI_INFO_TYPE_CONSTANT, (GIBaseInfo*)info,
444                                         rinfo->typelib, offset);
445 }
446
447 /**
448  * g_interface_info_get_iface_struct:
449  * @info: a #GIInterfaceInfo
450  *
451  * Returns the layout C structure associated with this #GInterface.
452  *
453  * Returns: (transfer full): the #GIStructInfo or %NULL. Free it with
454  * g_base_info_unref() when done.
455  */
456 GIStructInfo *
457 g_interface_info_get_iface_struct (GIInterfaceInfo *info)
458 {
459   GIRealInfo *rinfo = (GIRealInfo *)info;
460   InterfaceBlob *blob;
461
462   g_return_val_if_fail (info != NULL, NULL);
463   g_return_val_if_fail (GI_IS_INTERFACE_INFO (info), NULL);
464
465   blob = (InterfaceBlob *)&rinfo->typelib->data[rinfo->offset];
466
467   if (blob->gtype_struct)
468     return (GIStructInfo *) _g_info_from_entry (rinfo->repository,
469                                                 rinfo->typelib, blob->gtype_struct);
470   else
471     return NULL;
472 }
473