29
Gobject Inherit [email protected] elpam@Taiwan, Taipei, 2008

Gobject - Inherit (Chinese)

Embed Size (px)

DESCRIPTION

I rewrite this slide in 2009/12. Because i found too much different in GObject between previous version. ~ Enjoy it

Citation preview

Page 1: Gobject - Inherit (Chinese)

GobjectInherit

[email protected]@Taiwan, Taipei, 2008

Page 2: Gobject - Inherit (Chinese)

   [email protected]

LICENSE

● 本投影片授權方式為:

– 姓名標示─非商業性─相同方式分享 3.0  台灣版

– http://creativecommons.org/licenses/by­nc­sa/3.0/tw/

● 所有的範例程式皆為 Public Domain

Page 3: Gobject - Inherit (Chinese)

   [email protected]

About This Slides

● All example was build by (Ubuntu 9.10)– GCC­4.3.4– GLIB­2.22.2

Page 4: Gobject - Inherit (Chinese)

   [email protected]

Agenda

● GObject 's Introduction● First Gobject Klass● Inherit

Page 5: Gobject - Inherit (Chinese)

   [email protected]

GObject's Introduction

Page 6: Gobject - Inherit (Chinese)

   [email protected]

GTK+ (The GIMP Toolkit)

● GTK+ 最初是 GIMP 的專用開發庫,後來發展為Unix­like 系統下開發圖形界面的應用程序的主流開發工具之一– 相較於 QT 

● GLib/GObject/GDK/GTK  都是為了發展 GIMP而產生的,而 GTK+ 2.0 之後將 GLib 與GObject 獨立出來讓其他的軟體使用

– March 11, 2002

Page 7: Gobject - Inherit (Chinese)

   [email protected]

GLib

● 大部份的程式語言都提供 OO 以及內建方便的演算法

● GLib 也提供了基本的資料結構 (Hash/Linked Lists)

● GLib Object System 則提供了簡單的 OO Framework for C

Page 8: Gobject - Inherit (Chinese)

   [email protected]

GObject

● GTK+, Pango  等函式庫裡的物件都繼承了GOjbect 而實作

● 提供– 管理 Construction/ Destruction– property access method 

● 不好用 ....

– message passing/ signal support

Page 9: Gobject - Inherit (Chinese)

   [email protected]

Before GObject

●學習 GObject 最好的管道– Official Documents

– Trace Code/ GTK+ 裡頭的數十個範例

●Gobject 因為沒有語法的限制,所以很容易 '' 無意/ 惡意 '' 的造成 Memory leaking ,所以:

● 依造前人留下來的方式使用。 ( 勿異想天開 )● 真正的了解到每一個 Macro 的用法 / 意義,並嘗試展

開之

Page 10: Gobject - Inherit (Chinese)

   [email protected]

GObject 4­1First Klass

Step By Step

Page 11: Gobject - Inherit (Chinese)

   [email protected]

範例

● maman­bar.{h|c}● main.c

Page 12: Gobject - Inherit (Chinese)

   [email protected]

Step 1 :定義物件

● maman­bar.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

Page 13: Gobject - Inherit (Chinese)

   [email protected]

將 function 與 value 分開

● 在 GObject 的設計裡,與 C++ 最大的概念上的不同就是 member function 與 member value擺放的位置– maman_bar 放 member value

– maman_bar_class 放 member function

● Why?– member value 是每一個物件都要有一份

– member function 則是每一個物件都使用同一份

Page 14: Gobject - Inherit (Chinese)

   [email protected]

精確的說

● class 裡頭放的資料是所有的物件都存取同一份– C++ 的 static member value 

– 是的, class 裡不一定要放 function point

– 不是 static member function

● 而 base 裡頭放則是各個物件都保存一份

Page 15: Gobject - Inherit (Chinese)

   [email protected]

Step 2 :註冊 GType

● /* maman­bar.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 填寫於此

Page 16: Gobject - Inherit (Chinese)

   [email protected]

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 : 0­terminated 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.

Page 17: Gobject - Inherit (Chinese)

   [email protected]

什麼是 Type ( 形別 )

● TypeInfo 裡記錄了物件的 Constructor/ Destructor/  記憶體大小及 Parent 為何

● Glib 有實作 Type System ,管理所有的 Type– 動態的 , interpreted languages

– 靜態的 , compiled languages

Page 18: Gobject - Inherit (Chinese)

   [email protected]

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;

Page 19: Gobject - Inherit (Chinese)

   [email protected]

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 */    };

Page 20: Gobject - Inherit (Chinese)

   [email protected]

Step 3 :撰寫 basic_init

● 也就是 Constructor

static voidmaman_bar_init ( MamanBar* self ){  self ­> a = 1;  self ­> b = 2;}

Page 21: Gobject - Inherit (Chinese)

   [email protected]

● 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 */    };

Page 22: Gobject - Inherit (Chinese)

   [email protected]

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

Page 23: Gobject - Inherit (Chinese)

   [email protected]

GObject 4­2Inherit

Step By Step

Page 24: Gobject - Inherit (Chinese)

   [email protected]

Step 1 : Define SubBar

● Please Notice This Color /* sub­bar.h */

typedef struct _MamanBar MamanBar;typedef struct _MamanBarClass MamanBarClass;

struct _SubBar { MamanBar parent;  /* instance members */ int c; int d;};

繼承 GObject

Page 25: Gobject - Inherit (Chinese)

   [email protected]

Step 2 : Register SubBar's GType

● Please Notice This Color /* sub­bar.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;

}

Page 26: Gobject - Inherit (Chinese)

   [email protected]

Step 3 : Try Casting

● /* main.c */● When we need to access parent's member 

value ((MamanBar*)subbar)­>a,   ((MamanBar*)subbar)­>b 

Page 27: Gobject - Inherit (Chinese)

   [email protected]

Step 4 : ....

● Yes, it's very simple. No trick like previous slide ● Please monitor the order of function launched

Page 28: Gobject - Inherit (Chinese)

   [email protected]

Conclusion

● The above example show the inheritance implementation by using Gobject.

● Reader can write to child inherit sub­bar, and monitor the timing of constructor. 

Page 29: Gobject - Inherit (Chinese)

   [email protected]

Next

● We''ll talk about Virtual Function later