Add gobject-introspection.changes file
[profile/ivi/gobject-introspection.git] / girepository / gitypeinfo.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2  * GObject introspection: Type 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:gitypeinfo
31  * @Short_description: Struct representing a type
32  * @Title: GITypeInfo
33  *
34  * GITypeInfo represents a type. You can retrieve a type info from
35  * an argument (see #GIArgInfo), a functions return value (see #GIFunctionInfo),
36  * a field (see #GIFieldInfo), a property (see #GIPropertyInfo), a constant
37  * (see #GIConstantInfo) or for a union discriminator (see #GIUnionInfo).
38  *
39  * A type can either be a of a basic type which is a standard C primitive
40  * type or an interface type. For interface types you need to call
41  * g_type_info_get_interface() to get a reference to the base info for that
42  * interface.
43  *
44  * <refsect1 id="gi-gitypeinfo.struct-hierarchy" role="struct_hierarchy">
45  * <title role="struct_hierarchy.title">Struct hierarchy</title>
46  * <synopsis>
47  *   <link linkend="gi-GIBaseInfo">GIBaseInfo</link>
48  *    +----GITypeInfo
49  * </synopsis>
50  * </refsect1>
51  *
52  */
53
54 /**
55  * g_type_info_is_pointer:
56  * @info: a #GITypeInfo
57  *
58  * Obtain if the type is passed as a reference.
59  *
60  * Returns: %TRUE if it is a pointer
61  */
62 gboolean
63 g_type_info_is_pointer (GITypeInfo *info)
64 {
65   GIRealInfo *rinfo = (GIRealInfo *)info;
66   SimpleTypeBlob *type;
67
68   g_return_val_if_fail (info != NULL, FALSE);
69   g_return_val_if_fail (GI_IS_TYPE_INFO (info), FALSE);
70
71   type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
72
73   if (type->flags.reserved == 0 && type->flags.reserved2 == 0)
74     return type->flags.pointer;
75   else
76     {
77       InterfaceTypeBlob *iface = (InterfaceTypeBlob *)&rinfo->typelib->data[rinfo->offset];
78
79       return iface->pointer;
80     }
81 }
82
83 /**
84  * g_type_info_get_tag:
85  * @info: a #GITypeInfo
86  *
87  * Obtain the type tag for the type. See #GITypeTag for a list
88  * of type tags.
89  *
90  * Returns: the type tag
91  */
92 GITypeTag
93 g_type_info_get_tag (GITypeInfo *info)
94 {
95   GIRealInfo *rinfo = (GIRealInfo *)info;
96   SimpleTypeBlob *type;
97
98   g_return_val_if_fail (info != NULL, GI_TYPE_TAG_BOOLEAN);
99   g_return_val_if_fail (GI_IS_TYPE_INFO (info), GI_TYPE_TAG_BOOLEAN);
100
101   type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
102
103   if (rinfo->type_is_embedded)
104     return GI_TYPE_TAG_INTERFACE;
105   else if (type->flags.reserved == 0 && type->flags.reserved2 == 0)
106     return type->flags.tag;
107   else
108     {
109       InterfaceTypeBlob *iface = (InterfaceTypeBlob *)&rinfo->typelib->data[rinfo->offset];
110
111       return iface->tag;
112     }
113 }
114
115 /**
116  * g_type_info_get_param_type:
117  * @info: a #GITypeInfo
118  * @n: index of the parameter
119  *
120  * Obtain the parameter type @n.
121  *
122  * Returns: (transfer full): the param type info
123  */
124 GITypeInfo *
125 g_type_info_get_param_type (GITypeInfo *info,
126                             gint        n)
127 {
128   GIRealInfo *rinfo = (GIRealInfo *)info;
129   SimpleTypeBlob *type;
130
131   g_return_val_if_fail (info != NULL, NULL);
132   g_return_val_if_fail (GI_IS_TYPE_INFO (info), NULL);
133
134   type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
135
136   if (!(type->flags.reserved == 0 && type->flags.reserved2 == 0))
137     {
138       ParamTypeBlob *param = (ParamTypeBlob *)&rinfo->typelib->data[rinfo->offset];
139
140       switch (param->tag)
141         {
142           case GI_TYPE_TAG_ARRAY:
143           case GI_TYPE_TAG_GLIST:
144           case GI_TYPE_TAG_GSLIST:
145           case GI_TYPE_TAG_GHASH:
146             return _g_type_info_new ((GIBaseInfo*)info, rinfo->typelib,
147                                     rinfo->offset + sizeof (ParamTypeBlob)
148                                     + sizeof (SimpleTypeBlob) * n);
149             break;
150           default:
151             break;
152         }
153     }
154
155   return NULL;
156 }
157
158 /**
159  * g_type_info_get_interface:
160  * @info: a #GITypeInfo
161  *
162  * For types which have #GI_TYPE_TAG_INTERFACE such as GObjects and boxed values,
163  * this function returns full information about the referenced type.  You can then
164  * inspect the type of the returned #GIBaseInfo to further query whether it is
165  * a concrete GObject, a GInterface, a structure, etc. using g_base_info_get_type().
166  *
167  * Returns: (transfer full): the #GIBaseInfo, or %NULL. Free it with
168  * g_base_info_unref() when done.
169  */
170 GIBaseInfo *
171 g_type_info_get_interface (GITypeInfo *info)
172 {
173   GIRealInfo *rinfo = (GIRealInfo *)info;
174
175   g_return_val_if_fail (info != NULL, NULL);
176   g_return_val_if_fail (GI_IS_TYPE_INFO (info), NULL);
177
178   /* For embedded types, the given offset is a pointer to the actual blob,
179    * after the end of the field.  In that case we know it's a "subclass" of
180    * CommonBlob, so use that to determine the info type.
181    */
182   if (rinfo->type_is_embedded)
183     {
184       CommonBlob *common = (CommonBlob *)&rinfo->typelib->data[rinfo->offset];
185       GIInfoType info_type;
186
187       switch (common->blob_type)
188         {
189           case BLOB_TYPE_CALLBACK:
190             info_type = GI_INFO_TYPE_CALLBACK;
191             break;
192           default:
193             g_assert_not_reached ();
194             return NULL;
195         }
196       return (GIBaseInfo *) g_info_new (info_type, (GIBaseInfo*)info, rinfo->typelib,
197                                         rinfo->offset);
198     }
199   else
200     {
201       SimpleTypeBlob *type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
202       if (!(type->flags.reserved == 0 && type->flags.reserved2 == 0))
203         {
204           InterfaceTypeBlob *blob = (InterfaceTypeBlob *)&rinfo->typelib->data[rinfo->offset];
205
206           if (blob->tag == GI_TYPE_TAG_INTERFACE)
207             return _g_info_from_entry (rinfo->repository, rinfo->typelib, blob->interface);
208         }
209     }
210
211   return NULL;
212 }
213
214 /**
215  * g_type_info_get_array_length:
216  * @info: a #GITypeInfo
217  *
218  * Obtain the array length of the type. The type tag must be a
219  * #GI_TYPE_TAG_ARRAY or -1 will returned.
220  *
221  * Returns: the array length, or -1 if the type is not an array
222  */
223 gint
224 g_type_info_get_array_length (GITypeInfo *info)
225 {
226   GIRealInfo *rinfo = (GIRealInfo *)info;
227   SimpleTypeBlob *type;
228
229   g_return_val_if_fail (info != NULL, -1);
230   g_return_val_if_fail (GI_IS_TYPE_INFO (info), -1);
231
232   type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
233
234   if (!(type->flags.reserved == 0 && type->flags.reserved2 == 0))
235     {
236       ArrayTypeBlob *blob = (ArrayTypeBlob *)&rinfo->typelib->data[rinfo->offset];
237
238       if (blob->tag == GI_TYPE_TAG_ARRAY)
239         {
240           if (blob->has_length)
241             return blob->dimensions.length;
242         }
243     }
244
245   return -1;
246 }
247
248 /**
249  * g_type_info_get_array_fixed_size:
250  * @info: a #GITypeInfo
251  *
252  * Obtain the fixed array size of the type. The type tag must be a
253  * #GI_TYPE_TAG_ARRAY or -1 will returned.
254  *
255  * Returns: the size or -1 if it's not an array
256  */
257 gint
258 g_type_info_get_array_fixed_size (GITypeInfo *info)
259 {
260   GIRealInfo *rinfo = (GIRealInfo *)info;
261   SimpleTypeBlob *type;
262
263   g_return_val_if_fail (info != NULL, 0);
264   g_return_val_if_fail (GI_IS_TYPE_INFO (info), 0);
265
266   type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
267
268   if (!(type->flags.reserved == 0 && type->flags.reserved2 == 0))
269     {
270       ArrayTypeBlob *blob = (ArrayTypeBlob *)&rinfo->typelib->data[rinfo->offset];
271
272       if (blob->tag == GI_TYPE_TAG_ARRAY)
273         {
274           if (blob->has_size)
275             return blob->dimensions.size;
276         }
277     }
278
279   return -1;
280 }
281
282 /**
283  * g_type_info_is_zero_terminated:
284  * @info: a #GITypeInfo
285  *
286  * Obtain if the last element of the array is %NULL. The type tag must be a
287  * #GI_TYPE_TAG_ARRAY or %FALSE will returned.
288  *
289  * Returns: %TRUE if zero terminated
290  */
291 gboolean
292 g_type_info_is_zero_terminated (GITypeInfo *info)
293 {
294   GIRealInfo *rinfo = (GIRealInfo *)info;
295   SimpleTypeBlob *type;
296
297   g_return_val_if_fail (info != NULL, FALSE);
298   g_return_val_if_fail (GI_IS_TYPE_INFO (info), FALSE);
299
300   type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
301
302   if (!(type->flags.reserved == 0 && type->flags.reserved2 == 0))
303     {
304       ArrayTypeBlob *blob = (ArrayTypeBlob *)&rinfo->typelib->data[rinfo->offset];
305
306       if (blob->tag == GI_TYPE_TAG_ARRAY)
307         return blob->zero_terminated;
308     }
309
310   return FALSE;
311 }
312
313 /**
314  * g_type_info_get_array_type:
315  * @info: a #GITypeInfo
316  *
317  * Obtain the array type for this type. See #GIArrayType for a list of
318  * possible values. If the type tag of this type is not array, -1 will be
319  * returned.
320  *
321  * Returns: the array type or -1
322  */
323 GIArrayType
324 g_type_info_get_array_type (GITypeInfo *info)
325 {
326   GIRealInfo *rinfo = (GIRealInfo *)info;
327   SimpleTypeBlob *type;
328
329   g_return_val_if_fail (info != NULL, -1);
330   g_return_val_if_fail (GI_IS_TYPE_INFO (info), -1);
331
332   type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
333
334   if (!(type->flags.reserved == 0 && type->flags.reserved2 == 0))
335     {
336       ArrayTypeBlob *blob = (ArrayTypeBlob *)&rinfo->typelib->data[rinfo->offset];
337       g_return_val_if_fail (blob->tag == GI_TYPE_TAG_ARRAY, -1);
338
339       return blob->array_type;
340     }
341
342   return -1;
343 }