Changeset 478 for cpp/frams/vm/classes
- Timestamp:
- 03/22/16 01:19:47 (9 years ago)
- Location:
- cpp/frams/vm/classes
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/vm/classes/collectionobj.cpp
r464 r478 49 49 ParamEntry dictionary_paramtab[]= 50 50 { 51 {"Dictionary",1,1 0,"Dictionary","Dictionary associates stored values with string keys "51 {"Dictionary",1,11,"Dictionary","Dictionary associates stored values with string keys " 52 52 "(\"key\" is the first argument in get/set/remove functions). Integer key can be " 53 53 "used to enumerate all elements (note that while iterating, the elements are returned in no particular order).\n" … … 76 76 {"toString",0,PARAM_READONLY | PARAM_NOSTATIC,"Textual form","s",GETONLY(toString),}, 77 77 {"clone",0,PARAM_NOSTATIC,"Create a clone","p oDictionary()",PROCEDURE(p_clone),"The resulting clone is a shallow copy (contains the same object references as the original). A deep copy can be obtained through serialization: String.deserialize(String.serialize(object));"}, 78 {"assign",0,PARAM_NOSTATIC,"Assign from another object","p(x)",PROCEDURE(p_assign),"Replaces current dictionary with dictionary contents from another object."}, 79 78 80 {0,0,0,}, 79 81 }; … … 372 374 } 373 375 376 ExtValue DictionaryObject::get(SString key) 377 { 378 ExtValue *val=(ExtValue*)hash.get(key); 379 if (val) 380 return *val; 381 return ExtValue::empty(); 382 } 383 384 ExtValue DictionaryObject::get(int index) 385 { 386 HashEntryIterator* iter=getIndexIterator(index); 387 if (iter && (*iter)->value) 388 return *((ExtValue*)(*iter)->value); 389 return ExtValue::empty(); 390 } 391 374 392 void DictionaryObject::p_get(PARAMPROCARGS) 375 393 { 376 394 if ((args->type==TInt)||(args->type==TDouble)) 377 { 378 HashEntryIterator* iter=getIndexIterator(args->getInt()); 379 if (iter && (*iter)->value) 380 { 381 *ret=*((ExtValue*)(*iter)->value); 382 return; 383 } 384 } 385 else 386 { 387 ExtValue *val=(ExtValue*)hash.get(args[0].getString()); 388 if (val) 389 { 390 *ret=*val; 391 return; 392 } 393 } 394 *ret=ExtValue(); 395 *ret=get(args->getInt()); 396 else 397 *ret=get(args[0].getString()); 395 398 } 396 399 … … 406 409 } 407 410 411 ExtValue DictionaryObject::set(SString key,ExtValue new_value) 412 { 413 ExtValue ret; 414 ExtValue *new_ext=(new_value.getType()==TUnknown) ? NULL : new ExtValue(new_value); 415 ExtValue *old_ext=(ExtValue*)hash.put(key,new_ext); 416 if (old_ext) { ret=*old_ext; delete old_ext;} 417 return ret; 418 } 419 408 420 void DictionaryObject::p_set(PARAMPROCARGS) 409 421 { 410 ExtValue *newval=(args[0].getType()==TUnknown)?0:new ExtValue(args[0]); 411 ExtValue *oldval=(ExtValue*)hash.put(args[1].getString(),newval); 412 if (oldval) {*ret=*oldval; delete oldval;} else *ret=ExtValue(); 422 *ret=set(args[1].getString(),args[0]); 413 423 } 414 424 … … 461 471 } 462 472 473 void DictionaryObject::copyFrom(DictionaryObject *other) 474 { 475 for(HashEntryIterator it(other->hash);it.isValid();it++) 476 { 477 ExtValue *v=(ExtValue*)it->value; 478 hash.put(it->key,v?new ExtValue(*v):NULL); 479 } 480 } 481 463 482 void DictionaryObject::p_clone(PARAMPROCARGS) 464 483 { 465 484 DictionaryObject *c=new DictionaryObject; 466 for(HashEntryIterator it(hash);it.isValid();it++) 467 { 468 ExtValue *v=(ExtValue*)it->value; 469 c->hash.put(it->key,v?new ExtValue(*v):NULL); 470 } 485 c->copyFrom(this); 471 486 ret->setObject(ExtObject(&par,c)); 487 } 488 489 void DictionaryObject::p_assign(PARAMPROCARGS) 490 { 491 clear(); 492 DictionaryObject *other=DictionaryObject::fromObject(args[0].getObject(),false); 493 if (other) 494 copyFrom(other); 495 ret->setEmpty(); 472 496 } 473 497 -
cpp/frams/vm/classes/collectionobj.h
r464 r478 72 72 PARAMGETDEF(toString); 73 73 PARAMPROCDEF(p_clone); 74 PARAMPROCDEF(p_assign); 74 75 #undef STATRICKCLASS 76 ExtValue get(SString key); 77 ExtValue get(int index); 78 ExtValue set(SString key,ExtValue new_value); 79 void copyFrom(DictionaryObject *other); 75 80 SString serialize(SerializationFormat format) const; 76 81 static void p_new(void*,ExtValue*args,ExtValue*ret)
Note: See TracChangeset
for help on using the changeset viewer.