1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 * GObject introspection: Constant 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 "config.h"
24
25 #include <glib.h>
26 #include <string.h> // memcpy
27
28 #include <girepository.h>
29 #include "girepository-private.h"
30 #include "gitypelib-internal.h"
31
32 /**
33 * SECTION:giconstantinfo
34 * @title: GIConstantInfo
35 * @short_description: Struct representing a constant
36 *
37 * GIConstantInfo represents a constant. A constant has a type associated
38 * which can be obtained by calling g_constant_info_get_type() and a value,
39 * which can be obtained by calling g_constant_info_get_value().
40 *
41 * <refsect1 id="gi-giconstantinfo.struct-hierarchy" role="struct_hierarchy">
42 * <title role="struct_hierarchy.title">Struct hierarchy</title>
43 * <synopsis>
44 * <link linkend="GIBaseInfo">GIBaseInfo</link>
45 * +----GIConstantInfo
46 * </synopsis>
47 * </refsect1>
48 */
49
50
51 /**
52 * g_constant_info_get_type:
53 * @info: a #GIConstantInfo
54 *
55 * Obtain the type of the constant as a #GITypeInfo.
56 *
57 * Returns: (transfer full): the #GITypeInfo. Free the struct by calling
58 * g_base_info_unref() when done.
59 */
60 GITypeInfo *
g_constant_info_get_type(GIConstantInfo * info)61 g_constant_info_get_type (GIConstantInfo *info)
62 {
63 GIRealInfo *rinfo = (GIRealInfo *)info;
64
65 g_return_val_if_fail (info != NULL, NULL);
66 g_return_val_if_fail (GI_IS_CONSTANT_INFO (info), NULL);
67
68 return _g_type_info_new ((GIBaseInfo*)info, rinfo->typelib, rinfo->offset + 8);
69 }
70
71 #define DO_ALIGNED_COPY(dest_addr, src_addr, type) \
72 memcpy((dest_addr), (src_addr), sizeof(type))
73
74 /**
75 * g_constant_info_free_value: (skip)
76 * @info: a #GIConstantInfo
77 * @value: the argument
78 *
79 * Free the value returned from g_constant_info_get_value().
80 *
81 * Since: 1.32
82 */
83 void
g_constant_info_free_value(GIConstantInfo * info,GIArgument * value)84 g_constant_info_free_value (GIConstantInfo *info,
85 GIArgument *value)
86 {
87 GIRealInfo *rinfo = (GIRealInfo *)info;
88 ConstantBlob *blob;
89
90 g_return_if_fail (info != NULL);
91 g_return_if_fail (GI_IS_CONSTANT_INFO (info));
92
93 blob = (ConstantBlob *)&rinfo->typelib->data[rinfo->offset];
94
95 /* FIXME non-basic types ? */
96 if (blob->type.flags.reserved == 0 && blob->type.flags.reserved2 == 0)
97 {
98 if (blob->type.flags.pointer)
99 g_free (value->v_pointer);
100 }
101 }
102
103 /**
104 * g_constant_info_get_value: (skip)
105 * @info: a #GIConstantInfo
106 * @value: (out): an argument
107 *
108 * Obtain the value associated with the #GIConstantInfo and store it in the
109 * @value parameter. @argument needs to be allocated before passing it in.
110 * The size of the constant value stored in @argument will be returned.
111 * Free the value with g_constant_info_free_value().
112 *
113 * Returns: size of the constant
114 */
115 gint
g_constant_info_get_value(GIConstantInfo * info,GIArgument * value)116 g_constant_info_get_value (GIConstantInfo *info,
117 GIArgument *value)
118 {
119 GIRealInfo *rinfo = (GIRealInfo *)info;
120 ConstantBlob *blob;
121
122 g_return_val_if_fail (info != NULL, 0);
123 g_return_val_if_fail (GI_IS_CONSTANT_INFO (info), 0);
124
125 blob = (ConstantBlob *)&rinfo->typelib->data[rinfo->offset];
126
127 /* FIXME non-basic types ? */
128 if (blob->type.flags.reserved == 0 && blob->type.flags.reserved2 == 0)
129 {
130 if (blob->type.flags.pointer)
131 {
132 #if GLIB_CHECK_VERSION (2, 67, 5)
133 gsize blob_size = blob->size;
134
135 value->v_pointer = g_memdup2 (&rinfo->typelib->data[blob->offset], blob_size);
136 #else
137 value->v_pointer = g_memdup (&rinfo->typelib->data[blob->offset], blob->size);
138 #endif
139 }
140 else
141 {
142 switch (blob->type.flags.tag)
143 {
144 case GI_TYPE_TAG_BOOLEAN:
145 value->v_boolean = *(gboolean*)&rinfo->typelib->data[blob->offset];
146 break;
147 case GI_TYPE_TAG_INT8:
148 value->v_int8 = *(gint8*)&rinfo->typelib->data[blob->offset];
149 break;
150 case GI_TYPE_TAG_UINT8:
151 value->v_uint8 = *(guint8*)&rinfo->typelib->data[blob->offset];
152 break;
153 case GI_TYPE_TAG_INT16:
154 value->v_int16 = *(gint16*)&rinfo->typelib->data[blob->offset];
155 break;
156 case GI_TYPE_TAG_UINT16:
157 value->v_uint16 = *(guint16*)&rinfo->typelib->data[blob->offset];
158 break;
159 case GI_TYPE_TAG_INT32:
160 value->v_int32 = *(gint32*)&rinfo->typelib->data[blob->offset];
161 break;
162 case GI_TYPE_TAG_UINT32:
163 value->v_uint32 = *(guint32*)&rinfo->typelib->data[blob->offset];
164 break;
165 case GI_TYPE_TAG_INT64:
166 DO_ALIGNED_COPY(&value->v_int64, &rinfo->typelib->data[blob->offset], gint64);
167 break;
168 case GI_TYPE_TAG_UINT64:
169 DO_ALIGNED_COPY(&value->v_uint64, &rinfo->typelib->data[blob->offset], guint64);
170 break;
171 case GI_TYPE_TAG_FLOAT:
172 DO_ALIGNED_COPY(&value->v_float, &rinfo->typelib->data[blob->offset], gfloat);
173 break;
174 case GI_TYPE_TAG_DOUBLE:
175 DO_ALIGNED_COPY(&value->v_double, &rinfo->typelib->data[blob->offset], gdouble);
176 break;
177 default:
178 g_assert_not_reached ();
179 }
180 }
181 }
182
183 return blob->size;
184 }
185
186