GObject指南1--如何定义并实现一个新的GObject对象?

上一篇 / 下一篇  2006-10-09 22:12:42



QUOTE:
  GObject参考手册

QUOTE:
How To define and implement a new GObject?
如何定义并实现一个新的GObject对象?

QUOTE:
Boilerplate header code
头文件代码模版

QUOTE:
Boilerplate code
实现代码模版

QUOTE:
Object Construction
对象的构造

QUOTE:
Object Destruction
对象的析构

QUOTE:
Object methods
对象的方法

QUOTE:
Non-virtual public methods
非虚拟的公有方法

QUOTE:
Virtual public methods
虚拟的公有方法

QUOTE:
Virtual private Methods
虚拟的私有方法

QUOTE:
Chaining up
链接

QUOTE:
Clearly, this is one of the most common question people ask: they just want to crank code and implement a subclass of a GObject. Sometimes because they want to create their own class hierarchy, sometimes because they want to subclass one of GTK+'s widget. This chapter will focus on the implementation of a subtype of GObject. The sample source code associated to this section can be found in the documentation's source tarball, in the sample/gobject directory:
人们经常提出这样的问题:他们只是想实现GObject的一个子类。可能因为他们要实现自己的类层次,或者他们需要一个GTK+物件的子类。这一章介绍如何实现GObject的子类。与本节相关的源码在tar包中,sample/gobject目录下:

QUOTE:
  • maman-bar.{h|c}: this is the source for a object which derives from GObject and which shows how to declare different types of methods on the object.
  • maman-bar.{h|c}: 此文件为一个源自GObject的对象的实现代码,演示了如何声明对象的各种方法。


QUOTE:
  • maman-subbar.{h|c}: this is the source for a object which derives from MamanBar and which shows how to override some of its parent's methods.


QUOTE:
  • maman-foo.{h|c}: this is the source for an object which derives from GObject and which declares a signal.


QUOTE:
  • test.c: this is the main source which instantiates an instance of type and exercises their API.


QUOTE:
Boilerplate header code
头文件代码模版
The first step before writing the code for your GObject is to write the type's header which contains the needed type, function and macro definitions. Each of these elements is nothing but a convention which is followed not only by GTK+'s code but also by most users of GObject. If you feel the need not to obey the rules stated below, think about it twice:

If your users are a bit accustomed to GTK+ code or any Glib code, they will be a bit surprised and getting used to the conventions you decided upon will take time (money) and will make them grumpy (not a good thing)

You must assess the fact that these conventions might have been designed by both smart and experienced people: maybe they were at least partly right. Try to put your ego aside.


Pick a name convention for your headers and source code and stick to it:

use a dash to separate the prefix from the typename: maman-bar.h and maman-bar.c (this is the convention used by Nautilus and most Gnome libraries).

use an underscore to separate the prefix from the typename: maman_bar.h and maman_bar.c.

Do not separate the prefix from the typename: mamanbar.h and mamanbar.c. (this is the convention used by GTK+)

I personally like the first solution better: it makes reading file names easier for those with poor eyesight like me.

When you need some private (internal) declarations in several (sub)classes, you can define them in a private header file which is often named by appending the private keyword to the public header name. For example, one could use maman-bar-private.h, maman_bar_private.h or mamanbarprivate.h. Typically, such private header files are not installed.

The basic conventions for any header which exposes a GType are described in the section called “Conventions”. Most GObject-based code also obeys onf of the following conventions: pick one and stick to it.

If you want to declare a type named bar with prefix maman, name the type instance MamanBar and its class MamanBarClass (name is case-sensitive). It is customary to declare them with code similar to the following:[ DISCUZ_CODE_143 ]Most GTK+ types declare their private fields in the public header with a /* private */ comment, relying on their user's intelligence not to try to play with these fields. Fields not marked private are considered public by default. The /* protected */ comment (same semantics as those of C++) is also used, mainly in the GType library, in code written by Tim Janik.[ DISCUZ_CODE_144 ]All of Nautilus code and a lot of Gnome libraries use private indirection members, as described by Herb Sutter in his Pimpl articles (see Compilation Firewalls and The Fast Pimpl Idiom : he summarizes the different issues better than I will).[ DISCUZ_CODE_145 ]Note
Do not call this private, as that is a registered c++ keyword.

The private structure is then defined in the .c file, instantiated in the object's init function and destroyed in the object's finalize function.[ DISCUZ_CODE_146 ]A similar alternative, available since Glib version 2.4, is to define a private structure in the .c file, declare it as a private structure in maman_bar_class_init using g_type_class_add_private. Instead of allocating memory in maman_bar_init a pointer to the private memory area is stored in the instance to allow convenient access to this structure. A private structure will then be attached to each newly created object by the GObject system. You dont need to free or allocate the private structure, only the objects or pointers that it may contain. Another advantage of this to the previous version is that is lessens memory fragmentation, as the public and private parts of the instance memory are allocated at once.[ DISCUZ_CODE_147 ]Finally, there are different header include conventions. Again, pick one and stick to it. I personally use indifferently any of the two, depending on the codebase I work on: the rule is consistency.

Some people add at the top of their headers a number of #include directives to pull in all the headers needed to compile client code. This allows client code to simply #include "maman-bar.h".

Other do not #include anything and expect the client to #include themselves the headers they need before including your header. This speeds up compilation because it minimizes the amount of pre-processor work. This can be used in conjunction with the re-declaration of certain unused types in the client code to minimize compile-time dependencies and thus speed up compilation.


TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2009-01-08  
    123
45678910
11121314151617
18192021222324
25262728293031

我的存档

数据统计

  • 访问量: 944
  • 日志数: 4
  • 建立时间: 2005-10-05
  • 更新时间: 2005-10-05

RSS订阅

Open Toolbar