1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 * GObject introspection: Function 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.
27 #include <girepository.h>
28 #include "girepository-private.h"
29 #include "gitypelib-internal.h"
32 * SECTION:gifunctioninfo
33 * @Short_description: Struct representing a function
34 * @Title: GIFunctionInfo
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().
40 * See also #GICallableInfo for information on how to retreive arguments and
43 * <refsect1 id="gi-gifunctioninfo.struct-hierarchy" role="struct_hierarchy">
44 * <title role="struct_hierarchy.title">Struct hierarchy</title>
46 * <link linkend="gi-GIBaseInfo">GIBaseInfo</link>
47 * +----<link linkend="gi-GICallableInfo">GICallableInfo</link>
49 * +----<link linkend="gi-GISignalInfo">GISignalInfo</link>
50 * +----<link linkend="gi-GIVFuncInfo">GIVFuncInfo</link>
56 _g_base_info_find_method (GIBaseInfo *base,
62 GIRealInfo *rinfo = (GIRealInfo*)base;
63 Header *header = (Header *)rinfo->typelib->data;
66 for (i = 0; i < n_methods; i++)
68 FunctionBlob *fblob = (FunctionBlob *)&rinfo->typelib->data[offset];
69 const gchar *fname = (const gchar *)&rinfo->typelib->data[fblob->name];
71 if (strcmp (name, fname) == 0)
72 return (GIFunctionInfo *) g_info_new (GI_INFO_TYPE_FUNCTION, base,
73 rinfo->typelib, offset);
75 offset += header->function_blob_size;
82 * g_function_info_get_symbol:
83 * @info: a #GIFunctionInfo
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
92 g_function_info_get_symbol (GIFunctionInfo *info)
97 g_return_val_if_fail (info != NULL, NULL);
98 g_return_val_if_fail (GI_IS_FUNCTION_INFO (info), NULL);
100 rinfo = (GIRealInfo *)info;
101 blob = (FunctionBlob *)&rinfo->typelib->data[rinfo->offset];
103 return g_typelib_get_string (rinfo->typelib, blob->symbol);
107 * g_function_info_get_flags:
108 * @info: a #GIFunctionInfo
110 * Obtain the #GIFunctionInfoFlags for the @info.
115 g_function_info_get_flags (GIFunctionInfo *info)
117 GIFunctionInfoFlags flags;
121 g_return_val_if_fail (info != NULL, -1);
122 g_return_val_if_fail (GI_IS_FUNCTION_INFO (info), -1);
124 rinfo = (GIRealInfo *)info;
125 blob = (FunctionBlob *)&rinfo->typelib->data[rinfo->offset];
129 /* Make sure we don't flag Constructors as methods */
130 if (!blob->constructor && !blob->is_static)
131 flags = flags | GI_FUNCTION_IS_METHOD;
133 if (blob->constructor)
134 flags = flags | GI_FUNCTION_IS_CONSTRUCTOR;
137 flags = flags | GI_FUNCTION_IS_GETTER;
140 flags = flags | GI_FUNCTION_IS_SETTER;
142 if (blob->wraps_vfunc)
143 flags = flags | GI_FUNCTION_WRAPS_VFUNC;
146 flags = flags | GI_FUNCTION_THROWS;
152 * g_function_info_get_property:
153 * @info: a #GIFunctionInfo
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.
160 * Returns: (transfer full): the property or %NULL if not set. Free it with
161 * g_base_info_unref() when done.
164 g_function_info_get_property (GIFunctionInfo *info)
168 GIInterfaceInfo *container;
170 g_return_val_if_fail (info != NULL, NULL);
171 g_return_val_if_fail (GI_IS_FUNCTION_INFO (info), NULL);
173 rinfo = (GIRealInfo *)info;
174 blob = (FunctionBlob *)&rinfo->typelib->data[rinfo->offset];
175 container = (GIInterfaceInfo *)rinfo->container;
177 return g_interface_info_get_property (container, blob->index);
181 * g_function_info_get_vfunc:
182 * @info: a #GIFunctionInfo
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.
188 * Returns: (transfer full): the virtual function or %NULL if not set.
189 * Free it by calling g_base_info_unref() when done.
192 g_function_info_get_vfunc (GIFunctionInfo *info)
196 GIInterfaceInfo *container;
198 g_return_val_if_fail (info != NULL, NULL);
199 g_return_val_if_fail (GI_IS_FUNCTION_INFO (info), NULL);
201 rinfo = (GIRealInfo *)info;
202 blob = (FunctionBlob *)&rinfo->typelib->data[rinfo->offset];
203 container = (GIInterfaceInfo *)rinfo->container;
205 return g_interface_info_get_vfunc (container, blob->index);
209 g_invoke_error_quark (void)
211 static GQuark quark = 0;
213 quark = g_quark_from_static_string ("g-invoke-error-quark");
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
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
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
231 * @error: return location for detailed error information, or %NULL
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.
240 * Returns: %TRUE if the function has been invoked, %FALSE if an
244 g_function_info_invoke (GIFunctionInfo *info,
245 const GIArgument *in_args,
247 const GIArgument *out_args,
249 GIArgument *return_value,
257 symbol = g_function_info_get_symbol (info);
259 if (!g_typelib_symbol (g_base_info_get_typelib((GIBaseInfo *) info),
264 G_INVOKE_ERROR_SYMBOL_NOT_FOUND,
265 "Could not locate %s: %s", symbol, g_module_error ());
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;
274 return g_callable_info_invoke ((GICallableInfo*) info,