-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvalue.c
239 lines (216 loc) · 5.21 KB
/
value.c
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <stdlib.h>
#include "oi.h"
@generateheader
@trait Value
{
PropertyType type;
union {
float as_float;
double as_double;
int as_int;
const char *as_string;
void *as_pointer;
var as_oi; /* in code.. using the oi directly
* if possible - likely will be more
* elegant..
*/
} value;
} PACKED;
static void scrub ()
{
switch (this->type)
{
case OI_PTYPE_OI:
/**/
this->value.as_oi@ref:dec();
break;
case OI_PTYPE_STRING:
oi_strfree (this->value.as_string);
break;
default:
break;
}
this->type = OI_PTYPE_INT;
this->value.as_int = 0;
}
PropertyType type ()
{
if (!self)
return 0;
return this->type;
}
void set_double (double value)
{
self@value:scrub();
this->value.as_double = value;
this->type = OI_PTYPE_DOUBLE;
}
void set_float (float value)
{
self@value:scrub();
this->value.as_float = value;
this->type = OI_PTYPE_FLOAT;
}
void set_int (int value)
{
self@value:scrub();
this->value.as_int = value;
this->type = OI_PTYPE_INT;
}
void set_string (const char *value)
{
self@value:scrub();
this->type = OI_PTYPE_STRING;
if (value)
this->value.as_string = oi_strdup (value);
else
this->value.as_string = NULL;
}
void set_oi (var value)
{
self@value:scrub();
if (value@trait:get (VALUE)) /* if we're setting something that already is
* a value..
*/
{
switch (value@value:type())
{
case OI_PTYPE_INT:/**/
self@value:set_int (value@value:get_int());
break;
case OI_PTYPE_FLOAT:/**/
self@value:set_float (value@value:get_float());
break;
case OI_PTYPE_DOUBLE:/**/
self@value:set_double (value@value:get_double());
break;
case OI_PTYPE_STRING:/**/
self@value:set_string (value@value:get_string());
break;
case OI_PTYPE_POINTER:/**/
self@value:set_pointer (value@value:get_pointer ());
break;
case OI_PTYPE_OI:/**/
self@value:set_oi (value@value:get_oi ());
break;
}
return;
}
this->type = OI_PTYPE_OI;
if (value)
this->value.as_oi = value@ref:inc();
else
this->value.as_oi = NULL;
}
void set_pointer (void *value)
{
self@value:scrub();
this->type = OI_PTYPE_POINTER;
if (value)
this->value.as_pointer = value;
else
this->value.as_pointer = NULL;
}
double get_double ()
{
if (!self)
return 0.0;
switch (this->type)
{
case OI_PTYPE_INT: return this->value.as_int;
case OI_PTYPE_FLOAT: return this->value.as_float;
case OI_PTYPE_DOUBLE: return this->value.as_double;
case OI_PTYPE_STRING: return atof(this->value.as_string);
default: return 0.0;
}
}
float get_float ()
{
if (!self)
return 0.0;
switch (this->type)
{
case OI_PTYPE_INT: return this->value.as_int;
case OI_PTYPE_FLOAT: return this->value.as_float;
case OI_PTYPE_DOUBLE: return this->value.as_double;
case OI_PTYPE_STRING: return atof(this->value.as_string);
default: return 0.0;
}
}
int get_int ()
{
if (!self)
return 0;
switch (this->type)
{
case OI_PTYPE_INT: return this->value.as_int;
case OI_PTYPE_FLOAT: return this->value.as_float;
case OI_PTYPE_DOUBLE: return this->value.as_double;
case OI_PTYPE_STRING: return atof(this->value.as_string);
default: return 0.0;
}
}
const char *get_string ()
{
/* can possibly get away without having a lock on it.. */
static char ret_buf[128][32];
static int sretbuf_no = 0;
int retbuf_no = sretbuf_no++;
if (sretbuf_no>=128)
sretbuf_no=0;
if (!self)
return "";
switch (this->type)
{
case OI_PTYPE_STRING: return this->value.as_string;
case OI_PTYPE_INT:
sprintf(ret_buf[retbuf_no], "%i", this->value.as_int);
return ret_buf[retbuf_no];
case OI_PTYPE_FLOAT:
sprintf(ret_buf[retbuf_no], "%f", this->value.as_float);
return ret_buf[retbuf_no];
case OI_PTYPE_DOUBLE:
sprintf(ret_buf[retbuf_no], "%f", this->value.as_double);
return ret_buf[retbuf_no];
case OI_PTYPE_POINTER:
sprintf(ret_buf[retbuf_no], "<%p>", this->value.as_pointer);
case OI_PTYPE_OI:
if (this->value.as_oi@trait:get(STRING))
return (this->value.as_oi@string:get());
sprintf(ret_buf[retbuf_no], "<oi%p>", this->value.as_oi);
return ret_buf[retbuf_no];
default: return "()";
}
}
void *get_pointer ()
{
if (!self)
return NULL;
switch (this->type)
{
case OI_PTYPE_POINTER: return this->value.as_pointer;
default: return NULL;
}
}
var get_oi ()
{
if (!self)
return NULL;
switch (this->type)
{
case OI_PTYPE_OI: return (this->value.as_oi@ref:inc());
break;
default:
return (self@ref:inc());
}
}
static void init ()
{
this->type = OI_PTYPE_INT;
this->value.as_int = 0;
}
static void destroy ()
{
self@value:scrub ();
}
@end