1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 * GObject introspection: Struct 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"
30 * SECTION:gistructinfo
31 * @Short_description: Struct representing a C structure
32 * @Title: GIStructInfo
34 * GIStructInfo represents a generic C structure type.
36 * A structure has methods and fields.
38 * <refsect1 id="gi-giobjectinfo.struct-hierarchy" role="struct_hierarchy">
39 * <title role="struct_hierarchy.title">Struct hierarchy</title>
41 * <link linkend="gi-GIBaseInfo">GIBaseInfo</link>
42 * +----<link linkend="gi-GIRegisteredTypeInfo">GIRegisteredTypeInfo</link>
49 * g_struct_info_get_n_fields:
50 * @info: a #GIStructInfo
52 * Obtain the number of fields this structure has.
54 * Returns: number of fields
57 g_struct_info_get_n_fields (GIStructInfo *info)
59 GIRealInfo *rinfo = (GIRealInfo *)info;
60 StructBlob *blob = (StructBlob *)&rinfo->typelib->data[rinfo->offset];
62 return blob->n_fields;
66 * g_struct_info_get_field_offset:
67 * @info: a #GIStructInfo
68 * @n: index of queried field
70 * Obtain the offset of the specified field.
72 * Returns: field offset in bytes
75 g_struct_get_field_offset (GIStructInfo *info,
78 GIRealInfo *rinfo = (GIRealInfo *)info;
79 Header *header = (Header *)rinfo->typelib->data;
80 guint32 offset = rinfo->offset + header->struct_blob_size;
82 FieldBlob *field_blob;
84 for (i = 0; i < n; i++)
86 field_blob = (FieldBlob *)&rinfo->typelib->data[offset];
87 offset += header->field_blob_size;
88 if (field_blob->has_embedded_type)
89 offset += header->callback_blob_size;
96 * g_struct_info_get_field:
97 * @info: a #GIStructInfo
100 * Obtain the type information for field with specified index.
102 * Returns: (transfer full): the #GIFieldInfo, free it with g_base_info_unref()
106 g_struct_info_get_field (GIStructInfo *info,
109 GIRealInfo *rinfo = (GIRealInfo *)info;
111 return (GIFieldInfo *) g_info_new (GI_INFO_TYPE_FIELD, (GIBaseInfo*)info, rinfo->typelib,
112 g_struct_get_field_offset (info, n));
116 * g_struct_info_get_n_methods:
117 * @info: a #GIStructInfo
119 * Obtain the number of methods this structure has.
121 * Returns: number of methods
124 g_struct_info_get_n_methods (GIStructInfo *info)
126 GIRealInfo *rinfo = (GIRealInfo *)info;
127 StructBlob *blob = (StructBlob *)&rinfo->typelib->data[rinfo->offset];
129 return blob->n_methods;
133 * g_struct_info_get_method:
134 * @info: a #GIStructInfo
137 * Obtain the type information for method with specified index.
139 * Returns: (transfer full): the #GIFunctionInfo, free it with g_base_info_unref()
143 g_struct_info_get_method (GIStructInfo *info,
146 GIRealInfo *rinfo = (GIRealInfo *)info;
147 StructBlob *blob = (StructBlob *)&rinfo->typelib->data[rinfo->offset];
148 Header *header = (Header *)rinfo->typelib->data;
151 offset = g_struct_get_field_offset (info, blob->n_fields) + n * header->function_blob_size;
152 return (GIFunctionInfo *) g_info_new (GI_INFO_TYPE_FUNCTION, (GIBaseInfo*)info,
153 rinfo->typelib, offset);
157 * g_struct_info_find_method:
158 * @info: a #GIStructInfo
159 * @name: a method name
161 * Obtain the type information for method named @name.
163 * Returns: (transfer full): the #GIFunctionInfo, free it with g_base_info_unref()
167 g_struct_info_find_method (GIStructInfo *info,
171 GIRealInfo *rinfo = (GIRealInfo *)info;
172 Header *header = (Header *)rinfo->typelib->data;
173 StructBlob *blob = (StructBlob *)&rinfo->typelib->data[rinfo->offset];
175 offset = rinfo->offset + header->struct_blob_size
176 + blob->n_fields * header->field_blob_size;
178 return _g_base_info_find_method ((GIBaseInfo*)info, offset, blob->n_methods, name);
182 * g_struct_info_get_size:
183 * @info: a #GIStructInfo
185 * Obtain the total size of the structure.
187 * Returns: size of the structure in bytes
190 g_struct_info_get_size (GIStructInfo *info)
192 GIRealInfo *rinfo = (GIRealInfo *)info;
193 StructBlob *blob = (StructBlob *)&rinfo->typelib->data[rinfo->offset];
199 * g_struct_info_get_alignment:
200 * @info: a #GIStructInfo
202 * Obtain the required alignment of the structure.
204 * Returns: required alignment in bytes
207 g_struct_info_get_alignment (GIStructInfo *info)
209 GIRealInfo *rinfo = (GIRealInfo *)info;
210 StructBlob *blob = (StructBlob *)&rinfo->typelib->data[rinfo->offset];
212 return blob->alignment;
216 g_struct_info_is_foreign (GIStructInfo *info)
218 GIRealInfo *rinfo = (GIRealInfo *)info;
219 StructBlob *blob = (StructBlob *)&rinfo->typelib->data[rinfo->offset];
221 return blob->foreign;
225 * g_struct_info_is_gtype_struct:
226 * @info: a #GIStructInfo
228 * Return true if this structure represents the "class structure" for some
229 * #GObject or #GInterface. This function is mainly useful to hide this kind of structure
230 * from generated public APIs.
232 * Returns: %TRUE if this is a class struct, %FALSE otherwise
235 g_struct_info_is_gtype_struct (GIStructInfo *info)
237 GIRealInfo *rinfo = (GIRealInfo *)info;
238 StructBlob *blob = (StructBlob *)&rinfo->typelib->data[rinfo->offset];
240 return blob->is_gtype_struct;