---
title: "GObject 2-01: A convention-compliant minimal complete working example."
author: "cloverprince"
date: "2009-12-18T01:19:54Z"
tags: ["Blog", "C", "C#", "C++", "GCC"]
categories: ["编程语言"]
slug: "gobject-2-01-a-convention-compliant-minimal-complete-working-example"
draft: false
lastmod: "2009-12-18T01:19:54Z"
id: 550978
url: "https://cloverprince.iteye.com/blog/550978"
---

My coding style has been so weird that no proper GObject programmers would ever use. GObject do have their coding/naming convention which is "designed by both smart and experienced people" and I am supposed "to put my ego aside". Here are the official document about the "conventions": :: http://library.gnome.org/devel/gobject/stable/gtype-conventions.html ----------------------- Here is a minimal \[b]working\[/b] example that is \[b]compliant to the coding convention\[/b]. Yes, it compiles and runs! I admit I had a hard time making my first GObject program running. These code is mostly based on the "boilerplate" examples in the official GObject reference. Comments omitted for cleanness and minimality. Save this as maman-bar.h: \[code="c"] #ifndef \_\_MAMAN\_BAR\_H\__ #define \_\_MAMAN\_BAR\_H\__ #include #define MAMAN\_TYPE\_BAR (maman\_bar\_get\_type ()) #define MAMAN\_BAR(obj) (G\_TYPE\_CHECK\_INSTANCE\_CAST ((obj), MAMAN\_TYPE\_BAR, MamanBar)) #define MAMAN\_IS\_BAR(obj) (G\_TYPE\_CHECK\_INSTANCE\_TYPE ((obj), MAMAN\_TYPE\_BAR)) #define MAMAN\_BAR\_CLASS(klass) (G\_TYPE\_CHECK\_CLASS\_CAST ((klass), MAMAN\_TYPE\_BAR, MamanBarClass)) #define MAMAN\_IS\_BAR\_CLASS(klass) (G\_TYPE\_CHECK\_CLASS\_TYPE ((klass), MAMAN\_TYPE\_BAR)) #define MAMAN\_BAR\_GET\_CLASS(obj) (G\_TYPE\_INSTANCE\_GET\_CLASS ((obj), MAMAN\_TYPE\_BAR, MamanBarClass)) typedef struct \_MamanBar MamanBar; typedef struct \_MamanBarClass MamanBarClass; struct \_MamanBar { GObject parent\_instance; }; struct \_MamanBarClass { GObjectClass parent\_class; }; GType maman\_bar\_get\_type (void); #endif /* \_\_MAMAN\_BAR\_H\__ \*/ \[/code] Save this as maman-bar.c: \[code="c"] #include "maman-bar.h" G\_DEFINE\_TYPE (MamanBar, maman\_bar, G\_TYPE\_OBJECT); static void maman\_bar\_init (MamanBar \*self) { } static void maman\_bar\_class\_init (MamanBarClass \*klass) { } \[/code] Save this as main.c: \[code="c"] #include #include "maman-bar.h" int main() { g\_type\_init(); /* NOTE: Don't forget this!!!! \*/ MamanBar \*bar = g\_object\_new(MAMAN\_TYPE\_BAR, NULL); g\_object\_unref(bar); return 0; } \[/code] Compile with: \[quote] gcc $(pkg-config --cflags --libs gobject-2.0) \*.c -o main \[/quote] There should be no warnings or errors. ------------------------ Let's now have a look at our code. We are creating a class named Bar in the namespace "Maman". Quite different from how we would define a class in C++: \[code="c++"] namespace Maman { class Bar : public GObject { ... }; } \[/code] we instead follow the "convention" of GObject. Convention is important since we are writing in C. There is no language-level notion of "object", "type", "class", "instance", "type-id", "dynamic type casting", "runtime type query", "runtime membership checking" or all other object-oriented buzzwords. Remember how we "define" a class in GObject? (see http://cloverprince.iteye.com/blog/498390 if you don't) We register our new class with the g\_type\_register\_static function, passing a filled GTypeInfo struct and some other parameters into it and it is done. After this, GObject will recognize our new class. How could we define a class in a "convention"-compliant way? Let's look at our maman-bar.c. There is a macro: \[code="c"] G\_DEFINE\_TYPE (MamanBar, maman\_bar, G\_TYPE\_OBJECT); \[/code] See the reference about how to use it. This will expand to (almost): \[code="c"] static void maman\_bar\_init(MamanBar * self); static void maman\_bar\_class\_init(MamanBarClass * klass); static gpointer maman\_bar\_parent\_class = ((void \*) 0); static void maman\_bar\_class\_intern\_init(gpointer klass) { maman\_bar\_parent\_class = g\_type\_class\_peek\_parent(klass); maman\_bar\_class\_init((MamanBarClass \*) klass); } GType maman\_bar\_get\_type(void) { static volatile gsize g\_define\_type\_id\_\_volatile = 0; if (g\_once\_init\_enter(&g\_define\_type\_id\_\_volatile)) { GType g\_define\_type\_id = g\_type\_register\_static\_simple(((GType) ((20) &lt;&lt; (2))), g\_intern\_static\_string ("MamanBar"), sizeof(MamanBarClass), (GClassInitFunc) maman\_bar\_class\_intern\_init, sizeof(MamanBar), (GInstanceInitFunc) maman\_bar\_init, (GTypeFlags) 0); { { { }; } } g\_once\_init\_leave(&g\_define\_type\_id\_\_volatile, g\_define\_type\_id); } } \[/code] You could apply the -E option of gcc to see yourself. It generates a function named maman\_bar\_get\_type. Our new type-id is stored as a static variable. During the first time we call this function, it will be initialized. On subsequent calls, this type-id will be simply returned. Such a function is always (by convention) named \_\_get\_type and takes no argument. Also in maman-bar.c, there are: \[code="c"] static void maman\_bar\_init (MamanBar \*self) { } static void maman\_bar\_class\_init (MamanBarClass \*klass) { } \[/code] What are they? They are initializer functions, much like "constructors" in C++. Well, you could omit the "constructor" in C++. Actually you COULD omit those init functions if you do not use the G\_DEFINE\_TYPE macro and register your type with g\_type\_register\_static by yourself. Since we are using G\_DEFINE\_TYPE, we need to supply them. By the way, initializing functions are (by convention) named \_\_init and \_\_class\_init for instance init function and class init function, respectively. See my previous articles if you are not familiar with these concepts. The complete maman-bar.c should look like this: \[code="c"] /* * Copyright information \*/ /* Include the header file (see below) \*/ #include "maman-bar.h" G\_DEFINE\_TYPE (MamanBar, maman\_bar, G\_TYPE\_OBJECT); static void maman\_bar\_init (MamanBar \*self) { /* TODO: Initialize \*self here \*/ } static void maman\_bar\_class\_init (MamanBarClass \*klass) { GObjectClass \*gobject\_class = G\_OBJECT\_CLASS (klass); /* TODO: Initialize \*klass here \*/ } \[/code] The above maman-bar.c implements the class. Now we need an interface to the outer world so that other people could use our class. Just look into the well-commented version of maman-bar.h: \[code="c"] /* * Copyright/Licensing information. \*/ /* Inclusion guard. * So that this header file will be included twice. \*/ #ifndef \_\_MAMAN\_BAR\_H\__ #define \_\_MAMAN\_BAR\_H\__ #include /* * TODO: Potentially, include other headers on which this header depends. \*/ /* * Type macros. * * What are they? * * They are not mandatory. I deliberately omitted them in my * previous articles in order to show their unnecessity. * However, other GObject programmers are expecting me to * provide these CONVENIENCE macros. I could also safely assume * that other (properly written) GObject programs provide * similar macros, too. \*/ /* This macro returns the type-id. It just call the maman\_bar\_get\_type() \*/ #define MAMAN\_TYPE\_BAR (maman\_bar\_get\_type ()) /* This macro casts obj (an instance) to this type. * Run-time type checking is performs to make sure the conversion * is successful. \*/ #define MAMAN\_BAR(obj) (G\_TYPE\_CHECK\_INSTANCE\_CAST ((obj), MAMAN\_TYPE\_BAR, MamanBar)) /* This macro checks whether obj (an instance) is an instance of * MamanBar or its derived classes. \*/ #define MAMAN\_IS\_BAR(obj) (G\_TYPE\_CHECK\_INSTANCE\_TYPE ((obj), MAMAN\_TYPE\_BAR)) /* These two macros are similar, but for classes, not instances. \*/ #define MAMAN\_BAR\_CLASS(klass) (G\_TYPE\_CHECK\_CLASS\_CAST ((klass), MAMAN\_TYPE\_BAR, MamanBarClass)) #define MAMAN\_IS\_BAR\_CLASS(klass) (G\_TYPE\_CHECK\_CLASS\_TYPE ((klass), MAMAN\_TYPE\_BAR)) /* This macro obtains the class struct (MamanBarClass) from obj (an instance). \*/ #define MAMAN\_BAR\_GET\_CLASS(obj) (G\_TYPE\_INSTANCE\_GET\_CLASS ((obj), MAMAN\_TYPE\_BAR, MamanBarClass)) /* typedef the instance struct and class struct. By convention, * these two struct types are named in PascalCase and accessed * without the "struct" keyword. \*/ typedef struct \_MamanBar MamanBar; typedef struct \_MamanBarClass MamanBarClass; /* The actual instance struct. \*/ struct \_MamanBar { GObject parent\_instance; /* TODO: Add instance members (member variables) here. \*/ }; /* The actual class struct. \*/ struct \_MamanBarClass { GObjectClass parent\_class; /* TODO: Add class members (virtual member functions) here. \*/ }; /* used by MAMAN\_TYPE\_BAR \*/ GType maman\_bar\_get\_type (void); /* * TODO: Add method definitions. \*/ #endif /* \_\_MAMAN\_BAR\_H\__ \*/ \[/code] In the main.c, #include the maman-bar.h to use this class. Don't forget to call g\_type\_init(), which is required by all GObject-related libraries. Summerize: In order to define and use a class, you: \[list] \[\*] Define the .h file, which includes an instance struct, a class struct, some convenience macros and prototypes of member functions. (Copy and paste the boilerplate is not a sin. Some code-generators also work well.) \[\*] Define the .c file, which #include the .h file, uses the G\_DEFINE\_TYPE macro, and defines the two init functions. \[\*] Call g\_type\_init() in main(). \[\*] #include the .h file and use it. \[/list]