开发者

GTK+ typecast style for function arguments

开发者 https://www.devze.com 2023-01-18 11:13 出处:网络
In GTK+ calls, arguments should (but don\'t have to) be casted from GtkWidget to the most specific class needed by the function before passing the argument. For example, sometimes I see

In GTK+ calls, arguments should (but don't have to) be casted from GtkWidget to the most specific class needed by the function before passing the argument. For example, sometimes I see

some_call(GTK_WINDOW(window));

while other times, I see

some_call((GtkWindow *) window);

What's the diffe开发者_高级运维rence?


GTK_WINDOW is a macro that does the cast.

As seen here

#define GTK_WINDOW(obj)  (GTK_CHECK_CAST ((obj), GTK_TYPE_WINDOW, GtkWindow))

Again

#define GTK_CHECK_CAST G_TYPE_CHECK_INSTANCE_CAST

and

#define G_TYPE_CHECK_INSTANCE_CAST(instance, g_type, c_type) (_G_TYPE_CIC ((instance), (g_type), c_type))

which is...

#define _G_TYPE_CIC(ip,gt,ct)    \
    ((ct*) g_type_check_instance_cast ((GTypeInstance*) ip, gt))

The code for g_type_check_instance_cast can be found here

GTypeInstance*
g_type_check_instance_cast (GTypeInstance *type_instance,
                            GType          iface_type)
{
  if (type_instance)
    {
      if (type_instance->g_class)
        {
          TypeNode *node, *iface;
          gboolean is_instantiatable, check;

          node = lookup_type_node_I (type_instance->g_class->g_type);
          is_instantiatable = node && node->is_instantiatable;
          iface = lookup_type_node_I (iface_type);
          check = is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE);
          if (check)
            return type_instance;

          if (is_instantiatable)
            g_warning ("invalid cast from `%s' to `%s'",
                       type_descriptive_name_I (type_instance->g_class->g_type),
                       type_descriptive_name_I (iface_type));
          else
            g_warning ("invalid uninstantiatable type `%s' in cast to `%s'",
                       type_descriptive_name_I (type_instance->g_class->g_type),
                       type_descriptive_name_I (iface_type));
        }
      else
        g_warning ("invalid unclassed pointer in cast to `%s'",
                   type_descriptive_name_I (iface_type));
    }

  return type_instance;
}


The first one is a macro that can both test whether the cast is possible and performs the cast. It's GTKs version/attempt of a type-safe cast. You should use that one.


GTK_WINDOW() is simply a macro that would look something like this:

#define GTK_WINDOW(a) ((GtkWindow*)a)

This is identical to doing an explicit cast by yourself, and the two statements you should in your post are identical.

0

精彩评论

暂无评论...
验证码 换一张
取 消