SlideShare a Scribd company logo
Gobject
Inherit


          elpam.tw@gmail.com
          elpam@Taiwan, Taipei, 2008
LICENSE
    ●   本投影片授權方式為:
        –   姓名標示─非商業性─相同方式分享 3.0  台灣版
        –   http://creativecommons.org/licenses/by­nc­sa/3.0/tw/




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




                                           
                                  elpam.tw@gmail.com
About This Slides
    ●   All example was build by (Ubuntu 9.10)
        –   GCC­4.3.4
        –   GLIB­2.22.2




                                   
                          elpam.tw@gmail.com
Agenda

    ●   GObject 's Introduction
    ●   First Gobject Klass
    ●   Inherit




                                   
                          elpam.tw@gmail.com
GObject's Introduction




                   
          elpam.tw@gmail.com
GTK+ (The GIMP Toolkit)
    ●
        GTK+ 最初是 GIMP 的專用開發庫,後來發展為
        Unix­like 系統下開發圖形界面的應用程序的主流
        開發工具之一
        –   相較於 QT 
    ●   GLib/GObject/GDK/GTK  都是為了發展 GIMP
        而產生的,而 GTK+ 2.0 之後將 GLib 與
        GObject 獨立出來讓其他的軟體使用
             –   March 11, 2002

                                      
                             elpam.tw@gmail.com
GLib
    ●
        大部份的程式語言都提供 OO 以及內建方便的演
        算法
    ●   GLib 也提供了基本的資料結構 (Hash/Linked 
        Lists)
    ●   GLib Object System 則提供了簡單的 OO 
        Framework for C



                              
                     elpam.tw@gmail.com
GObject
    ●
        GTK+, Pango  等函式庫裡的物件都繼承了
        GOjbect 而實作
    ●   提供
        –   管理 Construction/ Destruction
        –   property access method 
             ●   不好用 ....
        –   message passing/ signal support


                                     
                            elpam.tw@gmail.com
Before GObject
●
    學習 GObject 最好的管道
         –   Official Documents
         –   Trace Code/ GTK+ 裡頭的數十個範例
●Gobject 因為沒有語法的限制,所以很容易 '' 無意
/ 惡意 '' 的造成 Memory leaking ,所以:
     ●   依造前人留下來的方式使用。 ( 勿異想天開 )
     ●   真正的了解到每一個 Macro 的用法 / 意義,並嘗試展
         開之
                                      
                             elpam.tw@gmail.com
GObject 4­1
     First Klass
    Step By Step




              
     elpam.tw@gmail.com
範例
    ●   maman­bar.{h|c}
    ●   main.c




                                   
                          elpam.tw@gmail.com
Step 1 :定義物件
      ●   maman­bar.h
typedef struct _MamanBar MamanBar;
typedef struct _MamanBarClass MamanBarClass;

struct _MamanBar {                   繼承 GObject
 GObject parent;
  /* instance members */
 int a;
 int b;
};

struct _MamanBarClass {
  GObjectClass parent;
  /* class members */
};
                                        
                               elpam.tw@gmail.com
將 function 與 value 分開
    ●
        在 GObject 的設計裡,與 C++ 最大的概念上的
        不同就是 member function 與 member value
        擺放的位置
        –   maman_bar 放 member value
        –   maman_bar_class 放 member function
    ●   Why?
        –   member value 是每一個物件都要有一份
        –   member function 則是每一個物件都使用同一份
                                    
                           elpam.tw@gmail.com
精確的說
    ●
        class 裡頭放的資料是所有的物件都存取同一份
        –   C++ 的 static member value 
        –   是的, class 裡不一定要放 function point
        –   不是 static member function
    ●   而 base 裡頭放則是各個物件都保存一份




                                     
                            elpam.tw@gmail.com
Step 2 :註冊 GType
      ●   /* maman­bar.c */
                                     請注意:此 Function 為 Singleton ,而此函式只
