Branch data Line data Source code
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 : 0 : gi_constant_info_get_type_info (GIConstantInfo *info) 60 : : { 61 : 0 : GIRealInfo *rinfo = (GIRealInfo *)info; 62 : : 63 : 0 : g_return_val_if_fail (info != NULL, NULL); 64 : 0 : g_return_val_if_fail (GI_IS_CONSTANT_INFO (info), NULL); 65 : : 66 : 0 : 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 : 0 : gi_constant_info_free_value (GIConstantInfo *info, 83 : : GIArgument *value) 84 : : { 85 : 0 : GIRealInfo *rinfo = (GIRealInfo *)info; 86 : : ConstantBlob *blob; 87 : : 88 : 0 : g_return_if_fail (info != NULL); 89 : 0 : g_return_if_fail (GI_IS_CONSTANT_INFO (info)); 90 : : 91 : 0 : blob = (ConstantBlob *)&rinfo->typelib->data[rinfo->offset]; 92 : : 93 : : /* FIXME non-basic types ? */ 94 [ # # # # ]: 0 : if (blob->type.flags.reserved == 0 && blob->type.flags.reserved2 == 0) 95 : : { 96 [ # # ]: 0 : if (blob->type.flags.pointer) 97 : 0 : 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 : : size_t 120 : 0 : gi_constant_info_get_value (GIConstantInfo *info, 121 : : GIArgument *value) 122 : : { 123 : 0 : GIRealInfo *rinfo = (GIRealInfo *)info; 124 : : ConstantBlob *blob; 125 : : 126 : 0 : g_return_val_if_fail (info != NULL, 0); 127 : 0 : g_return_val_if_fail (GI_IS_CONSTANT_INFO (info), 0); 128 : : 129 : 0 : blob = (ConstantBlob *)&rinfo->typelib->data[rinfo->offset]; 130 : : 131 : : /* FIXME non-basic types ? */ 132 [ # # # # ]: 0 : if (blob->type.flags.reserved == 0 && blob->type.flags.reserved2 == 0) 133 : : { 134 [ # # ]: 0 : if (blob->type.flags.pointer) 135 : : { 136 : 0 : size_t blob_size = blob->size; 137 : : 138 : 0 : value->v_pointer = g_memdup2 (&rinfo->typelib->data[blob->offset], blob_size); 139 : : } 140 : : else 141 : : { 142 [ # # # # : 0 : switch (blob->type.flags.tag) # # # # # # # # ] 143 : : { 144 : 0 : case GI_TYPE_TAG_BOOLEAN: 145 : 0 : value->v_boolean = *(gboolean*)&rinfo->typelib->data[blob->offset]; 146 : 0 : break; 147 : 0 : case GI_TYPE_TAG_INT8: 148 : 0 : value->v_int8 = *(int8_t*)&rinfo->typelib->data[blob->offset]; 149 : 0 : break; 150 : 0 : case GI_TYPE_TAG_UINT8: 151 : 0 : value->v_uint8 = *(uint8_t*)&rinfo->typelib->data[blob->offset]; 152 : 0 : break; 153 : 0 : case GI_TYPE_TAG_INT16: 154 : 0 : value->v_int16 = *(int16_t*)&rinfo->typelib->data[blob->offset]; 155 : 0 : break; 156 : 0 : case GI_TYPE_TAG_UINT16: 157 : 0 : value->v_uint16 = *(uint16_t*)&rinfo->typelib->data[blob->offset]; 158 : 0 : break; 159 : 0 : case GI_TYPE_TAG_INT32: 160 : 0 : value->v_int32 = *(int32_t*)&rinfo->typelib->data[blob->offset]; 161 : 0 : break; 162 : 0 : case GI_TYPE_TAG_UINT32: 163 : 0 : value->v_uint32 = *(uint32_t*)&rinfo->typelib->data[blob->offset]; 164 : 0 : break; 165 : 0 : case GI_TYPE_TAG_INT64: 166 : 0 : DO_ALIGNED_COPY (&value->v_int64, &rinfo->typelib->data[blob->offset], int64_t); 167 : 0 : break; 168 : 0 : case GI_TYPE_TAG_UINT64: 169 : 0 : DO_ALIGNED_COPY (&value->v_uint64, &rinfo->typelib->data[blob->offset], uint64_t); 170 : 0 : break; 171 : 0 : case GI_TYPE_TAG_FLOAT: 172 : 0 : DO_ALIGNED_COPY (&value->v_float, &rinfo->typelib->data[blob->offset], float); 173 : 0 : break; 174 : 0 : case GI_TYPE_TAG_DOUBLE: 175 : 0 : DO_ALIGNED_COPY (&value->v_double, &rinfo->typelib->data[blob->offset], double); 176 : 0 : break; 177 : 0 : default: 178 : : g_assert_not_reached (); 179 : : } 180 : : } 181 : : } 182 : : 183 : 0 : return blob->size; 184 : : } 185 : : 186 : : void 187 : 2 : gi_constant_info_class_init (gpointer g_class, 188 : : gpointer class_data) 189 : : { 190 : 2 : GIBaseInfoClass *info_class = g_class; 191 : : 192 : 2 : info_class->info_type = GI_INFO_TYPE_CONSTANT; 193 : 2 : }