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