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