-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvalue.cpp
388 lines (333 loc) · 9.64 KB
/
value.cpp
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
** - Cedric GESTES <gestes@aldebaran-robotics.com>
**
** Copyright (C) 2013 Aldebaran Robotics
*/
#include <qi/log.hpp>
#include <qic/value.h>
#include "value_p.h"
#include "object_p.h"
qiLogCategory("qi.c");
//# GENERIC POD IMPL
template<typename T>
int qi_value_set_pod(qi_value_t *msg, T val) {
qi::AnyValue &gv = qi_value_cpp(msg);
try {
gv.set<T>(val);
return 1;
} catch (std::runtime_error &) {}
return 0;
}
template<typename T>
int qi_value_get_pod(qi_value_t *msg, T *result) {
qi::AnyValue &gv = qi_value_cpp(msg);
try {
*result = gv.to<T>();
return 1;
} catch (std::runtime_error &) {
}
return 0;
}
template<typename T>
T qi_value_get_pod_default(qi_value_t *msg, T defvalue) {
qi::AnyValue &gv = qi_value_cpp(msg);
try {
return gv.to<T>();
} catch (std::runtime_error &) {
}
return defvalue;
}
#ifdef __cplusplus
extern "C"
{
#endif
qi_value_t* qi_value_create(const char *signature)
{
qi::AnyValue* v;
if (!signature || !strcmp(signature, ""))
v = new qi::AnyValue();
else {
//TODO: check signature correctness
qi::TypeInterface *type = qi::TypeInterface::fromSignature(signature);
v = new qi::AnyValue(type);
}
return (qi_value_t*)v;
}
void qi_value_destroy(qi_value_t* val)
{
qi::AnyValue *v = &qi_value_cpp(val);
delete v;
}
int qi_value_reset(qi_value_t* value, const char *signature)
{
qi::AnyValue *v = &qi_value_cpp(value);
if (!signature || !strcmp(signature, ""))
v->reset();
else {
//TODO: check signature correctness
qi::TypeInterface *type = qi::TypeInterface::fromSignature(signature);
v->reset(type);
}
return 1;
}
void qi_value_swap(qi_value_t* dest, qi_value_t* src)
{
qi::AnyValue *s = &qi_value_cpp(src);
qi::AnyValue *d = &qi_value_cpp(dest);
s->swap(*d);
}
qi_value_t* qi_value_copy(qi_value_t* src)
{
qi::AnyValue *s = &qi_value_cpp(src);
qi::AnyValue *d = new qi::AnyValue;
*d = *s;
return (qi_value_t*)d;
}
qi_value_kind_t qi_value_get_kind(qi_value_t* value) {
if (!value)
return QI_VALUE_KIND_UNKNOWN;
qi::AnyValue &val = qi_value_cpp(value);
return (qi_value_kind_t)(val.kind());
}
const char* qi_value_get_signature(qi_value_t* value, int resolveDynamics)
{
qi::AnyValue &val = qi_value_cpp(value);
return qi::os::strdup(val.signature(!!resolveDynamics).toString().c_str());
}
//# UINT64
int qi_value_set_uint64(qi_value_t* container, unsigned long long value)
{ return qi_value_set_pod<unsigned long long>(container, value); }
int qi_value_get_uint64(qi_value_t* container, unsigned long long *result)
{ return qi_value_get_pod<unsigned long long>(container, result); }
unsigned long long qi_value_get_uint64_default(qi_value_t *container, unsigned long long defvalue)
{ return qi_value_get_pod_default<unsigned long long>(container, defvalue); }
//# INT64
int qi_value_set_int64(qi_value_t* container, long long value)
{ return qi_value_set_pod<long long>(container, value); }
int qi_value_get_int64(qi_value_t* container, long long *result)
{ return qi_value_get_pod<long long>(container, result); }
long long qi_value_get_int64_default(qi_value_t *container, long long defvalue)
{ return qi_value_get_pod_default<long long>(container, defvalue); }
//# FLOAT
int qi_value_set_float(qi_value_t* container, float value)
{ return qi_value_set_pod<float>(container, value); }
int qi_value_get_float(qi_value_t* container, float *result)
{ return qi_value_get_pod<float>(container, result); }
float qi_value_get_float_default(qi_value_t *container, float defvalue)
{ return qi_value_get_pod_default<float>(container, defvalue); }
//# DOUBLE
int qi_value_set_double(qi_value_t* container, double value)
{ return qi_value_set_pod<double>(container, value); }
int qi_value_get_double(qi_value_t* container, double *result)
{ return qi_value_get_pod<double>(container, result); }
double qi_value_get_double_default(qi_value_t *container, double defvalue)
{ return qi_value_get_pod_default<double>(container, defvalue); }
//# STRING
int qi_value_set_string(qi_value_t *container, const char *s)
{
qi::AnyValue &gv = qi_value_cpp(container);
try {
gv.setString(s);
return 1;
} catch (std::runtime_error &) {
}
return 0;
}
const char* qi_value_get_string(qi_value_t *msg)
{
if (!msg)
return 0;
qi::AnyValue &gv = qi_value_cpp(msg);
try {
return qi::os::strdup(gv.toString().c_str());
} catch (std::runtime_error &) {
}
return 0;
}
//# TUPLE
int qi_value_tuple_set(qi_value_t *msg, unsigned int idx, qi_value_t *value)
{
qi::AnyValue &container = qi_value_cpp(msg);
qi::AnyValue &val = qi_value_cpp(value);
try {
if (container.kind() != qi::TypeKind_Tuple || idx >= container.size()) {
return 0;
}
container[idx].set(val);
return 1;
} catch (std::runtime_error &) {}
return 0;
}
qi_value_t* qi_value_tuple_get(qi_value_t *msg, unsigned int idx) {
qi::AnyValue &container = qi_value_cpp(msg);
if (container.kind() != qi::TypeKind_Tuple || idx >= container.size()) {
return 0;
}
qi_value_t* ret = qi_value_create("");
qi::AnyValue &gv = qi_value_cpp(ret);
gv = container[idx];
return ret;
}
int qi_value_tuple_size(qi_value_t *msg)
{
qi::AnyValue &container = qi_value_cpp(msg);
if (container.kind() != qi::TypeKind_Tuple) {
return -1;
}
return container.size();
}
//# LIST
int qi_value_list_set(qi_value_t *msg, unsigned int idx, qi_value_t *value)
{
qi::AnyValue &container = qi_value_cpp(msg);
qi::AnyValue &val = qi_value_cpp(value);
if ((container.kind() != qi::TypeKind_VarArgs && container.kind() != qi::TypeKind_List) || idx >= container.size()) {
return 0;
}
try {
container[idx].set(val);
return 1;
} catch (std::runtime_error &e) {
qiLogError() << "Can't set list item at index " << idx << " :" << e.what();
}
return 0;
}
qi_value_t* qi_value_list_get(qi_value_t *msg, unsigned int idx)
{
qi::AnyValue &container = qi_value_cpp(msg);
if ((container.kind() != qi::TypeKind_VarArgs && container.kind() != qi::TypeKind_List) || idx >= container.size()) {
return 0;
}
qi_value_t* ret = qi_value_create("");
qi::AnyValue &gv = qi_value_cpp(ret);
gv = container[idx];
return ret;
}
int qi_value_list_push_back(qi_value_t *msg, qi_value_t*val)
{
qi::AnyValue &container = qi_value_cpp(msg);
qi::AnyValue &gval = qi_value_cpp(val);
if (container.kind() != qi::TypeKind_List && container.kind() != qi::TypeKind_VarArgs) {
return 0;
}
container.append(gval);
return 1;
}
int qi_value_list_size(qi_value_t *msg)
{
qi::AnyValue &container = qi_value_cpp(msg);
if (container.kind() != qi::TypeKind_List && container.kind() != qi::TypeKind_VarArgs) {
return -1;
}
return container.size();
}
//# OBJECT
qi_object_t* qi_value_get_object(qi_value_t* val)
{
qi::AnyValue &gv = qi_value_cpp(val);
try {
qi::AnyObject obj = gv.as<qi::AnyObject>();
return qi_object_create_from(obj);
} catch(std::runtime_error) {}
return 0;
}
int qi_value_set_object(qi_value_t* value, qi_object_t *o)
{
qi::AnyValue &gv = qi_value_cpp(value);
qi::AnyObject &obj = qi_object_cpp(o);
try {
gv.set<qi::AnyObject>(obj);
return 1;
} catch (std::runtime_error&) {}
return 0;
}
//# DYNAMIC
int qi_value_dynamic_set(qi_value_t *container, qi_value_t* value)
{
qi::AnyValue &gvcont = qi_value_cpp(container);
qi::AnyValue &gvval = qi_value_cpp(value);
try {
gvcont.setDynamic(gvval.asReference());
return 1;
} catch (std::runtime_error&) {}
return 0;
}
qi_value_t* qi_value_dynamic_get(qi_value_t *container)
{
qi::AnyValue &gv = qi_value_cpp(container);
try {
qi::AnyReference gvp = gv.content();
qi_value_t *ret = qi_value_create("");
qi::AnyValue &val = qi_value_cpp(ret);
val = gvp.clone();
return ret;
} catch (std::runtime_error&) {}
return 0;
}
//# MAP
unsigned int qi_value_map_size(qi_value_t *msg)
{
qi::AnyValue &container = qi_value_cpp(msg);
if (container.kind() != qi::TypeKind_Map) {
return -1;
}
return container.size();
}
int qi_value_map_set(qi_value_t *msg, qi_value_t *key, qi_value_t *value)
{
qi::AnyValue &container = qi_value_cpp(msg);
qi::AnyValue &k = qi_value_cpp(key);
qi::AnyValue &val = qi_value_cpp(value);
if (container.kind() != qi::TypeKind_Map) {
return 0;
}
try {
container[k].set(val);
return 1;
} catch (std::runtime_error &) {}
return 0;
}
qi_value_t* qi_value_map_get(qi_value_t *msg, qi_value_t *key)
{
qi::AnyValue &container = qi_value_cpp(msg);
qi::AnyValue &k = qi_value_cpp(key);
qi::AnyReference r;
if (container.kind() != qi::TypeKind_Map) {
return 0;
}
try {
r = container[k.asReference()];
qi_value_t* ret = qi_value_create("");
qi::AnyValue &gv = qi_value_cpp(ret);
gv = r;
return ret;
} catch (std::runtime_error &) {}
return 0;
}
qi_value_t* qi_value_map_keys(qi_value_t *msg)
{
qi::AnyValue &container = qi_value_cpp(msg);
std::map<qi::AnyReference, qi::AnyReference>::iterator it;
std::map<qi::AnyReference, qi::AnyReference> m = container.asMapValuePtr();
qi::TypeInterface* listType = qi::makeListType(((qi::MapTypeInterface*)container.type())->keyType());
qi::AnyValue* ar = new qi::AnyValue(listType);
//construct a list<k>
for (it = m.begin(); it != m.end(); ++it) {
ar->append(it->first);
}
return (qi_value_t*)(ar);
}
//# RAW
int qi_value_raw_set(qi_value_t* value, const char* data, int size){
return 0;
}
int qi_value_raw_get(qi_value_t* value, const char**data, int *size) {
return 0;
}
qi_type_t* qi_value_get_type(qi_value_t* value) {
qi::AnyValue &val = qi_value_cpp(value);
return (qi_type_t*)val.type();
}
#ifdef __cplusplus
}
#endif