1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2018 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 | bool ExtObject::callDelegate(const char* delegate, ExtValue *args, ExtValue *ret) |
---|
107 | { |
---|
108 | Param tmp; |
---|
109 | ParamInterface *pi = getParamInterface(tmp); |
---|
110 | if (pi) |
---|
111 | { |
---|
112 | int f = pi->findId(delegate); |
---|
113 | if (f >= 0) |
---|
114 | { |
---|
115 | pi->call(f, args, ret); |
---|
116 | return true; |
---|
117 | } |
---|
118 | } |
---|
119 | logPrintf("Genotype", "get", LOG_ERROR, "Could not call delegate '%s.%s'", pi ? pi->getName() : "NULL", delegate); |
---|
120 | return false; |
---|
121 | } |
---|
122 | |
---|
123 | SString ExtObject::toString() const |
---|
124 | { |
---|
125 | if (isEmpty()) return SString("null"); |
---|
126 | Param tmp_param; |
---|
127 | ParamInterface *p = getParamInterface(tmp_param); |
---|
128 | int tostr = p->findId("toString"); |
---|
129 | if (tostr >= 0) |
---|
130 | { |
---|
131 | return SString(p->getString(tostr)); |
---|
132 | } |
---|
133 | else |
---|
134 | { |
---|
135 | SString tmp("<"); |
---|
136 | tmp += p->getName(); |
---|
137 | tmp += SString::sprintf(" object at %p>", object ? object : paraminterface); |
---|
138 | return tmp; |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | THREAD_LOCAL_DEF(ExtObject::Serialization, ExtObject_serialization); |
---|
143 | |
---|
144 | void ExtObject::Serialization::begin() |
---|
145 | { |
---|
146 | if (level == 0) |
---|
147 | refs.clear(); |
---|
148 | level++; |
---|
149 | } |
---|
150 | |
---|
151 | int ExtObject::Serialization::add(const ExtObject &o) |
---|
152 | { |
---|
153 | if (o.isEmpty()) return -1; |
---|
154 | for (int i = 0; i < (int)refs.size(); i++) |
---|
155 | { |
---|
156 | ExtObject& r = refs[i]; |
---|
157 | if (r == o) return i; |
---|
158 | } |
---|
159 | refs.push_back(o); |
---|
160 | return -1; |
---|
161 | } |
---|
162 | |
---|
163 | void ExtObject::Serialization::replace(const ExtObject& o, const ExtObject& other) |
---|
164 | { |
---|
165 | if (o.isEmpty()) return; |
---|
166 | for (int i = 0; i < (int)refs.size(); i++) |
---|
167 | { |
---|
168 | ExtObject& r = refs[i]; |
---|
169 | if (r == o) |
---|
170 | { |
---|
171 | r = other; |
---|
172 | return; |
---|
173 | } |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | void ExtObject::Serialization::remove(const ExtObject& o) |
---|
178 | { |
---|
179 | if (o.isEmpty()) return; |
---|
180 | for (int i = 0; i < (int)refs.size(); i++) |
---|
181 | { |
---|
182 | ExtObject& r = refs[i]; |
---|
183 | if (o == r) refs.erase(refs.begin() + i); |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | const ExtObject* ExtObject::Serialization::get(int ref) |
---|
188 | { |
---|
189 | if (ref < 0) return NULL; |
---|
190 | if (ref >= (int)refs.size()) return NULL; |
---|
191 | return &refs[ref]; |
---|
192 | } |
---|
193 | |
---|
194 | void ExtObject::Serialization::end() |
---|
195 | { |
---|
196 | level--; |
---|
197 | if (level == 0) |
---|
198 | refs.clear(); |
---|
199 | } |
---|
200 | |
---|
201 | SString ExtObject::serialize_inner(SerializationFormat format) const |
---|
202 | { |
---|
203 | int ref = tlsGetRef(ExtObject_serialization).add(*this); |
---|
204 | SString ret; |
---|
205 | |
---|
206 | if (ref >= 0) |
---|
207 | { |
---|
208 | switch (format) |
---|
209 | { |
---|
210 | case NativeSerialization: return SString::sprintf("^%d", ref); |
---|
211 | case JSONSerialization: return SString("null"); |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | if (isEmpty()) return SString("null"); |
---|
216 | { |
---|
217 | VectorObject *vec = VectorObject::fromObject(*this, false); |
---|
218 | if (vec) |
---|
219 | { |
---|
220 | ret = vec->serialize(format); goto finally; |
---|
221 | } |
---|
222 | } |
---|
223 | { |
---|
224 | DictionaryObject *dic = DictionaryObject::fromObject(*this, false); |
---|
225 | if (dic) |
---|
226 | { |
---|
227 | ret = dic->serialize(format); goto finally; |
---|
228 | } |
---|
229 | } |
---|
230 | { |
---|
231 | Param tmp_param; |
---|
232 | ParamInterface *p = getParamInterface(tmp_param); |
---|
233 | int m = p->findId("toVector"); |
---|
234 | if (m < 0) |
---|
235 | m = p->findId("toDictionary"); |
---|
236 | if (m >= 0) |
---|
237 | { |
---|
238 | ExtObject o(p->getObject(m)); |
---|
239 | switch (format) |
---|
240 | { |
---|
241 | case NativeSerialization: ret = SString(interfaceName()) + o.serialize(format); break; |
---|
242 | case JSONSerialization: ret = SString::sprintf("{\"class\":\"%s\",\"data\":%s}", interfaceName(), o.serialize(format).c_str()); break; |
---|
243 | } |
---|
244 | goto finally; |
---|
245 | } |
---|
246 | m = p->findId("toString"); |
---|
247 | if (m >= 0) |
---|
248 | { |
---|
249 | SString str = p->getString(m); |
---|
250 | sstringQuote(str); |
---|
251 | switch (format) |
---|
252 | { |
---|
253 | case NativeSerialization: ret = SString(interfaceName()) + "\"" + str + "\""; break; |
---|
254 | case JSONSerialization: ret = SString::sprintf("{\"class\":\"%s\",\"data\":\"%s\"}", interfaceName(), str.c_str()); break; |
---|
255 | } |
---|
256 | goto finally; |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
260 | tlsGetRef(ExtObject_serialization).remove(*this);//undo nonserializable reference |
---|
261 | switch (format) |
---|
262 | { |
---|
263 | case NativeSerialization: return SString(interfaceName()) + SString::sprintf("<%p>", object ? object : paraminterface); break; |
---|
264 | case JSONSerialization: return SString::sprintf("{\"class\":\"%s\"}", interfaceName()); break; |
---|
265 | } |
---|
266 | |
---|
267 | finally: // not 100% "finally", the case of nonserializable reference (directly above) returns directly without going through finally |
---|
268 | |
---|
269 | switch (format) |
---|
270 | { |
---|
271 | case JSONSerialization: |
---|
272 | tlsGetRef(ExtObject_serialization).remove(*this);//JSON only tracks recursion, does not track reuse |
---|
273 | break; |
---|
274 | case NativeSerialization:; //nop (just to avoid compiler warning) |
---|
275 | } |
---|
276 | |
---|
277 | return ret; |
---|
278 | } |
---|
279 | |
---|
280 | SString ExtObject::serialize(SerializationFormat format) const |
---|
281 | { |
---|
282 | tlsGetRef(ExtObject_serialization).begin(); |
---|
283 | SString ret = serialize_inner(format); |
---|
284 | tlsGetRef(ExtObject_serialization).end(); |
---|
285 | return ret; |
---|
286 | } |
---|
287 | |
---|
288 | /////////////////////////////////////// |
---|
289 | |
---|
290 | SString ExtValue::typeDescription() const |
---|
291 | { |
---|
292 | switch (type) |
---|
293 | { |
---|
294 | case TInt: return SString("int"); |
---|
295 | case TDouble: return SString("float"); |
---|
296 | case TString: return SString("string"); |
---|
297 | case TUnknown: return SString("null"); |
---|
298 | case TInvalid: return SString("invalid"); |
---|
299 | case TObj: return getObject().isEmpty() ? SString("null") : SString(getObject().interfaceName()); |
---|
300 | } |
---|
301 | return SString::empty(); |
---|
302 | } |
---|
303 | |
---|
304 | SString ExtValue::typeAndValue() const |
---|
305 | { |
---|
306 | SString msg = typeDescription(); |
---|
307 | switch (type) |
---|
308 | { |
---|
309 | case TInt: case TDouble: case TString: case TObj: |
---|
310 | msg += " '"; |
---|
311 | msg += getString(); |
---|
312 | msg += "'"; |
---|
313 | default:; |
---|
314 | } |
---|
315 | return msg; |
---|
316 | } |
---|
317 | |
---|
318 | void *ExtValue::getObjectTarget(const char* classname, bool warn) const |
---|
319 | { |
---|
320 | if (type != TObj) |
---|
321 | { |
---|
322 | if (warn) |
---|
323 | { |
---|
324 | SString tmp = getString(); |
---|
325 | if (tmp.len() > 30) tmp = tmp.substr(0, 30) + "..."; |
---|
326 | if (type == TString) tmp = SString("\"") + tmp + SString("\""); |
---|
327 | logPrintf("ExtValue", "getObjectTarget", LOG_WARN, "%s object expected, %s found", classname, tmp.c_str()); |
---|
328 | } |
---|
329 | return NULL; |
---|
330 | } |
---|
331 | |
---|
332 | return getObject().getTarget(classname, true, warn); |
---|
333 | } |
---|
334 | |
---|
335 | void ExtValue::set(const ExtValue& src) |
---|
336 | { |
---|
337 | switch (src.type) |
---|
338 | { |
---|
339 | case TString: sets(src.sdata()); break; |
---|
340 | case TInt: seti(src.idata()); break; |
---|
341 | case TDouble: setd(src.ddata()); break; |
---|
342 | case TObj: seto(src.odata()); break; |
---|
343 | default:type = src.type; break; |
---|
344 | } |
---|
345 | } |
---|
346 | |
---|
347 | void ExtValue::setEmpty() |
---|
348 | { |
---|
349 | switch (type) |
---|
350 | { |
---|
351 | #ifdef EXTVALUEUNION |
---|
352 | case TString: sdata().~SString(); break; |
---|
353 | case TObj: odata().~ExtObject(); break; |
---|
354 | #else |
---|
355 | case TString: delete s; break; |
---|
356 | case TObj: delete o; break; |
---|
357 | #endif |
---|
358 | default:; |
---|
359 | } |
---|
360 | type = TUnknown; |
---|
361 | } |
---|
362 | |
---|
363 | void ExtValue::setError(const SString& msg) |
---|
364 | { |
---|
365 | ErrorObject *err = new ErrorObject; |
---|
366 | err->message = msg; |
---|
367 | setObject(ErrorObject::makeDynamicObject(err)); |
---|
368 | } |
---|
369 | |
---|
370 | static ExtValue::CompareResult longsign(paInt x) |
---|
371 | { |
---|
372 | if (x < 0) return ExtValue::ResultLower; |
---|
373 | if (x > 0) return ExtValue::ResultHigher; |
---|
374 | return ExtValue::ResultEqual; |
---|
375 | } |
---|
376 | |
---|
377 | static ExtValue::CompareResult compareNull(const ExtValue& v) |
---|
378 | { |
---|
379 | if (v.isNull()) return ExtValue::ResultEqualUnordered; |
---|
380 | if ((v.getType() == TInt) && (v.getInt() == 0)) return ExtValue::ResultUnequal_RelaxedEqual; |
---|
381 | return ExtValue::ResultUnequal_RelaxedUnequal; //comparing anything else with null is valid but null is neither higher nor lower than numbers or strings |
---|
382 | } |
---|
383 | |
---|
384 | static ExtValue::CompareResult compareInvalid(const ExtValue& v) |
---|
385 | { |
---|
386 | if (v.getType() == TInvalid) return ExtValue::ResultEqualUnordered; |
---|
387 | if ((v.getType() == TInt) && (v.getInt() == 0)) return ExtValue::ResultUnequal_RelaxedEqual; |
---|
388 | return ExtValue::ResultMismatch; //comparing anything else with invalid is invalid |
---|
389 | } |
---|
390 | |
---|
391 | static ExtValue::CompareResult compareFloat(double a, double b) |
---|
392 | { |
---|
393 | double t = a - b; |
---|
394 | if (t < 0) return ExtValue::ResultLower; |
---|
395 | else if (t > 0) return ExtValue::ResultHigher; |
---|
396 | return ExtValue::ResultEqual; |
---|
397 | } |
---|
398 | |
---|
399 | static ExtValue::CompareResult compareString(const SString &a, const SString &b) |
---|
400 | { |
---|
401 | const char* s1 = a.c_str(); |
---|
402 | const char* s2 = b.c_str(); |
---|
403 | return longsign(strcmp(s1, s2)); |
---|
404 | } |
---|
405 | |
---|
406 | ExtValue::CompareResult ExtValue::compare(const ExtValue& src) const |
---|
407 | { |
---|
408 | if (isNull()) |
---|
409 | return compareNull(src); |
---|
410 | else if (src.isNull()) |
---|
411 | return compareNull(*this); |
---|
412 | if (getType() == TInvalid) |
---|
413 | return compareInvalid(src); |
---|
414 | else if (src.getType() == TInvalid) |
---|
415 | return compareInvalid(*this); |
---|
416 | switch (type) |
---|
417 | { |
---|
418 | |
---|
419 | case TInt: |
---|
420 | |
---|
421 | if (src.getType() == TInt) |
---|
422 | { |
---|
423 | paInt t = src.getInt(); |
---|
424 | if (idata() > 0) |
---|
425 | { |
---|
426 | if (t > 0) return longsign(idata() - t); else return ResultHigher; |
---|
427 | } |
---|
428 | else |
---|
429 | { |
---|
430 | if (t <= 0) return longsign(idata() - t); else return ResultLower; |
---|
431 | } |
---|
432 | } |
---|
433 | else if (src.getType() == TDouble) |
---|
434 | return compareFloat((double)idata(), src.getDouble()); |
---|
435 | else |
---|
436 | return ResultMismatch;//comparing numbers with other things is invalid |
---|
437 | break; |
---|
438 | |
---|
439 | case TDouble: |
---|
440 | if ((src.getType() == TDouble) || (src.getType() == TInt)) |
---|
441 | return compareFloat(getDouble(), src.getDouble()); |
---|
442 | else |
---|
443 | return ResultMismatch; |
---|
444 | break; |
---|
445 | |
---|
446 | case TString: |
---|
447 | if (src.getType() == TString) |
---|
448 | return compareString(sdata(), src.getString()); |
---|
449 | else |
---|
450 | return ResultMismatch; |
---|
451 | break; |
---|
452 | |
---|
453 | case TObj: |
---|
454 | { |
---|
455 | if (src.type == TObj) |
---|
456 | return odata() == src.odata() ? ResultEqualUnordered : ResultUnequal_RelaxedUnequal; |
---|
457 | if ((src.type == TInt) && (src.getInt() == 0)) |
---|
458 | return ResultMismatch_RelaxedUnequal; |
---|
459 | return ResultMismatch; |
---|
460 | } |
---|
461 | default:; |
---|
462 | } |
---|
463 | return ResultMismatch; |
---|
464 | } |
---|
465 | |
---|
466 | const char* ExtValue::cmp_op_names[] = { "==", "!=", ">=", "<=", ">", "<", "~=", "!~", NULL }; |
---|
467 | |
---|
468 | int ExtValue::interpretCompare(CmpOperator op, CompareResult result, CmpContext *context) |
---|
469 | { |
---|
470 | CompareResult error_threshold = ResultUnequal_RelaxedEqual;//error when ResultUnequal_RelaxedEqual or higher (not comparable) |
---|
471 | int ret = 0; |
---|
472 | switch (op) |
---|
473 | { |
---|
474 | case CmpEQ: ret = (result == ResultEqual) || (result == ResultEqualUnordered); error_threshold = ResultMismatch_RelaxedUnequal; break; |
---|
475 | case CmpNE: ret = !((result == ResultEqual) || (result == ResultEqualUnordered)); error_threshold = ResultMismatch_RelaxedUnequal; break; |
---|
476 | case CmpGT: ret = (result == ResultHigher); error_threshold = ResultEqualUnordered; break; |
---|
477 | case CmpGE: ret = (result == ResultEqual) || (result == ResultHigher); error_threshold = ResultEqualUnordered; break; |
---|
478 | case CmpLT: ret = (result == ResultLower); error_threshold = ResultEqualUnordered; break; |
---|
479 | case CmpLE: ret = (result == ResultEqual) || (result == ResultLower); error_threshold = ResultEqualUnordered; break; |
---|
480 | case CmpREQ: ret = (result == ResultEqual) || (result == ResultEqualUnordered) || (result == ResultUnequal_RelaxedEqual); error_threshold = ResultMismatch; break; |
---|
481 | case CmpRNE: ret = !((result == ResultEqual) || (result == ResultEqualUnordered) || (result == ResultUnequal_RelaxedEqual)); error_threshold = ResultMismatch; break; |
---|
482 | default:; |
---|
483 | } |
---|
484 | if (result >= error_threshold) |
---|
485 | { |
---|
486 | SString msg = "Type mismatch while comparing"; |
---|
487 | if (context) |
---|
488 | { |
---|
489 | if (context->v1 && context->v2) |
---|
490 | msg += SString::sprintf(": %s %s %s", |
---|
491 | context->v1->typeAndValue().c_str(), |
---|
492 | cmp_op_names[op - CmpFIRST], |
---|
493 | context->v2->typeAndValue().c_str()); |
---|
494 | } |
---|
495 | logPrintf("ExtValue", "interpretCompare", LOG_ERROR, "%s", msg.c_str()); |
---|
496 | ret = -1; |
---|
497 | } |
---|
498 | return ret; |
---|
499 | } |
---|
500 | |
---|
501 | int ExtValue::operator==(const ExtValue& src) const |
---|
502 | { |
---|
503 | if (type != src.type) return 0; |
---|
504 | switch (type) |
---|
505 | { |
---|
506 | case TInt: return idata() == src.idata(); |
---|
507 | case TDouble: return ddata() == src.ddata(); |
---|
508 | case TString: return sdata() == src.sdata(); |
---|
509 | case TObj: return odata() == src.odata(); |
---|
510 | default:; |
---|
511 | } |
---|
512 | return 1; |
---|
513 | } |
---|
514 | |
---|
515 | void ExtValue::operator+=(const ExtValue& src) |
---|
516 | { |
---|
517 | // return = ok, break = fail |
---|
518 | switch (type) |
---|
519 | { |
---|
520 | case TInt: |
---|
521 | switch (src.getType()) |
---|
522 | { |
---|
523 | case TDouble: |
---|
524 | setDouble(double(getInt()) + src.getDouble()); |
---|
525 | return; |
---|
526 | case TString: |
---|
527 | break; |
---|
528 | default: |
---|
529 | idata() += src.getInt(); |
---|
530 | return; |
---|
531 | } |
---|
532 | break; |
---|
533 | case TDouble: |
---|
534 | switch (src.getType()) |
---|
535 | { |
---|
536 | case TString: |
---|
537 | break; |
---|
538 | default: |
---|
539 | ddata() += src.getDouble(); |
---|
540 | return; |
---|
541 | } |
---|
542 | break; |
---|
543 | case TString: sdata() += src.getString(); return; |
---|
544 | case TObj: |
---|
545 | { |
---|
546 | VectorObject *vec = VectorObject::fromObject(getObject(), false); |
---|
547 | VectorObject *vec2 = VectorObject::fromObject(src.getObject(), false); |
---|
548 | if (vec && vec2) |
---|
549 | { |
---|
550 | for (int i = 0; i < vec2->data.size(); i++) |
---|
551 | { |
---|
552 | ExtValue *s = (ExtValue*)vec2->data(i); |
---|
553 | ExtValue *d = s ? new ExtValue(*s) : NULL; |
---|
554 | vec->data += d; |
---|
555 | } |
---|
556 | return; |
---|
557 | } |
---|
558 | } |
---|
559 | //NO break; |
---|
560 | default:; |
---|
561 | } |
---|
562 | logPrintf("ExtValue", "add", LOG_ERROR, "Can't add %s to %s", src.typeAndValue().c_str(), typeAndValue().c_str()); |
---|
563 | } |
---|
564 | |
---|
565 | void ExtValue::operator-=(const ExtValue& src) |
---|
566 | { |
---|
567 | // return = ok, break = fail |
---|
568 | switch (type) |
---|
569 | { |
---|
570 | case TInt: |
---|
571 | switch (src.getType()) |
---|
572 | { |
---|
573 | case TInt: |
---|
574 | idata() -= src.getInt(); |
---|
575 | return; |
---|
576 | case TDouble: |
---|
577 | setDouble(double(getInt()) - src.getDouble()); |
---|
578 | return; |
---|
579 | default:; |
---|
580 | } |
---|
581 | break; |
---|
582 | case TDouble: |
---|
583 | switch (src.getType()) |
---|
584 | { |
---|
585 | case TDouble: |
---|
586 | case TInt: |
---|
587 | ddata() -= src.getDouble(); |
---|
588 | return; |
---|
589 | default:; |
---|
590 | } |
---|
591 | break; |
---|
592 | default:; |
---|
593 | } |
---|
594 | logPrintf("ExtValue", "subtract", LOG_ERROR, "Can't subtract %s from %s", src.typeAndValue().c_str(), typeAndValue().c_str()); |
---|
595 | } |
---|
596 | |
---|
597 | void ExtValue::operator*=(const ExtValue& src) |
---|
598 | { |
---|
599 | // return = ok, break = fail |
---|
600 | switch (type) |
---|
601 | { |
---|
602 | case TInt: |
---|
603 | switch (src.getType()) |
---|
604 | { |
---|
605 | case TInt: |
---|
606 | idata() *= src.getInt(); |
---|
607 | return; |
---|
608 | case TDouble: |
---|
609 | setDouble(double(getInt())*src.getDouble()); |
---|
610 | return; |
---|
611 | default:; |
---|
612 | } |
---|
613 | break; |
---|
614 | case TDouble: |
---|
615 | switch (src.getType()) |
---|
616 | { |
---|
617 | case TInt: |
---|
618 | case TDouble: |
---|
619 | ddata() *= src.getDouble(); |
---|
620 | return; |
---|
621 | default:; |
---|
622 | } |
---|
623 | break; |
---|
624 | case TString: |
---|
625 | switch (src.getType()) |
---|
626 | { |
---|
627 | case TInt: case TDouble: |
---|
628 | { |
---|
629 | SString t; |
---|
630 | for (int n = src.getInt(); n > 0; n--) |
---|
631 | t += getString(); |
---|
632 | setString(t); |
---|
633 | return; |
---|
634 | } |
---|
635 | default:; |
---|
636 | } |
---|
637 | break; |
---|
638 | case TObj: |
---|
639 | { |
---|
640 | VectorObject *vec = VectorObject::fromObject(getObject(), false); |
---|
641 | if (vec) |
---|
642 | { |
---|
643 | int n = src.getInt(); |
---|
644 | int orig_size = vec->data.size(); |
---|
645 | if (n <= 0) |
---|
646 | { |
---|
647 | vec->clear(); return; |
---|
648 | } |
---|
649 | for (; n > 1; n--) |
---|
650 | { |
---|
651 | for (int i = 0; i < orig_size; i++) |
---|
652 | { |
---|
653 | ExtValue *s = (ExtValue*)vec->data(i); |
---|
654 | ExtValue *d = s ? new ExtValue(*s) : NULL; |
---|
655 | vec->data += d; |
---|
656 | } |
---|
657 | } |
---|
658 | return; |
---|
659 | } |
---|
660 | } |
---|
661 | //NO break; |
---|
662 | default:; |
---|
663 | } |
---|
664 | logPrintf("ExtValue", "multiply", LOG_WARN, "Can't multiply %s by %s", typeAndValue().c_str(), src.typeAndValue().c_str()); |
---|
665 | } |
---|
666 | |
---|
667 | /*#include "fpu_control.h" |
---|
668 | #include <signal.h> |
---|
669 | |
---|
670 | static int fpuexception; |
---|
671 | void mathhandler(int sig) |
---|
672 | { |
---|
673 | printf("fpu exception!\n"); |
---|
674 | fpuexception=1; |
---|
675 | signal(SIGFPE,SIG_IGN); |
---|
676 | } */ |
---|
677 | |
---|
678 | void ExtValue::divInt(paInt a) |
---|
679 | { |
---|
680 | if (a) |
---|
681 | idata() /= a; |
---|
682 | else |
---|
683 | { |
---|
684 | logPrintf("ExtValue", "divide", LOG_ERROR, "Division by zero: %d/0", idata()); |
---|
685 | setInvalid(); |
---|
686 | } |
---|
687 | } |
---|
688 | |
---|
689 | void ExtValue::divDouble(double a) |
---|
690 | { |
---|
691 | if (a == 0.0) |
---|
692 | { |
---|
693 | logPrintf("ExtValue", "divide", LOG_ERROR, "Division by zero: %s/0.0", getString().c_str()); |
---|
694 | setInvalid(); |
---|
695 | } |
---|
696 | else |
---|
697 | { |
---|
698 | fpExceptDisable(); |
---|
699 | double tmp = getDouble() / a; |
---|
700 | if (!finite(tmp)) |
---|
701 | { |
---|
702 | logPrintf("ExtValue", "divide", LOG_ERROR, "Overflow %s/%g", getString().c_str(), a); setInvalid(); |
---|
703 | } |
---|
704 | else |
---|
705 | setDouble(tmp); |
---|
706 | // niby dobrze ale lepiej byloby to robic bardziej systematycznie a nie tylko w dzieleniu? |
---|
707 | //if (isnan(ddata())) //http://www.digitalmars.com/d/archives/c++/Traping_divide_by_zero_5728.html |
---|
708 | // { logPrintf("ExtValue","divide",LOG_ERROR,"not-a-number",(const char*)getString()); setInvalid(); } |
---|
709 | fpExceptEnable(); |
---|
710 | } |
---|
711 | } |
---|
712 | |
---|
713 | void ExtValue::operator/=(const ExtValue& src) |
---|
714 | { |
---|
715 | switch (type) |
---|
716 | { |
---|
717 | case TInt: |
---|
718 | switch (src.getType()) |
---|
719 | { |
---|
720 | case TInt: |
---|
721 | divInt(src.idata()); |
---|
722 | return; |
---|
723 | case TDouble: |
---|
724 | divDouble(src.ddata()); |
---|
725 | return; |
---|
726 | default:; |
---|
727 | } |
---|
728 | break; |
---|
729 | |
---|
730 | case TDouble: |
---|
731 | switch (src.getType()) |
---|
732 | { |
---|
733 | case TInt: |
---|
734 | divDouble(src.getDouble()); |
---|
735 | return; |
---|
736 | case TDouble: |
---|
737 | divDouble(src.ddata()); |
---|
738 | return; |
---|
739 | default:; |
---|
740 | } |
---|
741 | break; |
---|
742 | |
---|
743 | default:; |
---|
744 | } |
---|
745 | logPrintf("ExtValue", "divide", LOG_ERROR, "Can't divide %s by %s", typeAndValue().c_str(), src.typeAndValue().c_str()); |
---|
746 | } |
---|
747 | |
---|
748 | SString ExtValue::formatTime(char fmt, double value) |
---|
749 | { |
---|
750 | if (fmt == 'i') |
---|
751 | { //could be Convert::ctime() |
---|
752 | int d, h, m, ti = value; |
---|
753 | int ms = 1000 * (value - ti); |
---|
754 | d = ti / 86400; ti -= d * 86400; |
---|
755 | h = ti / 3600; ti -= h * 3600; |
---|
756 | m = ti / 60; ti -= m * 60; |
---|
757 | SString ret; |
---|
758 | if (d > 0) ret += SString::sprintf("%dd ", d); |
---|
759 | if (h > 0) ret += SString::sprintf("%d:", h); |
---|
760 | ret += SString::sprintf("%02d:%02d.%03d", m, ti, ms); |
---|
761 | return ret; |
---|
762 | } |
---|
763 | time_t ti = value; |
---|
764 | struct tm tm = Convert::localtime(ti); |
---|
765 | switch (fmt) |
---|
766 | { |
---|
767 | case 'T': return 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); |
---|
768 | case 't': return SString(Convert::asctime(tm).c_str()); |
---|
769 | case 'y': return SString::sprintf("%04d-%02d-%02d", 1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday); |
---|
770 | case 'm': return SString::sprintf("%04d-%02d-%02d %02d:%02d:%02d.%03d", 1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, int(1000 * (value - (double)ti))); |
---|
771 | } |
---|
772 | return SString(); |
---|
773 | } |
---|
774 | |
---|
775 | SString ExtValue::format(const SString& fmt, const ExtValue **values, int count) |
---|
776 | { |
---|
777 | SString ret; |
---|
778 | // "..........%.........%..........%........" |
---|
779 | // ^_cur ^_next |
---|
780 | // ^^^^^^^^^^___sub |
---|
781 | // |
---|
782 | // "..........%.........%..........%........" |
---|
783 | // ^-cur ^-next |
---|
784 | // ^^^^^^^^^^___sub |
---|
785 | const char* begin = fmt.c_str(), *end = begin + fmt.len(), *curr = begin; |
---|
786 | int type = 0; |
---|
787 | |
---|
788 | class Args |
---|
789 | { |
---|
790 | const ExtValue **values; |
---|
791 | int count; |
---|
792 | int arg; |
---|
793 | public: |
---|
794 | Args(const ExtValue **v, int c) :values(v), count(c), arg(0) {} |
---|
795 | bool finished() { return arg >= count; } |
---|
796 | const ExtValue *getNext() { const ExtValue *ret = NULL; if ((arg < count) && values[arg]) ret = values[arg]; arg++; return ret; } |
---|
797 | }; |
---|
798 | Args args(values, count); |
---|
799 | |
---|
800 | while (curr < end) |
---|
801 | { |
---|
802 | const char* next = strchr(curr, '%'); |
---|
803 | if (!next) next = end; else if ((next == curr) && (curr > begin)) |
---|
804 | { |
---|
805 | next = strchr(next + 1, '%'); if (!next) next = end; |
---|
806 | } |
---|
807 | type = 0; |
---|
808 | if (curr > begin) |
---|
809 | { |
---|
810 | type = 0; |
---|
811 | for (const char* t = curr; t < next; t++) |
---|
812 | switch (*t) |
---|
813 | { |
---|
814 | case 'd': case 'x': case 'X': case 'u': case 'p': case 'c': type = 'd'; t = next; break; |
---|
815 | case 'f': case 'g': case 'e': type = 'f'; t = next; break; |
---|
816 | case 's': type = 's'; t = next; break; |
---|
817 | case 't': case 'T': case 'y': case 'i': case 'm': type = *t; t = next; break; |
---|
818 | case '%': if (t > begin) { type = *t; t = next; } break; |
---|
819 | } |
---|
820 | } |
---|
821 | if (curr > begin) curr--; |
---|
822 | const ExtValue *a; |
---|
823 | if (args.finished() && (type != 0) && (type != '%')) |
---|
824 | { |
---|
825 | ret += fmt.substr((int)(curr - begin)); |
---|
826 | break; |
---|
827 | } |
---|
828 | SString sub = fmt.substr((int)(curr - begin), (int)(next - curr)); |
---|
829 | switch (type) |
---|
830 | { |
---|
831 | case 'd': a = args.getNext(); ret += SString::sprintf(sub.c_str(), a ? a->getInt() : 0); break; |
---|
832 | case 'f': a = args.getNext(); ret += SString::sprintf(sub.c_str(), a ? a->getDouble() : 0); break; |
---|
833 | case 's': {a = args.getNext(); SString tmp; if (a) tmp = a->getString(); ret += SString::sprintf(sub.c_str(), tmp.c_str()); } break; |
---|
834 | case 't': case 'T': case 'i': case 'y': case 'm': |
---|
835 | a = args.getNext(); |
---|
836 | ret += formatTime(type, a ? a->getDouble() : 0); |
---|
837 | ret += sub.substr(2); |
---|
838 | break; |
---|
839 | case '%': ret += '%'; ret += sub.substr(2); break; |
---|
840 | case 0: ret += sub; break; |
---|
841 | } |
---|
842 | curr = next + 1; |
---|
843 | } |
---|
844 | return ret; |
---|
845 | } |
---|
846 | |
---|
847 | |
---|
848 | void ExtValue::modInt(paInt a) |
---|
849 | { |
---|
850 | if (a) |
---|
851 | idata() %= a; |
---|
852 | else |
---|
853 | { |
---|
854 | logPrintf("ExtValue", "modulo", LOG_ERROR, "Modulo by zero: %d%%0", idata()); |
---|
855 | setInvalid(); |
---|
856 | } |
---|
857 | } |
---|
858 | |
---|
859 | void ExtValue::modDouble(double a) |
---|
860 | { |
---|
861 | if (a == 0.0) |
---|
862 | { |
---|
863 | logPrintf("ExtValue", "modulo", LOG_ERROR, "Modulo by zero: %s%%0.0", getString().c_str()); |
---|
864 | setInvalid(); |
---|
865 | } |
---|
866 | else |
---|
867 | setDouble(fmod(ddata(), a)); |
---|
868 | } |
---|
869 | |
---|
870 | void ExtValue::operator%=(const ExtValue& src) |
---|
871 | { |
---|
872 | switch (type) |
---|
873 | { |
---|
874 | case TInt: modInt(src.getInt()); break; |
---|
875 | case TDouble: modDouble(src.getDouble()); break; |
---|
876 | |
---|
877 | case TString: |
---|
878 | { |
---|
879 | VectorObject *vec = VectorObject::fromObject(src.getObject(), false); |
---|
880 | if (vec) |
---|
881 | sdata() = format(sdata(), (const ExtValue**)&vec->data.getref(0), vec->data.size()); |
---|
882 | else |
---|
883 | { |
---|
884 | const ExtValue *ptr = &src; sdata() = ExtValue::format(sdata(), &ptr, 1); |
---|
885 | } |
---|
886 | } |
---|
887 | break; |
---|
888 | |
---|
889 | case TObj: case TUnknown: case TInvalid: |
---|
890 | logPrintf("ExtValue", "modulo", LOG_WARN, "Can't apply modulo to %s", typeDescription().c_str()); |
---|
891 | |
---|
892 | default:; |
---|
893 | } |
---|
894 | } |
---|
895 | |
---|
896 | bool ExtValue::parseInt(const char* s, paInt &result, bool strict, bool error) |
---|
897 | { |
---|
898 | ExtValue tmp; |
---|
899 | const char* after = tmp.parseNumber(s, strict ? TInt : TUnknown); |
---|
900 | if ((after == NULL) || (after[0] != 0)) |
---|
901 | { |
---|
902 | if (error) |
---|
903 | logPrintf("ExtValue", "parseInt", LOG_ERROR, "Could not parse '%s'%s", s, strict ? " (strict)" : ""); |
---|
904 | return false; |
---|
905 | } |
---|
906 | result = tmp.getInt(); |
---|
907 | return true; |
---|
908 | } |
---|
909 | |
---|
910 | bool ExtValue::parseDouble(const char* s, double &result, bool error) |
---|
911 | { |
---|
912 | ExtValue tmp; |
---|
913 | const char* after = tmp.parseNumber(s, TDouble); |
---|
914 | if ((after == NULL) || (after[0] != 0)) |
---|
915 | { |
---|
916 | if (error) |
---|
917 | logPrintf("ExtValue", "parseDouble", LOG_ERROR, "Could not parse '%s'", s); |
---|
918 | return false; |
---|
919 | } |
---|
920 | result = tmp.getDouble(); |
---|
921 | return true; |
---|
922 | } |
---|
923 | |
---|
924 | paInt ExtValue::getInt(const char* s, bool strict) |
---|
925 | { |
---|
926 | paInt result; |
---|
927 | if (parseInt(s, result, strict, true)) |
---|
928 | return result; |
---|
929 | return 0; |
---|
930 | } |
---|
931 | |
---|
932 | double ExtValue::getDouble(const char* s) |
---|
933 | { |
---|
934 | double result; |
---|
935 | if (parseDouble(s, result, true)) |
---|
936 | return result; |
---|
937 | return 0; |
---|
938 | } |
---|
939 | |
---|
940 | paInt ExtValue::getInt() const |
---|
941 | { |
---|
942 | switch (type) |
---|
943 | { |
---|
944 | case TInt: return idata(); |
---|
945 | case TDouble: return (int)ddata(); |
---|
946 | case TString: return getInt(sdata().c_str()); |
---|
947 | case TObj: |
---|
948 | logPrintf("ExtValue", "getInt", LOG_ERROR, "Getting integer value from object reference (%s)", getString().c_str()); |
---|
949 | return (paInt)(intptr_t)odata().param; |
---|
950 | default:; |
---|
951 | } |
---|
952 | return 0; |
---|
953 | } |
---|
954 | |
---|
955 | double ExtValue::getDouble() const |
---|
956 | { |
---|
957 | switch (type) |
---|
958 | { |
---|
959 | case TDouble: return ddata(); |
---|
960 | case TInt: return (double)idata(); |
---|
961 | case TString: return getDouble(sdata().c_str()); |
---|
962 | case TObj: |
---|
963 | logPrintf("ExtValue", "getDouble", LOG_ERROR, "Getting floating point value from object reference (%s)", getString().c_str()); |
---|
964 | return (double)(intptr_t)odata().param; |
---|
965 | default:; |
---|
966 | } |
---|
967 | return 0.0; |
---|
968 | } |
---|
969 | |
---|
970 | SString ExtValue::getString() const |
---|
971 | { |
---|
972 | switch (type) |
---|
973 | { |
---|
974 | case TString: return sdata(); |
---|
975 | case TInt: return SString::valueOf(idata()); |
---|
976 | case TDouble: return SString::valueOf(ddata()); |
---|
977 | case TObj: return odata().toString(); |
---|
978 | case TInvalid: return SString("invalid"); |
---|
979 | default: return SString("null"); |
---|
980 | } |
---|
981 | } |
---|
982 | |
---|
983 | const SString* ExtValue::getStringPtr() const |
---|
984 | { |
---|
985 | if (type == TString) |
---|
986 | return &sdata(); |
---|
987 | return NULL; |
---|
988 | } |
---|
989 | |
---|
990 | SString ExtValue::serialize(SerializationFormat format) const |
---|
991 | { |
---|
992 | switch (type) |
---|
993 | { |
---|
994 | case TString: |
---|
995 | { |
---|
996 | SString q = sdata(); |
---|
997 | sstringQuote(q); |
---|
998 | return SString("\"") + q + SString("\""); |
---|
999 | } |
---|
1000 | case TInt: |
---|
1001 | return SString::valueOf(idata()); |
---|
1002 | case TDouble: |
---|
1003 | return SString::valueOf(ddata()); |
---|
1004 | case TObj: |
---|
1005 | return odata().serialize(format); |
---|
1006 | case TInvalid: |
---|
1007 | if (format == NativeSerialization) |
---|
1008 | return SString("invalid"); |
---|
1009 | // else null --v |
---|
1010 | default: |
---|
1011 | return SString("null"); |
---|
1012 | } |
---|
1013 | } |
---|
1014 | |
---|
1015 | /// returns the first character after the parsed number, or NULL if not a number |
---|
1016 | /// @param strict_type = restrict the allowed return value (TUnknown = unrestricted) |
---|
1017 | const char* ExtValue::parseNumber(const char* in, ExtPType strict_type) |
---|
1018 | { |
---|
1019 | char* after; |
---|
1020 | if (in == NULL) return NULL; |
---|
1021 | if (in[0] == 0) return NULL; |
---|
1022 | while (isspace(*in)) in++; |
---|
1023 | bool minus = (in[0] == '-'); |
---|
1024 | bool plus = (in[0] == '+'); |
---|
1025 | if (((in[0] == '0') && ((in[1] == 'x') || (in[1] == 'X'))) |
---|
1026 | || (((minus || plus) && (in[1] == '0') && ((in[2] == 'x') || (in[2] == 'X'))))) |
---|
1027 | { |
---|
1028 | in += (minus || plus) ? 3 : 2; |
---|
1029 | if (isspace(*in)) return NULL; |
---|
1030 | errno = 0; |
---|
1031 | unsigned long intvalue = strtoul(in, &after, 16); |
---|
1032 | if ((after > in) && (errno == 0) && (intvalue <= 0xffffffff)) |
---|
1033 | { |
---|
1034 | if (strict_type == TDouble) |
---|
1035 | setDouble(minus ? -(double)intvalue : (double)intvalue); |
---|
1036 | else |
---|
1037 | setInt(minus ? -(paInt)intvalue : (paInt)intvalue); |
---|
1038 | return after; |
---|
1039 | } |
---|
1040 | else |
---|
1041 | return NULL; |
---|
1042 | } |
---|
1043 | |
---|
1044 | errno = 0; |
---|
1045 | double fpvalue = strtod(in, &after); |
---|
1046 | if ((after > in) && (errno == 0)) |
---|
1047 | { |
---|
1048 | if (strict_type != TDouble) |
---|
1049 | { |
---|
1050 | if ((memchr(in, '.', after - in) == NULL) && (memchr(in, 'e', after - in) == NULL) && (memchr(in, 'E', after - in) == NULL) // no "special" characters |
---|
1051 | && (fpvalue == floor(fpvalue)) // value is integer |
---|
1052 | && (fpvalue >= INT_MIN) && (fpvalue <= INT_MAX)) // within limits |
---|
1053 | { |
---|
1054 | setInt(fpvalue); |
---|
1055 | return after; |
---|
1056 | } |
---|
1057 | else if (strict_type == TInt) |
---|
1058 | return NULL; |
---|
1059 | } |
---|
1060 | setDouble(fpvalue); |
---|
1061 | return after; |
---|
1062 | } |
---|
1063 | return NULL; |
---|
1064 | } |
---|
1065 | |
---|
1066 | PtrListTempl<ParamInterface*> &ExtValue::getDeserializableClasses() |
---|
1067 | { |
---|
1068 | static PtrListTempl<ParamInterface*> classes; |
---|
1069 | return classes; |
---|
1070 | } |
---|
1071 | |
---|
1072 | ParamInterface *ExtValue::findDeserializableClass(const char* name) |
---|
1073 | { |
---|
1074 | FOREACH(ParamInterface*, cls, getDeserializableClasses()) |
---|
1075 | if (!strcmp(cls->getName(), name)) |
---|
1076 | return cls; |
---|
1077 | return NULL; |
---|
1078 | } |
---|
1079 | |
---|
1080 | static const char* skipWord(const char* in) |
---|
1081 | { |
---|
1082 | while (isalpha(*in) || (*in == '_')) |
---|
1083 | in++; |
---|
1084 | return in; |
---|
1085 | } |
---|
1086 | |
---|
1087 | //returns the first character after the parsed portion or NULL if invalid format |
---|
1088 | const char* ExtValue::deserialize_inner(const char* in) |
---|
1089 | { |
---|
1090 | while (isspace(*in)) in++; |
---|
1091 | const char* ret = parseNumber(in); |
---|
1092 | if (ret) |
---|
1093 | return ret; |
---|
1094 | else if (*in == '\"') |
---|
1095 | { |
---|
1096 | ret = skipQuoteString(in + 1, NULL); |
---|
1097 | SString s(in + 1, (int)(ret - (in + 1))); |
---|
1098 | sstringUnquote(s); |
---|
1099 | setString(s); |
---|
1100 | if (*ret == '\"') |
---|
1101 | return ret + 1; |
---|
1102 | else |
---|
1103 | { |
---|
1104 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Missing '\"' in string: '%s'", ret); |
---|
1105 | return NULL; |
---|
1106 | } |
---|
1107 | } |
---|
1108 | else if (*in == '[') |
---|
1109 | { |
---|
1110 | VectorObject *vec = new VectorObject; |
---|
1111 | ExtObject o(&VectorObject::par, vec); |
---|
1112 | tlsGetRef(ExtObject_serialization).add(o); |
---|
1113 | const char* p = in + 1; |
---|
1114 | ExtValue tmp; bool first = true, comma = false; |
---|
1115 | const char* prev_element = p; |
---|
1116 | while (true) |
---|
1117 | { |
---|
1118 | while (isspace(*p)) p++; |
---|
1119 | if (*p == 0) |
---|
1120 | { |
---|
1121 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Missing ']' in Vector"); |
---|
1122 | return NULL; |
---|
1123 | } |
---|
1124 | if (*p == ']') |
---|
1125 | { |
---|
1126 | if (comma) |
---|
1127 | { |
---|
1128 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "No element after ',' in Vector"); |
---|
1129 | return NULL; |
---|
1130 | } |
---|
1131 | p++; |
---|
1132 | break; |
---|
1133 | } |
---|
1134 | if (!first && !comma) |
---|
1135 | { |
---|
1136 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Missing ',' in Vector: '%s'", prev_element); |
---|
1137 | return NULL; |
---|
1138 | } |
---|
1139 | ret = tmp.deserialize(p); |
---|
1140 | prev_element = p; first = false; comma = false; |
---|
1141 | if (ret) |
---|
1142 | { |
---|
1143 | vec->data += new ExtValue(tmp); |
---|
1144 | p = ret; |
---|
1145 | if (*p == ',') { p++; comma = true; } |
---|
1146 | } |
---|
1147 | else |
---|
1148 | { |
---|
1149 | p = NULL; |
---|
1150 | break; |
---|
1151 | } |
---|
1152 | } |
---|
1153 | setObject(o); |
---|
1154 | return p; |
---|
1155 | } |
---|
1156 | else if (*in == '{') |
---|
1157 | { |
---|
1158 | DictionaryObject *dic = new DictionaryObject; |
---|
1159 | ExtObject o(&DictionaryObject::par, dic); |
---|
1160 | tlsGetRef(ExtObject_serialization).add(o); |
---|
1161 | const char* p = in + 1; |
---|
1162 | ExtValue args[2]/*={value,key}*/, dummy_ret; |
---|
1163 | bool first = true, comma = false; |
---|
1164 | const char* prev_element = p; |
---|
1165 | while (true) |
---|
1166 | { |
---|
1167 | while (isspace(*p)) p++; |
---|
1168 | if (*p == 0) |
---|
1169 | { |
---|
1170 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Missing '}' in Dictionary"); |
---|
1171 | return NULL; |
---|
1172 | } |
---|
1173 | if (*p == '}') |
---|
1174 | { |
---|
1175 | if (comma) |
---|
1176 | { |
---|
1177 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "No element after ',' in Dictionary"); |
---|
1178 | return NULL; |
---|
1179 | } |
---|
1180 | p++; |
---|
1181 | break; |
---|
1182 | } |
---|
1183 | if (!first && !comma) |
---|
1184 | { |
---|
1185 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Missing ',' in Dictionary: '%s'", prev_element); |
---|
1186 | return NULL; |
---|
1187 | } |
---|
1188 | ret = args[1].deserialize(p); |
---|
1189 | prev_element = p; first = false; comma = false; |
---|
1190 | if ((!ret) || (args[1].getType() != TString)) { p = NULL; break; } |
---|
1191 | p = ret; |
---|
1192 | if (*p != ':') { logPrintf("ExtValue", "deserialize", LOG_ERROR, "Missing ':' in Dictionary: '%s'", p); p = NULL; break; } |
---|
1193 | p++; |
---|
1194 | ret = args[0].deserialize(p); |
---|
1195 | if (!ret) { p = NULL; break; } |
---|
1196 | p = ret; |
---|
1197 | dic->p_set(args, &dummy_ret); |
---|
1198 | if (*p == ',') { p++; comma = true; } |
---|
1199 | } |
---|
1200 | setObject(o); |
---|
1201 | return p; |
---|
1202 | } |
---|
1203 | else if (!strncmp(in, "null", 4)) |
---|
1204 | { |
---|
1205 | setEmpty(); |
---|
1206 | return in + 4; |
---|
1207 | } |
---|
1208 | else if (!strncmp(in, "true", 4)) |
---|
1209 | { |
---|
1210 | setInt(1); |
---|
1211 | return in + 4; |
---|
1212 | } |
---|
1213 | else if (!strncmp(in, "false", 5)) |
---|
1214 | { |
---|
1215 | setInt(0); |
---|
1216 | return in + 5; |
---|
1217 | } |
---|
1218 | else if (!strncmp(in, "invalid", 7)) |
---|
1219 | { |
---|
1220 | setInvalid(); |
---|
1221 | return in + 7; |
---|
1222 | } |
---|
1223 | else if (*in == '<') |
---|
1224 | { //unserializable object |
---|
1225 | const char* end = in + 1; |
---|
1226 | while (*end) |
---|
1227 | if (*end == '>') |
---|
1228 | { |
---|
1229 | setError(SString("Unserializable class: ") + SString(in + 1, end - in - 1)); |
---|
1230 | return end + 1; |
---|
1231 | } |
---|
1232 | else |
---|
1233 | end++; |
---|
1234 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Missing '>'"); |
---|
1235 | return NULL; |
---|
1236 | } |
---|
1237 | else if (*in == '^') |
---|
1238 | { |
---|
1239 | in++; |
---|
1240 | ExtValue ref; |
---|
1241 | ret = ref.parseNumber(in, TInt); |
---|
1242 | if (ret && (ref.getType() == TInt)) |
---|
1243 | { |
---|
1244 | const ExtObject* o = tlsGetRef(ExtObject_serialization).get(ref.getInt()); |
---|
1245 | if (o) |
---|
1246 | { |
---|
1247 | setObject(*o); |
---|
1248 | return ret; |
---|
1249 | } |
---|
1250 | } |
---|
1251 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Invalid reference: '%s'", in - 1); |
---|
1252 | return NULL; |
---|
1253 | } |
---|
1254 | else if ((ret = skipWord(in)) && (ret != in)) |
---|
1255 | { |
---|
1256 | SString clsname(in, (int)(ret - in)); |
---|
1257 | ExtValue tmp; |
---|
1258 | ret = tmp.deserialize(ret); |
---|
1259 | ParamInterface *cls = findDeserializableClass(clsname.c_str()); |
---|
1260 | if (cls && (tmp.getType() != TUnknown) && (tmp.getType() != TInvalid)) |
---|
1261 | { |
---|
1262 | VectorObject *vec = VectorObject::fromObject(tmp.getObject(), false); |
---|
1263 | if (vec) |
---|
1264 | { |
---|
1265 | int m = cls->findId("newFromVector"); |
---|
1266 | if (m >= 0) |
---|
1267 | { |
---|
1268 | cls->call(m, &tmp, this); |
---|
1269 | tlsGetRef(ExtObject_serialization).replace(tmp.getObject(), getObject()); |
---|
1270 | return ret; |
---|
1271 | } |
---|
1272 | } |
---|
1273 | DictionaryObject *dic = DictionaryObject::fromObject(tmp.getObject(), false); |
---|
1274 | if (dic) |
---|
1275 | { |
---|
1276 | int m = cls->findId("newFromDictionary"); |
---|
1277 | if (m >= 0) |
---|
1278 | { |
---|
1279 | cls->call(m, &tmp, this); |
---|
1280 | tlsGetRef(ExtObject_serialization).replace(tmp.getObject(), getObject()); |
---|
1281 | return ret; |
---|
1282 | } |
---|
1283 | } |
---|
1284 | if (tmp.getType() == TString) |
---|
1285 | { |
---|
1286 | int m = cls->findId("newFromString"); |
---|
1287 | if (m >= 0) |
---|
1288 | { |
---|
1289 | cls->call(m, &tmp, this); |
---|
1290 | tlsGetRef(ExtObject_serialization).replace(tmp.getObject(), getObject()); |
---|
1291 | return ret; |
---|
1292 | } |
---|
1293 | } |
---|
1294 | tlsGetRef(ExtObject_serialization).remove(tmp.getObject()); |
---|
1295 | setEmpty(); |
---|
1296 | } |
---|
1297 | setEmpty(); |
---|
1298 | logPrintf("ExtValue", "deserialize", LOG_WARN, "object of class \"%s\" could not be deserialized", clsname.c_str()); |
---|
1299 | return ret; |
---|
1300 | } |
---|
1301 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Bad syntax: '%s'", in); |
---|
1302 | setEmpty(); |
---|
1303 | return NULL; |
---|
1304 | } |
---|
1305 | |
---|
1306 | const char* ExtValue::deserialize(const char* in) |
---|
1307 | { |
---|
1308 | tlsGetRef(ExtObject_serialization).begin(); |
---|
1309 | const char* ret = deserialize_inner(in); |
---|
1310 | if (ret) while (isspace(*ret)) ret++; |
---|
1311 | tlsGetRef(ExtObject_serialization).end(); |
---|
1312 | return ret; |
---|
1313 | } |
---|
1314 | |
---|
1315 | ExtObject ExtValue::getObject() const |
---|
1316 | { |
---|
1317 | if (type == TObj) return odata(); |
---|
1318 | return ExtObject(); |
---|
1319 | } |
---|
1320 | |
---|
1321 | ExtValue ExtValue::getExtType() |
---|
1322 | { |
---|
1323 | static const char* typenames[] = { "null", "int", "float", "string", "", "invalid" }; |
---|
1324 | if (getType() != TObj) |
---|
1325 | return ExtValue(typenames[(int)getType()]); |
---|
1326 | ExtObject& o = odata(); |
---|
1327 | return ExtValue(SString(o.isEmpty() ? "" : o.interfaceName())); |
---|
1328 | } |
---|
1329 | |
---|
1330 | SString SString::valueOf(const ExtValue& v) |
---|
1331 | { |
---|
1332 | return v.getString(); |
---|
1333 | } |
---|
1334 | SString SString::valueOf(const ExtObject& v) |
---|
1335 | { |
---|
1336 | return v.toString(); |
---|
1337 | } |
---|
1338 | |
---|
1339 | #define FIELDSTRUCT ErrorObject |
---|
1340 | static ParamEntry errorobject_paramtab[] = |
---|
1341 | { |
---|
1342 | { "Error", 1, 3, "Error", }, |
---|
1343 | { "message", 0, 0, "Message", "s", FIELD(message), }, |
---|
1344 | { "newFromString", 0, 0, "create new object", "p oError(s message)", PROCEDURE(p_newfromstring), }, |
---|
1345 | { "toString", 0, PARAM_READONLY | PARAM_NOSTATIC, "Textual form", "s", GETONLY(toString), }, |
---|
1346 | { 0, 0, 0, }, |
---|
1347 | }; |
---|
1348 | #undef FIELDSTRUCT |
---|
1349 | |
---|
1350 | Param& ErrorObject::getParam() |
---|
1351 | { |
---|
1352 | static Param param(errorobject_paramtab); |
---|
1353 | return param; |
---|
1354 | } |
---|
1355 | |
---|
1356 | ExtObject ErrorObject::makeDynamicObject(ErrorObject* e) |
---|
1357 | { |
---|
1358 | return ExtObject(&getParam(), (DestrBase*)e); |
---|
1359 | } |
---|
1360 | |
---|
1361 | const SString ErrorObject::TO_STRING_PREFIX = "Error: "; |
---|
1362 | |
---|
1363 | void ErrorObject::get_toString(ExtValue* ret) |
---|
1364 | { |
---|
1365 | ret->setString(TO_STRING_PREFIX + message); |
---|
1366 | } |
---|
1367 | |
---|
1368 | void ErrorObject::p_newfromstring(ExtValue *args, ExtValue *ret) |
---|
1369 | { |
---|
1370 | ErrorObject *err = new ErrorObject(); |
---|
1371 | err->message = args[0].getString(); |
---|
1372 | if (err->message.startsWith(TO_STRING_PREFIX.c_str())) |
---|
1373 | err->message = err->message.substr(TO_STRING_PREFIX.len()); |
---|
1374 | *ret = makeDynamicObject(err); |
---|
1375 | } |
---|
1376 | |
---|
1377 | REGISTER_DESERIALIZABLE(ErrorObject) |
---|