2009-12-12

GObject - First Vala Programming


svn checkout http://gobject-learing.googlecode.com/svn/trunk/ gobject-learing-read-only


為達到最佳效果,請使用以上指令下載範例碼並與以下投影片一起服用。

Coppy from wiki:Vala "Vala is a programming language that was created with the goal of bringing modern language features to C, without additional runtime requirements and with little overhead, by targeting the GObject object system. It was developed by Jürg Billeter and Raffaele Sandrini. The syntax borrows heavily from C#. Rather than being compiled directly to assembler or to an intermediate language, Vala is compiled to C which is then compiled with the platform's standard C compiler."

According above description, any program written by Vala could translate to standard C code. So I add this topic between GObject tutorial. Because it really save our time for development.

I rewrote the sample previous GObject example by using Vala. You will find those boring redundant code are no longer need to self-written.


/* 6-1 - maman-bar.vala */
public class MamanBar : Object {
public int a;
public int b;

public MamanBar(){
a = 1; b = 2;
}
public void dumpall() {
stdout.printf ("a = %d b = %d\n",a,b);
}

}



#> valac -o maman-bar-vala maman-bar.vala main.vala
#> ./maman-bar-vala
a = 1 b = 2


I would like to demo how to connect vala and c together. We use the same C code from previous sample.

int main (int argc, char *argv[])
{
/* this function should be executed first. Before everything */
g_type_init();

/* Create our object */
MamanBar *bar = maman_bar_new();
maman_bar_dumpall( bar );

return 0;
}

The only difference are compile commands.

#> valac -C -H maman-bar.h maman-bar.vala
#> cc `pkg-config --cflags glib-2.0 gtk+-2.0` -c -o main.o main.c
#> cc `pkg-config --cflags glib-2.0 gtk+-2.0` -c -o maman-bar.o maman-bar.c
#> cc -o maman-bar-c main.o maman-bar.o `pkg-config --libs glib-2.0 gtk+-2.0`
#> ./maman-bar-c
a = 1 b = 2

2009-12-11

GObject - Virtual Function


svn checkout http://gobject-learing.googlecode.com/svn/trunk/ gobject-learing-read-only


為達到最佳效果,請使用以上指令下載範例碼並與以下投影片一起服用。





= Member Function =
: the functions which have a self parameter

If you ever write python code to implement about CLASS. The class member function may look like bellow:


def f(self):
return 'hello world'


You can find out the first parameter always be class itself. And the function will use 'self' to access the member value. The same principle we use in GObject programming.
We always use thiz (instead this, because C++ syntax) for first parameter in each member functions. Whether public function or private function.

= Public Member Function =
: What's different between "Just Function" and "Public Member Function"?

Same as C++, in GObject programming, all the functions define in the class structure header with first self parameter are public member functions.
In example 5-1 we use a public member function to replace what we done in main.c, dump all member value.


void maman_bar_dump_all_value( MamanBar* thiz )
{
printf(" bar->a = (%d) bar->b = (%d)\n", thiz->a, thiz->b );
}


= Private Member Function =
: just simple rule, static functions (with first self parameter) are private

It's too simple. I believe we don't need to explain this idea.


= Member Function with Virtual Feature =




/* 5-2 */
/* maman-bar.h */
struct _MamanBarClass {
GObjectClass parent;
/* class members */

void (* incAll) ( MamanBar* );
};

/* maman-bar.c */
static void maman_bar_class_init ( MamanBarClass* class)
{
class->incAll = maman_bar_inc_all;
}


In this step, we need to explain what's different between MamanBar and MamanBarClass. We use g_object_new to allocate a memory size from heap, and g_object_new invoke function maman_bar_init to initialize this memory region. But different from MamanBar, MamanBarClass should only exists one copy during one time period.


maman_bar_dump_all_value( bar );
MAMAN_BAR_GET_CLASS( bar ) -> incAll(bar);
maman_bar_dump_all_value( bar );

In above sample code, you can see we use MAMAN_BAR_GET_CLASS to access CLASS structure, and use it to invoke the class member functions.
The whole idea in virtual function is child class will use it's member function replace the parent class's member function pointer.

= Virtual Function =

Base on example 4-2. We implement sub-bar class and replace the parent's class function pointer.


/* 5-3 */
/* sub-bar.c */
static void sub_bar_class_init ( SubBarClass* class)
{
MamanBarClass* parent_class = class;
parent_class->incAll = sub_bar_inc_all;
}


After constructor invoke (base_init and class_init ...), we can invoke child function by using parent's fp. It's simple and easy to use.


/* main.c */
SubBar *subbar = g_object_new (SUB_BAR_TYPE, NULL);
sub_bar_dump_all_value( subbar );
MAMAN_BAR_GET_CLASS( subbar ) -> incAll( subbar );
sub_bar_dump_all_value( subbar );

2009-11-30

Gobject - Inherit (Chinese)

svn checkout http://gobject-learing.googlecode.com/svn/trunk/ gobject-learing-read-only 

Of course, you can download the source by using above command.
I found there's some different between GLIB v2.0 and v2.2.
So I rewrote the example code. and separated the "STEPS" more detail.

I believe it will be helpfull for someone(?) who wanna study GObject in dummy.


* BTW. i give up Google Doc in this time. because it can't not upload odf file. and i don't want to twit the slide every time :( .

2009-11-25

OpenEmbedded Build Process

I Just draw a diagram to explain the OpenEmbedded build process for internal project. If it's useful for someone else, just take it.





2009-11-23

OOP in C - Before GObject

svn checkout http://gobject-learing.googlecode.com/svn/trunk/ gobject-learing-read-only 


為達到最佳效果,請使用以上指令下載範例碼並與以下投影片一起服用。

we need to try write something before GObject programming. It let us understand more for "WHY USING GOBJECT". I write some example file, you can try to rewrite them after you read the slide and sample code.

Enjoy It.





2009-11-22

OOP in C - Virtual Function

svn checkout http://gobject-learing.googlecode.com/svn/trunk/ gobject-learing-read-only 


為達到最佳效果,請使用以上指令下載範例碼並與以下投影片一起服用。

OO的Member Function 其實是可以使用Function Pointer及Structure來描述 (廢話)

struct A 
{
int a;
FP  method_a;
};


再加上一點技巧, 就可以完成Virtual Function的實作 :)
Enjoy It.





2009-11-21

OOP in C - Inherit


svn checkout http://gobject-learing.googlecode.com/svn/trunk/ gobject-learing-read-only


為達到最佳效果,請使用以上指令下載範例碼並與以下投影片一起服用。

其實說穿了,整個概念就是以下的程式碼。


/* 1-4.h */

struct A{

int a;

int b;

};

struct B{

struct A parent;

int c;

};






2009-11-20

another half

well, let's start to sort out what i done before.
GObject learning for first. After that i plan to post out the real-mode programming tutorial.