在Gobject中,如何重写父类属于接口的方法?

发布于 2024-08-08 18:58:48 字数 140 浏览 5 评论 0原文

GObject 类 A 实现接口 IA,B 是 A 的派生类。B 如何重写属于接口 IA 的 A 方法?

或者,这在 GObject 中可能吗?

我知道如何重写父类方法,但是当继承遇到接口时,事情似乎更加复杂。

多谢!

GObject class A implements interface IA, B is a derived class of A. How can B override A's method that is part of the interface IA?

Or, is this possible in GObject?

I know how to override parent class methods, but when inheritance meets interface, things seems to be more complicated.

Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤芳又自赏 2024-08-15 18:58:48

是的,这是可能的:只需使用 G_IMPLMENT_INTERFACE() 重新实现接口,或者在 get_type() 函数中手动初始化它。

真正的痛苦是您是否需要链接旧方法。在这种情况下,你应该玩
g_type_interface_peek_parent 获取之前的接口类。

这是一个测试用例:

/* gcc -otest `pkg-config --cflags --libs gobject-2.0` test.c */
#include <glib-object.h>


/* Interface */

#define TYPE_IFACE   (iface_get_type())

typedef void Iface;
typedef struct {
    GTypeInterface parent_class;
    void (*action) (Iface *instance);
} IfaceClass;

GType
iface_get_type(void)
{
    static GType type = 0;

    if (G_UNLIKELY(type == 0)) {
        const GTypeInfo info = {
            sizeof(IfaceClass), 0,
        };

        type = g_type_register_static(G_TYPE_INTERFACE, "Iface", &info, 0);
    }

    return type;
}

void
iface_action(Iface *instance)
{
    G_TYPE_INSTANCE_GET_INTERFACE(instance, TYPE_IFACE, IfaceClass)->
        action(instance);
}


/* Base object */

#define TYPE_BASE    (base_get_type())

typedef GObject        Base;
typedef GObjectClass   BaseClass;

static void
base_action(Iface *instance)
{
    g_print("Running base action on a `%s' instance...\n",
            g_type_name(G_TYPE_FROM_INSTANCE(instance)));
}

static void
base_iface_init(IfaceClass *iface)
{
    iface->action = base_action;
}

G_DEFINE_TYPE_WITH_CODE(Base, base, G_TYPE_OBJECT,
                        G_IMPLEMENT_INTERFACE(TYPE_IFACE, base_iface_init));

static void
base_class_init(BaseClass *klass)
{
}

static void
base_init(Base *instance)
{
}


/* Derived object */

#define TYPE_DERIVED (derived_get_type())

typedef Base      Derived;
typedef BaseClass DerivedClass;

static void
derived_action(Iface *instance)
{
    IfaceClass *iface_class, *old_iface_class;

    iface_class = G_TYPE_INSTANCE_GET_INTERFACE(instance, TYPE_IFACE, IfaceClass);
    old_iface_class = g_type_interface_peek_parent(iface_class);

    g_print("Running derived action on a `%s' instance...\n",
            g_type_name(G_TYPE_FROM_INSTANCE(instance)));

    /* Chain up the old method */
    old_iface_class->action(instance);
}

static void
derived_iface_init(IfaceClass *iface)
{
    iface->action = derived_action;
}

G_DEFINE_TYPE_WITH_CODE(Derived, derived, TYPE_BASE,
                        G_IMPLEMENT_INTERFACE(TYPE_IFACE, derived_iface_init));

static void
derived_class_init(DerivedClass *klass)
{
}

static void
derived_init(Derived *instance)
{
}


int
main()
{
    GObject *object;

    g_type_init();

    object = g_object_new(TYPE_BASE, NULL);
    iface_action((Iface *) object);
    g_object_unref(object);

    object = g_object_new(TYPE_DERIVED, NULL);
    iface_action((Iface *) object);
    g_object_unref(object);

    return 0;
}

Yes, it is possible: just reimplement the interface as it was the first time, either using G_IMPLEMENT_INTERFACE() or manual initializing it in your get_type() function.

The real pain is if you need to chain up the old method. In this case, you should play with
g_type_interface_peek_parent to get the previous interface class.

