Initial packaging for Tizen
[profile/ivi/gobject-introspection.git] / girepository / gistructinfo.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2  * GObject introspection: Struct 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:gistructinfo
31  * @Short_description: Struct representing a C structure
32  * @Title: GIStructInfo
33  *
34  * GIStructInfo represents a generic C structure type.
35  *
36  * A structure has methods and fields.
37  *
38  * <refsect1 id="gi-giobjectinfo.struct-hierarchy" role="struct_hierarchy">
39  * <title role="struct_hierarchy.title">Struct hierarchy</title>
40  * <synopsis>
41  *   <link linkend="gi-GIBaseInfo">GIBaseInfo</link>
42  *    +----<link linkend="gi-GIRegisteredTypeInfo">GIRegisteredTypeInfo</link>
43  *          +----GIStructInfo
44  * </synopsis>
45  * </refsect1>
46  */
47
48 /**
49  * g_struct_info_get_n_fields:
50  * @info: a #GIStructInfo
51  *
52  * Obtain the number of fields this structure has.
53  *
54  * Returns: number of fields
55  */
56 gint
57 g_struct_info_get_n_fields (GIStructInfo *info)
58 {
59   GIRealInfo *rinfo = (GIRealInfo *)info;
60   StructBlob *blob = (StructBlob *)&rinfo->typelib->data[rinfo->offset];
61
62   return blob->n_fields;
63 }
64
65 /**
66  * g_struct_info_get_field_offset:
67  * @info: a #GIStructInfo
68  * @n: index of queried field
69  *
70  * Obtain the offset of the specified field.
71  *
72  * Returns: field offset in bytes
73  */
74 static gint32
75 g_struct_get_field_offset (GIStructInfo *info,
76                            gint         n)
77 {
78   GIRealInfo *rinfo = (GIRealInfo *)info;
79   Header *header = (Header *)rinfo->typelib->data;
80   guint32 offset = rinfo->offset + header->struct_blob_size;
81   gint i;
82   FieldBlob *field_blob;
83
84   for (i = 0; i < n; i++)
85     {
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;
90     }
91
92   return offset;
93 }
94
95 /**
96  * g_struct_info_get_field:
97  * @info: a #GIStructInfo
98  * @n: a field index
99  *
100  * Obtain the type information for field with specified index.
101  *
102  * Returns: (transfer full): the #GIFieldInfo, free it with g_base_info_unref()
103  * when done.
104  */
105 GIFieldInfo *
106 g_struct_info_get_field (GIStructInfo *info,
107                          gint          n)
108 {
109   GIRealInfo *rinfo = (GIRealInfo *)info;
110
111   return (GIFieldInfo *) g_info_new (GI_INFO_TYPE_FIELD, (GIBaseInfo*)info, rinfo->typelib,
112                                      g_struct_get_field_offset (info, n));
113 }
114
115 /**
116  * g_struct_info_get_n_methods:
117  * @info: a #GIStructInfo
118  *
119  * Obtain the number of methods this structure has.
120  *
121  * Returns: number of methods
122  */
123 gint
124 g_struct_info_get_n_methods (GIStructInfo *info)
125 {
126   GIRealInfo *rinfo = (GIRealInfo *)info;
127   StructBlob *blob = (StructBlob *)&rinfo->typelib->data[rinfo->offset];
128
129   return blob->n_methods;
130 }
131
132 /**
133  * g_struct_info_get_method:
134  * @info: a #GIStructInfo
135  * @n: a method index
136  *
137  * Obtain the type information for method with specified index.
138  *
139  * Returns: (transfer full): the #GIFunctionInfo, free it with g_base_info_unref()
140  * when done.
141  */
142 GIFunctionInfo *
143 g_struct_info_get_method (GIStructInfo *info,
144                           gint         n)
145 {
146   GIRealInfo *rinfo = (GIRealInfo *)info;
147   StructBlob *blob = (StructBlob *)&rinfo->typelib->data[rinfo->offset];
148   Header *header = (Header *)rinfo->typelib->data;
149   gint offset;
150
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);
154 }
155
156 /**
157  * g_struct_info_find_method:
158  * @info: a #GIStructInfo
159  * @name: a method name
160  *
161  * Obtain the type information for method named @name.
162  *
163  * Returns: (transfer full): the #GIFunctionInfo, free it with g_base_info_unref()
164  * when done.
165  */
166 GIFunctionInfo *
167 g_struct_info_find_method (GIStructInfo *info,
168                            const gchar  *name)
169 {
170   gint offset;
171   GIRealInfo *rinfo = (GIRealInfo *)info;
172   Header *header = (Header *)rinfo->typelib->data;
173   StructBlob *blob = (StructBlob *)&rinfo->typelib->data[rinfo->offset];
174
175   offset = rinfo->offset + header->struct_blob_size
176     + blob->n_fields * header->field_blob_size;
177
178   return _g_base_info_find_method ((GIBaseInfo*)info, offset, blob->n_methods, name);
179 }
180
181 /**
182  * g_struct_info_get_size:
183  * @info: a #GIStructInfo
184  *
185  * Obtain the total size of the structure.
186  *
187  * Returns: size of the structure in bytes
188  */
189 gsize
190 g_struct_info_get_size (GIStructInfo *info)
191 {
192   GIRealInfo *rinfo = (GIRealInfo *)info;
193   StructBlob *blob = (StructBlob *)&rinfo->typelib->data[rinfo->offset];
194
195   return blob->size;
196 }
197
198 /**
199  * g_struct_info_get_alignment:
200  * @info: a #GIStructInfo
201  *
202  * Obtain the required alignment of the structure.
203  *
204  * Returns: required alignment in bytes
205  */
206 gsize
207 g_struct_info_get_alignment (GIStructInfo *info)
208 {
209   GIRealInfo *rinfo = (GIRealInfo *)info;
210   StructBlob *blob = (StructBlob *)&rinfo->typelib->data[rinfo->offset];
211
212   return blob->alignment;
213 }
214
215 gboolean
216 g_struct_info_is_foreign (GIStructInfo *info)
217 {
218   GIRealInfo *rinfo = (GIRealInfo *)info;
219   StructBlob *blob = (StructBlob *)&rinfo->typelib->data[rinfo->offset];
220
221   return blob->foreign;
222 }
223
224 /**
225  * g_struct_info_is_gtype_struct:
226  * @info: a #GIStructInfo
227  *
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.
231  *
232  * Returns: %TRUE if this is a class struct, %FALSE otherwise
233  */
234 gboolean
235 g_struct_info_is_gtype_struct (GIStructInfo *info)
236 {
237   GIRealInfo *rinfo = (GIRealInfo *)info;
238   StructBlob *blob = (StructBlob *)&rinfo->typelib->data[rinfo->offset];
239
240   return blob->is_gtype_struct;
241 }