LCOV - code coverage report
Current view: top level - glib/girepository - gienuminfo.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 36 50 72.0 %
Date: 2024-05-07 05:15:23 Functions: 5 7 71.4 %
Branches: 0 2 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
       2                 :            :  * GObject introspection: Enum 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                 :            : 
      29                 :            : #include <girepository/girepository.h>
      30                 :            : #include "gibaseinfo-private.h"
      31                 :            : #include "girepository-private.h"
      32                 :            : #include "gitypelib-internal.h"
      33                 :            : #include "gienuminfo.h"
      34                 :            : 
      35                 :            : /**
      36                 :            :  * GIEnumInfo:
      37                 :            :  *
      38                 :            :  * A `GIEnumInfo` represents an enumeration.
      39                 :            :  *
      40                 :            :  * The `GIEnumInfo` contains a set of values (each a
      41                 :            :  * [class@GIRepository.ValueInfo]) and a type.
      42                 :            :  *
      43                 :            :  * The [class@GIRepository.ValueInfo] for a value is fetched by calling
      44                 :            :  * [method@GIRepository.EnumInfo.get_value] on a `GIEnumInfo`.
      45                 :            :  *
      46                 :            :  * Since: 2.80
      47                 :            :  */
      48                 :            : 
      49                 :            : /**
      50                 :            :  * gi_enum_info_get_n_values:
      51                 :            :  * @info: a #GIEnumInfo
      52                 :            :  *
      53                 :            :  * Obtain the number of values this enumeration contains.
      54                 :            :  *
      55                 :            :  * Returns: the number of enumeration values
      56                 :            :  * Since: 2.80
      57                 :            :  */
      58                 :            : unsigned int
      59                 :         83 : gi_enum_info_get_n_values (GIEnumInfo *info)
      60                 :            : {
      61                 :         83 :   GIRealInfo *rinfo = (GIRealInfo *)info;
      62                 :            :   EnumBlob *blob;
      63                 :            : 
      64                 :         83 :   g_return_val_if_fail (info != NULL, 0);
      65                 :         83 :   g_return_val_if_fail (GI_IS_ENUM_INFO (info), 0);
      66                 :            : 
      67                 :         83 :   blob = (EnumBlob *)&rinfo->typelib->data[rinfo->offset];
      68                 :            : 
      69                 :         83 :   return blob->n_values;
      70                 :            : }
      71                 :            : 
      72                 :            : /**
      73                 :            :  * gi_enum_info_get_error_domain:
      74                 :            :  * @info: a #GIEnumInfo
      75                 :            :  *
      76                 :            :  * Obtain the string form of the quark for the error domain associated with
      77                 :            :  * this enum, if any.
      78                 :            :  *
      79                 :            :  * Returns: (transfer none) (nullable): the string form of the error domain
      80                 :            :  *   associated with this enum, or `NULL`.
      81                 :            :  * Since: 2.80
      82                 :            :  */
      83                 :            : const char *
      84                 :          0 : gi_enum_info_get_error_domain (GIEnumInfo *info)
      85                 :            : {
      86                 :          0 :   GIRealInfo *rinfo = (GIRealInfo *)info;
      87                 :            :   EnumBlob *blob;
      88                 :            : 
      89                 :          0 :   g_return_val_if_fail (info != NULL, 0);
      90                 :          0 :   g_return_val_if_fail (GI_IS_ENUM_INFO (info), 0);
      91                 :            : 
      92                 :          0 :   blob = (EnumBlob *)&rinfo->typelib->data[rinfo->offset];
      93                 :            : 
      94         [ #  # ]:          0 :   if (blob->error_domain)
      95                 :          0 :     return gi_typelib_get_string (rinfo->typelib, blob->error_domain);
      96                 :            :   else
      97                 :          0 :     return NULL;
      98                 :            : }
      99                 :            : 
     100                 :            : /**
     101                 :            :  * gi_enum_info_get_value:
     102                 :            :  * @info: a #GIEnumInfo
     103                 :            :  * @n: index of value to fetch
     104                 :            :  *
     105                 :            :  * Obtain a value for this enumeration.
     106                 :            :  *
     107                 :            :  * Returns: (transfer full): the enumeration value, free the struct with
     108                 :            :  *   [method@GIRepository.BaseInfo.unref] when done.
     109                 :            :  * Since: 2.80
     110                 :            :  */
     111                 :            : GIValueInfo *
     112                 :        898 : gi_enum_info_get_value (GIEnumInfo *info,
     113                 :            :                         unsigned int n)
     114                 :            : {
     115                 :        898 :   GIRealInfo *rinfo = (GIRealInfo *)info;
     116                 :            :   Header *header;
     117                 :            :   size_t offset;
     118                 :            : 
     119                 :        898 :   g_return_val_if_fail (info != NULL, NULL);
     120                 :        898 :   g_return_val_if_fail (GI_IS_ENUM_INFO (info), NULL);
     121                 :        898 :   g_return_val_if_fail (n <= G_MAXUINT16, NULL);
     122                 :            : 
     123                 :        898 :   header = (Header *)rinfo->typelib->data;
     124                 :        898 :   offset = rinfo->offset + header->enum_blob_size
     125                 :        898 :     + n * header->value_blob_size;
     126                 :            : 
     127                 :        898 :   return (GIValueInfo *) gi_base_info_new (GI_INFO_TYPE_VALUE, (GIBaseInfo*)info, rinfo->typelib, offset);
     128                 :            : }
     129                 :            : 
     130                 :            : /**
     131                 :            :  * gi_enum_info_get_n_methods:
     132                 :            :  * @info: a #GIEnumInfo
     133                 :            :  *
     134                 :            :  * Obtain the number of methods that this enum type has.
     135                 :            :  *
     136                 :            :  * Returns: number of methods
     137                 :            :  * Since: 2.80
     138                 :            :  */
     139                 :            : unsigned int
     140                 :          1 : gi_enum_info_get_n_methods (GIEnumInfo *info)
     141                 :            : {
     142                 :          1 :   GIRealInfo *rinfo = (GIRealInfo *)info;
     143                 :            :   EnumBlob *blob;
     144                 :            : 
     145                 :          1 :   g_return_val_if_fail (info != NULL, 0);
     146                 :          1 :   g_return_val_if_fail (GI_IS_ENUM_INFO (info), 0);
     147                 :            : 
     148                 :          1 :   blob = (EnumBlob *)&rinfo->typelib->data[rinfo->offset];
     149                 :            : 
     150                 :          1 :   return blob->n_methods;
     151                 :            : }
     152                 :            : 
     153                 :            : /**
     154                 :            :  * gi_enum_info_get_method:
     155                 :            :  * @info: a #GIEnumInfo
     156                 :            :  * @n: index of method to get
     157                 :            :  *
     158                 :            :  * Obtain an enum type method at index @n.
     159                 :            :  *
     160                 :            :  * Returns: (transfer full): the [class@GIRepository.FunctionInfo]. Free the
     161                 :            :  *   struct by calling [method@GIRepository.BaseInfo.unref] when done.
     162                 :            :  * Since: 2.80
     163                 :            :  */
     164                 :            : GIFunctionInfo *
     165                 :          2 : gi_enum_info_get_method (GIEnumInfo *info,
     166                 :            :                          unsigned int n)
     167                 :            : {
     168                 :            :   size_t offset;
     169                 :          2 :   GIRealInfo *rinfo = (GIRealInfo *)info;
     170                 :            :   Header *header;
     171                 :            :   EnumBlob *blob;
     172                 :            : 
     173                 :          2 :   g_return_val_if_fail (info != NULL, NULL);
     174                 :          2 :   g_return_val_if_fail (GI_IS_ENUM_INFO (info), NULL);
     175                 :          2 :   g_return_val_if_fail (n <= G_MAXUINT16, NULL);
     176                 :            : 
     177                 :          2 :   header = (Header *)rinfo->typelib->data;
     178                 :          2 :   blob = (EnumBlob *)&rinfo->typelib->data[rinfo->offset];
     179                 :            : 
     180                 :          2 :   offset = rinfo->offset + header->enum_blob_size
     181                 :          2 :     + blob->n_values * header->value_blob_size
     182                 :          2 :     + n * header->function_blob_size;
     183                 :            : 
     184                 :          2 :   return (GIFunctionInfo *) gi_base_info_new (GI_INFO_TYPE_FUNCTION, (GIBaseInfo*)info,
     185                 :            :                                               rinfo->typelib, offset);
     186                 :            : }
     187                 :            : 
     188                 :            : /**
     189                 :            :  * gi_enum_info_get_storage_type:
     190                 :            :  * @info: a #GIEnumInfo
     191                 :            :  *
     192                 :            :  * Obtain the tag of the type used for the enum in the C ABI. This will
     193                 :            :  * will be a signed or unsigned integral type.
     194                 :            :  *
     195                 :            :  * Note that in the current implementation the width of the type is
     196                 :            :  * computed correctly, but the signed or unsigned nature of the type
     197                 :            :  * may not match the sign of the type used by the C compiler.
     198                 :            :  *
     199                 :            :  * Returns: the storage type for the enumeration
     200                 :            :  * Since: 2.80
     201                 :            :  */
     202                 :            : GITypeTag
     203                 :          0 : gi_enum_info_get_storage_type (GIEnumInfo *info)
     204                 :            : {
     205                 :          0 :   GIRealInfo *rinfo = (GIRealInfo *)info;
     206                 :            :   EnumBlob *blob;
     207                 :            : 
     208                 :          0 :   g_return_val_if_fail (info != NULL, GI_TYPE_TAG_BOOLEAN);
     209                 :          0 :   g_return_val_if_fail (GI_IS_ENUM_INFO (info), GI_TYPE_TAG_BOOLEAN);
     210                 :            : 
     211                 :          0 :   blob = (EnumBlob *)&rinfo->typelib->data[rinfo->offset];
     212                 :            : 
     213                 :          0 :   return blob->storage_type;
     214                 :            : }
     215                 :            : 
     216                 :            : void
     217                 :          2 : gi_enum_info_class_init (gpointer g_class,
     218                 :            :                          gpointer class_data)
     219                 :            : {
     220                 :          2 :   GIBaseInfoClass *info_class = g_class;
     221                 :            : 
     222                 :          2 :   info_class->info_type = GI_INFO_TYPE_ENUM;
     223                 :          2 : }

Generated by: LCOV version 1.14