]> git.draconx.ca Git - gob-dx.git/blob - examples/my-person.gob
d895a629562f12c40b0c9a3b4625f13dfdc8ad5e
[gob-dx.git] / examples / my-person.gob
1 requires 0.92.1
2
3 /* This will work with an older version (0.92.1 specifically), if you want
4  * to see a version with automatic argument<->datamember linking, automatic
5  * initialization and destruction, look at my-person2.gob */
6
7 %{
8 #include <time.h>
9 #include "my-person.h"
10 #include "my-person-private.h"
11 %}
12
13 class My:Person from Gtk:Object {
14         public char *name;
15         public long dob; /* date of birth as a time_t */
16         public long dod; /* date of death as a time_t */
17
18         private int rounds_in_shotgun; /* number of rounds in our shotgun */
19         
20         argument POINTER (type char *) name
21         get {
22                 /* note that g_strdup handles NULL correctly */
23                 ARG = g_strdup(self->name);
24         }
25         set {
26                 /* note that g_free and g_strdup handles NULL correctly */
27                 g_free(self->name);
28                 self->name = g_strdup(ARG);
29         };
30
31         argument LONG dob get { ARG = self->dob; } set { self->dob = ARG; };
32         argument LONG dod get { ARG = self->dod; } set { self->dod = ARG; };
33
34         init(self)
35         {
36                 self->name = g_strdup("Nobody");
37                 self->dob = 0;
38                 self->dod = 0;
39                 
40                 /* initially we have no rounds in the shotgun */
41                 self->_priv->rounds_in_shotgun = 0;
42         }
43
44         /* when the person gets born, sends out a signal, the caller
45            of the signal should provide the date of birth */
46         signal last NONE (LONG)
47         void
48         birth(self, long dob)
49         {
50                 self->dob = dob;
51         }
52         
53         /* when the person dies, sends out a signal, the caller
54            of the signal should provide the date of death */
55         signal last NONE (LONG)
56         void
57         death(self, long dod)
58         {
59                 self->dod = dod;
60         }
61
62         public
63         void
64         load_shotgun(self)
65         {
66                 /* add a round to our shotgun */
67                 self->_priv->rounds_in_shotgun++;
68         }
69
70         public
71         void
72         shoot_oneself_in_the_head(self)
73         {
74                 if(self->_priv->rounds_in_shotgun==0) {
75                         g_warning("No rounds in the shotgun!");
76                         return;
77                 }
78                 
79                 /* one round was fired */
80                 self->_priv->rounds_in_shotgun--;
81
82                 /* death is imminent if we shoot oneself in the head */
83                 death(self, (long)time(NULL));
84         }
85
86         /* override the destroy signal where we destroy data we need to free */
87         override (Gtk:Object)
88         void
89         destroy (Gtk:Object *self (check null type))
90         {
91                 g_free(MY_PERSON(self)->name);
92                 PARENT_HANDLER(self);
93         }
94
95         public GtkObject *
96         new(void)
97         {
98                 return (GtkObject *)GET_NEW;
99         }
100 }