GType maman_bar_get_type (void)      會 if(){} 裡只會被執行一次
{
   static GType type = 0;
       if (type == 0) {                            將自已所撰寫的 function 填寫於此
              static const GTypeInfo info = {
             /* You fill this structure. */
             };
       type = g_type_register_static (G_TYPE_OBJECT,
                                   "MamanBarType",
                                   &info, 0);
       }
       return type;
}

                                          
                                 elpam.tw@gmail.com
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.

                                                        
                                             elpam.tw@gmail.com
什麼是 Type ( 形別 )
    ●
        TypeInfo 裡記錄了物件的 Constructor/ 
        Destructor/  記憶體大小及 Parent 為何
    ●   Glib 有實作 Type System ,管理所有的 Type
        –   動態的 , interpreted languages
        –   靜態的 , compiled languages




                                    
                           elpam.tw@gmail.com
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;




                                             
                                    elpam.tw@gmail.com
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 */
    };


                                               
                                      elpam.tw@gmail.com
Step 3 :撰寫 basic_init
      ●
          也就是 Constructor
static void
maman_bar_init ( MamanBar* self )
{
  self ­> a = 1;
  self ­> b = 2;
}




                                             
                                    elpam.tw@gmail.com
●   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 */
    };


                                               
                                      elpam.tw@gmail.com
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();
                                                You can find out the constructor/ type register 
                                                will be launched by this function
        /* 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;
}
                                                 
                                       elpam.tw@gmail.com
GObject 4­2
       Inherit
    Step By Step




              
     elpam.tw@gmail.com
Step 1 : Define SubBar
      ●   Please Notice This Color /* sub­bar.h */



typedef struct _MamanBar MamanBar;
typedef struct _MamanBarClass MamanBarClass;

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


                                        
                               elpam.tw@gmail.com
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;                                           
}                                                 elpam.tw@gmail.com
Step 3 : Try Casting
    ●   /* main.c */
    ●   When we need to access parent's member 
        value 
                 ((MamanBar*)subbar)­>a,   ((MamanBar*)subbar)­>b 




                                        
                               elpam.tw@gmail.com
Step 4 : ....
    ●   Yes, it's very simple. No trick like previous slide 
    ●   Please monitor the order of function launched




                                    
                           elpam.tw@gmail.com
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. 




                                   
                          elpam.tw@gmail.com
Next
    ●   We''ll talk about Virtual Function later




                                    
                           elpam.tw@gmail.com

More Related Content

PDF
OOP in C - Virtual Function (Chinese Version)
PPT
Introduction to C++ over CLI
PPTX
認識 C++11 新標準及使用 AMP 函式庫作平行運算
PDF
C python 原始碼解析 投影片
PPTX
Intro to C++ Basic
PDF
竞赛中C++语言拾遗
PPT
第六章 函數與巨集
PPTX
Introduction to Basic Haskell Components (In Chinese)
OOP in C - Virtual Function (Chinese Version)
Introduction to C++ over CLI
認識 C++11 新標準及使用 AMP 函式庫作平行運算
C python 原始碼解析 投影片
Intro to C++ Basic
竞赛中C++语言拾遗
第六章 函數與巨集
Introduction to Basic Haskell Components (In Chinese)

What's hot (20)

