Changeset 522 for cpp/frams/util
- Timestamp:
- 06/23/16 00:15:59 (9 years ago)
- Location:
- cpp/frams/util
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/util/extvalue.cpp
r490 r522 104 104 } 105 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;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 121 } 122 122 … … 205 205 206 206 if (ref >= 0) 207 208 switch (format)209 210 211 212 213 207 { 208 switch (format) 209 { 210 case NativeSerialization: return SString::sprintf("^%d", ref); 211 case JSONSerialization: return SString("null"); 212 } 213 } 214 214 215 215 if (isEmpty()) return SString("null"); 216 216 { 217 VectorObject *vec = VectorObject::fromObject(*this, false); 218 if (vec) 219 { ret=vec->serialize(format); goto finally; } 220 } 221 { 222 DictionaryObject *dic = DictionaryObject::fromObject(*this, false); 223 if (dic) 224 { ret=dic->serialize(format); goto finally; } 225 } 226 { 227 Param tmp_param; 228 ParamInterface *p = getParamInterface(tmp_param); 229 int m = p->findId("toVector"); 230 if (m < 0) 231 m = p->findId("toDictionary"); 232 if (m >= 0) 233 { 234 ExtObject o(p->getObject(m)); 235 switch(format) 236 { 237 case NativeSerialization: ret=SString(interfaceName())+o.serialize(format); break; 238 case JSONSerialization: ret=SString::sprintf("{\"class\":\"%s\",\"data\":%s}",interfaceName(),o.serialize(format).c_str()); break; 239 } 240 goto finally; 241 } 242 m = p->findId("toString"); 243 if (m >= 0) 244 { 245 SString str = p->getString(m); 246 sstringQuote(str); 247 switch(format) 248 { 249 case NativeSerialization: ret=SString(interfaceName())+"\"" +str+"\""; break; 250 case JSONSerialization: ret=SString::sprintf("{\"class\":\"%s\",\"data\":\"%s\"}",interfaceName(),str.c_str()); break; 251 } 252 goto finally; 253 } 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 } 254 258 } 255 259 256 260 tlsGetRef(ExtObject_serialization).remove(*this);//undo nonserializable reference 257 switch (format)258 259 case NativeSerialization: return SString(interfaceName())+SString::sprintf("<%p>",object ? object : paraminterface); break;260 case JSONSerialization: return SString::sprintf("{\"class\":\"%s\"}",interfaceName()); break;261 262 263 264 265 switch (format)266 267 case JSONSerialization:268 269 270 271 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 } 272 276 273 277 return ret; … … 359 363 void ExtValue::setError(const SString& msg) 360 364 { 361 ErrorObject *err=new ErrorObject;362 err->message=msg;363 setObject(ErrorObject::makeDynamicObject(err));365 ErrorObject *err = new ErrorObject; 366 err->message = msg; 367 setObject(ErrorObject::makeDynamicObject(err)); 364 368 } 365 369 … … 380 384 static ExtValue::CompareResult compareInvalid(const ExtValue& v) 381 385 { 382 if (v.getType() ==TInvalid) return ExtValue::ResultEqualUnordered;386 if (v.getType() == TInvalid) return ExtValue::ResultEqualUnordered; 383 387 if ((v.getType() == TInt) && (v.getInt() == 0)) return ExtValue::ResultUnequal_RelaxedEqual; 384 388 return ExtValue::ResultMismatch; //comparing anything else with invalid is invalid … … 406 410 else if (src.isNull()) 407 411 return compareNull(*this); 408 if (getType() ==TInvalid)412 if (getType() == TInvalid) 409 413 return compareInvalid(src); 410 else if (src.getType() ==TInvalid)414 else if (src.getType() == TInvalid) 411 415 return compareInvalid(*this); 412 416 switch (type) … … 961 965 return odata().serialize(format); 962 966 case TInvalid: 963 if (format ==NativeSerialization)967 if (format == NativeSerialization) 964 968 return SString("invalid"); 965 969 // else null --v … … 1044 1048 const char* ExtValue::deserialize_inner(const char* in) 1045 1049 { 1046 while (isspace(*in)) in++;1050 while (isspace(*in)) in++; 1047 1051 const char* ret = parseNumber(in); 1048 1052 if (ret) … … 1146 1150 else if (*in == '<') 1147 1151 { //unserializable object 1148 const char* end =in+1;1152 const char* end = in + 1; 1149 1153 while (*end) 1150 1154 if (*end == '>') 1151 1152 setError(SString("Unserializable class: ")+SString(in+1,end-in-1));1153 return end+1;1154 1155 { 1156 setError(SString("Unserializable class: ") + SString(in + 1, end - in - 1)); 1157 return end + 1; 1158 } 1155 1159 else 1156 1160 end++; … … 1231 1235 tlsGetRef(ExtObject_serialization).begin(); 1232 1236 const char* ret = deserialize_inner(in); 1233 if (ret) while (isspace(*ret)) ret++;1237 if (ret) while (isspace(*ret)) ret++; 1234 1238 tlsGetRef(ExtObject_serialization).end(); 1235 1239 return ret; … … 1261 1265 1262 1266 #define FIELDSTRUCT ErrorObject 1263 static ParamEntry errorobject_paramtab[] =1264 { 1265 {"Error",1,3,"Error",},1266 {"message",0,0,"Message","s",FIELD(message),},1267 {"newFromString",0,0,"create new object","p oError(s message)",PROCEDURE(p_newfromstring),},1268 {"toString",0,PARAM_READONLY | PARAM_NOSTATIC,"Textual form","s",GETONLY(toString),},1269 {0,0,0,},1267 static ParamEntry errorobject_paramtab[] = 1268 { 1269 { "Error", 1, 3, "Error", }, 1270 { "message", 0, 0, "Message", "s", FIELD(message), }, 1271 { "newFromString", 0, 0, "create new object", "p oError(s message)", PROCEDURE(p_newfromstring), }, 1272 { "toString", 0, PARAM_READONLY | PARAM_NOSTATIC, "Textual form", "s", GETONLY(toString), }, 1273 { 0, 0, 0, }, 1270 1274 }; 1271 1275 #undef FIELDSTRUCT … … 1282 1286 } 1283 1287 1284 const SString ErrorObject::TO_STRING_PREFIX ="Error: ";1288 const SString ErrorObject::TO_STRING_PREFIX = "Error: "; 1285 1289 1286 1290 void ErrorObject::get_toString(ExtValue* ret) 1287 1291 { 1288 ret->setString(TO_STRING_PREFIX+message);1292 ret->setString(TO_STRING_PREFIX + message); 1289 1293 } 1290 1294 1291 1295 void ErrorObject::p_newfromstring(ExtValue *args, ExtValue *ret) 1292 1296 { 1293 ErrorObject *err=new ErrorObject();1294 err->message=args[0].getString();1295 if (err->message.startsWith(TO_STRING_PREFIX.c_str()))1296 err->message=err->message.substr(TO_STRING_PREFIX.len());1297 *ret = makeDynamicObject(err);1297 ErrorObject *err = new ErrorObject(); 1298 err->message = args[0].getString(); 1299 if (err->message.startsWith(TO_STRING_PREFIX.c_str())) 1300 err->message = err->message.substr(TO_STRING_PREFIX.len()); 1301 *ret = makeDynamicObject(err); 1298 1302 } 1299 1303 -
cpp/frams/util/extvalue.h
r490 r522 70 70 void* getTarget() const { return (subtype & 1) ? dbobject : object; } 71 71 void* getTarget(const char* classname, bool through_barrier = true, bool warn = true) const; 72 bool callDelegate(const char* delegate, ExtValue *args,ExtValue *ret);72 bool callDelegate(const char* delegate, ExtValue *args, ExtValue *ret); 73 73 void setEmpty() { decref(); subtype = 0; param = NULL; object = NULL; } 74 74 int isEmpty() const { return !param; } … … 84 84 SString serialize(SerializationFormat format) const; 85 85 86 ExtObject(Param *p, void *o) :subtype(2), object(o), param(p) { DEBUG_EXTOBJECT("(Param,void)"); }86 ExtObject(Param *p, void *o) :subtype(2), object(o), param(p) { DEBUG_EXTOBJECT("(Param,void)"); } 87 87 ExtObject(ParamInterface *p = 0) :subtype(0), object(0), paraminterface(p) { DEBUG_EXTOBJECT("(ParamInterface)"); } 88 88 ExtObject(Param *p, DestrBase *o) :subtype(1 + 2), dbobject(o), param(p){ DEBUG_EXTOBJECT("(Param,DestrBase)"); incref(); } … … 244 244 #define REGISTER_DESERIALIZABLE(name) ExtValue::AddDeserializable<name> deserializable_autoinit_ ## name; 245 245 246 class ErrorObject : public DestrBase247 { 248 249 SString message;250 static Param& getParam();251 static Param& getStaticParam() {return getParam();}252 static ExtObject makeDynamicObject(ErrorObject* e);253 static const SString TO_STRING_PREFIX;246 class ErrorObject : public DestrBase 247 { 248 public: 249 SString message; 250 static Param& getParam(); 251 static Param& getStaticParam() { return getParam(); } 252 static ExtObject makeDynamicObject(ErrorObject* e); 253 static const SString TO_STRING_PREFIX; 254 254 #define STATRICKCLASS ErrorObject 255 PARAMGETDEF(toString);256 PARAMPROCDEF(p_newfromstring);255 PARAMGETDEF(toString); 256 PARAMPROCDEF(p_newfromstring); 257 257 #undef STATRICKCLASS 258 258 };
Note: See TracChangeset
for help on using the changeset viewer.