Here is a test case:

/* gcc -otest `pkg-config --cflags --libs gobject-2.0` test.c */
#include <glib-object.h>


/* Interface */

#define TYPE_IFACE   (iface_get_type())

typedef void Iface;
typedef struct {
    GTypeInterface parent_class;
    void (*action) (Iface *instance);
} IfaceClass;

GType
iface_get_type(void)
{
    static GType type = 0;

    if (G_UNLIKELY(type == 0)) {
        const GTypeInfo info = {
            sizeof(IfaceClass), 0,
        };

        type = g_type_register_static(G_TYPE_INTERFACE, "Iface", &info, 0);
    }

    return type;
}

void
iface_action(Iface *instance)
{
    G_TYPE_INSTANCE_GET_INTERFACE(instance, TYPE_IFACE, IfaceClass)->
        action(instance);
}


/* Base object */

#define TYPE_BASE    (base_get_type())

typedef GObject        Base;
typedef GObjectClass   BaseClass;

static void
base_action(Iface *instance)
{
    g_print("Running base action on a `%s' instance...\n",
            g_type_name(G_TYPE_FROM_INSTANCE(instance)));
}

static void
base_iface_init(IfaceClass *iface)
{
    iface->action = base_action;
}

G_DEFINE_TYPE_WITH_CODE(Base, base, G_TYPE_OBJECT,
                        G_IMPLEMENT_INTERFACE(TYPE_IFACE, base_iface_init));

static void
base_class_init(BaseClass *klass)
{
}

static void
base_init(Base *instance)
{
}


/* Derived object */

#define TYPE_DERIVED (derived_get_type())

typedef Base      Derived;
typedef BaseClass DerivedClass;

static void
derived_action(Iface *instance)
{
    IfaceClass *iface_class, *old_iface_class;

    iface_class = G_TYPE_INSTANCE_GET_INTERFACE(instance, TYPE_IFACE, IfaceClass);
    old_iface_class = g_type_interface_peek_parent(iface_class);

    g_print("Running derived action on a `%s' instance...\n",
            g_type_name(G_TYPE_FROM_INSTANCE(instance)));

    /* Chain up the old method */
    old_iface_class->action(instance);
}

static void
derived_iface_init(IfaceClass *iface)
{
    iface->action = derived_action;
}

G_DEFINE_TYPE_WITH_CODE(Derived, derived, TYPE_BASE,
                        G_IMPLEMENT_INTERFACE(TYPE_IFACE, derived_iface_init));

static void
derived_class_init(DerivedClass *klass)
{
}

static void
derived_init(Derived *instance)
{
}


int
main()
{
    GObject *object;

    g_type_init();

    object = g_object_new(TYPE_BASE, NULL);
    iface_action((Iface *) object);
    g_object_unref(object);

    object = g_object_new(TYPE_DERIVED, NULL);
    iface_action((Iface *) object);
    g_object_unref(object);

    return 0;
}
乜一 2024-08-15 18:58:48

我认为更好的解决方案是使 A 的方法虚拟,而不是让 B 重新实现 A 所附加的接口(这可能需要更多的工作而不仅仅是重新定义一个函数),您可以这样做(示例应该是完整的)除了 fooable 接口定义之外):

#include <glib-object.h>
#include "fooable.h"

typedef struct {GObject parent;} A;
typedef struct {
    GObjectClass parent;
    gint (*foo) (Fooable *self, gdouble quux);
} AClass;

#define TYPE_A           (a_get_type())
#define A_CLASS(cls)     (G_TYPE_CHECK_CLASS_CAST((cls), TYPE_A, AClass))
#define A_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), TYPE_A, AClass))

gint a_foo_real (Fooable *self, gdouble quux) {
    g_print("a_foo_real(%g)\n", quux);
    return 5;
}

gint a_foo (Fooable *self, gdouble quux) {
    return A_GET_CLASS(self)->foo(self, quux);
}

void implement_fooable (FooableIface *iface) {iface->foo = a_foo;}
void a_class_init      (AClass *cls)         {cls->foo = a_foo_real;}
void a_init            (A *self)             {}

