Changeset 490 for cpp/frams


Ignore:
Timestamp:
03/30/16 17:08:08 (8 years ago)
Author:
Maciej Komosinski
Message:

Introduced general-use ErrorObject?, fixed enumeration of mixed private/public property lists

Location:
cpp/frams
Files:
6 edited

Legend:

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

    r478 r490  
    2323
    2424ParamEntry* ParamObject::makeParamTab(ParamInterface *pi, bool stripgroups, bool stripproc,
    25                                       int firstprop, int maxprops, bool dupentries, int flagsexclude, bool addnew, const char* rename, bool readonly_into_userreadonly)
    26 {
     25                                      int firstprop, int maxprops, bool dupentries, int flagsexclude_data, int flagsexclude_tab, bool addnew, const char* rename, bool readonly_into_userreadonly)
     26{
     27// flagsexclude_data - skip while calculating data offsets
     28// flagsexclude_tab - skip while creating paramtab
     29// usually _data==_tab, but vmneuron public properties need _data=0 and _tab=PARAM_USERHIDDEN (data object has all fields, paramtab skips private fields)
    2730        ParamEntry *tab, *t;
    2831        int i, n, offset;
     
    3033        int count = 0, gcount = 1;
    3134        if (!stripgroups) gcount = pi->getGroupCount();
    32         if (stripproc || flagsexclude)
     35        if (stripproc || flagsexclude_tab)
    3336                for (int i = firstprop; i < pi->getPropCount(); i++)
    3437                {
    3538                const char*t = pi->type(i);
    3639                if ((!stripproc) || (strchr("dfsox", *t)))
    37                         if ((!flagsexclude) || (!(pi->flags(i)&flagsexclude)))
     40                        if ((!flagsexclude_tab) || (!(pi->flags(i)&flagsexclude_tab)))
    3841                                if (++count >= maxprops) break;
    3942                }
     
    7275                if ((!stripproc) || (strchr("dfsox", type[0])))
    7376                {
    74                         if ((!flagsexclude) || (!(pi->flags(i)&flagsexclude)))
     77                        paInt flag=pi->flags(i);
     78                        int tmp_offset;
     79                        if ((!flagsexclude_data) || (!(flag&flagsexclude_data)))
    7580                        {
    7681                                if (type[0]=='p')
    77                                         t->offset=0;
     82                                        tmp_offset=0;
    7883                                else
    7984                                        {
    80                                         t->offset = offset;
    81                                         if (type[0] != 'x') t->offset += (((char*)&ex.data[0]) - ((char*)&ex));
     85                                        tmp_offset = offset;
     86                                        if (type[0] != 'x') tmp_offset += (((char*)&ex.data[0]) - ((char*)&ex));
    8287                                        offset += sizeof(ExtValue);
    8388                                        }
     89                        }
     90
     91                        if ((!flagsexclude_tab) || (!(flag&flagsexclude_tab)))
     92                        {
     93                                t->offset=tmp_offset;
    8494                                t->group = (paInt)(stripgroups ? 0 : pi->group(i));
    85                                 t->flags = (paInt)pi->flags(i);
     95                                t->flags = (paInt)flag;
    8696                                if (readonly_into_userreadonly && (t->flags & PARAM_READONLY))
    8797                                        t->flags=(t->flags & ~PARAM_READONLY) | PARAM_USERREADONLY;
     
    190200                case 'f': v->setDouble(0); v++; break;
    191201                case 's': v->setString(SString::empty()); v++; break;
    192                 case 'o': v->setObject(ExtObject()); v++; break;
     202                case 'o':
     203                        {
     204                        ExtObject new_obj;
     205                        if (t->flags & PARAM_OBJECTSET)
     206                                {
     207                                ParamInterface *cls=ExtValue::findDeserializableClass(t->type+1);
     208                                if (cls)
     209                                        {
     210                                        int new_fun = cls->findId("new");
     211                                        if (new_fun>=0)
     212                                                {
     213                                                ExtValue dummy,new_value;
     214                                                cls->call(new_fun, &dummy, &new_value);
     215                                                new_obj=new_value.getObject();
     216                                                }
     217                                        }
     218                                }
     219                        v->setObject(new_obj);
     220                        v++;
     221                        }
     222                        break;
    193223                case 'x': v++; break;
    194224        }
  • cpp/frams/param/paramobj.h

    r396 r490  
    5353                ParamObject::freeObject(obj2);
    5454                */
    55         static ParamEntry* makeParamTab(ParamInterface *pi, bool stripgroups = 0, bool stripproc = 0, int firstprop = 0, int maxprops = 9999, bool dupentries = false, int flagsexclude = 0, bool addnew = false, const char* rename = NULL,bool readonly_into_userreadonly=false);
     55        static ParamEntry* makeParamTab(ParamInterface *pi, bool stripgroups = 0, bool stripproc = 0, int firstprop = 0, int maxprops = 9999, bool dupentries = false, int flagsexclude_data = 0, int flagsexclude_tab = 0, bool addnew = false, const char* rename = NULL,bool readonly_into_userreadonly=false);
    5656
    5757        /** deallocate paramtab obtained from makeParamTab() */
  • cpp/frams/util/extvalue.cpp

    r478 r490  
    357357}
    358358
     359void ExtValue::setError(const SString& msg)
     360{
     361ErrorObject *err=new ErrorObject;
     362err->message=msg;
     363setObject(ErrorObject::makeDynamicObject(err));
     364}
     365
    359366static ExtValue::CompareResult longsign(paInt x)
    360367{
     
    369376        if ((v.getType() == TInt) && (v.getInt() == 0)) return ExtValue::ResultUnequal_RelaxedEqual;
    370377        return ExtValue::ResultUnequal_RelaxedUnequal; //comparing anything else with null is valid but null is neither higher nor lower than numbers or strings
     378}
     379
     380static ExtValue::CompareResult compareInvalid(const ExtValue& v)
     381{
     382        if (v.getType()==TInvalid) return ExtValue::ResultEqualUnordered;
     383        if ((v.getType() == TInt) && (v.getInt() == 0)) return ExtValue::ResultUnequal_RelaxedEqual;
     384        return ExtValue::ResultMismatch; //comparing anything else with invalid is invalid
    371385}
    372386
     
    392406        else if (src.isNull())
    393407                return compareNull(*this);
     408        if (getType()==TInvalid)
     409                return compareInvalid(src);
     410        else if (src.getType()==TInvalid)
     411                return compareInvalid(*this);
    394412        switch (type)
    395413        {
     
    11211139                return in + 5;
    11221140        }
    1123         else if (!strncmp(in, "invalid", 9))
     1141        else if (!strncmp(in, "invalid", 7))
    11241142        {
    11251143                setInvalid();
    1126                 return in + 9;
     1144                return in + 7;
    11271145        }
    11281146        else if (*in == '<')
    11291147        { //unserializable object
    1130                 setInvalid();
    1131                 while (*in)
    1132                         if (*in == '>')
    1133                                 return in + 1;
    1134                         else in++;
    1135                         return in;
     1148                const char* end=in+1;
     1149                while (*end)
     1150                        if (*end == '>')
     1151                                {
     1152                                setError(SString("Unserializable class: ")+SString(in+1,end-in-1));
     1153                                return end+1;
     1154                                }
     1155                        else
     1156                                end++;
     1157                logPrintf("ExtValue", "deserialize", LOG_ERROR, "Missing '>'");
     1158                return NULL;
    11361159        }
    11371160        else if (*in == '^')
     
    12361259        return v.toString();
    12371260}
     1261
     1262#define FIELDSTRUCT ErrorObject
     1263static 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,},
     1270};
     1271#undef FIELDSTRUCT
     1272
     1273Param& ErrorObject::getParam()
     1274{
     1275        static Param param(errorobject_paramtab);
     1276        return param;
     1277}
     1278
     1279ExtObject ErrorObject::makeDynamicObject(ErrorObject* e)
     1280{
     1281        return ExtObject(&getParam(), (DestrBase*)e);
     1282}
     1283
     1284const SString ErrorObject::TO_STRING_PREFIX="Error: ";
     1285
     1286void ErrorObject::get_toString(ExtValue* ret)
     1287{
     1288ret->setString(TO_STRING_PREFIX+message);
     1289}
     1290
     1291void ErrorObject::p_newfromstring(ExtValue *args, ExtValue *ret)
     1292{
     1293ErrorObject *err=new ErrorObject();
     1294err->message=args[0].getString();
     1295if (err->message.startsWith(TO_STRING_PREFIX.c_str()))
     1296        err->message=err->message.substr(TO_STRING_PREFIX.len());
     1297*ret = makeDynamicObject(err);
     1298}
     1299
     1300REGISTER_DESERIALIZABLE(ErrorObject)
  • cpp/frams/util/extvalue.h

    r482 r490  
    185185        void setEmpty();
    186186        void setInvalid() { setEmpty(); type = TInvalid; }
     187        void setError(const SString& msg);
    187188        bool makeUnique() { return (type == TObj) && odata().makeUnique(); } //< @return false if nothing has changed
    188189        ExtPType getType() const { return type; }
     
    243244#define REGISTER_DESERIALIZABLE(name) ExtValue::AddDeserializable<name> deserializable_autoinit_ ## name;
    244245
    245 #endif
     246class ErrorObject: public DestrBase
     247{
     248  public:
     249SString message;
     250static Param& getParam();
     251static Param& getStaticParam() {return getParam();}
     252static ExtObject makeDynamicObject(ErrorObject* e);
     253static const SString TO_STRING_PREFIX;
     254#define STATRICKCLASS ErrorObject
     255PARAMGETDEF(toString);
     256PARAMPROCDEF(p_newfromstring);
     257#undef STATRICKCLASS
     258};
     259
     260#endif
  • cpp/frams/vm/classes/collectionobj.cpp

    r478 r490  
    545545        ret->setEmpty();
    546546}
     547
     548// not actually needed for deserialization (vector and dict are special cases) but findDeserializableClass can be also used in other contexts
     549REGISTER_DESERIALIZABLE(VectorObject)
     550REGISTER_DESERIALIZABLE(DictionaryObject)
  • cpp/frams/vm/classes/collectionobj.h

    r478 r490  
    2525VectorObject():readonly(0),owndata(1) {}
    2626~VectorObject() {clear();}
     27static Param& getStaticParam() {return par;}
    2728#define STATRICKCLASS VectorObject
    2829PARAMPROCDEF(p_clear) {if (readonly) return; clear();}
     
    6263DictionaryObject():it(hash),it_index(-1) {}
    6364~DictionaryObject() {clear();}
     65static Param& getStaticParam() {return par;}
    6466#define STATRICKCLASS DictionaryObject
    6567PARAMPROCDEF(p_clear) {clear();}
Note: See TracChangeset for help on using the changeset viewer.