Repository navigation
Expand file tree
/
Copy pathtint_without_macro.c
More file actions
213 lines (176 loc) · 5.28 KB
/
Copy pathtint_without_macro.c
File metadata and controls
213 lines (176 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "tint.h"
#include "tdouble.h"
#include "tcomparable.h"
#define PROP_INT 1
static GParamSpec *int_property = NULL;
struct _TInt {
TNumber parent;
int value;
};
static void t_comparable_interface_init (TComparableInterface *iface);
/*G_DEFINE_TYPE_WITH_CODE (TInt, t_int, T_TYPE_NUMBER,*/
/* G_IMPLEMENT_INTERFACE (T_TYPE_COMPARABLE, t_comparable_interface_init))*/
static void
t_int_class_init (TIntClass *class);
static void
t_int_init (TInt *d);
GType
t_int_get_type (void)
{
static GType type = 0;
if (type == 0) {
const GTypeInfo info = {
sizeof (TIntClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) t_int_class_init, /* class_init */
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (TInt),
0, /* n_preallocs */
(GInstanceInitFunc) t_int_init, /* instance_init */
NULL /* value table */
};
const GInterfaceInfo comparable_info = {
(GInterfaceInitFunc) t_comparable_interface_init, /* interface_init */
NULL, /* interface_finalize */
NULL /* interface_data */
};
type = g_type_register_static (T_TYPE_NUMBER, "TInt", &info, 0);
g_type_add_interface_static (type, T_TYPE_COMPARABLE, &comparable_info);
}
return type;
}
static int
t_int_comparable_cmp (TComparable *self, TComparable *other) {
g_return_val_if_fail (T_IS_INT (self), -2);
g_return_val_if_fail (T_IS_INT (other) || T_IS_DOUBLE (other), -2);
int i;
double s, o;
g_object_get (self, "value", &i, NULL);
s = (double) i;
if (T_IS_INT (other)) {
g_object_get (other, "value", &i, NULL);
o = (double) i;
} else
g_object_get (other, "value", &o, NULL);
if (s > o)
return 1;
else if (s == o)
return 0;
else if (s < o)
return -1;
else /* This can't happen. */
return -2;
}
static void
t_comparable_interface_init (TComparableInterface *iface) {
iface->cmp = t_int_comparable_cmp;
}
static void
t_int_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) {
TInt *self = T_INT (object);
if (property_id == PROP_INT) {
self->value = g_value_get_int (value);
} else
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
static void
t_int_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) {
TInt *self = T_INT (object);
if (property_id == PROP_INT)
g_value_set_int (value, self->value);
else
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
static void
t_int_init (TInt *d) {
}
/* arithmetic operator */
/* These operators create a new instance and return a pointer to it. */
#define t_int_binary_op(op) \
int i; \
double d; \
if (T_IS_INT (other)) { \
g_object_get (T_INT (other), "value", &i, NULL); \
return T_NUMBER (t_int_new_with_value (T_INT(self)->value op i)); \
} else { \
g_object_get (T_DOUBLE (other), "value", &d, NULL); \
return T_NUMBER (t_int_new_with_value (T_INT(self)->value op (int) d)); \
}
static TNumber *
t_int_add (TNumber *self, TNumber *other) {
g_return_val_if_fail (T_IS_INT (self), NULL);
t_int_binary_op (+)
}
static TNumber *
t_int_sub (TNumber *self, TNumber *other) {
g_return_val_if_fail (T_IS_INT (self), NULL);
t_int_binary_op (-)
}
static TNumber *
t_int_mul (TNumber *self, TNumber *other) {
g_return_val_if_fail (T_IS_INT (self), NULL);
t_int_binary_op (*)
}
static TNumber *
t_int_div (TNumber *self, TNumber *other) {
g_return_val_if_fail (T_IS_INT (self), NULL);
int i;
double d;
if (T_IS_INT (other)) {
g_object_get (T_INT (other), "value", &i, NULL);
if (i == 0) {
g_signal_emit_by_name (self, "div-by-zero");
return NULL;
} else
return T_NUMBER (t_int_new_with_value (T_INT(self)->value / i));
} else {
g_object_get (T_DOUBLE (other), "value", &d, NULL);
if (d == 0) {
g_signal_emit_by_name (self, "div-by-zero");
return NULL;
} else
return T_NUMBER (t_int_new_with_value (T_INT(self)->value / (int) d));
}
}
static TNumber *
t_int_uminus (TNumber *self) {
g_return_val_if_fail (T_IS_INT (self), NULL);
return T_NUMBER (t_int_new_with_value (- T_INT(self)->value));
}
static char *
t_int_to_s (TNumber *self) {
g_return_val_if_fail (T_IS_INT (self), NULL);
int i;
g_object_get (T_INT (self), "value", &i, NULL);
return g_strdup_printf ("%d", i);
}
static void
t_int_class_init (TIntClass *class) {
TNumberClass *tnumber_class = T_NUMBER_CLASS (class);
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
/* override virtual functions */
tnumber_class->add = t_int_add;
tnumber_class->sub = t_int_sub;
tnumber_class->mul = t_int_mul;
tnumber_class->div = t_int_div;
tnumber_class->uminus = t_int_uminus;
tnumber_class->to_s = t_int_to_s;
gobject_class->set_property = t_int_set_property;
gobject_class->get_property = t_int_get_property;
int_property = g_param_spec_int ("value", "val", "Integer value", G_MININT, G_MAXINT, 0, G_PARAM_READWRITE);
g_object_class_install_property (gobject_class, PROP_INT, int_property);
}
TInt *
t_int_new_with_value (int value) {
TInt *d;
d = g_object_new (T_TYPE_INT, "value", value, NULL);
return d;
}
TInt *
t_int_new (void) {
TInt *d;
d = g_object_new (T_TYPE_INT, NULL);
return d;
}