Branch and submit for IVI panda
[profile/ivi/gobject-introspection.git] / girepository / gifunctioninfo.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2  * GObject introspection: Function 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 <string.h>
24
25 #include <glib.h>
26
27 #include <girepository.h>
28 #include "girepository-private.h"
29 #include "gitypelib-internal.h"
30
31 /**
32  * SECTION:gifunctioninfo
33  * @Short_description: Struct representing a function
34  * @Title: GIFunctionInfo
35  *
36  * GIFunctionInfo represents a function, method or constructor.
37  * To find out what kind of entity a #GIFunctionInfo represents, call
38  * g_function_info_get_flags().
39  *
40  * See also #GICallableInfo for information on how to retreive arguments and
41  * other metadata.
42  *
43  * <refsect1 id="gi-gifunctioninfo.struct-hierarchy" role="struct_hierarchy">
44  * <title role="struct_hierarchy.title">Struct hierarchy</title>
45  * <synopsis>
46  *   <link linkend="gi-GIBaseInfo">GIBaseInfo</link>
47  *    +----<link linkend="gi-GICallableInfo">GICallableInfo</link>
48  *          +----GIFunctionInfo
49  *          +----<link linkend="gi-GISignalInfo">GISignalInfo</link>
50  *          +----<link linkend="gi-GIVFuncInfo">GIVFuncInfo</link>
51  * </synopsis>
52  * </refsect1>
53  */
54
55 GIFunctionInfo *
56 _g_base_info_find_method (GIBaseInfo   *base,
57                           guint32       offset,
58                           gint          n_methods,
59                           const gchar  *name)
60 {
61   /* FIXME hash */
62   GIRealInfo *rinfo = (GIRealInfo*)base;
63   Header *header = (Header *)rinfo->typelib->data;
64   gint i;
65
66   for (i = 0; i < n_methods; i++)
67     {
68       FunctionBlob *fblob = (FunctionBlob *)&rinfo->typelib->data[offset];
69       const gchar *fname = (const gchar *)&rinfo->typelib->data[fblob->name];
70
71       if (strcmp (name, fname) == 0)
72         return (GIFunctionInfo *) g_info_new (GI_INFO_TYPE_FUNCTION, base,
73                                               rinfo->typelib, offset);
74
75       offset += header->function_blob_size;
76     }
77
78   return NULL;
79 }
80
81 /**
82  * g_function_info_get_symbol:
83  * @info: a #GIFunctionInfo
84  *
85  * Obtain the symbol of the function. The symbol is the name of the
86  * exported function, suitable to be used as an argument to
87  * g_module_symbol().
88  *
89  * Returns: the symbol
90  */
91 const gchar *
92 g_function_info_get_symbol (GIFunctionInfo *info)
93 {
94   GIRealInfo *rinfo;
95   FunctionBlob *blob;
96
97   g_return_val_if_fail (info != NULL, NULL);
98   g_return_val_if_fail (GI_IS_FUNCTION_INFO (info), NULL);
99
100   rinfo = (GIRealInfo *)info;
101   blob = (FunctionBlob *)&rinfo->typelib->data[rinfo->offset];
102
103   return g_typelib_get_string (rinfo->typelib, blob->symbol);
104 }
105
106 /**
107  * g_function_info_get_flags:
108  * @info: a #GIFunctionInfo
109  *
110  * Obtain the #GIFunctionInfoFlags for the @info.
111  *
112  * Returns: the flags
113  */
114 GIFunctionInfoFlags
115 g_function_info_get_flags (GIFunctionInfo *info)
116 {
117   GIFunctionInfoFlags flags;
118   GIRealInfo *rinfo;
119   FunctionBlob *blob;
120
121   g_return_val_if_fail (info != NULL, -1);
122   g_return_val_if_fail (GI_IS_FUNCTION_INFO (info), -1);
123
124   rinfo = (GIRealInfo *)info;
125   blob = (FunctionBlob *)&rinfo->typelib->data[rinfo->offset];
126
127   flags = 0;
128
129   /* Make sure we don't flag Constructors as methods */
130   if (!blob->constructor && !blob->is_static)
131     flags = flags | GI_FUNCTION_IS_METHOD;
132
133   if (blob->constructor)
134     flags = flags | GI_FUNCTION_IS_CONSTRUCTOR;
135
136   if (blob->getter)
137     flags = flags | GI_FUNCTION_IS_GETTER;
138
139   if (blob->setter)
140     flags = flags | GI_FUNCTION_IS_SETTER;
141
142   if (blob->wraps_vfunc)
143     flags = flags | GI_FUNCTION_WRAPS_VFUNC;
144
145   if (blob->throws)
146     flags = flags | GI_FUNCTION_THROWS;
147
148   return flags;
149 }
150
151 /**
152  * g_function_info_get_property:
153  * @info: a #GIFunctionInfo
154  *
155  * Obtain the property associated with this #GIFunctionInfo.
156  * Only #GIFunctionInfo with the flag %GI_FUNCTION_IS_GETTER or
157  * %GI_FUNCTION_IS_SETTER have a property set. For other cases,
158  * %NULL will be returned.
159  *
160  * Returns: (transfer full): the property or %NULL if not set. Free it with
161  * g_base_info_unref() when done.
162  */
163 GIPropertyInfo *
164 g_function_info_get_property (GIFunctionInfo *info)
165 {
166   GIRealInfo *rinfo;
167   FunctionBlob *blob;
168   GIInterfaceInfo *container;
169
170   g_return_val_if_fail (info != NULL, NULL);
171   g_return_val_if_fail (GI_IS_FUNCTION_INFO (info), NULL);
172
173   rinfo = (GIRealInfo *)info;
174   blob = (FunctionBlob *)&rinfo->typelib->data[rinfo->offset];
175   container = (GIInterfaceInfo *)rinfo->container;
176
177   return g_interface_info_get_property (container, blob->index);
178 }
179
180 /**
181  * g_function_info_get_vfunc:
182  * @info: a #GIFunctionInfo
183  *
184  * Obtain the virtual function associated with this #GIFunctionInfo.
185  * Only #GIFunctionInfo with the flag %GI_FUNCTION_WRAPS_VFUNC has
186  * a virtual function set. For other cases, %NULL will be returned.
187  *
188  * Returns: (transfer full): the virtual function or %NULL if not set.
189  * Free it by calling g_base_info_unref() when done.
190  */
191 GIVFuncInfo *
192 g_function_info_get_vfunc (GIFunctionInfo *info)
193 {
194   GIRealInfo *rinfo;
195   FunctionBlob *blob;
196   GIInterfaceInfo *container;
197
198   g_return_val_if_fail (info != NULL, NULL);
199   g_return_val_if_fail (GI_IS_FUNCTION_INFO (info), NULL);
200
201   rinfo = (GIRealInfo *)info;
202   blob = (FunctionBlob *)&rinfo->typelib->data[rinfo->offset];
203   container = (GIInterfaceInfo *)rinfo->container;
204
205   return g_interface_info_get_vfunc (container, blob->index);
206 }
207
208 GQuark
209 g_invoke_error_quark (void)
210 {
211   static GQuark quark = 0;
212   if (quark == 0)
213     quark = g_quark_from_static_string ("g-invoke-error-quark");
214   return quark;
215 }
216
217 /**
218  * g_function_info_invoke: (skip)
219  * @info: a #GIFunctionInfo describing the function to invoke
220  * @in_args: an array of #GIArgument<!-- -->s, one for each in
221  *    parameter of @info. If there are no in parameter, @in_args
222  *    can be %NULL
223  * @n_in_args: the length of the @in_args array
224  * @out_args: an array of #GIArgument<!-- -->s, one for each out
225  *    parameter of @info. If there are no out parameters, @out_args
226  *    may be %NULL
227  * @n_out_args: the length of the @out_args array
228  * @return_value: return location for the return value of the
229  *    function. If the function returns void, @return_value may be
230  *    %NULL
231  * @error: return location for detailed error information, or %NULL
232  *
233  * Invokes the function described in @info with the given
234  * arguments. Note that inout parameters must appear in both
235  * argument lists. This function uses dlsym() to obtain a pointer
236  * to the function, so the library or shared object containing the
237  * described function must either be linked to the caller, or must
238  * have been g_module_symbol()<!-- -->ed before calling this function.
239  *
240  * Returns: %TRUE if the function has been invoked, %FALSE if an
241  *   error occurred.
242  */
243 gboolean
244 g_function_info_invoke (GIFunctionInfo *info,
245                         const GIArgument  *in_args,
246                         int               n_in_args,
247                         const GIArgument  *out_args,
248                         int               n_out_args,
249                         GIArgument        *return_value,
250                         GError          **error)
251 {
252   const gchar *symbol;
253   gpointer func;
254   gboolean is_method;
255   gboolean throws;
256
257   symbol = g_function_info_get_symbol (info);
258
259   if (!g_typelib_symbol (g_base_info_get_typelib((GIBaseInfo *) info),
260                          symbol, &func))
261     {
262       g_set_error (error,
263                    G_INVOKE_ERROR,
264                    G_INVOKE_ERROR_SYMBOL_NOT_FOUND,
265                    "Could not locate %s: %s", symbol, g_module_error ());
266
267       return FALSE;
268     }
269
270   is_method = (g_function_info_get_flags (info) & GI_FUNCTION_IS_METHOD) != 0
271     && (g_function_info_get_flags (info) & GI_FUNCTION_IS_CONSTRUCTOR) == 0;
272   throws = g_function_info_get_flags (info) & GI_FUNCTION_THROWS;
273
274   return g_callable_info_invoke ((GICallableInfo*) info,
275                                  func,
276                                  in_args,
277                                  n_in_args,
278                                  out_args,
279                                  n_out_args,
280                                  return_value,
281                                  is_method,
282                                  throws,
283                                  error);
284 }