PDF
Java 開發者的函數式程式設計
PDF
lambda/closure – JavaScript、Python、Scala 到 Java SE 7
PPTX
Golang 入門初體驗
PPTX
Golangintro
PDF
Python&GUI
PPT
第4章函数
PPTX
Corona 初探 lua 語言,玩跨平台(iOS & android) 行動裝置開發工具
PPTX
Ecma script edition5-小试
PDF
Golang server design pattern
PDF
Ch 8
PDF
Python變數與資料運算
PPT
1 C入門教學
PDF
Python 迴圈作業
PDF
Objc under the_hood_2013
PDF
C++11 smart pointers
PPT
Scala function-and-closures
PDF
functional-scala
PDF
iOs app 101
PDF
C語言應用前置處理
PDF
Ptyhon 教學 003 函數
Java 開發者的函數式程式設計
lambda/closure – JavaScript、Python、Scala 到 Java SE 7
Golang 入門初體驗
Golangintro
Python&GUI
第4章函数
Corona 初探 lua 語言,玩跨平台(iOS & android) 行動裝置開發工具
Ecma script edition5-小试
Golang server design pattern
Ch 8
Python變數與資料運算
1 C入門教學
Python 迴圈作業
Objc under the_hood_2013
C++11 smart pointers
Scala function-and-closures
functional-scala
iOs app 101
C語言應用前置處理
Ptyhon 教學 003 函數
Ad

Similar to Gobject - Inherit (Chinese) (15)

PDF
Gtk to qt
PDF
GNOME3 延伸套件教學
PDF
Using vim
PDF
大家來學GObject
PDF
版本控制 使用Git & git hub
PDF
Python 中 += 與 join比較
PDF
Git 入门实战
PDF
Git Branch Practice
PPTX
Git入門介紹
PPTX
大家應該都要會的工具 Git 從放棄到會用2-分支篇
PDF
recover_pdb 原理與介紹
PPTX
Git 入門與實作
PDF
COSCUP 2015 開源之道-Git工作坊教學簡報
PDF
Django development
PPTX
张所勇:前端开发工具推荐
Gtk to qt
GNOME3 延伸套件教學
Using vim
大家來學GObject
版本控制 使用Git & git hub
Python 中 += 與 join比較
Git 入门实战
Git Branch Practice
Git入門介紹
大家應該都要會的工具 Git 從放棄到會用2-分支篇
recover_pdb 原理與介紹
Git 入門與實作
COSCUP 2015 開源之道-Git工作坊教學簡報
Django development
张所勇:前端开发工具推荐
Ad

