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 (errno == ERANGE) |
---|
1071 | { |
---|
1072 | logPrintf("ExtValue", "parseNumber", LOG_WARN, "Unrepresentable number and strtod() signalled ERANGE: parsed %d chars of '%s' and returned '%f'", after - in, in, fpvalue); |
---|
1073 | if (fpvalue == 0) errno = 0; //otherwise we have +inf or -inf from a string number because it was too large for double-type, and we don't want to have infinities in ExtValue so we leave errno!=0 |
---|
1074 | } |
---|
1075 | if ((after > in) && (errno == 0)) |
---|
1076 | { |
---|
1077 | if (strict_type != TDouble) |
---|
1078 | { |
---|
1079 | if ((memchr(in, '.', after - in) == NULL) && (memchr(in, 'e', after - in) == NULL) && (memchr(in, 'E', after - in) == NULL) // no "special" characters |
---|
1080 | && (fpvalue == floor(fpvalue)) // value is integer |
---|
1081 | && (fpvalue >= INT_MIN) && (fpvalue <= INT_MAX)) // within limits |
---|
1082 | { |
---|
1083 | setInt(fpvalue); |
---|
1084 | return after; |
---|
1085 | } |
---|
1086 | else if (strict_type == TInt) |
---|
1087 | return NULL; |
---|
1088 | } |
---|
1089 | setDouble(fpvalue); |
---|
1090 | return after; |
---|
1091 | } |
---|
1092 | return NULL; |
---|
1093 | } |
---|
1094 | |
---|
1095 | PtrListTempl<ParamInterface*> &ExtValue::getDeserializableClasses() |
---|
1096 | { |
---|
1097 | static PtrListTempl<ParamInterface*> classes; |
---|
1098 | return classes; |
---|
1099 | } |
---|
1100 | |
---|
1101 | ParamInterface *ExtValue::findDeserializableClass(const char* name) |
---|
1102 | { |
---|
1103 | FOREACH(ParamInterface*, cls, getDeserializableClasses()) |
---|
1104 | if (!strcmp(cls->getName(), name)) |
---|
1105 | return cls; |
---|
1106 | return NULL; |
---|
1107 | } |
---|
1108 | |
---|
1109 | static const char* skipWord(const char* in) |
---|
1110 | { |
---|
1111 | while (isalpha(*in) || (*in == '_')) |
---|
1112 | in++; |
---|
1113 | return in; |
---|
1114 | } |
---|
1115 | |
---|
1116 | //returns the first character after the parsed portion or NULL if invalid format |
---|
1117 | const char* ExtValue::deserialize_inner(const char* in) |
---|
1118 | { |
---|
1119 | while (isspace(*in)) in++; |
---|
1120 | const char* ret = parseNumber(in); |
---|
1121 | if (ret) |
---|
1122 | return ret; |
---|
1123 | else if (*in == '\"') |
---|
1124 | { |
---|
1125 | ret = skipQuoteString(in + 1, NULL); |
---|
1126 | SString s(in + 1, (int)(ret - (in + 1))); |
---|
1127 | sstringUnquote(s); |
---|
1128 | setString(s); |
---|
1129 | if (*ret == '\"') |
---|
1130 | return ret + 1; |
---|
1131 | else |
---|
1132 | { |
---|
1133 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Missing '\"' in string: '%s'", ret); |
---|
1134 | return NULL; |
---|
1135 | } |
---|
1136 | } |
---|
1137 | else if (*in == '[') |
---|
1138 | { |
---|
1139 | VectorObject *vec = new VectorObject; |
---|
1140 | ExtObject o(&VectorObject::par, vec); |
---|
1141 | tlsGetRef(ExtObject_serialization).add(o); |
---|
1142 | const char* p = in + 1; |
---|
1143 | ExtValue tmp; bool first = true, comma = false; |
---|
1144 | const char* prev_element = p; |
---|
1145 | while (true) |
---|
1146 | { |
---|
1147 | while (isspace(*p)) p++; |
---|
1148 | if (*p == 0) |
---|
1149 | { |
---|
1150 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Missing ']' in Vector"); |
---|
1151 | return NULL; |
---|
1152 | } |
---|
1153 | if (*p == ']') |
---|
1154 | { |
---|
1155 | if (comma) |
---|
1156 | { |
---|
1157 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "No element after ',' in Vector"); |
---|
1158 | return NULL; |
---|
1159 | } |
---|
1160 | p++; |
---|
1161 | break; |
---|
1162 | } |
---|
1163 | if (!first && !comma) |
---|
1164 | { |
---|
1165 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Missing ',' in Vector: '%s'", prev_element); |
---|
1166 | return NULL; |
---|
1167 | } |
---|
1168 | ret = tmp.deserialize(p); |
---|
1169 | prev_element = p; first = false; comma = false; |
---|
1170 | if (ret) |
---|
1171 | { |
---|
1172 | vec->data += new ExtValue(tmp); |
---|
1173 | p = ret; |
---|
1174 | if (*p == ',') { p++; comma = true; } |
---|
1175 | } |
---|
1176 | else |
---|
1177 | { |
---|
1178 | p = NULL; |
---|
1179 | break; |
---|
1180 | } |
---|
1181 | } |
---|
1182 | setObject(o); |
---|
1183 | return p; |
---|
1184 | } |
---|
1185 | else if (*in == '{') |
---|
1186 | { |
---|
1187 | DictionaryObject *dic = new DictionaryObject; |
---|
1188 | ExtObject o(&DictionaryObject::par, dic); |
---|
1189 | tlsGetRef(ExtObject_serialization).add(o); |
---|
1190 | const char* p = in + 1; |
---|
1191 | ExtValue args[2]/*={value,key}*/, dummy_ret; |
---|
1192 | bool first = true, comma = false; |
---|
1193 | const char* prev_element = p; |
---|
1194 | while (true) |
---|
1195 | { |
---|
1196 | while (isspace(*p)) p++; |
---|
1197 | if (*p == 0) |
---|
1198 | { |
---|
1199 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Missing '}' in Dictionary"); |
---|
1200 | return NULL; |
---|
1201 | } |
---|
1202 | if (*p == '}') |
---|
1203 | { |
---|
1204 | if (comma) |
---|
1205 | { |
---|
1206 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "No element after ',' in Dictionary"); |
---|
1207 | return NULL; |
---|
1208 | } |
---|
1209 | p++; |
---|
1210 | break; |
---|
1211 | } |
---|
1212 | if (!first && !comma) |
---|
1213 | { |
---|
1214 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Missing ',' in Dictionary: '%s'", prev_element); |
---|
1215 | return NULL; |
---|
1216 | } |
---|
1217 | ret = args[1].deserialize(p); |
---|
1218 | prev_element = p; first = false; comma = false; |
---|
1219 | if ((!ret) || (args[1].getType() != TString)) { p = NULL; break; } |
---|
1220 | p = ret; |
---|
1221 | if (*p != ':') { logPrintf("ExtValue", "deserialize", LOG_ERROR, "Missing ':' in Dictionary: '%s'", p); p = NULL; break; } |
---|
1222 | p++; |
---|
1223 | ret = args[0].deserialize(p); |
---|
1224 | if (!ret) { p = NULL; break; } |
---|
1225 | p = ret; |
---|
1226 | dic->p_set(args, &dummy_ret); |
---|
1227 | if (*p == ',') { p++; comma = true; } |
---|
1228 | } |
---|
1229 | setObject(o); |
---|
1230 | return p; |
---|
1231 | } |
---|
1232 | else if (!strncmp(in, "null", 4)) |
---|
1233 | { |
---|
1234 | setEmpty(); |
---|
1235 | return in + 4; |
---|
1236 | } |
---|
1237 | else if (!strncmp(in, "true", 4)) |
---|
1238 | { |
---|
1239 | setInt(1); |
---|
1240 | return in + 4; |
---|
1241 | } |
---|
1242 | else if (!strncmp(in, "false", 5)) |
---|
1243 | { |
---|
1244 | setInt(0); |
---|
1245 | return in + 5; |
---|
1246 | } |
---|
1247 | else if (!strncmp(in, "invalid", 7)) |
---|
1248 | { |
---|
1249 | setInvalid(); |
---|
1250 | return in + 7; |
---|
1251 | } |
---|
1252 | else if (*in == '<') |
---|
1253 | { //unserializable object |
---|
1254 | const char* end = in + 1; |
---|
1255 | while (*end) |
---|
1256 | if (*end == '>') |
---|
1257 | { |
---|
1258 | setError(SString("Unserializable class: ") + SString(in + 1, end - in - 1)); |
---|
1259 | return end + 1; |
---|
1260 | } |
---|
1261 | else |
---|
1262 | end++; |
---|
1263 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Missing '>'"); |
---|
1264 | return NULL; |
---|
1265 | } |
---|
1266 | else if (*in == '^') |
---|
1267 | { |
---|
1268 | in++; |
---|
1269 | ExtValue ref; |
---|
1270 | ret = ref.parseNumber(in, TInt); |
---|
1271 | if (ret && (ref.getType() == TInt)) |
---|
1272 | { |
---|
1273 | const ExtObject* o = tlsGetRef(ExtObject_serialization).get(ref.getInt()); |
---|
1274 | if (o) |
---|
1275 | { |
---|
1276 | setObject(*o); |
---|
1277 | return ret; |
---|
1278 | } |
---|
1279 | } |
---|
1280 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Invalid reference: '%s'", in - 1); |
---|
1281 | return NULL; |
---|
1282 | } |
---|
1283 | else if ((ret = skipWord(in)) && (ret != in)) |
---|
1284 | { |
---|
1285 | SString clsname(in, (int)(ret - in)); |
---|
1286 | ExtValue tmp; |
---|
1287 | ret = tmp.deserialize(ret); |
---|
1288 | ParamInterface *cls = findDeserializableClass(clsname.c_str()); |
---|
1289 | if (cls && (tmp.getType() != TUnknown) && (tmp.getType() != TInvalid)) |
---|
1290 | { |
---|
1291 | VectorObject *vec = VectorObject::fromObject(tmp.getObject(), false); |
---|
1292 | if (vec) |
---|
1293 | { |
---|
1294 | int m = cls->findId("newFromVector"); |
---|
1295 | if (m >= 0) |
---|
1296 | { |
---|
1297 | cls->call(m, &tmp, this); |
---|
1298 | tlsGetRef(ExtObject_serialization).replace(tmp.getObject(), getObject()); |
---|
1299 | return ret; |
---|
1300 | } |
---|
1301 | } |
---|
1302 | DictionaryObject *dic = DictionaryObject::fromObject(tmp.getObject(), false); |
---|
1303 | if (dic) |
---|
1304 | { |
---|
1305 | int m = cls->findId("newFromDictionary"); |
---|
1306 | if (m >= 0) |
---|
1307 | { |
---|
1308 | cls->call(m, &tmp, this); |
---|
1309 | tlsGetRef(ExtObject_serialization).replace(tmp.getObject(), getObject()); |
---|
1310 | return ret; |
---|
1311 | } |
---|
1312 | } |
---|
1313 | if (tmp.getType() == TString) |
---|
1314 | { |
---|
1315 | int m = cls->findId("newFromString"); |
---|
1316 | if (m >= 0) |
---|
1317 | { |
---|
1318 | cls->call(m, &tmp, this); |
---|
1319 | tlsGetRef(ExtObject_serialization).replace(tmp.getObject(), getObject()); |
---|
1320 | return ret; |
---|
1321 | } |
---|
1322 | } |
---|
1323 | tlsGetRef(ExtObject_serialization).remove(tmp.getObject()); |
---|
1324 | setEmpty(); |
---|
1325 | } |
---|
1326 | setEmpty(); |
---|
1327 | logPrintf("ExtValue", "deserialize", LOG_WARN, "object of class \"%s\" could not be deserialized", clsname.c_str()); |
---|
1328 | return ret; |
---|
1329 | } |
---|
1330 | logPrintf("ExtValue", "deserialize", LOG_ERROR, "Bad syntax: '%s'", in); |
---|
1331 | setEmpty(); |
---|
1332 | return NULL; |
---|
1333 | } |
---|
1334 | |
---|
1335 | const char* ExtValue::deserialize(const char* in) |
---|
1336 | { |
---|
1337 | tlsGetRef(ExtObject_serialization).begin(); |
---|
1338 | const char* ret = deserialize_inner(in); |
---|
1339 | if (ret) while (isspace(*ret)) ret++; |
---|
1340 | tlsGetRef(ExtObject_serialization).end(); |
---|
1341 | return ret; |
---|
1342 | } |
---|
1343 | |
---|
1344 | ExtObject ExtValue::getObject() const |
---|
1345 | { |
---|
1346 | if (type == TObj) return odata(); |
---|
1347 | return ExtObject(); |
---|
1348 | } |
---|
1349 | |
---|
1350 | ExtValue ExtValue::getExtType() |
---|
1351 | { |
---|
1352 | static const char* typenames[] = { "null", "int", "float", "string", "", "invalid" }; |
---|
1353 | if (getType() != TObj) |
---|
1354 | return ExtValue(typenames[(int)getType()]); |
---|
1355 | ExtObject& o = odata(); |
---|
1356 | return ExtValue(SString(o.isEmpty() ? "" : o.interfaceName())); |
---|
1357 | } |
---|
1358 | |
---|
1359 | SString SString::valueOf(const ExtValue& v) |
---|
1360 | { |
---|
1361 | return v.getString(); |
---|
1362 | } |
---|
1363 | SString SString::valueOf(const ExtObject& v) |
---|
1364 | { |
---|
1365 | return v.toString(); |
---|
1366 | } |
---|
1367 | |
---|
1368 | #define FIELDSTRUCT ErrorObject |
---|
1369 | static ParamEntry errorobject_paramtab[] = |
---|
1370 | { |
---|
1371 | { "Error", 1, 3, "Error", }, |
---|
1372 | { "message", 0, 0, "Message", "s", FIELD(message), }, |
---|
1373 | { "newFromString", 0, 0, "create new object", "p oError(s message)", PROCEDURE(p_newfromstring), }, |
---|
1374 | { "toString", 0, PARAM_READONLY | PARAM_NOSTATIC, "Textual form", "s", GETONLY(toString), }, |
---|
1375 | { 0, 0, 0, }, |
---|
1376 | }; |
---|
1377 | #undef FIELDSTRUCT |
---|
1378 | |
---|
1379 | Param& ErrorObject::getParam() |
---|
1380 | { |
---|
1381 | static Param param(errorobject_paramtab); |
---|
1382 | return param; |
---|
1383 | } |
---|
1384 | |
---|
1385 | ExtObject ErrorObject::makeDynamicObject(ErrorObject* e) |
---|
1386 | { |
---|
1387 | return ExtObject(&getParam(), (DestrBase*)e); |
---|
1388 | } |
---|
1389 | |
---|
1390 | const SString ErrorObject::TO_STRING_PREFIX = "Error: "; |
---|
1391 | |
---|
1392 | void ErrorObject::get_toString(ExtValue* ret) |
---|
1393 | { |
---|
1394 | ret->setString(TO_STRING_PREFIX + message); |
---|
1395 | } |
---|
1396 | |
---|
1397 | void ErrorObject::p_newfromstring(ExtValue *args, ExtValue *ret) |
---|
1398 | { |
---|
1399 | ErrorObject *err = new ErrorObject(); |
---|
1400 | err->message = args[0].getString(); |
---|
1401 | if (err->message.startsWith(TO_STRING_PREFIX.c_str())) |
---|
1402 | err->message = err->message.substr(TO_STRING_PREFIX.len()); |
---|
1403 | *ret = makeDynamicObject(err); |
---|
1404 | } |
---|
1405 | |
---|
1406 | REGISTER_DESERIALIZABLE(ErrorObject) |
---|