(root)/
glib-2.79.0/
girepository/
gipropertyinfo.c
       1  /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
       2   * GObject introspection: Property 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 "gipropertyinfo.h"
      34  
      35  /**
      36   * GIPropertyInfo:
      37   *
      38   * `GIPropertyInfo` represents a property in a [class@GObject.Object].
      39   *
      40   * A property belongs to either a [class@GIRepository.ObjectInfo] or a
      41   * [class@GIRepository.InterfaceInfo].
      42   *
      43   * Since: 2.80
      44   */
      45  
      46  /**
      47   * gi_property_info_get_flags:
      48   * @info: a #GIPropertyInfo
      49   *
      50   * Obtain the flags for this property info.
      51   *
      52   * See [type@GObject.ParamFlags] for more information about possible flag
      53   * values.
      54   *
      55   * Returns: the flags
      56   * Since: 2.80
      57   */
      58  GParamFlags
      59  gi_property_info_get_flags (GIPropertyInfo *info)
      60  {
      61    GParamFlags flags;
      62    GIRealInfo *rinfo = (GIRealInfo *)info;
      63    PropertyBlob *blob;
      64  
      65    g_return_val_if_fail (info != NULL, 0);
      66    g_return_val_if_fail (GI_IS_PROPERTY_INFO (info), 0);
      67  
      68    blob = (PropertyBlob *)&rinfo->typelib->data[rinfo->offset];
      69  
      70    flags = 0;
      71  
      72    if (blob->readable)
      73      flags = flags | G_PARAM_READABLE;
      74  
      75    if (blob->writable)
      76      flags = flags | G_PARAM_WRITABLE;
      77  
      78    if (blob->construct)
      79      flags = flags | G_PARAM_CONSTRUCT;
      80  
      81    if (blob->construct_only)
      82      flags = flags | G_PARAM_CONSTRUCT_ONLY;
      83  
      84    return flags;
      85  }
      86  
      87  /**
      88   * gi_property_info_get_type_info:
      89   * @info: a #GIPropertyInfo
      90   *
      91   * Obtain the type information for the property @info.
      92   *
      93   * Returns: (transfer full): The [class@GIRepository.TypeInfo]. Free it with
      94   *   [method@GIRepository.BaseInfo.unref] when done.
      95   * Since: 2.80
      96   */
      97  GITypeInfo *
      98  gi_property_info_get_type_info (GIPropertyInfo *info)
      99  {
     100    GIRealInfo *rinfo = (GIRealInfo *)info;
     101  
     102    g_return_val_if_fail (info != NULL, NULL);
     103    g_return_val_if_fail (GI_IS_PROPERTY_INFO (info), NULL);
     104  
     105    return gi_type_info_new ((GIBaseInfo*)info,
     106                             rinfo->typelib,
     107                             rinfo->offset + G_STRUCT_OFFSET (PropertyBlob, type));
     108  }
     109  
     110  /**
     111   * gi_property_info_get_ownership_transfer:
     112   * @info: a #GIPropertyInfo
     113   *
     114   * Obtain the ownership transfer for this property.
     115   *
     116   * See [type@GIRepository.Transfer] for more information about transfer values.
     117   *
     118   * Returns: the transfer
     119   * Since: 2.80
     120   */
     121  GITransfer
     122  gi_property_info_get_ownership_transfer (GIPropertyInfo *info)
     123  {
     124    GIRealInfo *rinfo = (GIRealInfo *)info;
     125    PropertyBlob *blob;
     126  
     127    g_return_val_if_fail (info != NULL, -1);
     128    g_return_val_if_fail (GI_IS_PROPERTY_INFO (info), -1);
     129  
     130    blob = (PropertyBlob *)&rinfo->typelib->data[rinfo->offset];
     131  
     132    if (blob->transfer_ownership)
     133      return GI_TRANSFER_EVERYTHING;
     134    else if (blob->transfer_container_ownership)
     135      return GI_TRANSFER_CONTAINER;
     136    else
     137      return GI_TRANSFER_NOTHING;
     138  }
     139  
     140  /**
     141   * gi_property_info_get_setter:
     142   * @info: a #GIPropertyInfo
     143   *
     144   * Obtains the setter function associated with this `GIPropertyInfo`.
     145   *
     146   * The setter is only available for `G_PARAM_WRITABLE` properties that
     147   * are also not `G_PARAM_CONSTRUCT_ONLY`.
     148   *
     149   * Returns: (transfer full) (nullable): The function info, or `NULL` if not set.
     150   *   Free it with [method@GIRepository.BaseInfo.unref] when done.
     151   * Since: 2.80
     152   */
     153  GIFunctionInfo *
     154  gi_property_info_get_setter (GIPropertyInfo *info)
     155  {
     156    GIRealInfo *rinfo = (GIRealInfo *)info;
     157    PropertyBlob *blob;
     158    GIBaseInfo *container;
     159    GIInfoType parent_type;
     160  
     161    g_return_val_if_fail (info != NULL, NULL);
     162    g_return_val_if_fail (GI_IS_PROPERTY_INFO (info), NULL);
     163  
     164    blob = (PropertyBlob *)&rinfo->typelib->data[rinfo->offset];
     165    if (!blob->writable || blob->construct_only)
     166      return NULL;
     167  
     168    if (blob->setter == ACCESSOR_SENTINEL)
     169      return NULL;
     170  
     171    container = rinfo->container;
     172    parent_type = gi_base_info_get_info_type (container);
     173    if (parent_type == GI_INFO_TYPE_OBJECT)
     174      return gi_object_info_get_method ((GIObjectInfo *) container, blob->setter);
     175    else if (parent_type == GI_INFO_TYPE_INTERFACE)
     176      return gi_interface_info_get_method ((GIInterfaceInfo *) container, blob->setter);
     177    else
     178      return NULL;
     179  }
     180  
     181  /**
     182   * gi_property_info_get_getter:
     183   * @info: a #GIPropertyInfo
     184   *
     185   * Obtains the getter function associated with this `GIPropertyInfo`.
     186   *
     187   * The setter is only available for `G_PARAM_READABLE` properties.
     188   *
     189   * Returns: (transfer full) (nullable): The function info, or `NULL` if not set.
     190   *   Free it with [method@GIRepository.BaseInfo.unref] when done.
     191   * Since: 2.80
     192   */
     193  GIFunctionInfo *
     194  gi_property_info_get_getter (GIPropertyInfo *info)
     195  {
     196    GIRealInfo *rinfo = (GIRealInfo *)info;
     197    PropertyBlob *blob;
     198    GIBaseInfo *container;
     199    GIInfoType parent_type;
     200  
     201    g_return_val_if_fail (info != NULL, NULL);
     202    g_return_val_if_fail (GI_IS_PROPERTY_INFO (info), NULL);
     203  
     204    blob = (PropertyBlob *)&rinfo->typelib->data[rinfo->offset];
     205    if (!blob->readable)
     206      return NULL;
     207  
     208    if (blob->getter == ACCESSOR_SENTINEL)
     209      return NULL;
     210  
     211    container = rinfo->container;
     212    parent_type = gi_base_info_get_info_type (container);
     213    if (parent_type == GI_INFO_TYPE_OBJECT)
     214      return gi_object_info_get_method ((GIObjectInfo *) container, blob->getter);
     215    else if (parent_type == GI_INFO_TYPE_INTERFACE)
     216      return gi_interface_info_get_method ((GIInterfaceInfo *) container, blob->getter);
     217    else
     218      return NULL;
     219  }
     220  
     221  void
     222  gi_property_info_class_init (gpointer g_class,
     223                               gpointer class_data)
     224  {
     225    GIBaseInfoClass *info_class = g_class;
     226  
     227    info_class->info_type = GI_INFO_TYPE_PROPERTY;
     228  }