1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 * GObject introspection: Type implementation
4 * Copyright (C) 2005 Matthias Clasen
5 * Copyright (C) 2008,2009 Red Hat, Inc.
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.
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.
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.
25 #include <girepository.h>
26 #include "girepository-private.h"
27 #include "gitypelib-internal.h"
31 * @Short_description: Struct representing a type
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).
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
44 * <refsect1 id="gi-gitypeinfo.struct-hierarchy" role="struct_hierarchy">
45 * <title role="struct_hierarchy.title">Struct hierarchy</title>
47 * <link linkend="gi-GIBaseInfo">GIBaseInfo</link>
55 * g_type_info_is_pointer:
56 * @info: a #GITypeInfo
58 * Obtain if the type is passed as a reference.
60 * Returns: %TRUE if it is a pointer
63 g_type_info_is_pointer (GITypeInfo *info)
65 GIRealInfo *rinfo = (GIRealInfo *)info;
68 g_return_val_if_fail (info != NULL, FALSE);
69 g_return_val_if_fail (GI_IS_TYPE_INFO (info), FALSE);
71 type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
73 if (type->flags.reserved == 0 && type->flags.reserved2 == 0)
74 return type->flags.pointer;
77 InterfaceTypeBlob *iface = (InterfaceTypeBlob *)&rinfo->typelib->data[rinfo->offset];
79 return iface->pointer;
84 * g_type_info_get_tag:
85 * @info: a #GITypeInfo
87 * Obtain the type tag for the type. See #GITypeTag for a list
90 * Returns: the type tag
93 g_type_info_get_tag (GITypeInfo *info)
95 GIRealInfo *rinfo = (GIRealInfo *)info;
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);
101 type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
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;
109 InterfaceTypeBlob *iface = (InterfaceTypeBlob *)&rinfo->typelib->data[rinfo->offset];
116 * g_type_info_get_param_type:
117 * @info: a #GITypeInfo
118 * @n: index of the parameter
120 * Obtain the parameter type @n.
122 * Returns: (transfer full): the param type info
125 g_type_info_get_param_type (GITypeInfo *info,
128 GIRealInfo *rinfo = (GIRealInfo *)info;
129 SimpleTypeBlob *type;
131 g_return_val_if_fail (info != NULL, NULL);
132 g_return_val_if_fail (GI_IS_TYPE_INFO (info), NULL);
134 type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
136 if (!(type->flags.reserved == 0 && type->flags.reserved2 == 0))
138 ParamTypeBlob *param = (ParamTypeBlob *)&rinfo->typelib->data[rinfo->offset];
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);
159 * g_type_info_get_interface:
160 * @info: a #GITypeInfo
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().
167 * Returns: (transfer full): the #GIBaseInfo, or %NULL. Free it with
168 * g_base_info_unref() when done.
171 g_type_info_get_interface (GITypeInfo *info)
173 GIRealInfo *rinfo = (GIRealInfo *)info;
175 g_return_val_if_fail (info != NULL, NULL);
176 g_return_val_if_fail (GI_IS_TYPE_INFO (info), NULL);
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.
182 if (rinfo->type_is_embedded)
184 CommonBlob *common = (CommonBlob *)&rinfo->typelib->data[rinfo->offset];
185 GIInfoType info_type;
187 switch (common->blob_type)
189 case BLOB_TYPE_CALLBACK:
190 info_type = GI_INFO_TYPE_CALLBACK;
193 g_assert_not_reached ();
196 return (GIBaseInfo *) g_info_new (info_type, (GIBaseInfo*)info, rinfo->typelib,
201 SimpleTypeBlob *type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
202 if (!(type->flags.reserved == 0 && type->flags.reserved2 == 0))
204 InterfaceTypeBlob *blob = (InterfaceTypeBlob *)&rinfo->typelib->data[rinfo->offset];
206 if (blob->tag == GI_TYPE_TAG_INTERFACE)
207 return _g_info_from_entry (rinfo->repository, rinfo->typelib, blob->interface);
215 * g_type_info_get_array_length:
216 * @info: a #GITypeInfo
218 * Obtain the array length of the type. The type tag must be a
219 * #GI_TYPE_TAG_ARRAY or -1 will returned.
221 * Returns: the array length, or -1 if the type is not an array
224 g_type_info_get_array_length (GITypeInfo *info)
226 GIRealInfo *rinfo = (GIRealInfo *)info;
227 SimpleTypeBlob *type;
229 g_return_val_if_fail (info != NULL, -1);
230 g_return_val_if_fail (GI_IS_TYPE_INFO (info), -1);
232 type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
234 if (!(type->flags.reserved == 0 && type->flags.reserved2 == 0))
236 ArrayTypeBlob *blob = (ArrayTypeBlob *)&rinfo->typelib->data[rinfo->offset];
238 if (blob->tag == GI_TYPE_TAG_ARRAY)
240 if (blob->has_length)
241 return blob->dimensions.length;
249 * g_type_info_get_array_fixed_size:
250 * @info: a #GITypeInfo
252 * Obtain the fixed array size of the type. The type tag must be a
253 * #GI_TYPE_TAG_ARRAY or -1 will returned.
255 * Returns: the size or -1 if it's not an array
258 g_type_info_get_array_fixed_size (GITypeInfo *info)
260 GIRealInfo *rinfo = (GIRealInfo *)info;
261 SimpleTypeBlob *type;
263 g_return_val_if_fail (info != NULL, 0);
264 g_return_val_if_fail (GI_IS_TYPE_INFO (info), 0);
266 type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
268 if (!(type->flags.reserved == 0 && type->flags.reserved2 == 0))
270 ArrayTypeBlob *blob = (ArrayTypeBlob *)&rinfo->typelib->data[rinfo->offset];
272 if (blob->tag == GI_TYPE_TAG_ARRAY)
275 return blob->dimensions.size;
283 * g_type_info_is_zero_terminated:
284 * @info: a #GITypeInfo
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.
289 * Returns: %TRUE if zero terminated
292 g_type_info_is_zero_terminated (GITypeInfo *info)
294 GIRealInfo *rinfo = (GIRealInfo *)info;
295 SimpleTypeBlob *type;
297 g_return_val_if_fail (info != NULL, FALSE);
298 g_return_val_if_fail (GI_IS_TYPE_INFO (info), FALSE);
300 type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
302 if (!(type->flags.reserved == 0 && type->flags.reserved2 == 0))
304 ArrayTypeBlob *blob = (ArrayTypeBlob *)&rinfo->typelib->data[rinfo->offset];
306 if (blob->tag == GI_TYPE_TAG_ARRAY)
307 return blob->zero_terminated;
314 * g_type_info_get_array_type:
315 * @info: a #GITypeInfo
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
321 * Returns: the array type or -1
324 g_type_info_get_array_type (GITypeInfo *info)
326 GIRealInfo *rinfo = (GIRealInfo *)info;
327 SimpleTypeBlob *type;
329 g_return_val_if_fail (info != NULL, -1);
330 g_return_val_if_fail (GI_IS_TYPE_INFO (info), -1);
332 type = (SimpleTypeBlob *)&rinfo->typelib->data[rinfo->offset];
334 if (!(type->flags.reserved == 0 && type->flags.reserved2 == 0))
336 ArrayTypeBlob *blob = (ArrayTypeBlob *)&rinfo->typelib->data[rinfo->offset];
337 g_return_val_if_fail (blob->tag == GI_TYPE_TAG_ARRAY, -1);
339 return blob->array_type;