Gobject - Inherit (Chinese)

  • 1. Gobject Inherit elpam.tw@gmail.com elpam@Taiwan, Taipei, 2008
  • 2. LICENSE ● 本投影片授權方式為: – 姓名標示─非商業性─相同方式分享 3.0  台灣版 – http://creativecommons.org/licenses/by­nc­sa/3.0/tw/ ● 所有的範例程式皆為 Public Domain     elpam.tw@gmail.com
  • 3. About This Slides ● All example was build by (Ubuntu 9.10) – GCC­4.3.4 – GLIB­2.22.2     elpam.tw@gmail.com
  • 4. Agenda ● GObject 's Introduction ● First Gobject Klass ● Inherit     elpam.tw@gmail.com
  • 5. GObject's Introduction     elpam.tw@gmail.com
  • 6. GTK+ (The GIMP Toolkit) ● GTK+ 最初是 GIMP 的專用開發庫,後來發展為 Unix­like 系統下開發圖形界面的應用程序的主流 開發工具之一 – 相較於 QT  ● GLib/GObject/GDK/GTK  都是為了發展 GIMP 而產生的,而 GTK+ 2.0 之後將 GLib 與 GObject 獨立出來讓其他的軟體使用 – March 11, 2002     elpam.tw@gmail.com
  • 7. GLib ● 大部份的程式語言都提供 OO 以及內建方便的演 算法 ● GLib 也提供了基本的資料結構 (Hash/Linked  Lists) ● GLib Object System 則提供了簡單的 OO  Framework for C     elpam.tw@gmail.com
  • 8. GObject ● GTK+, Pango  等函式庫裡的物件都繼承了 GOjbect 而實作 ● 提供 – 管理 Construction/ Destruction – property access method  ● 不好用 .... – message passing/ signal support     elpam.tw@gmail.com
  • 9. Before GObject ● 學習 GObject 最好的管道 – Official Documents – Trace Code/ GTK+ 裡頭的數十個範例 ●Gobject 因為沒有語法的限制,所以很容易 '' 無意 / 惡意 '' 的造成 Memory leaking ,所以: ● 依造前人留下來的方式使用。 ( 勿異想天開 ) ● 真正的了解到每一個 Macro 的用法 / 意義,並嘗試展 開之     elpam.tw@gmail.com
  • 10. GObject 4­1 First Klass Step By Step     elpam.tw@gmail.com
  • 11. 範例 ● maman­bar.{h|c} ● main.c     elpam.tw@gmail.com
  • 12. Step 1 :定義物件 ● maman­bar.h typedef struct _MamanBar MamanBar; typedef struct _MamanBarClass MamanBarClass; struct _MamanBar { 繼承 GObject  GObject parent;   /* instance members */  int a;  int b; }; struct _MamanBarClass {   GObjectClass parent;   /* class members */ };     elpam.tw@gmail.com
  • 13. 將 function 與 value 分開 ● 在 GObject 的設計裡,與 C++ 最大的概念上的 不同就是 member function 與 member value 擺放的位置 – maman_bar 放 member value – maman_bar_class 放 member function ● Why? – member value 是每一個物件都要有一份 – member function 則是每一個物件都使用同一份     elpam.tw@gmail.com
  • 14. 精確的說 ● class 裡頭放的資料是所有的物件都存取同一份 – C++ 的 static member value  – 是的, class 裡不一定要放 function point – 不是 static member function ● 而 base 裡頭放則是各個物件都保存一份     elpam.tw@gmail.com
  • 15. Step 2 :註冊 GType ● /* maman­bar.c */ 請注意:此 Function 為 Singleton ,而此函式只 GType maman_bar_get_type (void) 會 if(){} 裡只會被執行一次 {    static GType type = 0; if (type == 0) { 將自已所撰寫的 function 填寫於此 static const GTypeInfo info = {       /* You fill this structure. */       }; type = g_type_register_static (G_TYPE_OBJECT,                                    "MamanBarType",                                    &info, 0); } return type; }     elpam.tw@gmail.com
  • 16. 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.     elpam.tw@gmail.com
  • 17. 什麼是 Type ( 形別 ) ● TypeInfo 裡記錄了物件的 Constructor/  Destructor/  記憶體大小及 Parent 為何 ● Glib 有實作 Type System ,管理所有的 Type – 動態的 , interpreted languages – 靜態的 , compiled languages     elpam.tw@gmail.com
  • 18. 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;     elpam.tw@gmail.com
  • 19. 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 */     };     elpam.tw@gmail.com
  • 20. Step 3 :撰寫 basic_init ● 也就是 Constructor static void maman_bar_init ( MamanBar* self ) {   self ­> a = 1;   self ­> b = 2; }     elpam.tw@gmail.com
  • 21. 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 */     };     elpam.tw@gmail.com
  • 22. 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(); You can find out the constructor/ type register  will be launched by this function         /* 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; }     elpam.tw@gmail.com
  • 23. GObject 4­2 Inherit Step By Step     elpam.tw@gmail.com
  • 24. Step 1 : Define SubBar ● Please Notice This Color /* sub­bar.h */ typedef struct _MamanBar MamanBar; typedef struct _MamanBarClass MamanBarClass; struct _SubBar { 繼承 GObject  MamanBar parent;   /* instance members */  int c;  int d; };     elpam.tw@gmail.com
  • 25. 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;   } elpam.tw@gmail.com
  • 26. Step 3 : Try Casting ● /* main.c */ ● When we need to access parent's member  value  ((MamanBar*)subbar)­>a,   ((MamanBar*)subbar)­>b      elpam.tw@gmail.com
  • 27. Step 4 : .... ● Yes, it's very simple. No trick like previous slide  ● Please monitor the order of function launched     elpam.tw@gmail.com
  • 28. 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.      elpam.tw@gmail.com
  • 29. Next ● We''ll talk about Virtual Function later     elpam.tw@gmail.com