All pastes #593477 Raw Edit

Someone

public c v1 · immutable
#593477 ·published 2007-06-28 10:37 UTC
rendered paste body
/* This file is part of herzi's playground * * AUTHORS *     Sven Herzberg  <herzi@gnome-de.org> * * Copyright (C) 2006  Sven Herzberg <herzi@gnome-de.org> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA */#include "gobject-helpers.h"/* interface definition */typedef struct _Printable Printable;typedef struct _PrintableIface {	GTypeInterface iface;	void (*print) (Printable* self);} PrintableIface;#define PRINTABLE(i) G_TYPE_CHECK_INSTANCE_CAST((i), printable_get_type(), Printable)G_DEFINE_IFACE(Printable, printable, G_TYPE_INTERFACE);static voidprintable_print(Printable* self) {	g_return_if_fail(G_TYPE_INSTANCE_GET_INTERFACE(self, printable_get_type(), PrintableIface)->print);	G_TYPE_INSTANCE_GET_INTERFACE(self, printable_get_type(), PrintableIface)->print(self);}/* parent class */typedef GObject      Foo;typedef GObjectClass FooClass;static void foo_print(Printable* printable) {	g_print("Foo instance: %p\n", printable);}static void foo_impl_printable(PrintableIface* iface) {	iface->print = foo_print;}G_DEFINE_TYPE_WITH_CODE(Foo, foo, G_TYPE_OBJECT,			G_IMPLEMENT_INTERFACE(printable_get_type(), foo_impl_printable));static void foo_init(Foo* self) {};static void foo_class_init(FooClass* self_class) {}/* first child class (no chaining up) */typedef Foo      FooBar;typedef FooClass FooBarClass;static void foo_bar_print(Printable* printable) {	g_print("FooBar instance: %p (is also Foo, but we don't want to tell :P)\n", printable);}static void foo_bar_impl_printable(PrintableIface* iface) {	iface->print = foo_bar_print;}G_DEFINE_TYPE_WITH_CODE(FooBar, foo_bar, foo_get_type(),			G_IMPLEMENT_INTERFACE(printable_get_type(), foo_bar_impl_printable));static void foo_bar_init(FooBar* self) {}static void foo_bar_class_init(FooBarClass* self_class) {}/* second child class (with evil up-chaining) */typedef Foo      Foobar;typedef FooClass FoobarClass;static void foobar_print(Printable* printable);static void foobar_impl_printable(PrintableIface* iface) {	iface->print = foobar_print;}G_DEFINE_TYPE_WITH_CODE(Foobar, foobar, foo_get_type(),			G_IMPLEMENT_INTERFACE(printable_get_type(), foobar_impl_printable));static void foobar_print(Printable* printable) {	if(((PrintableIface*)g_type_interface_peek(foobar_parent_class, printable_get_type()))->print) {		((PrintableIface*)g_type_interface_peek(foobar_parent_class, printable_get_type()))->print(printable);	}	g_print("Foobar instance: %p\n", printable);}static void foobar_init(Foobar* self) {}static void foobar_class_init(FoobarClass* self_class) {}intmain(int argc, char** argv) {	gpointer test;	g_type_init();	test = g_object_new(foo_get_type(), NULL);	printable_print(PRINTABLE(test));	g_object_unref(test);	test = g_object_new(foo_bar_get_type(), NULL);	printable_print(PRINTABLE(test));	g_object_unref(test);	test = g_object_new(foobar_get_type(), NULL);	printable_print(PRINTABLE(test));	g_object_unref(test);	return 0;}