Changeset 478 for cpp


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
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/param/mutableparam.cpp

    r348 r478  
    171171ParamEntry *pe=new ParamEntry();
    172172pe->fun1=0; pe->fun2=0;
    173 pe->group=(short)group;
    174 pe->flags=(short)(flags | MUTPARAM_ALLOCENTRY);
     173pe->group=(paInt)group;
     174pe->flags=(paInt)(flags | MUTPARAM_ALLOCENTRY);
    175175pe->offset=(intptr_t)data;
    176176pe->id=strdup(id);
  • cpp/frams/param/param.cpp

    r464 r478  
    602602                        if (v.type == TObj)
    603603                        {
    604                                 logPrintf("ParamInterface", "set", LOG_WARN, "Getting integer value from object reference (%s)", v.getString().c_str());
     604                                logPrintf("ParamInterface", "set", LOG_ERROR, "Setting int '%s.%s' from object reference (%s)", getName(), id(i), v.getString().c_str());
    605605                                return 0;
    606606                        }
     
    614614                        if (v.type == TObj)
    615615                        {
    616                                 logPrintf("ParamInterface", "set", LOG_WARN, "Getting floating point value from object reference (%s)", v.getString().c_str());
     616                                logPrintf("ParamInterface", "set", LOG_ERROR, "Setting float '%s.%s' from object reference (%s)", getName(), id(i), v.getString().c_str());
    617617                                return 0;
    618618                        }
     
    621621                }
    622622        case 's': { SString t = v.getString(); return setString(i, t); }
    623         case 'o': return setObject(i, v.getObject());
     623        case 'o':
     624                if ((v.type!=TUnknown)&&(v.type!=TObj))
     625                        logPrintf("ParamInterface", "set", LOG_ERROR, "Setting object '%s.%s' from %s", getName(), id(i), v.typeAndValue().c_str());
     626                else
     627                        return setObject(i, v.getObject());
     628                break;
    624629        case 'x': return setExtValue(i, v);
    625630        default: logPrintf("ParamInterface", "set", LOG_ERROR, "'%s.%s' is not a field", getName(), id(i));
     
    645650                        if ((after == NULL) || (*after))
    646651                        {
    647                                 logPrintf("ParamInterface", "set", LOG_WARN, "serialization format mismatch in %s.%s", (getName() ? getName() : "<Unknown>"), id(i));
     652                                logPrintf("ParamInterface", "set", LOG_ERROR, "serialization format mismatch in %s.%s", (getName() ? getName() : "<Unknown>"), id(i));
    648653                                e.setEmpty();
    649654                        }
     
    956961        ParamEntry *pe = entry(i);
    957962        if (pe->flags&PARAM_READONLY) return PSET_RONLY;
     963        if (pe->flags&PARAM_OBJECTSET)
     964                {
     965                ExtObject o=getObject(i);
     966                Param tmp;
     967                ParamInterface* oif=o.getParamInterface(tmp);
     968                int ass;
     969                if (oif && ((ass=oif->findId("assign"))>=0))
     970                        {
     971                        ExtValue arg=x;
     972                        oif->call(ass,&arg,&v);
     973                        }
     974                else
     975                        logPrintf("SimpleAbstractParam", "setObject", LOG_ERROR,
     976                                  "'%s.%s' is PARAM_OBJECTSET but no 'assign()' in %s", getName(), pe->id, o.interfaceName());
     977                return PSET_CHANGED;
     978                }
    958979        ExtObject xcopy = x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below
    959980        if (pe->fun2)
  • cpp/frams/param/param.h

    r393 r478  
    3333#define PARAM_DEPRECATED  8192  //< this member is deprecated
    3434#define PARAM_LINECOMMENT 16384 //< Param::load() adds "@line ..." comment when loading multiline (internal use)
     35#define PARAM_OBJECTSET 32768   //< setting this field is handled by the object's assign(...) function and cannot change the object reference
    3536
    3637typedef int32_t paInt;
     
    216217{
    217218        const char *id;
    218         short group, flags;
     219        paInt group, flags;
    219220        const char *name, *type;
    220221        intptr_t offset;
     
    227228{
    228229public:
    229         ParamEntryConstructor(const char *_id, short _group = 0, short _flags = 0, const char *_name = 0, const char *_type = 0, intptr_t _offset = 0, void *_fun1 = 0, void *_fun2 = 0, const char *_help = 0)
     230        ParamEntryConstructor(const char *_id, paInt _group = 0, paInt _flags = 0, const char *_name = 0, const char *_type = 0, intptr_t _offset = 0, void *_fun1 = 0, void *_fun2 = 0, const char *_help = 0)
    230231        {
    231232                id = _id; group = _group; flags = _flags; name = _name; type = _type; offset = _offset; fun1 = _fun1; fun2 = _fun2; help = _help;
  • cpp/frams/param/paramobj.cpp

    r396 r478  
    4141        if (addnew) count++;
    4242        t = tab = (ParamEntry*)malloc(sizeof(ParamEntry)*(count + gcount + 1));
    43         t->group = (short)gcount;
    44         t->flags = (short)count;
     43        t->group = (paInt)gcount;
     44        t->flags = (paInt)count;
    4545        t->name = maybedup(dupentries, rename ? rename : pi->getName());
    4646        t->type = maybedup(dupentries, pi->getDescription());
     
    8282                                        offset += sizeof(ExtValue);
    8383                                        }
    84                                 t->group = (short)(stripgroups ? 0 : pi->group(i));
    85                                 t->flags = (short)pi->flags(i);
     84                                t->group = (paInt)(stripgroups ? 0 : pi->group(i));
     85                                t->flags = (paInt)pi->flags(i);
    8686                                if (readonly_into_userreadonly && (t->flags & PARAM_READONLY))
    8787                                        t->flags=(t->flags & ~PARAM_READONLY) | PARAM_USERREADONLY;
  • cpp/frams/param/paramtabobj.cpp

    r286 r478  
    2525memmove(tab+siz-count,p,sizeof(ParamEntry)*count);
    2626memset(tab+siz,0,sizeof(ParamEntry));
    27 if (siz>0) tab[0].flags=(short)(siz-tab[0].group);
     27if (siz>0) tab[0].flags=(paInt)(siz-tab[0].group);
    2828return siz-1;
    2929}
     
    3434resize(siz-count);
    3535memset(tab+siz,0,sizeof(ParamEntry));
    36 if (siz>0) tab[0].flags=(short)(siz-tab[0].group);
     36if (siz>0) tab[0].flags=(paInt)(siz-tab[0].group);
    3737}
    3838
  • cpp/frams/util/extvalue.cpp

    r472 r478  
    104104}
    105105
     106bool ExtObject::callDelegate(const char* delegate,ExtValue *args,ExtValue *ret)
     107{
     108Param tmp;
     109ParamInterface *pi=getParamInterface(tmp);
     110if (pi)
     111        {
     112        int f=pi->findId(delegate);
     113        if (f>=0)
     114                {
     115                pi->call(f,args,ret);
     116                return true;
     117                }
     118        }
     119logPrintf("Genotype","get", LOG_ERROR, "Could not call delegate '%s.%s'", pi?pi->getName():"NULL",delegate);
     120return false;
     121}
    106122
    107123SString ExtObject::toString() const
     
    644660        else
    645661        {
    646                 logPrintf("ExtValue", "divide", LOG_CRITICAL, "Division by zero: %d/0", idata());
     662                logPrintf("ExtValue", "divide", LOG_ERROR, "Division by zero: %d/0", idata());
    647663                setInvalid();
    648664        }
     
    653669        if (a == 0.0)
    654670        {
    655                 logPrintf("ExtValue", "divide", LOG_CRITICAL, "Division by zero: %s/0.0", getString().c_str());
     671                logPrintf("ExtValue", "divide", LOG_ERROR, "Division by zero: %s/0.0", getString().c_str());
    656672                setInvalid();
    657673        }
     
    662678                if (!finite(tmp))
    663679                {
    664                         logPrintf("ExtValue", "divide", LOG_CRITICAL, "Overflow %s/%g", getString().c_str(), a); setInvalid();
     680                        logPrintf("ExtValue", "divide", LOG_ERROR, "Overflow %s/%g", getString().c_str(), a); setInvalid();
    665681                }
    666682                else
     
    868884        case TString: return getInt(sdata().c_str());
    869885        case TObj:
    870                 logPrintf("ExtValue", "getInt", LOG_WARN, "Getting integer value from object reference (%s)", getString().c_str());
     886                logPrintf("ExtValue", "getInt", LOG_ERROR, "Getting integer value from object reference (%s)", getString().c_str());
    871887                return (paInt)(intptr_t)odata().param;
    872888        default:;
     
    883899        case TString: return getDouble(sdata().c_str());
    884900        case TObj:
    885                 logPrintf("ExtValue", "getDouble", LOG_WARN, "Getting floating point value from object reference (%s)", getString().c_str());
     901                logPrintf("ExtValue", "getDouble", LOG_ERROR, "Getting floating point value from object reference (%s)", getString().c_str());
    886902                return (double)(intptr_t)odata().param;
    887903        default:;
  • cpp/frams/util/extvalue.h

    r464 r478  
    6767        void* getTarget() const { return (subtype & 1) ? dbobject : object; }
    6868        void* getTarget(const char* classname, bool through_barrier = true, bool warn = true) const;
     69        bool callDelegate(const char* delegate,ExtValue *args,ExtValue *ret);
    6970        void setEmpty() { decref(); subtype = 0; param = NULL; object = NULL; }
    7071        int isEmpty() const { return !param; }
  • 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.