G_DEFINE_TYPE_WITH_CODE(A, a, G_TYPE_OBJECT,
    G_IMPLEMENT_INTERFACE(TYPE_FOOABLE, implement_fooable));

/* derive class B from A  */
typedef struct {A parent;} B;
typedef struct {AClass parent;} BClass;

#define TYPE_B (b_get_type())

gint b_foo_real (Fooable *self, gdouble quux) {
    g_print("b_foo_real(%g)\n", quux);
    return 55;
}

void b_class_init (BClass *cls) {A_CLASS(cls)->foo = b_foo_real;}
void b_init       (B *self)     {}

G_DEFINE_TYPE(B, b, TYPE_A);

int main () {
    g_type_init();
    A *a = g_object_new(TYPE_A, NULL);
    B *b = g_object_new(TYPE_B, NULL);
    fooable_foo(FOOABLE(a), 87.0); // a_foo_real(87.0) and returns 5
    fooable_foo(FOOABLE(b), 32.0); // b_foo_real(32.0) and returns 55
    return 0;
}

这是我能做的最简短的例子。当您调用 fooable_foo() 时,该函数将在其 vtable 中查找您实现接口时定义的函数,即 a_foo(),它会查看 A 类的 vtable 来确定哪个实际调用的函数。 B 类定义用自己的定义覆盖 A 类的 a_foo_real() 。如果您需要 B 类的 b_foo_real 进行链接,那很简单(使用在 G_DEFINE_TYPE 宏中为您定义的 A_CLASS(b_parent_class)->foo()

I think a better solution would be to make A's method virtual, rather than have B re-implement the interface A is attached to (this may require more work than just redefining one function), which you can do like this (example should be complete other than the fooable interface definition):

#include <glib-object.h>
#include "fooable.h"

typedef struct {GObject parent;} A;
typedef struct {
    GObjectClass parent;
    gint (*foo) (Fooable *self, gdouble quux);
} AClass;

#define TYPE_A           (a_get_type())
#define A_CLASS(cls)     (G_TYPE_CHECK_CLASS_CAST((cls), TYPE_A, AClass))
#define A_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), TYPE_A, AClass))

gint a_foo_real (Fooable *self, gdouble quux) {
    g_print("a_foo_real(%g)\n", quux);
    return 5;
}

gint a_foo (Fooable *self, gdouble quux) {
    return A_GET_CLASS(self)->foo(self, quux);
}

void implement_fooable (FooableIface *iface) {iface->foo = a_foo;}
void a_class_init      (AClass *cls)         {cls->foo = a_foo_real;}
void a_init            (A *self)             {}

G_DEFINE_TYPE_WITH_CODE(A, a, G_TYPE_OBJECT,
    G_IMPLEMENT_INTERFACE(TYPE_FOOABLE, implement_fooable));

/* derive class B from A  */
typedef struct {A parent;} B;
typedef struct {AClass parent;} BClass;

#define TYPE_B (b_get_type())

gint b_foo_real (Fooable *self, gdouble quux) {
    g_print("b_foo_real(%g)\n", quux);
    return 55;
}

void b_class_init (BClass *cls) {A_CLASS(cls)->foo = b_foo_real;}
void b_init       (B *self)     {}

G_DEFINE_TYPE(B, b, TYPE_A);

int main () {
    g_type_init();
    A *a = g_object_new(TYPE_A, NULL);
    B *b = g_object_new(TYPE_B, NULL);
    fooable_foo(FOOABLE(a), 87.0); // a_foo_real(87.0) and returns 5
    fooable_foo(FOOABLE(b), 32.0); // b_foo_real(32.0) and returns 55
    return 0;
}

That's as brief of an example as I can make it. When you call fooable_foo() the function will look at its vtable for the function defined when you implemented the interface which is a_foo() which looks at A class's vtable to determine which function to actually call. The B class definition overrides A class's a_foo_real() with its own. If you need B class's b_foo_real to chain up, that's an easy enough (use A_CLASS(b_parent_class)->foo() which is defined for you in the G_DEFINE_TYPE macro)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文