Initial packaging for Tizen
[profile/ivi/gobject-introspection.git] / girepository / giregisteredtypeinfo.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2  * GObject introspection: Registered Type 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:giregisteredtypeinfo
33  * @Short_description: Struct representing a struct with a GType
34  * @Title: GIRegisteredTypeInfo
35  *
36  * GIRegisteredTypeInfo represents an entity with a GType associated. Could
37  * be either a #GIEnumInfo, #GIInterfaceInfo, #GIObjectInfo, #GIStructInfo or a
38  * #GIUnionInfo.
39  *
40  * A registered type info struct has a name and a type function.
41  * To get the name call g_registered_type_info_get_type_name().
42  * Most users want to call g_registered_type_info_get_g_type() and don't worry
43  * about the rest of the details.
44  *
45  * <refsect1 id="gi-giregisteredtypeinfo.struct-hierarchy" role="struct_hierarchy">
46  * <title role="struct_hierarchy.title">Struct hierarchy</title>
47  * <synopsis>
48  *   <link linkend="gi-GIBaseInfo">GIBaseInfo</link>
49  *    +----GIRegisteredTypeInfo
50  *          +----<link linkend="gi-GIEnumInfo">GIEnumInfo</link>
51  *          +----<link linkend="gi-GIInterfaceInfo">GIInterfaceInfo</link>
52  *          +----<link linkend="gi-GIObjectInfo">GIObjectInfo</link>
53  *          +----<link linkend="gi-GIStructInfo">GIStructInfo</link>
54  *          +----<link linkend="gi-GIUnionInfo">GIUnionInfo</link>
55  * </synopsis>
56  * </refsect1>
57  */
58
59 /**
60  * g_registered_type_info_get_type_name:
61  * @info: a #GIRegisteredTypeInfo
62  *
63  * Obtain the type name of the struct within the GObject type system.
64  * This type can be passed to g_type_name() to get a #GType.
65  *
66  * Returns: the type name
67  */
68 const gchar *
69 g_registered_type_info_get_type_name (GIRegisteredTypeInfo *info)
70 {
71   GIRealInfo *rinfo = (GIRealInfo *)info;
72   RegisteredTypeBlob *blob;
73
74   g_return_val_if_fail (info != NULL, NULL);
75   g_return_val_if_fail (GI_IS_REGISTERED_TYPE_INFO (info), NULL);
76
77   blob = (RegisteredTypeBlob *)&rinfo->typelib->data[rinfo->offset];
78
79   if (blob->gtype_name)
80     return g_typelib_get_string (rinfo->typelib, blob->gtype_name);
81
82   return NULL;
83 }
84
85 /**
86  * g_registered_type_info_get_type_init:
87  * @info: a #GIRegisteredTypeInfo
88  *
89  * Obtain the type init function for @info. The type init function is the
90  * function which will register the GType within the GObject type system.
91  * Usually this is not called by langauge bindings or applications, use
92  * g_registered_type_info_get_g_type() directly instead.
93  *
94  * Returns: the symbol name of the type init function, suitable for
95  * passing into g_module_symbol().
96  */
97 const gchar *
98 g_registered_type_info_get_type_init (GIRegisteredTypeInfo *info)
99 {
100   GIRealInfo *rinfo = (GIRealInfo *)info;
101   RegisteredTypeBlob *blob;
102
103   g_return_val_if_fail (info != NULL, NULL);
104   g_return_val_if_fail (GI_IS_REGISTERED_TYPE_INFO (info), NULL);
105
106   blob = (RegisteredTypeBlob *)&rinfo->typelib->data[rinfo->offset];
107
108   if (blob->gtype_init)
109     return g_typelib_get_string (rinfo->typelib, blob->gtype_init);
110
111   return NULL;
112 }
113
114 /**
115  * g_registered_type_info_get_g_type:
116  * @info: a #GIRegisteredTypeInfo
117  *
118  * Obtain the #GType for this registered type or G_TYPE_NONE which a special meaning.
119  * It means that either there is no type information associated with this @info or
120  * that the shared library which provides the type_init function for this
121  * @info cannot be called.
122  *
123  * Returns: the #GType.
124  */
125 GType
126 g_registered_type_info_get_g_type (GIRegisteredTypeInfo *info)
127 {
128   const char *type_init;
129   GType (* get_type_func) (void);
130   GIRealInfo *rinfo = (GIRealInfo*)info;
131
132   g_return_val_if_fail (info != NULL, G_TYPE_INVALID);
133   g_return_val_if_fail (GI_IS_REGISTERED_TYPE_INFO (info), G_TYPE_INVALID);
134
135   type_init = g_registered_type_info_get_type_init (info);
136
137   if (type_init == NULL)
138     return G_TYPE_NONE;
139   else if (!strcmp (type_init, "intern"))
140     /* The special string "intern" is used for some types exposed by libgobject
141        (that therefore should be always available) */
142     return g_type_from_name (g_registered_type_info_get_type_name (info));
143
144   get_type_func = NULL;
145   if (!g_typelib_symbol (rinfo->typelib,
146                          type_init,
147                          (void**) &get_type_func))
148     return G_TYPE_NONE;
149
150   return (* get_type_func) ();
151 }
152