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 * SPDX-License-Identifier: LGPL-2.1-or-later
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
23 */
24
25 #include "config.h"
26
27 #include <glib.h>
28 #include <string.h> // memcpy
29
30 #include <girepository/girepository.h>
31 #include "gibaseinfo-private.h"
32 #include "girepository-private.h"
33 #include "gitypelib-internal.h"
34 #include "giconstantinfo.h"
35
36 /**
37 * GIConstantInfo:
38 *
39 * `GIConstantInfo` represents a constant.
40 *
41 * A constant has a type associated – which can be obtained by calling
42 * [method@GIRepository.ConstantInfo.get_type_info] – and a value – which can be
43 * obtained by calling [method@GIRepository.ConstantInfo.get_value].
44 *
45 * Since: 2.80
46 */
47
48 /**
49 * gi_constant_info_get_type_info:
50 * @info: a #GIConstantInfo
51 *
52 * Obtain the type of the constant as a [class@GIRepository.TypeInfo].
53 *
54 * Returns: (transfer full): The [class@GIRepository.TypeInfo]. Free the struct
55 * by calling [method@GIRepository.BaseInfo.unref] when done.
56 * Since: 2.80
57 */
58 GITypeInfo *
59 gi_constant_info_get_type_info (GIConstantInfo *info)
60 {
61 GIRealInfo *rinfo = (GIRealInfo *)info;
62
63 g_return_val_if_fail (info != NULL, NULL);
64 g_return_val_if_fail (GI_IS_CONSTANT_INFO (info), NULL);
65
66 return gi_type_info_new ((GIBaseInfo*)info, rinfo->typelib, rinfo->offset + 8);
67 }
68
69 #define DO_ALIGNED_COPY(dest_addr, src_addr, type) \
70 memcpy((dest_addr), (src_addr), sizeof(type))
71
72 /**
73 * gi_constant_info_free_value: (skip)
74 * @info: a #GIConstantInfo
75 * @value: the argument
76 *
77 * Free the value returned from [method@GIRepository.ConstantInfo.get_value].
78 *
79 * Since: 2.80
80 */
81 void
82 gi_constant_info_free_value (GIConstantInfo *info,
83 GIArgument *value)
84 {
85 GIRealInfo *rinfo = (GIRealInfo *)info;
86 ConstantBlob *blob;
87
88 g_return_if_fail (info != NULL);
89 g_return_if_fail (GI_IS_CONSTANT_INFO (info));
90
91 blob = (ConstantBlob *)&rinfo->typelib->data[rinfo->offset];
92
93 /* FIXME non-basic types ? */
94 if (blob->type.flags.reserved == 0 && blob->type.flags.reserved2 == 0)
95 {
96 if (blob->type.flags.pointer)
97 g_free (value->v_pointer);
98 }
99 }
100
101 /**
102 * gi_constant_info_get_value: (skip)
103 * @info: a #GIConstantInfo
104 * @value: (out caller-allocates): an argument
105 *
106 * Obtain the value associated with the `GIConstantInfo` and store it in the
107 * @value parameter.
108 *
109 * @argument needs to be allocated before passing it in.
110 *
111 * The size of the constant value (in bytes) stored in @argument will be
112 * returned.
113 *
114 * Free the value with [method@GIRepository.ConstantInfo.free_value].
115 *
116 * Returns: size of the constant, in bytes
117 * Since: 2.80
118 */
119 gsize
120 gi_constant_info_get_value (GIConstantInfo *info,
121 GIArgument *value)
122 {
123 GIRealInfo *rinfo = (GIRealInfo *)info;
124 ConstantBlob *blob;
125
126 g_return_val_if_fail (info != NULL, 0);
127 g_return_val_if_fail (GI_IS_CONSTANT_INFO (info), 0);
128
129 blob = (ConstantBlob *)&rinfo->typelib->data[rinfo->offset];
130
131 /* FIXME non-basic types ? */
132 if (blob->type.flags.reserved == 0 && blob->type.flags.reserved2 == 0)
133 {
134 if (blob->type.flags.pointer)
135 {
136 gsize blob_size = blob->size;
137
138 value->v_pointer = g_memdup2 (&rinfo->typelib->data[blob->offset], blob_size);
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 void
187 gi_constant_info_class_init (gpointer g_class,
188 gpointer class_data)
189 {
190 GIBaseInfoClass *info_class = g_class;
191
192 info_class->info_type = GI_INFO_TYPE_CONSTANT;
193 }