Gobject - Inherit (Chinese)
-
Upload
kai-feng-chou -
Category
Self Improvement
-
view
1.569 -
download
1
description
Transcript of Gobject - Inherit (Chinese)
GobjectInherit
[email protected]@Taiwan, Taipei, 2008
LICENSE
● 本投影片授權方式為:
– 姓名標示─非商業性─相同方式分享 3.0 台灣版
– http://creativecommons.org/licenses/byncsa/3.0/tw/
● 所有的範例程式皆為 Public Domain
About This Slides
● All example was build by (Ubuntu 9.10)– GCC4.3.4– GLIB2.22.2
GObject's Introduction
GTK+ (The GIMP Toolkit)
● GTK+ 最初是 GIMP 的專用開發庫,後來發展為Unixlike 系統下開發圖形界面的應用程序的主流開發工具之一– 相較於 QT
● GLib/GObject/GDK/GTK 都是為了發展 GIMP而產生的,而 GTK+ 2.0 之後將 GLib 與GObject 獨立出來讓其他的軟體使用
– March 11, 2002
GLib
● 大部份的程式語言都提供 OO 以及內建方便的演算法
● GLib 也提供了基本的資料結構 (Hash/Linked Lists)
● GLib Object System 則提供了簡單的 OO Framework for C
GObject
● GTK+, Pango 等函式庫裡的物件都繼承了GOjbect 而實作
● 提供– 管理 Construction/ Destruction– property access method
● 不好用 ....
– message passing/ signal support
Before GObject
●學習 GObject 最好的管道– Official Documents
– Trace Code/ GTK+ 裡頭的數十個範例
●Gobject 因為沒有語法的限制,所以很容易 '' 無意/ 惡意 '' 的造成 Memory leaking ,所以:
● 依造前人留下來的方式使用。 ( 勿異想天開 )● 真正的了解到每一個 Macro 的用法 / 意義,並嘗試展
開之
Step 1 :定義物件
● mamanbar.h
typedef struct _MamanBar MamanBar;typedef struct _MamanBarClass MamanBarClass;
struct _MamanBar { GObject parent; /* instance members */ int a; int b;};
struct _MamanBarClass { GObjectClass parent; /* class members */};
繼承 GObject
將 function 與 value 分開
● 在 GObject 的設計裡,與 C++ 最大的概念上的不同就是 member function 與 member value擺放的位置– maman_bar 放 member value
– maman_bar_class 放 member function
● Why?– member value 是每一個物件都要有一份
– member function 則是每一個物件都使用同一份
精確的說
● class 裡頭放的資料是所有的物件都存取同一份– C++ 的 static member value
– 是的, class 裡不一定要放 function point
– 不是 static member function
● 而 base 裡頭放則是各個物件都保存一份
Step 2 :註冊 GType
● /* mamanbar.c */
GType maman_bar_get_type (void){ static GType type = 0;
if (type == 0) {static const GTypeInfo info = {
/* You fill this structure. */ };type = g_type_register_static (G_TYPE_OBJECT,
"MamanBarType", &info, 0);
}return type;
}
請注意:此 Function 為 Singleton ,而此函式只會 if(){} 裡只會被執行一次
將自已所撰寫的 function 填寫於此
Step 2 :註冊 GType (cont')
type = g_type_register_static (G_TYPE_OBJECT, "MamanBarType", &info, 0);
GType g_type_register_static (GType parent_type, const gchar *type_name, const GTypeInfo *info, GTypeFlags flags);
parent_type : Type from which this type will be derived.type_name : 0terminated string used as the name of the new type.info : The GTypeInfo structure for this type.flags : Bitwise combination of GTypeFlags values.
Returns : The new type identifier.
什麼是 Type ( 形別 )
● TypeInfo 裡記錄了物件的 Constructor/ Destructor/ 記憶體大小及 Parent 為何
● Glib 有實作 Type System ,管理所有的 Type– 動態的 , interpreted languages
– 靜態的 , compiled languages
typedef struct { /* interface types, classed types, instantiated types */ guint16 class_size; GbaseInitFunc base_init; GbaseFinalizeFunc base_finalize; /* interface types, classed types, instantiated types */ GclassInitFunc class_init; GclassFinalizeFunc class_finalize; gconstpointer class_data; /* instantiated types */ guint16 instance_size; guint16 n_preallocs; GinstanceInitFunc instance_init; /* value handling */ const GtypeValueTable *value_table;} GTypeInfo;
Step 2 :註冊 GType(cont')
● 目前可以填寫的資料
static const GTypeInfo info = { sizeof (MamanBarClass), NULL, /* base_init */ NULL, /* base_finalize */ NULL, /* class_init */ NULL, /* class_finalize */ NULL, /* class_data */ sizeof (MamanBar), 0, /* n_preallocs */ NULL /* instance_init */ };
Step 3 :撰寫 basic_init
● 也就是 Constructor
static voidmaman_bar_init ( MamanBar* self ){ self > a = 1; self > b = 2;}
● Register function pointer into structure
static const GTypeInfo info = { sizeof (MamanBarClass), NULL, /* base_init */ NULL, /* base_finalize */ NULL, /* class_init */ NULL, /* class_finalize */ NULL, /* class_data */ sizeof (MamanBar), 0, /* n_preallocs */ ????? /* instance_init */ };
Step 4 : use it
● Add print out information in the maman_bar_init● Please monitor the order of function launched
int main (int argc, char *argv[]){ /* this function should be executed first. Before everything */ g_type_init();
/* Create our object */ MamanBar *bar = g_object_new (MAMAN_TYPE_BAR, NULL);
printf(" bar>a = (%d) bar>b = (%d)\n", bar>a, bar>b ); return 0;}
You can find out the constructor/ type register will be launched by this function
Step 1 : Define SubBar
● Please Notice This Color /* subbar.h */
typedef struct _MamanBar MamanBar;typedef struct _MamanBarClass MamanBarClass;
struct _SubBar { MamanBar parent; /* instance members */ int c; int d;};
繼承 GObject
Step 2 : Register SubBar's GType
● Please Notice This Color /* subbar.c */
GType maman_bar_get_type (void){ static GType type = 0;
if (type == 0) {static const GTypeInfo info = {
sizeof (SubBarClass), NULL, /* base_init */ NULL, /* base_finalize */ NULL, /* class_init */ NULL, /* class_finalize */ NULL, /* class_data */ sizeof (SubBar), 0, /* n_preallocs */ sub_bar_init /* instance_init */
};type = g_type_register_static (MAMAN_BAR_TYPE, "SubBarType", &info, 0);}return type;
}
Step 3 : Try Casting
● /* main.c */● When we need to access parent's member
value ((MamanBar*)subbar)>a, ((MamanBar*)subbar)>b
Step 4 : ....
● Yes, it's very simple. No trick like previous slide ● Please monitor the order of function launched
Conclusion
● The above example show the inheritance implementation by using Gobject.
● Reader can write to child inherit subbar, and monitor the timing of constructor.