OOP Using GObject (1) – A Fundamental Type

These days, I tried to write C code with OO support. I found GObject is such a well-designed library to simplify my implementation. However, the official documents is not so clear sometimes.  It do not provide sufficient information about all its stuff. I had to write my own demo applications to test the usage of some function. Moreover, the source code were also checked for reference.

There are 3 types in GObject type system: Fundamental, static and dynamic. Fundamental types are top-most types. The do not have parent types. They are seldom used, since all fundamental types are pre-defined rather than user-defined.

In this article, I will try to define a fundamental type using in GObject.Here’s the code on how to register a basic fundamental type in GObject, and how it can be used.

NOTE: PLEASE READ ALL COMMENT CAREFULLY.

My fundamental type is created by calling g_type_register_fundamental() function. A GTypeInfo and a GTypeFundamentalInfo struct are passed as parameters. And here comes the linux Makefile. You can use pkg-config to replace my include and lib paths:

NOTE: REPLACE THE WHITESPACE WITH TAB IN MAKEFILES, IF YOU COPY THE CODE DIRECTLY.

The fundamental type is of no use at all presently. In the next article, I will extend my code to add class meta info.

All source code is available in my skydrive: http://cid-481cbe104492a3af.office.live.com/browse.aspx/share/dev/TestOO. In the TestGObject-{date}.zip/TestGObject1 folder.

Leave a Reply

Your email address will not be published. Required fields are marked *