1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
3 | // See LICENSE.txt for details. |
---|
4 | |
---|
5 | #include "extvalue.h" |
---|
6 | #include <frams/param/param.h> |
---|
7 | #include "sstringutils.h" |
---|
8 | #include <ctype.h> |
---|
9 | #include <frams/vm/classes/collectionobj.h> |
---|
10 | #include <frams/vm/classes/3dobject.h> |
---|
11 | #include <frams/vm/classes/genoobj.h> |
---|
12 | #include <common/nonstd_math.h> |
---|
13 | #include <common/Convert.h> |
---|
14 | #include <climits> |
---|
15 | #include <errno.h> |
---|
16 | |
---|
17 | #ifndef NO_BARRIER |
---|
18 | #include <frams/simul/barrier.h> |
---|
19 | #include <common/threads.h> |
---|
20 | #endif |
---|
21 | |
---|
22 | #ifdef MULTITHREADED |
---|
23 | #include <pthread.h> |
---|
24 | //this lock only protects against ref.counter corruption caused by concurrent reads. |
---|
25 | //read/write conficts and nonatomicity are handled by BarrierObject (at least in theory ;-)) |
---|
26 | static pthread_mutex_t extobject_ref_lock = PTHREAD_MUTEX_INITIALIZER; |
---|
27 | #define REF_LOCK pthread_mutex_lock(&extobject_ref_lock) |
---|
28 | #define REF_UNLOCK pthread_mutex_unlock(&extobject_ref_lock) |
---|
29 | #else |
---|
30 | #define REF_LOCK |
---|
31 | #define REF_UNLOCK |
---|
32 | #endif |
---|
33 | |
---|
34 | void ExtObject::incref() const |
---|
35 | { |
---|
36 | if (subtype & 1) |
---|
37 | { |
---|
38 | REF_LOCK; |
---|
39 | dbobject->refcount++; |
---|
40 | REF_UNLOCK; |
---|
41 | } |
---|
42 | } |
---|
43 | |
---|
44 | void ExtObject::decref() const |
---|
45 | { |
---|
46 | if (subtype & 1) |
---|
47 | { |
---|
48 | REF_LOCK; |
---|
49 | bool destroy = !--dbobject->refcount; |
---|
50 | REF_UNLOCK; |
---|
51 | //another thread can now access the object while we are deleting it |
---|
52 | //but this is not a bug since we only guarantee read/read safety |
---|
53 | if (destroy) delete dbobject; |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | bool ExtObject::operator==(const ExtObject& src) const |
---|
58 | { |
---|
59 | if (object != src.object) return false; |
---|
60 | const char* n1 = interfaceName(); |
---|
61 | const char* n2 = src.interfaceName(); |
---|
62 | return (n1 == n2) || (strcmp(n1, n2) == 0); |
---|
63 | } |
---|
64 | |
---|
65 | bool ExtObject::makeUnique() |
---|
66 | { |
---|
67 | if (!(subtype & 1)) return false; |
---|
68 | if (dbobject->refcount == 1) return false; |
---|
69 | VectorObject* v = VectorObject::fromObject(*this, false); |
---|
70 | if (v) |
---|
71 | { |
---|
72 | VectorObject* n = new VectorObject; |
---|
73 | n->data.setSize(n->data.size()); |
---|
74 | for (int i = 0; i < v->data.size(); i++) |
---|
75 | { |
---|
76 | ExtValue *x = (ExtValue*)v->data(i); |
---|
77 | n->data.set(i, x ? new ExtValue(*x) : NULL); |
---|
78 | } |
---|
79 | operator=(n->makeObject()); |
---|
80 | return true; |
---|
81 | } |
---|
82 | return false; |
---|
83 | } |
---|
84 | |
---|
85 | void* ExtObject::getTarget(const char* classname, bool through_barrier, bool warn) const |
---|
86 | { |
---|
87 | if (!strcmp(interfaceName(), classname)) |
---|
88 | return getTarget(); |
---|
89 | #ifndef NO_BARRIER |
---|
90 | if (through_barrier) |
---|
91 | { |
---|
92 | BarrierObject *bo = BarrierObject::fromObject(*this); |
---|
93 | if (bo) |
---|
94 | return bo->getSourceObject().getTarget(classname, true, warn); |
---|
95 | } |
---|
96 | #endif |
---|
97 | |
---|
98 | if (warn) |
---|
99 | { |
---|
100 | logPrintf("ExtValue", "getObjectTarget", LOG_WARN, "%s object expected, %s found", classname, interfaceName()); |
---|
101 | } |
---|
102 | |
---|
103 | return NULL; |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | SString ExtObject::toString() const |
---|
108 | { |
---|
109 | if (isEmpty()) return SString("null"); |
---|
110 | Param tmp_param; |
---|
111 | ParamInterface *p = getParamInterface(tmp_param); |
---|
112 | int tostr = p->findId("toString"); |
---|
113 | if (tostr >= 0) |
---|
114 | { |
---|
115 | return SString(p->getString(tostr)); |
---|
116 | } |
---|
117 | else |
---|
118 | { |
---|
119 | SString tmp("<"); |
---|
120 | tmp += p->getName(); |
---|
121 | tmp += SString::sprintf(" object at %p>", object ? object : paraminterface); |
---|
122 | return tmp; |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | THREAD_LOCAL_DEF(ExtObject::Serialization, ExtObject_serialization); |
---|
127 | |
---|
128 | void ExtObject::Serialization::begin() |
---|
129 | { |
---|
130 | if (level == 0) |
---|
131 | refs.clear(); |
---|
132 | level++; |
---|
133 | } |
---|
134 | |
---|
135 | int ExtObject::Serialization::add(const ExtObject &o) |
---|
136 | { |
---|
137 | if (o.isEmpty()) return -1; |
---|
138 | for (int i = 0; i < (int)refs.size(); i++) |
---|
139 | { |
---|
140 | ExtObject& r = refs[i]; |
---|
141 | if (r == o) return i; |
---|
142 | } |
---|
143 | refs.push_back(o); |
---|
144 | return -1; |
---|
145 | } |
---|
146 | |
---|
147 | void ExtObject::Serialization::replace(const ExtObject& o, const ExtObject& other) |
---|
148 | { |
---|
149 | if (o.isEmpty()) return; |
---|
150 | for (int i = 0; i < (int)refs.size(); i++) |
---|
151 | { |
---|
152 | ExtObject& r = refs[i]; |
---|
153 | if (r == o) |
---|
154 | { |
---|
155 | r = other; |
---|
156 | return; |
---|
157 | } |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | void ExtObject::Serialization::remove(const ExtObject& o) |
---|
162 | { |
---|
163 | if (o.isEmpty()) return; |
---|
164 | for (int i = 0; i < (int)refs.size(); i++) |
---|
165 | { |
---|
166 | ExtObject& r = refs[i]; |
---|
167 | if (o == r) refs.erase(refs.begin() + i); |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | const ExtObject* ExtObject::Serialization::get(int ref) |
---|
172 | { |
---|
173 | if (ref < 0) return NULL; |
---|
174 | if (ref >= (int)refs.size()) return NULL; |
---|
175 | return &refs[ref]; |
---|
176 | } |
---|
177 | |
---|
178 | void ExtObject::Serialization::end() |
---|
179 | { |
---|
180 | level--; |
---|
181 | if (level == 0) |
---|
182 | refs.clear(); |
---|
183 | } |
---|
184 | |
---|
185 | SString ExtObject::serialize_inner(SerializationFormat format) const |
---|
186 | { |
---|
187 | int ref = tlsGetRef(ExtObject_serialization).add(*this); |
---|
188 | SString ret; |
---|
189 | |
---|
190 | if (ref >= 0) |
---|
191 | { |
---|
192 | switch(format) |
---|
193 | { |
---|
194 | case NativeSerialization: return SString::sprintf("^%d", ref); |
---|
195 | case JSONSerialization: return SString("null"); |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | if (isEmpty()) return SString("null"); |
---|
200 | { |
---|
201 | VectorObject *vec = VectorObject::fromObject(*this, false); |
---|
202 | if (vec) |
---|
203 | { ret=vec->serialize(format); goto finally; } |
---|
204 | } |
---|
205 | { |
---|
206 | DictionaryObject *dic = DictionaryObject::fromObject(*this, false); |
---|
207 | if (dic) |
---|
208 | { ret=dic->serialize(format); goto finally; } |
---|
209 | } |
---|
210 | { |
---|
211 | Param tmp_param; |
---|
212 | ParamInterface *p = getParamInterface(tmp_param); |
---|
213 | int m = p->findId("toVector"); |
---|
214 | if (m < 0) |
---|
215 | m = p->findId("toDictionary"); |
---|
216 | if (m >= 0) |
---|
217 | { |
---|
218 | ExtObject o(p->getObject(m)); |
---|
219 | switch(format) |
---|
220 | { |
---|
221 | case NativeSerialization: ret=SString(interfaceName())+o.serialize(format); break; |
---|
222 | case JSONSerialization: ret=SString::sprintf("{\"class\":\"%s\",\"data\":%s}",interfaceName(),o.serialize(format).c_str()); break; |
---|
223 | } |
---|
224 | goto finally; |
---|
225 | } |
---|
226 | m = p->findId("toString"); |
---|
227 | if (m >= 0) |
---|
228 | { |
---|
229 | SString str = p->getString(m); |
---|
230 | sstringQuote(str); |
---|
231 | switch(format) |
---|
232 | { |
---|
233 | case NativeSerialization: ret=SString(interfaceName())+"\"" +str+"\""; break; |
---|
234 | case JSONSerialization: ret=SString::sprintf("{\"class\":\"%s\",\"data\":\"%s\"}",interfaceName(),str.c_str()); break; |
---|
235 | } |
---|
236 | goto finally; |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | tlsGetRef(ExtObject_serialization).remove(*this);//undo nonserializable reference |
---|
241 | switch(format) |
---|
242 | { |
---|
243 | case NativeSerialization: return SString(interfaceName())+SString::sprintf("<%p>",object ? object : paraminterface); break; |
---|
244 | case JSONSerialization: return SString::sprintf("{\"class\":\"%s\"}",interfaceName()); break; |
---|
245 | } |
---|
246 | |
---|
247 | finally: // not 100% "finally", the case of nonserializable reference (directly above) returns directly without going through finally |
---|
248 | |
---|
249 | switch(format) |
---|
250 | { |
---|
251 | case JSONSerialization: |
---|
252 | tlsGetRef(ExtObject_serialization).remove(*this);//JSON only tracks recursion, does not track reuse |
---|
253 | break; |
---|
254 | } |
---|
255 | |
---|
256 | return ret; |
---|
257 | } |
---|
258 | |
---|
259 | SString ExtObject::serialize(SerializationFormat format) const |
---|
260 | { |
---|
261 | tlsGetRef(ExtObject_serialization).begin(); |
---|
262 | SString ret = serialize_inner(format); |
---|
263 | tlsGetRef(ExtObject_serialization).end(); |
---|
264 | return ret; |
---|
265 | } |
---|
266 | |
---|
267 | /////////////////////////////////////// |
---|
268 | |
---|
269 | SString ExtValue::typeDescription() const |
---|
270 | { |
---|
271 | switch (type) |
---|
272 | { |
---|
273 | case TInt: return SString("int"); |
---|
274 | case TDouble: return SString("float"); |
---|
275 | case TString: return SString("string"); |
---|
276 | case TUnknown: return SString("null"); |
---|
277 | case TInvalid: return SString("invalid"); |
---|
278 | case TObj: return getObject().isEmpty() ? SString("null") : SString(getObject().interfaceName()); |
---|
279 | } |
---|
280 | return SString::empty(); |
---|
281 | } |
---|
282 | |
---|
283 | SString ExtValue::typeAndValue() const |
---|
284 | { |
---|
285 | SString msg = typeDescription(); |
---|
286 | switch (type) |
---|
287 | { |
---|
288 | case TInt: case TDouble: case TString: case TObj: |
---|
289 | msg += " '"; |
---|
290 | msg += getString(); |
---|
291 | msg += "'"; |
---|
292 | default:; |
---|
293 | } |
---|
294 | return msg; |
---|
295 | } |
---|
296 | |
---|
297 | void *ExtValue::getObjectTarget(const char* classname, bool warn) const |
---|
298 | { |
---|
299 | if (type != TObj) |
---|
300 | { |
---|
301 | if (warn) |
---|
302 | { |
---|
303 | SString tmp = getString(); |
---|
304 | if (tmp.len() > 30) tmp = tmp.substr(0, 30) + "..."; |
---|
305 | if (type == TString) tmp = SString("\"") + tmp + SString("\""); |
---|
306 | logPrintf("ExtValue", "getObjectTarget", LOG_WARN, "%s object expected, %s found", classname, tmp.c_str()); |
---|
307 | } |
---|
308 | return NULL; |
---|
309 | } |
---|
310 | |
---|
311 | return getObject().getTarget(classname, true, warn); |
---|
312 | } |
---|
313 | |
---|
314 | void ExtValue::set(const ExtValue& src) |
---|
315 | { |
---|
316 | switch (src.type) |
---|
317 | { |
---|
318 | case TString: sets(src.sdata()); break; |
---|
319 | case TInt: seti(src.idata()); break; |
---|
320 | case TDouble: setd(src.ddata()); break; |
---|
321 | case TObj: seto(src.odata()); break; |
---|
322 | default:type = src.type; break; |
---|
323 | } |
---|
324 | } |
---|
325 | |
---|
326 | void ExtValue::setEmpty() |
---|
327 | { |
---|
328 | switch (type) |
---|
329 | { |
---|
330 | #ifdef EXTVALUEUNION |
---|
331 | case TString: sdata().~SString(); break; |
---|
332 | case TObj: odata().~ExtObject(); break; |
---|
333 | #else |
---|
334 | case TString: delete s; break; |
---|
335 | case TObj: delete o; break; |
---|
336 | #endif |
---|
337 | default:; |
---|
338 | } |
---|
339 | type = TUnknown; |
---|
340 | } |
---|
341 | |
---|
342 | static ExtValue::CompareResult longsign(paInt x) |
---|
343 | { |
---|
344 | if (x < 0) return ExtValue::ResultLower; |
---|
345 | if (x > 0) return ExtValue::ResultHigher; |
---|
346 | return ExtValue::ResultEqual; |
---|
347 | } |
---|
348 | |
---|
349 | static ExtValue::CompareResult compareNull(const ExtValue& v) |
---|
350 | { |
---|
351 | if (v.isNull()) return ExtValue::ResultEqualUnordered; |
---|
352 | if ((v.getType() == TInt) && (v.getInt() == 0)) return ExtValue::ResultUnequal_RelaxedEqual; |
---|
353 | return ExtValue::ResultUnequal_RelaxedUnequal; //comparing anything else with null is valid but null is neither higher nor lower than numbers or strings |
---|
354 | } |
---|
355 | |
---|
356 | static ExtValue::CompareResult compareFloat(double a, double b) |
---|
357 | { |
---|
358 | double t = a - b; |
---|
359 | if (t < 0) return ExtValue::ResultLower; |
---|
360 | else if (t > 0) return ExtValue::ResultHigher; |
---|
361 | return ExtValue::ResultEqual; |
---|
362 | } |
---|
363 | |
---|
364 | static ExtValue::CompareResult compareString(const SString &a, const SString &b) |
---|
365 | { |
---|
366 | const char* s1 = a.c_str(); |
---|
367 | const char* s2 = b.c_str(); |
---|
368 | return longsign(strcmp(s1, s2)); |
---|
369 | } |
---|
370 | |
---|
371 | ExtValue::CompareResult ExtValue::compare(const ExtValue& src) const |
---|
372 | { |
---|
373 | if (isNull()) |
---|
374 | return compareNull(src); |
---|
375 | else if (src.isNull()) |
---|
376 | return compareNull(*this); |
---|
377 | switch (type) |
---|
378 | { |
---|
379 | |
---|
380 | case TInt: |
---|
381 | |
---|
382 | if (src.getType() == TInt) |
---|
383 | { |
---|
384 | paInt t = src.getInt(); |
---|
385 | if (idata() > 0) |
---|
386 | { |
---|
387 | if (t > 0) return longsign(idata() - t); else return ResultHigher; |
---|
388 | } |
---|
389 | else |
---|
390 | { |
---|
391 | if (t <= 0) return longsign(idata() - t); else return ResultLower; |
---|
392 | } |
---|
393 | } |
---|
394 | else if (src.getType() == TDouble) |
---|
395 | return compareFloat((double)idata(), src.getDouble()); |
---|
396 | else |
---|
397 | return ResultMismatch;//comparing numbers with other things is invalid |
---|
398 | break; |
---|
399 | |
---|
400 | case TDouble: |
---|
401 | if ((src.getType() == TDouble) || (src.getType() == TInt)) |
---|
402 | return compareFloat(getDouble(), src.getDouble()); |
---|
403 | else |
---|
404 | return ResultMismatch; |
---|
405 | break; |
---|
406 | |
---|
407 | case TString: |
---|
408 | if (src.getType() == TString) |
---|
409 | return compareString(sdata(), src.getString()); |
---|
410 | else |
---|
411 | return ResultMismatch; |
---|
412 | break; |
---|
413 | |
---|
414 | case TObj: |
---|
415 | { |
---|
416 | if (src.type == TObj) |
---|
417 | return odata() == src.odata() ? ResultEqualUnordered : ResultUnequal_RelaxedUnequal; |
---|
418 | if ((src.type == TInt) && (src.getInt() == 0)) |
---|
419 | return ResultMismatch_RelaxedUnequal; |
---|
420 | return ResultMismatch; |
---|
421 | } |
---|
422 | default:; |
---|
423 | } |
---|
424 | return ResultMismatch; |
---|
425 | } |
---|
426 | |
---|
427 | const char* ExtValue::cmp_op_names[] = { "==", "!=", ">=", "<=", ">", "<", "~=", "!~", NULL }; |
---|
428 | |
---|
429 | int ExtValue::interpretCompare(CmpOperator op, CompareResult result, CmpContext *context) |
---|
430 | { |
---|
431 | CompareResult error_threshold = ResultUnequal_RelaxedEqual;//error when ResultUnequal_RelaxedEqual or higher (not comparable) |
---|
432 | int ret = 0; |
---|
433 | switch (op) |
---|
434 | { |
---|
435 | case CmpEQ: ret = (result == ResultEqual) || (result == ResultEqualUnordered); error_threshold = ResultMismatch_RelaxedUnequal; break; |
---|
436 | case CmpNE: ret = !((result == ResultEqual) || (result == ResultEqualUnordered)); error_threshold = ResultMismatch_RelaxedUnequal; break; |
---|
437 | case CmpGT: ret = (result == ResultHigher); error_threshold = ResultEqualUnordered; break; |
---|
438 | case CmpGE: ret = (result == ResultEqual) || (result == ResultHigher); error_threshold = ResultEqualUnordered; break; |
---|
439 | case CmpLT: ret = (result == ResultLower); error_threshold = ResultEqualUnordered; break; |
---|
440 | case CmpLE: ret = (result == ResultEqual) || (result == ResultLower); error_threshold = ResultEqualUnordered; break; |
---|
441 | case CmpREQ: ret = (result == ResultEqual) || (result == ResultEqualUnordered) || (result == ResultUnequal_RelaxedEqual); error_threshold = ResultMismatch; break; |
---|
442 | case CmpRNE: ret = !((result == ResultEqual) || (result == ResultEqualUnordered) || (result == ResultUnequal_RelaxedEqual)); error_threshold = ResultMismatch; break; |
---|
443 | default:; |
---|
444 | } |
---|
445 | if (result >= error_threshold) |
---|
446 | { |
---|
447 | SString msg = "Type mismatch while comparing"; |
---|
448 | if (context) |
---|
449 | { |
---|
450 | if (context->v1 && context->v2) |
---|
451 | msg += SString::sprintf(": %s %s %s", |
---|
452 | context->v1->typeAndValue().c_str(), |
---|
453 | cmp_op_names[op - CmpFIRST], |
---|
454 | context->v2->typeAndValue().c_str()); |
---|
455 | } |
---|
456 | logPrintf("ExtValue", "interpretCompare", LOG_ERROR, "%s", msg.c_str()); |
---|
457 | ret = -1; |
---|
458 | } |
---|
459 | return ret; |
---|
460 | } |
---|
461 | |
---|
462 | int ExtValue::operator==(const ExtValue& src) const |
---|
463 | { |
---|
464 | if (type != src.type) return 0; |
---|
465 | switch (type) |
---|
466 | { |
---|
467 | case TInt: return idata() == src.idata(); |
---|
468 | case TDouble: return ddata() == src.ddata(); |
---|
469 | case TString: return sdata() == src.sdata(); |
---|
470 | case TObj: return odata() == src.odata(); |
---|
471 | default:; |
---|
472 | } |
---|
473 | return 1; |
---|
474 | } |
---|
475 | |
---|
476 | void ExtValue::operator+=(const ExtValue& src) |
---|
477 | { |
---|
478 | // return = ok, break = fail |
---|
479 | switch (type) |
---|
480 | { |
---|
481 | case TInt: |
---|
482 | switch (src.getType()) |
---|
483 | { |
---|
484 | case TDouble: |
---|
485 | setDouble(double(getInt()) + src.getDouble()); |
---|
486 | return; |
---|
487 | case TString: |
---|
488 | break; |
---|
489 | default: |
---|
490 | idata() += src.getInt(); |
---|
491 | return; |
---|
492 | } |
---|
493 | break; |
---|
494 | case TDouble: |
---|
495 | switch (src.getType()) |
---|
496 | { |
---|
497 | case TString: |
---|
498 | break; |
---|
499 | default: |
---|
500 | ddata() += src.getDouble(); |
---|
501 | return; |
---|
502 | } |
---|
503 | break; |
---|
504 | case TString: sdata() += src.getString(); return; |
---|
505 | case TObj: |
---|
506 | { |
---|
507 | VectorObject *vec = VectorObject::fromObject(getObject(), false); |
---|
508 | VectorObject *vec2 = VectorObject::fromObject(src.getObject(), false); |
---|
509 | if (vec && vec2) |
---|
510 | { |
---|
511 | for (int i = 0; i < vec2->data.size(); i++) |
---|
512 | { |
---|
513 | ExtValue *s = (ExtValue*)vec2->data(i); |
---|
514 | ExtValue *d = s ? new ExtValue(*s) : NULL; |
---|
515 | vec->data += d; |
---|
516 | } |
---|
517 | return; |
---|
518 | } |
---|
519 | } |
---|
520 | //NO break; |
---|
521 | default:; |
---|
522 | } |
---|
523 | logPrintf("ExtValue", "add", LOG_ERROR, "Can't add %s to %s", src.typeAndValue().c_str(), typeAndValue().c_str()); |
---|
524 | } |
---|
525 | |
---|
526 | void ExtValue::operator-=(const ExtValue& src) |
---|
527 | { |
---|
528 | // return = ok, break = fail |
---|
529 | switch (type) |
---|
530 | { |
---|
531 | case TInt: |
---|
532 | switch (src.getType()) |
---|
533 | { |
---|
534 | case TInt: |
---|
535 | idata() -= src.getInt(); |
---|
536 | return; |
---|
537 | case TDouble: |
---|
538 | setDouble(double(getInt()) - src.getDouble()); |
---|
539 | return; |
---|
540 | default:; |
---|
541 | } |
---|
542 | break; |
---|
543 | case TDouble: |
---|
544 | switch (src.getType()) |
---|
545 | { |
---|
546 | case TDouble: |
---|
547 | case TInt: |
---|
548 | ddata() -= src.getDouble(); |
---|
549 | return; |
---|
550 | default:; |
---|
551 | } |
---|
552 | break; |
---|
553 | default:; |
---|
554 | } |
---|
555 | logPrintf("ExtValue", "subtract", LOG_ERROR, "Can't subtract %s from %s", src.typeAndValue().c_str(), typeAndValue().c_str()); |
---|
556 | } |
---|
557 | |
---|
558 | void ExtValue::operator*=(const ExtValue& src) |
---|
559 | { |
---|
560 | // return = ok, break = fail |
---|
561 | switch (type) |
---|
562 | { |
---|
563 | case TInt: |
---|
564 | switch (src.getType()) |
---|
565 | { |
---|
566 | case TInt: |
---|
567 | idata() *= src.getInt(); |
---|
568 | return; |
---|
569 | case TDouble: |
---|
570 | setDouble(double(getInt())*src.getDouble()); |
---|
571 | return; |
---|
572 | default:; |
---|
573 | } |
---|
574 | break; |
---|
575 | case TDouble: |
---|
576 | switch (src.getType()) |
---|
577 | { |
---|
578 | case TInt: |
---|
579 | case TDouble: |
---|
580 | ddata() *= src.getDouble(); |
---|
581 | return; |
---|
582 | default:; |
---|
583 | } |
---|
584 | break; |
---|
585 | case TString: |
---|
586 | switch (src.getType()) |
---|
587 | { |
---|
588 | case TInt: case TDouble: |
---|
589 | { |
---|
590 | SString t; |
---|
591 | for (int n = src.getInt(); n > 0; n--) |
---|
592 | t += getString(); |
---|
593 | setString(t); |
---|
594 | return; |
---|
595 | } |
---|
596 | default:; |
---|
597 | } |
---|
598 | break; |
---|
599 | case TObj: |
---|
600 | { |
---|
601 | VectorObject *vec = VectorObject::fromObject(getObject(), false); |
---|
602 | if (vec) |
---|
603 | { |
---|
604 | int n = src.getInt(); |
---|
605 | int orig_size = vec->data.size(); |
---|
606 | if (n <= 0) |
---|
607 | { |
---|
608 | vec->clear(); return; |
---|
609 | } |
---|
610 | for (; n > 1; n--) |
---|
611 | { |
---|
612 | for (int i = 0; i < orig_size; i++) |
---|
613 | { |
---|
614 | ExtValue *s = (ExtValue*)vec->data(i); |
---|
615 | ExtValue *d = s ? new ExtValue(*s) : NULL; |
---|
616 | vec->data += d; |
---|
617 | } |
---|
618 | } |
---|
619 | return; |
---|
620 | } |
---|
621 | } |
---|
622 | //NO break; |
---|
623 | default:; |
---|
624 | } |
---|
625 | logPrintf("ExtValue", "multiply", LOG_WARN, "Can't multiply %s by %s", typeAndValue().c_str(), src.typeAndValue().c_str()); |
---|
626 | } |
---|
627 | |
---|
628 | /*#include "fpu_control.h" |
---|
629 | #include <signal.h> |
---|
630 | |
---|
631 | static int fpuexception; |
---|
632 | void mathhandler(int sig) |
---|
633 | { |
---|
634 | printf("fpu exception!\n"); |
---|
635 | fpuexception=1; |
---|
636 | signal(SIGFPE,SIG_IGN); |
---|
637 | } */ |
---|
638 | |
---|
639 | void ExtValue::divInt(paInt a) |
---|
640 | { |
---|
641 | if (a) |
---|
642 | idata() /= a; |
---|
643 | else |
---|
644 | { |
---|
645 | logPrintf("ExtValue", "divide", LOG_CRITICAL, "Division by zero: %d/0", idata()); |
---|
646 | setInvalid(); |
---|
647 | } |
---|
648 | } |
---|
649 | |
---|
650 | void ExtValue::divDouble(double a) |
---|
651 | { |
---|
652 | if (a == 0.0) |
---|
653 | { |
---|
654 | logPrintf("ExtValue", "divide", LOG_CRITICAL, "Division by zero: %s/0.0", getString().c_str()); |
---|
655 | setInvalid(); |
---|
656 | } |
---|
657 | else |
---|
658 | { |
---|
659 | fpExceptDisable(); |
---|
660 | double tmp = getDouble() / a; |
---|
661 | if (!finite(tmp)) |
---|
662 | { |
---|
663 | logPrintf("ExtValue", "divide", LOG_CRITICAL, "Overflow %s/%g", getString().c_str(), a); setInvalid(); |
---|
664 | } |
---|
665 | else |
---|
666 | setDouble(tmp); |
---|
667 | // niby dobrze ale lepiej byloby to robic bardziej systematycznie a nie tylko w dzieleniu? |
---|
668 | //if (isnan(ddata())) //http://www.digitalmars.com/d/archives/c++/Traping_divide_by_zero_5728.html |
---|
669 | // { logPrintf("ExtValue","divide",LOG_ERROR,"not-a-number",(const char*)getString()); setInvalid(); } |
---|
670 | fpExceptEnable(); |
---|
671 | } |
---|
672 | } |
---|
673 | |
---|
674 | void ExtValue::operator/=(const ExtValue& src) |
---|
675 | { |
---|
676 | switch (type) |
---|
677 | { |
---|
678 | case TInt: |
---|
679 | switch (src.getType()) |
---|
680 | { |
---|
681 | case TInt: |
---|
682 | divInt(src.idata()); |
---|
683 | return; |
---|
684 | case TDouble: |
---|
685 | divDouble(src.ddata()); |
---|
686 | return; |
---|
687 | default:; |
---|
688 | } |
---|
689 | break; |
---|
690 | |
---|
691 | case TDouble: |
---|
692 | switch (src.getType()) |
---|
693 | { |
---|
694 | case TInt: |
---|
695 | divDouble(src.getDouble()); |
---|
696 | return; |
---|
697 | case TDouble: |
---|
698 | divDouble(src.ddata()); |
---|
699 | return; |
---|
700 | default:; |
---|
701 | } |
---|
702 | break; |
---|
703 | |
---|
704 | default:; |
---|
705 | } |
---|
706 | logPrintf("ExtValue", "divide", LOG_ERROR, "Can't divide %s by %s", typeAndValue().c_str(), src.typeAndValue().c_str()); |
---|
707 | } |
---|
708 | |
---|
709 | SString ExtValue::format(SString& fmt, const ExtValue **values, int count) |
---|
710 | { |
---|
711 | SString ret; |
---|
712 | // "..........%.........%..........%........" |
---|
713 | // ^_cur ^_next |
---|
714 | // ^^^^^^^^^^___sub |
---|
715 | // |
---|
716 | // "..........%.........%..........%........" |
---|
717 | // ^-cur ^-next |
---|
718 | // ^^^^^^^^^^___sub |
---|
719 | const char* begin = fmt.c_str(), *end = begin + fmt.len(), *curr = begin; |
---|
720 | int type = 0; |
---|
721 | |
---|
722 | class Args |
---|
723 | { |
---|
724 | const ExtValue **values; |
---|
725 | int count; |
---|
726 | int arg; |
---|
727 | public: |
---|
728 | Args(const ExtValue **v, int c) :values(v), count(c), arg(0) {} |
---|
729 | bool finished() { return arg >= count; } |
---|
730 | const ExtValue *getNext() { const ExtValue *ret = NULL; if ((arg < count) && values[arg]) ret = values[arg]; arg++; return ret; } |
---|
731 | }; |
---|
732 | Args args(values, count); |
---|
733 | |
---|
734 | while (curr < end) |
---|
735 | { |
---|
736 | const char* next = strchr(curr, '%'); |
---|
737 | if (!next) next = end; else if ((next == curr) && (curr > begin)) |
---|
738 | { |
---|
739 | next = strchr(next + 1, '%'); if (!next) next = end; |
---|
740 | } |
---|
741 | type = 0; |
---|
742 | if (curr > begin) |
---|
743 | { |
---|
744 | type = 0; |
---|
745 | for (const char* t = curr; t < next; t++) |
---|
746 | switch (*t) |
---|
747 | { |
---|
748 | case 'd': case 'x': case 'X': case 'u': case 'p': case 'c': type = 'd'; t = next; break; |
---|
749 | case 'f': case 'g': case 'e': type = 'f'; t = next; break; |
---|
750 | case 's': type = 's'; t = next; break; |
---|
751 | case 't': case 'T': type = *t; t = next; break; |
---|
752 | case '%': if (t > begin) { type = *t; t = next; } break; |
---|
753 | } |
---|
754 | } |
---|
755 | if (curr > begin) curr--; |
---|
756 | const ExtValue *a; |
---|
757 | if (args.finished() && (type != 0) && (type != '%')) |
---|
758 | { |
---|
759 | ret += fmt.substr((int)(curr - begin)); |
---|
760 | break; |
---|
761 | } |
---|
762 | SString sub = fmt.substr((int)(curr - begin), (int)(next - curr)); |
---|
763 | switch (type) |
---|
764 | { |
---|
765 | case 'd': a = args.getNext(); ret += SString::sprintf(sub.c_str(), a ? a->getInt() : 0); break; |
---|
766 | case 'f': a = args.getNext(); ret += SString::sprintf(sub.c_str(), a ? a->getDouble() : 0); break; |
---|
767 | case 's': {a = args.getNext(); SString tmp; if (a) tmp = a->getString(); ret += SString::sprintf(sub.c_str(), tmp.c_str()); } break; |
---|
768 | case 't': case 'T': |
---|
769 | { |
---|
770 | a = args.getNext(); |
---|
771 | time_t ti = a ? a->getInt() : 0; |
---|
772 | struct tm tm = Convert::localtime(ti); |
---|
773 | SString timtxt; |
---|
774 | if (type == 'T') |
---|
775 | timtxt = SString::sprintf("%04d-%02d-%02d %02d:%02d:%02d", 1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); |
---|
776 | else |
---|
777 | timtxt = Convert::asctime(tm).c_str(); |
---|
778 | ret += timtxt; |
---|
779 | ret += sub.substr(2); |
---|
780 | } |
---|
781 | break; |
---|
782 | case '%': ret += '%'; ret += sub.substr(2); break; |
---|
783 | case 0: ret += sub; break; |
---|
784 | } |
---|
785 | curr = next + 1; |
---|
786 | } |
---|
787 | return ret; |
---|
788 | } |
---|
789 | |
---|
790 | |
---|
791 | void ExtValue::operator%=(const ExtValue& src) |
---|
792 | { |
---|
793 | switch (type) |
---|
794 | { |
---|
795 | case TInt: idata() = idata() % src.getInt(); break; |
---|
796 | case TDouble: ddata() = fmod(ddata(), src.getDouble()); break; |
---|
797 | |
---|
798 | case TString: |
---|
799 | { |
---|
800 | VectorObject *vec = VectorObject::fromObject(src.getObject(), false); |
---|
801 | if (vec) |
---|
802 | sdata() = format(sdata(), (const ExtValue**)&vec->data.getref(0), vec->data.size()); |
---|
803 | else |
---|
804 | { |
---|
805 | const ExtValue *ptr = &src; sdata() = ExtValue::format(sdata(), &ptr, 1); |
---|
806 | } |
---|
807 | } |
---|
808 | break; |
---|
809 | |
---|
810 | case TObj: case TUnknown: case TInvalid: |
---|
811 | logPrintf("ExtValue", "modulo", LOG_WARN, "Can't apply modulo to %s", typeDescription().c_str()); |
---|
812 | |
---|
813 | default:; |
---|
814 | } |
---|
815 | } |
---|
816 | |
---|
817 | bool ExtValue::parseInt(const char* s, paInt &result, bool strict, bool error) |
---|
818 | { |
---|
819 | ExtValue tmp; |
---|
820 | const char* after = tmp.parseNumber(s, strict ? TInt : TUnknown); |
---|
821 | if ((after == NULL) || (after[0] != 0)) |
---|
822 | { |
---|
823 | if (error) |
---|
824 | logPrintf("ExtValue", "parseInt", LOG_ERROR, "Could not parse '%s'%s", s, strict ? " (strict)" : ""); |
---|
825 | return false; |
---|
826 | } |
---|
827 | result = tmp.getInt(); |
---|
828 | return true; |
---|
829 | } |
---|
830 | |
---|
831 | bool ExtValue::parseDouble(const char* s, double &result, bool error) |
---|
832 | { |
---|
833 | ExtValue tmp; |
---|
834 | const char* after = tmp.parseNumber(s, TDouble); |
---|
835 | if ((after == NULL) || (after[0] != 0)) |
---|
836 | { |
---|
837 | if (error) |
---|
838 | logPrintf("ExtValue", "parseDouble", LOG_ERROR, "Could not parse '%s'", s); |
---|
839 | return false; |
---|
840 | } |
---|
841 | result = tmp.getDouble(); |
---|
842 | return true; |
---|
843 | } |
---|
844 | |
---|
845 | paInt ExtValue::getInt(const char* s, bool strict) |
---|
846 | { |
---|
847 | paInt result; |
---|
848 | if (parseInt(s, result, strict, true)) |
---|
849 | return result; |
---|
850 | return 0; |
---|
851 | } |
---|
852 | |
---|
853 | double ExtValue::getDouble(const char* s) |
---|
854 | { |
---|
855 | double result; |
---|
856 | if (parseDouble(s, result, true)) |
---|
857 | return result; |
---|
858 | return 0; |
---|
859 | } |
---|
860 | |
---|
861 | paInt ExtValue::getInt() const |
---|
862 | { |
---|
863 | switch (type) |
---|
864 | { |
---|
865 | case TInt: return idata(); |
---|
866 | case TDouble: return (int)ddata(); |
---|
867 | case TString: return getInt(sdata().c_str()); |
---|
868 | case TObj: |
---|
869 | logPrintf("ExtValue", "getInt", LOG_WARN, "Getting integer value from object reference (%s)", getString().c_str()); |
---|
870 | return (paInt)(intptr_t)odata().param; |
---|
871 | default:; |
---|
872 | } |
---|
873 | return 0; |
---|
874 | } |
---|
875 | |
---|
876 | double ExtValue::getDouble() const |
---|
877 | { |
---|
878 | switch (type) |
---|
879 | { |
---|
880 | case TDouble: return ddata(); |
---|
881 | case TInt: return (double)idata(); |
---|
882 | case TString: return getDouble(sdata().c_str()); |
---|
883 | case TObj: |
---|
884 | logPrintf("ExtValue", "getDouble", LOG_WARN, "Getting floating point value from object reference (%s)", getString().c_str()); |
---|
885 | return (double)(intptr_t)odata().param; |
---|
886 | default:; |
---|
887 | } |
---|
888 | return 0.0; |
---|
889 | } |
---|
890 | |
---|
891 | SString ExtValue::getString() const |
---|
892 | { |
---|
893 | switch (type) |
---|
894 | { |
---|
895 | case TString: return sdata(); |
---|
896 | case TInt: return SString::valueOf(idata()); |
---|
897 | case TDouble: return SString::valueOf(ddata()); |
---|
898 | case TObj: return odata().toString(); |
---|
899 | case TInvalid: return SString("invalid"); |
---|
900 | default: return SString("null"); |
---|
901 | } |
---|
902 | } |
---|
903 | |
---|
904 | const SString* ExtValue::getStringPtr() const |
---|
905 | { |
---|
906 | if (type == TString) |
---|
907 | return &sdata(); |
---|
908 | return NULL; |
---|
909 | } |
---|
910 | |
---|
911 | SString ExtValue::serialize(SerializationFormat format) const |
---|
912 | { |
---|
913 | switch (type) |
---|
914 | { |
---|
915 | case TString: |
---|
916 | { |
---|
917 | SString q = sdata(); |
---|
918 | sstringQuote(q); |
---|
919 | return SString("\"") + q + SString("\""); |
---|
920 | } |
---|
921 | case TInt: |
---|
922 | return SString::valueOf(idata()); |
---|
923 | case TDouble: |
---|
924 | return SString::valueOf(ddata()); |
---|
925 | case TObj: |
---|
926 | return odata().serialize(format); |
---|
927 | case TInvalid: |
---|
928 | if (format==NativeSerialization) |
---|
929 | return SString("invalid"); |
---|
930 | // else null --v |
---|
931 | default: |
---|
932 | return SString("null"); |
---|
933 | } |
---|
934 | } |
---|
935 | |
---|
936 | /// returns the first character after the parsed number, or NULL if not a number |
---|
937 | /// @param strict_type = restrict the allowed return value (TUnknown = unrestricted) |
---|
938 | const char* ExtValue::parseNumber(const char* in, ExtPType strict_type) |
---|
939 | { |
---|
940 | char* after; |
---|
941 | if (in == NULL) return NULL; |
---|
942 | if (in[0] == 0) return NULL; |
---|
943 | while (isspace(*in)) in++; |
---|
944 | bool minus = (in[0] == '-'); |
---|
945 | bool plus = (in[0] == '+'); |
---|
946 | if (((in[0] == '0') && ((in[1] == 'x') || (in[1] == 'X'))) |
---|
947 | || (((minus || plus) && (in[1] == '0') && ((in[2] == 'x') || (in[2] == 'X'))))) |
---|
948 | { |
---|
949 | in += (minus || plus) ? 3 : 2; |
---|
950 | if (isspace(*in)) return NULL; |
---|
951 | errno = 0; |
---|
952 | unsigned long intvalue = strtoul(in, &after, 16); |
---|
953 | if ((after > in) && (errno == 0) && (intvalue <= 0xffffffff)) |
---|
954 | { |
---|
955 | if (strict_type == TDouble) |
---|
956 | setDouble(minus ? -(double)intvalue : (double)intvalue); |
---|
957 | else |
---|
958 | setInt(minus ? -intvalue : intvalue); |
---|
959 | return after; |
---|
960 | } |
---|
961 | else |
---|
962 | return NULL; |
---|
963 | } |
---|
964 | |
---|
965 | errno = 0; |
---|
966 | double fpvalue = strtod(in, &after); |
---|
967 | if ((after > in) && (errno == 0)) |
---|
968 | { |
---|
969 | if (strict_type != TDouble) |
---|
970 | { |
---|
971 | if ((memchr(in, '.', after - in) == NULL) && (memchr(in, 'e', after - in) == NULL) && (memchr(in, 'E', after - in) == NULL) // no "special" characters |
---|
972 | && (fpvalue == floor(fpvalue)) // value is integer |
---|
973 | && (fpvalue >= INT_MIN) && (fpvalue <= INT_MAX)) // within limits |
---|
974 | { |
---|
975 | setInt(fpvalue); |
---|
976 | return after; |
---|
977 | } |
---|
978 | else if (strict_type == TInt) |
---|
979 | return NULL; |
---|
980 | } |
---|
981 | setDouble(fpvalue); |
---|
982 | return after; |
---|
983 | } |
---|
984 | return NULL; |
---|
985 | } |
---|
986 | |
---|
987 | PtrListTempl<ParamInterface*> &ExtValue::getDeserializableClasses() |
---|
988 | { |
---|
989 | static PtrListTempl<ParamInterface*> classes; |
---|
990 | return classes; |
---|
991 | } |
---|
992 | |
---|
993 | ParamInterface *ExtValue::findDeserializableClass(const char* name) |
---|
994 | { |
---|
995 | FOREACH(ParamInterface*, cls, getDeserializableClasses()) |
---|
996 | if (!strcmp(cls->getName(), name)) |
---|
997 | return cls; |
---|
998 | return NULL; |
---|
999 | } |
---|
1000 | |
---|
1001 | static const char* skipWord(const char* in) |
---|
1002 | { |
---|
1003 | while (isalpha(*in) || (*in == '_')) |
---|
1004 | in++; |
---|
1005 | return in; |
---|
1006 | } |
---|
1007 | |
---|
1008 | //returns the first character after the parsed portion or NULL if invalid format |
---|
1009 | const char* ExtValue::deserialize_inner(const char* in) |
---|
1010 | { |
---|
1011 | const char* ret = parseNumber(in); |
---|
1012 | if (ret) |
---|
1013 | return ret; |
---|
1014 | else if (*in == '\"') |
---|
1015 | { |
---|
1016 | ret = skipQuoteString(in + 1, NULL); |
---|
1017 | SString s(in + 1, (int)(ret - (in + 1))); |
---|
1018 | sstringUnquote(s); |
---|
1019 | setString(s); |
---|
1020 | if (*ret == '\"') |
---|
1021 | return ret + 1; |
---|
1022 | else |
---|
1023 | { |
---|
1024 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Missing '\"' in string: '%s'", ret); |
---|
1025 | return NULL; |
---|
1026 | } |
---|
1027 | } |
---|
1028 | else if (*in == '[') |
---|
1029 | { |
---|
1030 | VectorObject *vec = new VectorObject; |
---|
1031 | ExtObject o(&VectorObject::par, vec); |
---|
1032 | tlsGetRef(ExtObject_serialization).add(o); |
---|
1033 | const char* p = in + 1; |
---|
1034 | ExtValue tmp; |
---|
1035 | while (*p) |
---|
1036 | { |
---|
1037 | if (*p == ']') { p++; break; } |
---|
1038 | ret = tmp.deserialize(p); |
---|
1039 | if (ret) |
---|
1040 | { |
---|
1041 | vec->data += new ExtValue(tmp); |
---|
1042 | p = ret; |
---|
1043 | if (*p == ',') p++; |
---|
1044 | else if (*p != ']') |
---|
1045 | { |
---|
1046 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Missing ',' in Vector: '%s'", p); |
---|
1047 | return NULL; |
---|
1048 | } |
---|
1049 | } |
---|
1050 | else |
---|
1051 | { |
---|
1052 | p = NULL; |
---|
1053 | break; |
---|
1054 | } |
---|
1055 | } |
---|
1056 | setObject(o); |
---|
1057 | return p; |
---|
1058 | } |
---|
1059 | else if (*in == '{') |
---|
1060 | { |
---|
1061 | DictionaryObject *dic = new DictionaryObject; |
---|
1062 | ExtObject o(&DictionaryObject::par, dic); |
---|
1063 | tlsGetRef(ExtObject_serialization).add(o); |
---|
1064 | const char* p = in + 1; |
---|
1065 | ExtValue args[2]/*={value,key}*/, dummy_ret; |
---|
1066 | while (*p) |
---|
1067 | { |
---|
1068 | if (*p == '}') { p++; break; } |
---|
1069 | ret = args[1].deserialize(p); |
---|
1070 | if ((!ret) || (args[1].getType() != TString)) { p = NULL; break; } |
---|
1071 | p = ret; |
---|
1072 | if (*p != ':') { logPrintf("ExtValue", "deserialize", LOG_ERROR, "Missing ':' in Dictionary: '%s'", p); p = NULL; break; } |
---|
1073 | p++; |
---|
1074 | ret = args[0].deserialize(p); |
---|
1075 | if (!ret) { p = NULL; break; } |
---|
1076 | p = ret; |
---|
1077 | dic->p_set(args, &dummy_ret); |
---|
1078 | if (*p == ',') p++; |
---|
1079 | else if (*p != '}') |
---|
1080 | { |
---|
1081 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Missing ',' in Dictionary: '%s'", p); |
---|
1082 | return NULL; |
---|
1083 | } |
---|
1084 | } |
---|
1085 | setObject(o); |
---|
1086 | return p; |
---|
1087 | } |
---|
1088 | else if (!strncmp(in, "null", 4)) |
---|
1089 | { |
---|
1090 | setEmpty(); |
---|
1091 | return in + 4; |
---|
1092 | } |
---|
1093 | else if (!strncmp(in, "invalid", 9)) |
---|
1094 | { |
---|
1095 | setInvalid(); |
---|
1096 | return in + 9; |
---|
1097 | } |
---|
1098 | else if (*in == '<') |
---|
1099 | { //unserializable object |
---|
1100 | setInvalid(); |
---|
1101 | while (*in) |
---|
1102 | if (*in == '>') |
---|
1103 | return in + 1; |
---|
1104 | else in++; |
---|
1105 | return in; |
---|
1106 | } |
---|
1107 | else if (*in == '^') |
---|
1108 | { |
---|
1109 | in++; |
---|
1110 | ExtValue ref; |
---|
1111 | ret = ref.parseNumber(in, TInt); |
---|
1112 | if (ret && (ref.getType() == TInt)) |
---|
1113 | { |
---|
1114 | const ExtObject* o = tlsGetRef(ExtObject_serialization).get(ref.getInt()); |
---|
1115 | if (o) |
---|
1116 | { |
---|
1117 | setObject(*o); |
---|
1118 | return ret; |
---|
1119 | } |
---|
1120 | } |
---|
1121 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Invalid reference: '%s'", in - 1); |
---|
1122 | return NULL; |
---|
1123 | } |
---|
1124 | else if ((ret = skipWord(in)) && (ret != in)) |
---|
1125 | { |
---|
1126 | SString clsname(in, (int)(ret - in)); |
---|
1127 | ExtValue tmp; |
---|
1128 | ret = tmp.deserialize(ret); |
---|
1129 | ParamInterface *cls = findDeserializableClass(clsname.c_str()); |
---|
1130 | if (cls && (tmp.getType() != TUnknown) && (tmp.getType() != TInvalid)) |
---|
1131 | { |
---|
1132 | VectorObject *vec = VectorObject::fromObject(tmp.getObject(), false); |
---|
1133 | if (vec) |
---|
1134 | { |
---|
1135 | int m = cls->findId("newFromVector"); |
---|
1136 | if (m >= 0) |
---|
1137 | { |
---|
1138 | cls->call(m, &tmp, this); |
---|
1139 | tlsGetRef(ExtObject_serialization).replace(tmp.getObject(), getObject()); |
---|
1140 | return ret; |
---|
1141 | } |
---|
1142 | } |
---|
1143 | DictionaryObject *dic = DictionaryObject::fromObject(tmp.getObject(), false); |
---|
1144 | if (dic) |
---|
1145 | { |
---|
1146 | int m = cls->findId("newFromDictionary"); |
---|
1147 | if (m >= 0) |
---|
1148 | { |
---|
1149 | cls->call(m, &tmp, this); |
---|
1150 | tlsGetRef(ExtObject_serialization).replace(tmp.getObject(), getObject()); |
---|
1151 | return ret; |
---|
1152 | } |
---|
1153 | } |
---|
1154 | if (tmp.getType() == TString) |
---|
1155 | { |
---|
1156 | int m = cls->findId("newFromString"); |
---|
1157 | if (m >= 0) |
---|
1158 | { |
---|
1159 | cls->call(m, &tmp, this); |
---|
1160 | tlsGetRef(ExtObject_serialization).replace(tmp.getObject(), getObject()); |
---|
1161 | return ret; |
---|
1162 | } |
---|
1163 | } |
---|
1164 | tlsGetRef(ExtObject_serialization).remove(tmp.getObject()); |
---|
1165 | setEmpty(); |
---|
1166 | } |
---|
1167 | setEmpty(); |
---|
1168 | logPrintf("ExtValue", "deserialize", LOG_WARN, "object of class \"%s\" could not be deserialized", clsname.c_str()); |
---|
1169 | return ret; |
---|
1170 | } |
---|
1171 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Bad syntax: '%s'", in); |
---|
1172 | setEmpty(); |
---|
1173 | return NULL; |
---|
1174 | } |
---|
1175 | |
---|
1176 | const char* ExtValue::deserialize(const char* in) |
---|
1177 | { |
---|
1178 | tlsGetRef(ExtObject_serialization).begin(); |
---|
1179 | const char* ret = deserialize_inner(in); |
---|
1180 | tlsGetRef(ExtObject_serialization).end(); |
---|
1181 | return ret; |
---|
1182 | } |
---|
1183 | |
---|
1184 | ExtObject ExtValue::getObject() const |
---|
1185 | { |
---|
1186 | if (type == TObj) return odata(); |
---|
1187 | return ExtObject(); |
---|
1188 | } |
---|
1189 | |
---|
1190 | ExtValue ExtValue::getExtType() |
---|
1191 | { |
---|
1192 | static const char* typenames[] = { "null", "int", "float", "string", "", "invalid" }; |
---|
1193 | if (getType() != TObj) |
---|
1194 | return ExtValue(typenames[(int)getType()]); |
---|
1195 | ExtObject& o = odata(); |
---|
1196 | return ExtValue(SString(o.isEmpty() ? "" : o.interfaceName())); |
---|
1197 | } |
---|
1198 | |
---|
1199 | SString SString::valueOf(const ExtValue& v) |
---|
1200 | { |
---|
1201 | return v.getString(); |
---|
1202 | } |
---|
1203 | SString SString::valueOf(const ExtObject& v) |
---|
1204 | { |
---|
1205 | return v.toString(); |
---|
1206 | } |
---|