Changeset 93


Ignore:
Timestamp:
07/02/13 22:37:44 (11 years ago)
Author:
Maciej Komosinski
Message:

every param.set(value) produces a helpful warning when the "value" exceeds allowed range for this param, e.g.

World.wrldtyp=5;

[WARN] Param::set - Setting 'World.wrldtyp = 5' exceeded allowed range (too big). Adjusted to 2.

World.wrldtyp=-5;

[WARN] Param::set - Setting 'World.wrldtyp = -5' exceeded allowed range (too small). Adjusted to 0.

Location:
cpp/gdk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cpp/gdk/param.cpp

    r81 r93  
    11// This file is a part of the Framsticks GDK library.
    2 // Copyright (C) 2002-2011  Szymon Ulatowski.  See LICENSE.txt for details.
     2// Copyright (C) 2002-2013  Szymon Ulatowski.  See LICENSE.txt for details.
    33// Refer to http://www.framsticks.com/ for further information.
    44
     
    209209int ParamInterface::setExtValueById(const char* prop,const ExtValue &v)
    210210{int i=findId(prop); if (i>=0) return setExtValue(i,v); else return PSET_NOPROPERTY;}
     211int ParamInterface::setById(const char* prop,const ExtValue &v)
     212{int i=findId(prop); if (i>=0) return set(i,v); else return PSET_NOPROPERTY;}
    211213
    212214int ParamInterface::save(VirtFILE* f,const SString* altname,bool force)
     
    339341int p_len;
    340342bool eol,loaded;
    341 int ret;
    342343while(fgets0(t,100,f,eol))
    343344        {
     
    355356                        int ch; while((ch=fgetc(f))!=EOF) if (ch=='\n') break;
    356357                        unquoteTilde(s);
    357                         ret=set(i,(const char*)s);
     358                        set(i,(const char*)s);
    358359                        }
    359360                else
    360361                        {
    361362                        if (eol)
    362                                 ret=set(i,p0+p_len+1);
     363                                set(i,p0+p_len+1);
    363364                        else
    364365                                {
     
    369370                                        tmp+=t;
    370371                                        }
    371                                 ret=set(i,(const char*)tmp);
     372                                set(i,(const char*)tmp);
    372373                                }
    373374                        }
    374                 if (ret & (PSET_HITMIN | PSET_HITMAX))
    375                         FMprintf("Param","load2",FMLV_WARN,"Adjusted '%s' in '%s' (was too %s)",
    376                                  id(i),getName(),(ret&PSET_HITMAX)?"big":"small");
    377375                loaded=true;
    378376                }
     
    654652ParamEntry *pe=entry(i);
    655653if (pe->flags&PARAM_READONLY) return PSET_RONLY;
    656 long a=0,b=0,result=0;
     654long xcopy=x; //only needed for helpful printed messages: retain original, requested value of x because it may be changed below
     655long a=0,b=0;
     656int result=0;
    657657const char* t=pe->type+1;
    658658while(*t) if (*t==' ') break; else t++;
     
    667667        {
    668668        v.setInt(x);
    669         return result | (*(int(*)(void*,const ExtValue*))pe->fun2)(object,&v);
     669        result |= (*(int(*)(void*,const ExtValue*))pe->fun2)(object,&v);
    670670        }
    671671else
     
    677677                        *((long*)target)=x;
    678678                        }
     679        }
     680    messageOnExceedRange(i,result,xcopy);
    679681        return result;
    680         }
    681682}
    682683
     
    686687ParamEntry *pe=entry(i);
    687688if (pe->flags&PARAM_READONLY) return PSET_RONLY;
    688 double a=0,b=0; int result=0;
     689double xcopy=x; //only needed for helpful printed messages: retain original, requested value of x because it may be changed below
     690double a=0,b=0;
     691int result=0;
    689692const char* t=pe->type+1;
    690693while(*t) if (*t==' ') break; else t++;
     
    699702        {
    700703        v.setDouble(x);
    701         return result | (*(int(*)(void*,const ExtValue*))pe->fun2)(object,&v);
     704        result |= (*(int(*)(void*,const ExtValue*))pe->fun2)(object,&v);
    702705        }
    703706else
     
    709712                *((double*)target)=x;
    710713                }
     714        }
     715    messageOnExceedRange(i,result,xcopy);
    711716        return result;
    712         }
    713717}
    714718
     
    720724ParamEntry *pe=entry(i);
    721725if (pe->flags&PARAM_READONLY) return PSET_RONLY;
     726SString xcopy=x; //only needed for helpful printed messages: retain original, requested value of x because it may be changed below
    722727const char* t=pe->type+1;
    723728while(*t) if (*t==' ') break; else t++;
    724 long a=0,b=0,result=0;
     729long a=0,b=0;
     730int result=0;
    725731if (sscanf(t,"%ld %ld",&a,&b)==2)
    726732        {
     
    736742        {
    737743        v.setString(*xx);
    738         return result | (*(int(*)(void*,const ExtValue*))pe->fun2)(object,&v);
     744        result |= (*(int(*)(void*,const ExtValue*))pe->fun2)(object,&v);
    739745        }
    740746else
     
    746752                *((SString*)target)=x;
    747753                }
     754        }
     755    messageOnExceedRange(i,result,xcopy);
    748756        return result;
    749         }
    750757}
    751758
     
    755762ParamEntry *pe=entry(i);
    756763if (pe->flags&PARAM_READONLY) return PSET_RONLY;
     764ExtObject xcopy=x; //only needed for helpful printed messages: retain original, requested value of x because it may be changed below
    757765if (pe->fun2)
    758766        {
    759767        v.setObject(x);
    760         return (*(int(*)(void*,const ExtValue*))pe->fun2)(object,&v);
     768        int result=(*(int(*)(void*,const ExtValue*))pe->fun2)(object,&v);
     769    messageOnExceedRange(i,result,xcopy);
     770    return result;
    761771        }
    762772else
     
    772782ParamEntry *pe=entry(i);
    773783if (pe->flags&PARAM_READONLY) return PSET_RONLY;
     784ExtValue xcopy=x; //only needed for messageOnExceedRange(): retain original, requested value of x because it may be changed below
    774785if (pe->fun2)
    775786        {
    776         return (*(int(*)(void*,const ExtValue*))pe->fun2)(object,&x);
     787        int result=(*(int(*)(void*,const ExtValue*))pe->fun2)(object,&x);
     788    messageOnExceedRange(i,result,xcopy);
     789    return result;
    777790        }
    778791else
     
    793806        {
    794807        FMprintf("SimpleAbstractParam","call",FMLV_ERROR,
    795                  (*pe->type!='p')?"'%s.%s' is not a function":"internal error - undefined function pointer for '%s.%s'",getName(),pe->id);
     808                 (*pe->type!='p')?"'%s.%s' is not a function":"Internal error - undefined function pointer for '%s.%s'",getName(),pe->id);
    796809        }
    797810}
     
    942955return -9999;
    943956}
     957
  • cpp/gdk/param.h

    r81 r93  
    1111#include "statrick.h"
    1212#include "virtfile.h"
     13#include "framsg.h"
    1314
    1415class ExtValue;
     
    118119int setStringById(const char* prop,const SString &);///< set string value, you can only use this for "s" type prop
    119120int setObjectById(const char* prop,const ExtObject &);///< set object reference, you can only use this for "o" type prop
    120 
    121 int setExtValueById(const char* prop,const ExtValue &); ///< 4 in 1
     121int setExtValueById(const char* prop,const ExtValue &); ///< for ExtValue types only
     122int setById(const char* prop,const ExtValue &);///< can be used for all property types
    122123
    123124/** get valid minimum, maximum and default value for property 'prop'
     
    239240ExtValue getExtValue(int);
    240241
     242template<typename T> void messageOnExceedRange(int i,int setflags, T valuetoset) ///< prints a warning when setflags indicates that allowed param range has been exceeded during set
     243{
     244        if (setflags & (PSET_HITMIN | PSET_HITMAX))
     245    {
     246        SString svaluetoset=SString::valueOf(valuetoset); //converts any type to SString
     247        SString actual=get(i);
     248                FMprintf("Param","set",FMLV_WARN,"Setting '%s.%s = %s' exceeded allowed range (too %s). Adjusted to %s.",
     249                                 getName(),id(i),(const char*)svaluetoset,(setflags&PSET_HITMAX)?"big":"small",(const char*)actual);
     250    }
     251}                         
     252
    241253int setInt(int,long);
    242254int setDouble(int,double);
Note: See TracChangeset for help on using the changeset viewer.