Changeset 478 for cpp/frams/vm/classes


Ignore:
Timestamp:
03/22/16 01:19:47 (9 years ago)
Author:
Maciej Komosinski
Message:

Accessing const objects, short -> paInt, less critical messages when not necessary, accessing dictionaries with "->"

Location:
cpp/frams/vm/classes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/vm/classes/collectionobj.cpp

    r464 r478  
    4949ParamEntry dictionary_paramtab[]=
    5050{
    51 {"Dictionary",1,10,"Dictionary","Dictionary associates stored values with string keys "
     51{"Dictionary",1,11,"Dictionary","Dictionary associates stored values with string keys "
    5252 "(\"key\" is the first argument in get/set/remove functions). Integer key can be "
    5353 "used to enumerate all elements (note that while iterating, the elements are returned in no particular order).\n"
     
    7676{"toString",0,PARAM_READONLY | PARAM_NOSTATIC,"Textual form","s",GETONLY(toString),},
    7777{"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
    7880{0,0,0,},
    7981};
     
    372374}
    373375
     376ExtValue DictionaryObject::get(SString key)
     377{
     378ExtValue *val=(ExtValue*)hash.get(key);
     379if (val)
     380        return *val;
     381return ExtValue::empty();
     382}
     383
     384ExtValue DictionaryObject::get(int index)
     385{
     386HashEntryIterator* iter=getIndexIterator(index);
     387if (iter && (*iter)->value)
     388        return *((ExtValue*)(*iter)->value);
     389return ExtValue::empty();
     390}
     391
    374392void DictionaryObject::p_get(PARAMPROCARGS)
    375393{
    376394if ((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());
     396else
     397        *ret=get(args[0].getString());
    395398}
    396399
     
    406409}
    407410
     411ExtValue DictionaryObject::set(SString key,ExtValue new_value)
     412{
     413ExtValue ret;
     414ExtValue *new_ext=(new_value.getType()==TUnknown) ? NULL : new ExtValue(new_value);
     415ExtValue *old_ext=(ExtValue*)hash.put(key,new_ext);
     416if (old_ext) { ret=*old_ext; delete old_ext;}
     417return ret;
     418}
     419
    408420void DictionaryObject::p_set(PARAMPROCARGS)
    409421{
    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]);
    413423}
    414424
     
    461471}
    462472
     473void DictionaryObject::copyFrom(DictionaryObject *other)
     474{
     475for(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
    463482void DictionaryObject::p_clone(PARAMPROCARGS)
    464483{
    465484DictionaryObject *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         }
     485c->copyFrom(this);
    471486ret->setObject(ExtObject(&par,c));
     487}
     488
     489void DictionaryObject::p_assign(PARAMPROCARGS)
     490{
     491clear();
     492DictionaryObject *other=DictionaryObject::fromObject(args[0].getObject(),false);
     493if (other)
     494                copyFrom(other);
     495ret->setEmpty();
    472496}
    473497
  • cpp/frams/vm/classes/collectionobj.h

    r464 r478  
    7272PARAMGETDEF(toString);
    7373PARAMPROCDEF(p_clone);
     74PARAMPROCDEF(p_assign);
    7475#undef STATRICKCLASS
     76ExtValue get(SString key);
     77ExtValue get(int index);
     78ExtValue set(SString key,ExtValue new_value);
     79void copyFrom(DictionaryObject *other);
    7580SString serialize(SerializationFormat format) const;
    7681static void p_new(void*,ExtValue*args,ExtValue*ret)
Note: See TracChangeset for help on using the changeset viewer.