Last active
May 9, 2018 22:22
-
-
Save ProfAvery/505e01eb41b12a38db23511333ab2b43 to your computer and use it in GitHub Desktop.
GObject Template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <class>.c | |
#include "<class>.h" | |
typedef struct _<Class>Private <Class>Private; | |
G_DEFINE_TYPE(<Class>, <class>, G_TYPE_OBJECT) | |
#define <CLASS>(object) \ | |
(G_TYPE_INSTANCE_GET_CLASS((object), TYPE_<CLASS>, <Class>)) | |
#define <CLASS>_GET_CLASS(object) \ | |
(G_TYPE_INSTANCE_GET_CLASS((object), TYPE_<CLASS>, <Class>Class)) | |
struct _<Class>Private { | |
// private member variables | |
}; | |
#define <CLASS>_GET_PRIVATE(object) \ | |
(G_TYPE_INSTANCE_GET_PRIVATE((object), TYPE_<CLASS>, <Class>Private)) | |
void <class>_private_<virtual_method>(<Class> *self, <type>); | |
static void <class>_class_init(<Class>Class* klass) | |
{ | |
g_type_class_add_private(klass, sizeof(<Class>Private)); | |
klass-><virtual_method> = <class>_private_<virtual_method>; | |
} | |
static void <class>_init(<Class> *self) | |
{ | |
// initialize fields; see below for private get/set | |
} | |
<Class> *<class>_new(void) | |
{ | |
return g_object_new(TYPE_<CLASS>, NULL); | |
} | |
void <class>_set_<field>(<Class> *<object>, <type> <value>) | |
{ | |
<Class>Private *priv = <CLASS>_GET_PRIVATE(<object>); | |
priv-><field> = <value>; | |
} | |
<type> <class>_get_<field>(<Class> *<object>) | |
{ | |
<Class>Private *priv = <CLASS>_GET_PRIVATE(<object>); | |
return priv-><field>; | |
} | |
void <class>_<virtual_method>(<Class> *self, <type> <argument>) | |
{ | |
<Class>Class *klass = _<CLASS>_GET_CLASS(self); | |
klass-><virtual_method>(self, <argument>); | |
} | |
void <class>_private_<virtual_method>(<Class> *self, <type> <argument>) | |
{ | |
// implement method; see above for private get/set | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <class>.h | |
#ifndef _<CLASS>_H | |
#define _<CLASS>_H | |
#include <glib-object.h> | |
#define TYPE_<CLASS> (<class>_get_type()) | |
G_DECLARE_DERIVABLE_TYPE(<Class>, <class>, , <CLASS>, GObject) | |
typedef struct _<Class>Class { | |
GObjectClass parent_class; | |
void (*<virtual_method>)(<Class> *, <type>); | |
gpointer padding[10]; | |
} <Class>Class; | |
GType <class>_get_type(void); | |
<Class> *<class>_new(void); | |
void <class>_set_<field>(<Class> *, <type>); | |
<type> <class>_get_<field>(<Class> *); | |
void <class>_<virtual_method>(<Class> *, <type>); | |
// should also include dispose and finalize methods | |
#endif | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <subclass>.c | |
#include "<subclass>.h" | |
typedef struct _<Subclass>Private <Subclass>Private; | |
G_DEFINE_TYPE(<Subclass>, <subclass>, TYPE_<CLASS>) | |
#define <SUBCLASS>(object) \ | |
(G_TYPE_CHECK_INSTANCE_CAST((object), TYPE_<SUBCLASS>, <Subclass>)) | |
#define <SUBCLASS>_GET_CLASS(object) \ | |
(G_TYPE_INSTANCE_GET_CLASS((object), TYPE_<SUBCLASS>, <Subclass>Class)) | |
struct _<Subclass>Private { | |
// private member variables | |
}; | |
#define <SUBCLASS>_GET_PRIVATE(object) \ | |
(G_TYPE_INSTANCE_GET_PRIVATE((object), TYPE_<SUBCLASS>, <Subclass>Private)) | |
void <subclass>_private_<virtual_method>(<Subclass> *self, <type>); | |
static void <subclass>_class_init(<Subclass>Class* klass) | |
{ | |
g_type_class_add_private(klass, sizeof(<Subclass>Private)); | |
klass-><virtual_method> = <subclass>_private_<virtual_method>; | |
} | |
static void <subclass>_init(<Subclass> *self) | |
{ | |
// initialize fields; see below for private get/set | |
} | |
<Subclass> *<subclass>_new(void) | |
{ | |
return g_object_new(TYPE_<SUBCLASS>, NULL); | |
} | |
void <subclass>_set_<field>(<Subclass> *<object>, <type> <value>) | |
{ | |
<Subclass>Private *priv = <SUBCLASS>_GET_PRIVATE(<object>); | |
priv-><field> = <value>; | |
} | |
<type> <subclass>_get_<field>(<Subclass> *<object>) | |
{ | |
<Subclass>Private *priv = <SUBCLASS>_GET_PRIVATE(<object>); | |
return priv-><field>; | |
} | |
void <subclass>_<virtual_method>(<Subclass> *self, <type> <argument>) | |
{ | |
<Subclass>Class *klass = <SUBCLASS>_GET_CLASS(self); | |
klass-><virtual_method>(self, <argument>); | |
} | |
void <subclass>_private_<virtual_method>(<Subclass> *self, <type> <argument>) | |
{ | |
// implement method; see above for private get/set | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <subclass>.h | |
#ifndef _<SUBCLASS>_H | |
#define <SUBCLASS>_H | |
#include <glib-object.h> | |
#include "<class>.h" | |
#define TYPE_<SUBCLASS> (<subclass>_get_type()) | |
G_DECLARE_DERIVABLE_TYPE(<Subclass>, <subclass>, , <SUBCLASS>, <Class>) | |
typedef struct _<Subclass>Class { | |
<Class> parent_class; | |
} <Subclass>Class; | |
GType <subclass>_get_type(void); | |
<Subclass> *<subclass>_new(void); | |
void <subclass>_set_<field>(<Subclass> *, <type>); | |
<type> <subclass>_get_<field>(<Subclass> *); | |
// should also include dispose and finalize methods | |
#endif | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// main.c | |
#include <glib.h> | |
#include <glib/gprintf.h> | |
#include "<class>.h" | |
#include "<subclass>.h" | |
int main(void) | |
{ | |
<Class> *<object> = <class>_new(); | |
<class>_set_<field>(<object>, <value>); | |
g_printf(<format>, <class>_get_<field>(<object>)); | |
<class>_<virtual_method>(<object>, <argument>); | |
<Subclass> *<object2> = <subclass>_new(); | |
<subclass>_set_<field>(<object2>, <value>); | |
g_printf(<format>, <subclass>_get_<field>(<object2>)); | |
<subclass>_<virtual_method>(<object2>, <argument>); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See http://sgros.blogspot.com/2016/01/few-tips-about-gobject-for-oo.html for additional details.