15. 頁首在這裡
GObject進階課程
在進階課程裡,我們會瞭解GObject建構和解構的順序,和GObject中的訊號.
類別的建構與解構
在[2]裡有兩張表在解釋GObjec和GObject Interfacet的建構和解構.我們這邊簡單的在這邊再出現一次.
GObject的解構和解構
Invocation time Function Invoked Function's parameters
First call type's base_init function On the inheritance tree of
tog_type_create_instancefor classes from fundamental
target type type to target type. base_init
is invoked once for each class
structure.
target type's class_init function On target type's class structure
interface initialization, see
the section called “Interface
Initialization”
Each call target type's instance_init On object's instance
tog_type_create_instancefor function
target type
Last call interface destruction, see
tog_type_free_instance for the section called “Interface
target type Destruction”
target type's class_finalize On target type's class structure
function
type's base_finalize function On the inheritance tree of
classes from fundamental type
to target type. base_finalize is
invoked once for each class
structure.
GObject Interface的建構
Invocation time Function Invoked Function's parameters Remark
First call interface' base_init On interface' vtable Register interface'
頁尾在這裡
16. 頁首在這裡
tog_type_create_ function signals here (use a
instance for type local static boolean
implementing interface variable as described
above to make sure
not to register them
twice.).
interface' interface_init On interface' vtable Initialize interface'
function implementation.
That is, initialize the
interface method
pointers in the
interface structure
to the function's
implementation.
GObject Interface的解構
Invocation time Function Invoked Function's parameters
Last call to interface' interface_finalize On interface' vtable
g_type_free_instance for type function
implementing interface
interface' base_finalize function On interface' vtable
訊號
在GObject提供了訊號(Signal)這個機制,讓物件操作者可以透過callback函式得知物件狀態改變了,更簡單
的說,訊號是GObject中的觀察者模式(Observer Pattern).
GObject必需在class_init函式裡使用g_signal_new註冊要提供外界觀察的訊號,之後物件操作者才能註冊
callback函式.首先我們在類別結構裡宣告預設的訊號處理函式和一個變數儲存訊號的ID(第27~28行),然後在
class_init函式裡先指定訊號處理函式的位置(第100行)然後再註冊一個訊號(第115~127行),比較特殊的
是,除了訊號處理函式,我們還額外註冊了一個accumulator函式(第120行),緊接著每一次的訊號calback函
式之後accumulator函式就會接著被呼叫一次.
1. /* maman-bar.h */
2. #ifndef __MAMAN_BAR_H__
3. #define __MAMAN_BAR_H__
4. #include <glib-object.h>
5. G_BEGIN_DECLS
6. #define MAMAN_TYPE_BAR (maman_bar_get_type ())
7. #define MAMAN_BAR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MAMAN_TYPE_BAR, MamanBar))
8. #define MAMAN_BAR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MAMAN_TYPE_BAR, MamanBarClass))
9. #define MAMAN_IS_BAR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MAMAN_TYPE_BAR))
頁尾在這裡