In last article, we defined a fundamental type. But nothing can be done with it. Now, we will extend it to be a classed type, say adding class info into our fundamental type. To do this, we should define a class struct, which can be regard as the meta info of a C++ class:

NOTE: PLEASE READ ALL COMMENT CAREFULLY.

GTypeClass should be the first member of a class struct. You can image the i field to be the version of the class. And we can add a string field to hold the author of this class. There’s also a function pointer bar(). As you may already know, it is used to implement polymorphism, which can be regard as virtual function of a C++ class.

When registering our fundamental type, additional field in GTypeInfo and GTypeFundamentalInfo are filled:

GTypeInfo is the key data structure of GObject type system. It defines how a classed type should be initialized and finalized. Here, we just assigned the class_init() callback. It is called when our FooClass needs initialization. For fundamental and static types, their class_finalize() are never called. We will demo the usage of this callback when introducing dynamic types. Please also note the G_TYPE_FLAG_CLASSED flag passed into GTypeFundamentalInfo struct.

Now, let’s implement our foo_class_init() function. This function is used to initialize fields and assign virtual functions in most time:

Now, we’ve finished our definition of the class struct. Let’s see how to use it:

See? We use g_type_class_ref() and g_type_class_unref() to ref/unref a class, and invoke a function. But its function is still limited. We can just get/set its meta info. It still cannot be instantiated. This will be discussed in the next article.

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

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.

Long time no post here. Recently, I reported 3 bugs to QTerm project and patches were provided. This is my first time to contribute an open-source project.

Then I was able to build my private patched debian package of QTerm with the guide here. I just use the official debian package meta info found in this mirror site. Some notes to take:
1. The path should be like: /home/<your_name>/packages/<your_project>/
2. Before running “dpkg-buildpackage -rfakeroot”, check the debian/control file to see what packages is required to build.
3. The revision of package seems to be controlled by debian/changelog.

My private build file can be found in my skydrive:
– For Hardy(8.04): QTerm 0.5.7
– For Lucid(10.04): QTerm 0.5.11

The article is originally inspired by this one: http://www.openrce.org/articles/full_view/23. The undocumented parameters in MSVC++ compiler are: /d1reportSingleClassLayout<classname> and /d1reportAllClassLayout.

A simple example:

The dumped layout:

You see: When using virtual inheritance, an additional vbptr is added into class layout. There is also a separated section containing the virtual base class, with vbptr pointing to it. So, the object size of virtual inheritance is bigger than non-virtual inheritance.

Now, here is a complex example:

The dumped layout:

The layout of CDerive class is so complicated. First, it has 3 base classes, 1 field and 1 virtual base section. The the first 2 base classes(CBase2, CBase3) have their vbptr pointed to the address of the virtual base section.

I read source code in QTerm and FQTerm today. Since I want to find reference for Ascii rendering control in my QAnsiEditor project. After hours of tracing and debugging, I was able to use the rendering control in simplest code. Here’s the patch in src/main.cpp:

The screenshot of standalone mode:

qterm057_1

The complete patch and patched source can be found here:

http://cid-481cbe104492a3af.office.live.com/browse.aspx/share/dev/QAnsiEditor