LCOV - code coverage report
Current view: top level - glib/girepository - giregisteredtypeinfo.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 43 46 93.5 %
Date: 2024-05-07 05:15:23 Functions: 5 5 100.0 %
Branches: 13 16 81.2 %

           Branch data     Line data    Source code
       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                 :            :  * 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 <string.h>
      28                 :            : 
      29                 :            : #include <glib.h>
      30                 :            : 
      31                 :            : #include <girepository/girepository.h>
      32                 :            : #include "gibaseinfo-private.h"
      33                 :            : #include "girepository-private.h"
      34                 :            : #include "gitypelib-internal.h"
      35                 :            : #include "giregisteredtypeinfo.h"
      36                 :            : 
      37                 :            : /**
      38                 :            :  * GIRegisteredTypeInfo:
      39                 :            :  *
      40                 :            :  * `GIRegisteredTypeInfo` represents an entity with a [type@GObject.Type]
      41                 :            :  * associated.
      42                 :            :  *
      43                 :            :  * Could be either a [class@GIRepository.EnumInfo],
      44                 :            :  * [class@GIRepository.InterfaceInfo], [class@GIRepository.ObjectInfo],
      45                 :            :  * [class@GIRepository.StructInfo] or a [class@GIRepository.UnionInfo].
      46                 :            :  *
      47                 :            :  * A registered type info struct has a name and a type function.
      48                 :            :  *
      49                 :            :  * To get the name call [method@GIRepository.RegisteredTypeInfo.get_type_name].
      50                 :            :  * Most users want to call [method@GIRepository.RegisteredTypeInfo.get_g_type]
      51                 :            :  * and don’t worry about the rest of the details.
      52                 :            :  *
      53                 :            :  * If the registered type is a subtype of `G_TYPE_BOXED`,
      54                 :            :  * [method@GIRepository.RegisteredTypeInfo.is_boxed] will return true, and
      55                 :            :  * [method@GIRepository.RegisteredTypeInfo.get_type_name] is guaranteed to
      56                 :            :  * return a non-`NULL` value. This is relevant for the
      57                 :            :  * [class@GIRepository.StructInfo] and [class@GIRepository.UnionInfo]
      58                 :            :  * subclasses.
      59                 :            :  *
      60                 :            :  * Since: 2.80
      61                 :            :  */
      62                 :            : 
      63                 :            : /**
      64                 :            :  * gi_registered_type_info_get_type_name:
      65                 :            :  * @info: a #GIRegisteredTypeInfo
      66                 :            :  *
      67                 :            :  * Obtain the type name of the struct within the GObject type system.
      68                 :            :  *
      69                 :            :  * This type can be passed to [func@GObject.type_name] to get a
      70                 :            :  * [type@GObject.Type].
      71                 :            :  *
      72                 :            :  * Returns: (nullable): the type name, or `NULL` if unknown
      73                 :            :  * Since: 2.80
      74                 :            :  */
      75                 :            : const char *
      76                 :          8 : gi_registered_type_info_get_type_name (GIRegisteredTypeInfo *info)
      77                 :            : {
      78                 :          8 :   GIRealInfo *rinfo = (GIRealInfo *)info;
      79                 :            :   RegisteredTypeBlob *blob;
      80                 :            : 
      81                 :          8 :   g_return_val_if_fail (info != NULL, NULL);
      82                 :          8 :   g_return_val_if_fail (GI_IS_REGISTERED_TYPE_INFO (info), NULL);
      83                 :            : 
      84                 :          8 :   blob = (RegisteredTypeBlob *)&rinfo->typelib->data[rinfo->offset];
      85                 :            : 
      86         [ +  + ]:          8 :   if (blob->gtype_name)
      87                 :          5 :     return gi_typelib_get_string (rinfo->typelib, blob->gtype_name);
      88                 :            : 
      89                 :          3 :   return NULL;
      90                 :            : }
      91                 :            : 
      92                 :            : /**
      93                 :            :  * gi_registered_type_info_get_type_init_function_name:
      94                 :            :  * @info: a #GIRegisteredTypeInfo
      95                 :            :  *
      96                 :            :  * Obtain the type init function for @info.
      97                 :            :  *
      98                 :            :  * The type init function is the function which will register the
      99                 :            :  * [type@GObject.Type] within the GObject type system. Usually this is not
     100                 :            :  * called by language bindings or applications — use
     101                 :            :  * [method@GIRepository.RegisteredTypeInfo.get_g_type] directly instead.
     102                 :            :  *
     103                 :            :  * Returns: (nullable): the symbol name of the type init function, suitable for
     104                 :            :  *   passing into [method@GModule.Module.symbol], or `NULL` if unknown
     105                 :            :  * Since: 2.80
     106                 :            :  */
     107                 :            : const char *
     108                 :          7 : gi_registered_type_info_get_type_init_function_name (GIRegisteredTypeInfo *info)
     109                 :            : {
     110                 :          7 :   GIRealInfo *rinfo = (GIRealInfo *)info;
     111                 :            :   RegisteredTypeBlob *blob;
     112                 :            : 
     113                 :          7 :   g_return_val_if_fail (info != NULL, NULL);
     114                 :          7 :   g_return_val_if_fail (GI_IS_REGISTERED_TYPE_INFO (info), NULL);
     115                 :            : 
     116                 :          7 :   blob = (RegisteredTypeBlob *)&rinfo->typelib->data[rinfo->offset];
     117                 :            : 
     118         [ +  + ]:          7 :   if (blob->gtype_init)
     119                 :          4 :     return gi_typelib_get_string (rinfo->typelib, blob->gtype_init);
     120                 :            : 
     121                 :          3 :   return NULL;
     122                 :            : }
     123                 :            : 
     124                 :            : /**
     125                 :            :  * gi_registered_type_info_get_g_type:
     126                 :            :  * @info: a #GIRegisteredTypeInfo
     127                 :            :  *
     128                 :            :  * Obtain the [type@GObject.Type] for this registered type.
     129                 :            :  *
     130                 :            :  * If there is no type information associated with @info, or the shared library
     131                 :            :  * which provides the `type_init` function for @info cannot be called, then
     132                 :            :  * `G_TYPE_NONE` is returned.
     133                 :            :  *
     134                 :            :  * Returns: the [type@GObject.Type], or `G_TYPE_NONE` if unknown
     135                 :            :  * Since: 2.80
     136                 :            :  */
     137                 :            : GType
     138                 :          1 : gi_registered_type_info_get_g_type (GIRegisteredTypeInfo *info)
     139                 :            : {
     140                 :            :   const char *type_init;
     141                 :            :   GType (* get_type_func) (void);
     142                 :          1 :   GIRealInfo *rinfo = (GIRealInfo*)info;
     143                 :            : 
     144                 :          1 :   g_return_val_if_fail (info != NULL, G_TYPE_INVALID);
     145                 :          1 :   g_return_val_if_fail (GI_IS_REGISTERED_TYPE_INFO (info), G_TYPE_INVALID);
     146                 :            : 
     147                 :          1 :   type_init = gi_registered_type_info_get_type_init_function_name (info);
     148                 :            : 
     149         [ -  + ]:          1 :   if (type_init == NULL)
     150                 :          0 :     return G_TYPE_NONE;
     151         [ -  + ]:          1 :   else if (!strcmp (type_init, "intern"))
     152                 :            :     /* The special string "intern" is used for some types exposed by libgobject
     153                 :            :        (that therefore should be always available) */
     154                 :          0 :     return g_type_from_name (gi_registered_type_info_get_type_name (info));
     155                 :            : 
     156                 :          1 :   get_type_func = NULL;
     157         [ -  + ]:          1 :   if (!gi_typelib_symbol (rinfo->typelib,
     158                 :            :                           type_init,
     159                 :            :                           (void**) &get_type_func))
     160                 :          0 :     return G_TYPE_NONE;
     161                 :            : 
     162                 :          1 :   return (* get_type_func) ();
     163                 :            : }
     164                 :            : 
     165                 :            : /**
     166                 :            :  * gi_registered_type_info_is_boxed:
     167                 :            :  * @info: a #GIRegisteredTypeInfo
     168                 :            :  *
     169                 :            :  * Get whether the registered type is a boxed type.
     170                 :            :  *
     171                 :            :  * A boxed type is a subtype of the fundamental `G_TYPE_BOXED` type.
     172                 :            :  * It’s a type which has registered a [type@GObject.Type], and which has
     173                 :            :  * associated copy and free functions.
     174                 :            :  *
     175                 :            :  * Most boxed types are `struct`s; some are `union`s; and it’s possible for a
     176                 :            :  * boxed type to be neither, but that is currently unsupported by
     177                 :            :  * libgirepository. It’s also possible for a `struct` or `union` to have
     178                 :            :  * associated copy and/or free functions *without* being a boxed type, by virtue
     179                 :            :  * of not having registered a [type@GObject.Type].
     180                 :            :  *
     181                 :            :  * This function will return false for [type@GObject.Type]s which are not boxed,
     182                 :            :  * such as classes or interfaces. It will also return false for the `struct`s
     183                 :            :  * associated with a class or interface, which return true from
     184                 :            :  * [method@GIRepository.StructInfo.is_gtype_struct].
     185                 :            :  *
     186                 :            :  * Returns: true if @info is a boxed type
     187                 :            :  * Since: 2.80
     188                 :            :  */
     189                 :            : gboolean
     190                 :          7 : gi_registered_type_info_is_boxed (GIRegisteredTypeInfo *info)
     191                 :            : {
     192                 :          7 :   GIBaseInfo *base_info = GI_BASE_INFO (info);
     193                 :            :   const RegisteredTypeBlob *blob;
     194                 :            : 
     195                 :          7 :   g_return_val_if_fail (GI_IS_REGISTERED_TYPE_INFO (info), G_TYPE_INVALID);
     196                 :            : 
     197                 :          7 :   blob = (const RegisteredTypeBlob *) &base_info->typelib->data[base_info->offset];
     198                 :            : 
     199         [ +  + ]:          7 :   if (blob->blob_type == BLOB_TYPE_BOXED)
     200                 :            :     {
     201                 :          2 :       return TRUE;
     202                 :            :     }
     203         [ +  + ]:          5 :   else if (blob->blob_type == BLOB_TYPE_STRUCT)
     204                 :            :     {
     205                 :          3 :       const StructBlob *struct_blob = (const StructBlob *) &base_info->typelib->data[base_info->offset];
     206                 :            : 
     207                 :          3 :       return !struct_blob->unregistered;
     208                 :            :     }
     209         [ +  + ]:          2 :   else if (blob->blob_type == BLOB_TYPE_UNION)
     210                 :            :     {
     211                 :          1 :       const UnionBlob *union_blob = (const UnionBlob *) &base_info->typelib->data[base_info->offset];
     212                 :            : 
     213                 :          1 :       return !union_blob->unregistered;
     214                 :            :     }
     215                 :            : 
     216                 :            :   /* We don’t currently support boxed ‘other’ types (boxed types which aren’t
     217                 :            :    * a struct or union. */
     218                 :            : 
     219                 :          1 :   return FALSE;
     220                 :            : }
     221                 :            : 
     222                 :            : void
     223                 :          7 : gi_registered_type_info_class_init (gpointer g_class,
     224                 :            :                                     gpointer class_data)
     225                 :            : {
     226                 :          7 :   GIBaseInfoClass *info_class = g_class;
     227                 :            : 
     228                 :          7 :   info_class->info_type = GI_INFO_TYPE_REGISTERED_TYPE;
     229                 :          7 : }

Generated by: LCOV version 1.14