]> git.draconx.ca Git - gob-dx.git/blob - examples/my-person.gob
Replace gnulib patch with new common helper macro.
[gob-dx.git] / examples / my-person.gob
1 /*
2  * This file is a basic example of how a .gob file would be constructed,
3  * how to add data members, properties and methods
4  */
5 requires 2.0.0
6
7 %{
8 #include <time.h>
9 #include "my-person.h"
10 #include "my-person-private.h"
11 %}
12
13 class My:Person from G:Object {
14         /* the name of the person */
15         private char *name = {g_strdup("Nobody")}
16                 destroywith g_free;
17         property STRING name
18                 (nick = "Name",
19                  blurb = "Name of the person",
20                  default_value = "Nobody",
21                  /* Export get/set functions for this property */
22                  export,
23                  /* link to the data memeber 'name' */
24                  link);
25
26         /* date of birth as a time_t */
27         private long dob = 0;
28         property LONG dob
29                 (nick = "Date of birth",
30                  blurb = "Date of birth of the person",
31                  minimum = 0,
32                  maximum = LONG_MAX,
33                  default_value = 0,
34                  export)
35                 /* We could use 'link' as above, but the code below
36                  * shows how to do this without linking */
37                 set {
38                         self->_priv->dob = g_value_get_long (VAL);
39                 }
40                 get {
41                         g_value_set_long (VAL, self->_priv->dob);
42                 };
43
44         /* date of death as a time_t */
45         private long dod = 0;
46         property LONG dod
47                 (nick = "Date of death",
48                  blurb = "Date of death of the person",
49                  minimum = 0,
50                  maximum = LONG_MAX,
51                  default_value = 0,
52                  export,
53                  link);
54
55         /* number of rounds in our shotgun */
56         private int rounds_in_shotgun = 0;
57
58         /* when the person gets born, sends out a signal, the caller
59            of the signal should provide the date of birth */
60         signal last NONE (LONG)
61         void
62         birth (self, long dob)
63         {
64                 g_object_set (G_OBJECT (self),
65                               "dob", dob,
66                               NULL);
67         }
68         
69         /* when the person dies, sends out a signal, the caller
70            of the signal should provide the date of death */
71         signal last NONE (LONG)
72         void
73         death (self, long dod)
74         {
75                 g_object_set (G_OBJECT (self),
76                               "dod", dod,
77                               NULL);
78         }
79
80         public
81         void
82         load_shotgun (self)
83         {
84                 /* add a round to our shotgun */
85                 self->_priv->rounds_in_shotgun++;
86         }
87
88         public
89         void
90         shoot_oneself_in_the_head (self)
91         {
92                 if (self->_priv->rounds_in_shotgun == 0) {
93                         g_warning ("No rounds in the shotgun!");
94                         return;
95                 }
96                 
97                 /* one round was fired */
98                 self->_priv->rounds_in_shotgun--;
99
100                 /* death is imminent if we shoot oneself in the head */
101                 self_death (self, (long)time (NULL));
102         }
103
104         public GObject *
105         new (void)
106         {
107                 return (GObject *)GET_NEW;
108         }
109 }