]> git.draconx.ca Git - gob-dx.git/blob - examples/my-person.gob
Release 0.91.0
[gob-dx.git] / examples / my-person.gob
1 %{
2 #include <time.h>
3 #include "my-person.h"
4 %}
5
6 class My:Person from Gtk:Object {
7         public char *name;
8         public long dob; /* date of birth as a time_t */
9         public long dod; /* date of death as a time_t */
10
11         private int rounds_in_shotgun; /* number of rounds in our shotgun */
12
13         argument POINTER name
14         get {
15                 if(self->name)
16                         ARG = g_strdup(self->name);
17                 else
18                         ARG = NULL;
19         }
20         set {
21                 g_free(self->name);
22                 if(ARG)
23                         self->name = g_strdup(ARG);
24                 else
25                         self->name = NULL;
26         };
27
28         argument LONG dob get { ARG = self->dob; } set { self->dob = ARG; };
29         argument LONG dod get { ARG = self->dod; } set { self->dod = ARG; };
30
31         init(person)
32         {
33                 person->name = g_strdup("Nobody");
34                 person->dob = 0;
35                 person->dod = 0;
36                 
37                 /* initially we have no rounds in the shotgun */
38                 person->_priv->rounds_in_shotgun = 0;
39         }
40
41         /* when the person gets born, sends out a signal, the caller
42            of the signal should provide the date of birth */
43         signal last NONE (LONG)
44         void
45         birth(self, long dob)
46         {
47                 self->dob = dob;
48         }
49         
50         /* when the person dies, sends out a signal, the caller
51            of the signal should provide the date of death */
52         signal last NONE (LONG)
53         void
54         death(self, long dod)
55         {
56                 self->dod = dod;
57         }
58
59         public
60         void
61         load_shotgun(self)
62         {
63                 /* add a round to our shotgun */
64                 self->_priv->rounds_in_shotgun++;
65         }
66
67         public
68         void
69         shoot_oneself_in_the_head(self)
70         {
71                 if(self->_priv->rounds_in_shotgun==0) {
72                         g_warning("No rounds in the shotgun!");
73                         return;
74                 }
75                 
76                 /* one round was fired */
77                 self->_priv->rounds_in_shotgun--;
78
79                 /* death is imminent if we shoot oneself in the head */
80                 death(self,(long)time(NULL));
81         }
82 }