X-Git-Url: http://git.draconx.ca/gitweb/gob-dx.git/blobdiff_plain/3b10bbd3a88d6e16146414d91d06bb2f36347bfc..HEAD:/examples/my-person.gob diff --git a/examples/my-person.gob b/examples/my-person.gob index 06a0aaa..ba7be44 100644 --- a/examples/my-person.gob +++ b/examples/my-person.gob @@ -1,8 +1,8 @@ -requires 0.92.1 - -/* This will work with an older version (0.92.1 specifically), if you want - * to see a version with automatic argument<->datamember linking, automatic - * initialization and destruction, look at my-person2.gob */ +/* + * This file is a basic example of how a .gob file would be constructed, + * how to add data members, properties and methods + */ +requires 2.0.0 %{ #include @@ -10,58 +10,76 @@ requires 0.92.1 #include "my-person-private.h" %} -class My:Person from Gtk:Object { - public char *name; - public long dob; /* date of birth as a time_t */ - public long dod; /* date of death as a time_t */ - - private int rounds_in_shotgun; /* number of rounds in our shotgun */ +class My:Person from G:Object { + /* the name of the person */ + private char *name = {g_strdup("Nobody")} + destroywith g_free; + property STRING name + (nick = "Name", + blurb = "Name of the person", + default_value = "Nobody", + /* Export get/set functions for this property */ + export, + /* link to the data memeber 'name' */ + link); - argument POINTER (type char *) name - get { - /* note that strdup handles NULL correctly */ - ARG = g_strdup(self->name); - } - set { - /* note that strdup handles NULL correctly */ - g_free(self->name); - self->name = g_strdup(ARG); - }; + /* date of birth as a time_t */ + private long dob = 0; + property LONG dob + (nick = "Date of birth", + blurb = "Date of birth of the person", + minimum = 0, + maximum = LONG_MAX, + default_value = 0, + export) + /* We could use 'link' as above, but the code below + * shows how to do this without linking */ + set { + self->_priv->dob = g_value_get_long (VAL); + } + get { + g_value_set_long (VAL, self->_priv->dob); + }; - argument LONG dob get { ARG = self->dob; } set { self->dob = ARG; }; - argument LONG dod get { ARG = self->dod; } set { self->dod = ARG; }; + /* date of death as a time_t */ + private long dod = 0; + property LONG dod + (nick = "Date of death", + blurb = "Date of death of the person", + minimum = 0, + maximum = LONG_MAX, + default_value = 0, + export, + link); - init(self) - { - self->name = g_strdup("Nobody"); - self->dob = 0; - self->dod = 0; - - /* initially we have no rounds in the shotgun */ - self->_priv->rounds_in_shotgun = 0; - } + /* number of rounds in our shotgun */ + private int rounds_in_shotgun = 0; /* when the person gets born, sends out a signal, the caller of the signal should provide the date of birth */ signal last NONE (LONG) void - birth(self, long dob) + birth (self, long dob) { - self->dob = dob; + g_object_set (G_OBJECT (self), + "dob", dob, + NULL); } /* when the person dies, sends out a signal, the caller of the signal should provide the date of death */ signal last NONE (LONG) void - death(self, long dod) + death (self, long dod) { - self->dod = dod; + g_object_set (G_OBJECT (self), + "dod", dod, + NULL); } public void - load_shotgun(self) + load_shotgun (self) { /* add a round to our shotgun */ self->_priv->rounds_in_shotgun++; @@ -69,10 +87,10 @@ class My:Person from Gtk:Object { public void - shoot_oneself_in_the_head(self) + shoot_oneself_in_the_head (self) { - if(self->_priv->rounds_in_shotgun==0) { - g_warning("No rounds in the shotgun!"); + if (self->_priv->rounds_in_shotgun == 0) { + g_warning ("No rounds in the shotgun!"); return; } @@ -80,15 +98,12 @@ class My:Person from Gtk:Object { self->_priv->rounds_in_shotgun--; /* death is imminent if we shoot oneself in the head */ - death(self, (long)time(NULL)); + self_death (self, (long)time (NULL)); } - /* override the destroy signal where we destroy data we need to free */ - override (Gtk:Object) - void - destroy (Gtk:Object *self (check null type)) + public GObject * + new (void) { - g_free(MY_PERSON(self)->name); - PARENT_HANDLER(self); + return (GObject *)GET_NEW; } }