[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
| 2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
| 3 | // See LICENSE.txt for details. |
---|
[109] | 4 | |
---|
| 5 | #include <frams/param/paramobj.h> |
---|
| 6 | #include <frams/util/extvalue.h> |
---|
| 7 | #include <common/nonstd_stl.h> |
---|
| 8 | |
---|
[326] | 9 | static const char* maybedup(bool dup, const char* src) |
---|
| 10 | { |
---|
| 11 | return dup ? (src ? strdup(src) : 0) : src; |
---|
| 12 | } |
---|
[109] | 13 | static void maybefree(void* mem) |
---|
[326] | 14 | { |
---|
| 15 | if (mem) free(mem); |
---|
| 16 | } |
---|
[109] | 17 | |
---|
[326] | 18 | int ParamObject::firstFieldOffset() |
---|
[109] | 19 | { |
---|
[326] | 20 | static ParamObject dummy(0, NULL); |
---|
| 21 | return ((char*)&dummy.fields[0]) - (char*)&dummy; |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | ParamEntry* ParamObject::makeParamTab(ParamInterface *pi, bool stripgroups, bool stripproc, |
---|
[792] | 25 | int firstprop, int maxprops, bool dupentries, int flagsexclude_data, int flagsexclude_tab, bool addnew, const char* rename, bool readonly_into_userreadonly) |
---|
[326] | 26 | { |
---|
[792] | 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) |
---|
[326] | 30 | ParamEntry *tab, *t; |
---|
| 31 | int i, n, offset; |
---|
| 32 | static ExtValue ex; |
---|
| 33 | int count = 0, gcount = 1; |
---|
| 34 | if (!stripgroups) gcount = pi->getGroupCount(); |
---|
[490] | 35 | if (stripproc || flagsexclude_tab) |
---|
[326] | 36 | for (int i = firstprop; i < pi->getPropCount(); i++) |
---|
| 37 | { |
---|
| 38 | const char*t = pi->type(i); |
---|
| 39 | if ((!stripproc) || (strchr("dfsox", *t))) |
---|
[490] | 40 | if ((!flagsexclude_tab) || (!(pi->flags(i)&flagsexclude_tab))) |
---|
[326] | 41 | if (++count >= maxprops) break; |
---|
| 42 | } |
---|
| 43 | else count = pi->getPropCount() - firstprop; |
---|
| 44 | if (addnew) count++; |
---|
| 45 | t = tab = (ParamEntry*)malloc(sizeof(ParamEntry)*(count + gcount + 1)); |
---|
[478] | 46 | t->group = (paInt)gcount; |
---|
| 47 | t->flags = (paInt)count; |
---|
[326] | 48 | t->name = maybedup(dupentries, rename ? rename : pi->getName()); |
---|
| 49 | t->type = maybedup(dupentries, pi->getDescription()); |
---|
| 50 | for (i = 0; i < gcount; i++) |
---|
[109] | 51 | { |
---|
[326] | 52 | t->id = maybedup(dupentries, pi->grname(i)); |
---|
| 53 | t->offset = 0; |
---|
| 54 | t++; |
---|
[109] | 55 | } |
---|
[326] | 56 | n = 1; |
---|
| 57 | offset = firstFieldOffset(); |
---|
| 58 | if (addnew) |
---|
[109] | 59 | { |
---|
[326] | 60 | t->id = maybedup(dupentries, "new"); |
---|
| 61 | t->name = maybedup(dupentries, "create new object"); |
---|
| 62 | SString tmp = SString::sprintf("p o%s()", pi->getName()); |
---|
[348] | 63 | t->type = maybedup(dupentries, tmp.c_str()); |
---|
[326] | 64 | t->help = maybedup(dupentries, pi->help(i)); |
---|
| 65 | t->flags = 0; |
---|
| 66 | t->group = 0; |
---|
| 67 | t->offset = PARAM_ILLEGAL_OFFSET; |
---|
| 68 | t->fun1 = (void*)p_new; |
---|
| 69 | t->fun2 = 0; |
---|
| 70 | t++; |
---|
[109] | 71 | } |
---|
[326] | 72 | for (i = firstprop; i < pi->getPropCount(); i++) |
---|
[109] | 73 | { |
---|
[792] | 74 | const char* type = pi->type(i); |
---|
[396] | 75 | if ((!stripproc) || (strchr("dfsox", type[0]))) |
---|
[109] | 76 | { |
---|
[792] | 77 | paInt flag = pi->flags(i); |
---|
[490] | 78 | int tmp_offset; |
---|
| 79 | if ((!flagsexclude_data) || (!(flag&flagsexclude_data))) |
---|
[109] | 80 | { |
---|
[792] | 81 | if (type[0] == 'p') |
---|
| 82 | tmp_offset = 0; |
---|
[396] | 83 | else |
---|
[792] | 84 | { |
---|
[490] | 85 | tmp_offset = offset; |
---|
| 86 | if (type[0] != 'x') tmp_offset += (((char*)&ex.data[0]) - ((char*)&ex)); |
---|
[396] | 87 | offset += sizeof(ExtValue); |
---|
[792] | 88 | } |
---|
[490] | 89 | } |
---|
| 90 | |
---|
| 91 | if ((!flagsexclude_tab) || (!(flag&flagsexclude_tab))) |
---|
| 92 | { |
---|
[792] | 93 | t->offset = tmp_offset; |
---|
[478] | 94 | t->group = (paInt)(stripgroups ? 0 : pi->group(i)); |
---|
[490] | 95 | t->flags = (paInt)flag; |
---|
[396] | 96 | if (readonly_into_userreadonly && (t->flags & PARAM_READONLY)) |
---|
[792] | 97 | t->flags = (t->flags & ~PARAM_READONLY) | PARAM_USERREADONLY; |
---|
[326] | 98 | t->fun1 = 0; |
---|
| 99 | t->fun2 = 0; |
---|
| 100 | t->id = maybedup(dupentries, pi->id(i)); |
---|
| 101 | t->name = maybedup(dupentries, pi->name(i)); |
---|
[396] | 102 | t->type = maybedup(dupentries, type); |
---|
[326] | 103 | t->help = maybedup(dupentries, pi->help(i)); |
---|
[396] | 104 | t++; n++; |
---|
[326] | 105 | if (n > count) break; |
---|
[109] | 106 | } |
---|
| 107 | } |
---|
| 108 | } |
---|
[326] | 109 | t->id = 0; t->group = 0; t->flags = dupentries ? MUTPARAM_ALLOCENTRY : 0; |
---|
| 110 | return tab; |
---|
[109] | 111 | } |
---|
| 112 | |
---|
[326] | 113 | void ParamObject::setParamTabText(ParamEntry *pe, const char* &ptr, const char* txt) |
---|
| 114 | { |
---|
| 115 | if (!paramTabAllocatedString(pe)) |
---|
| 116 | return; |
---|
| 117 | maybefree((char*)ptr); |
---|
| 118 | ptr = maybedup(true, txt); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | bool ParamObject::paramTabAllocatedString(ParamEntry *pe) |
---|
| 122 | { |
---|
| 123 | return (pe[pe->flags + pe->group].flags & MUTPARAM_ALLOCENTRY) ? true : false; |
---|
| 124 | } |
---|
| 125 | |
---|
[109] | 126 | void ParamObject::freeParamTab(ParamEntry *pe) |
---|
| 127 | { |
---|
[326] | 128 | if (paramTabAllocatedString(pe)) |
---|
[109] | 129 | { |
---|
[326] | 130 | int i; |
---|
| 131 | ParamEntry *e; |
---|
| 132 | maybefree((void*)pe->name); |
---|
| 133 | maybefree((void*)pe->type); |
---|
| 134 | for (i = 0, e = pe; i < pe->group; i++, e++) |
---|
| 135 | maybefree((void*)e->id); |
---|
| 136 | for (i = pe->group, e = pe + i; i < pe->group + pe->flags; i++, e++) |
---|
[109] | 137 | { |
---|
[326] | 138 | maybefree((void*)e->id); |
---|
| 139 | maybefree((void*)e->name); |
---|
| 140 | maybefree((void*)e->type); |
---|
| 141 | maybefree((void*)e->help); |
---|
[109] | 142 | } |
---|
| 143 | } |
---|
[326] | 144 | free(pe); |
---|
[109] | 145 | } |
---|
| 146 | |
---|
[326] | 147 | bool ParamObject::paramTabEqual(ParamEntry *pe1, ParamEntry *pe2) |
---|
[109] | 148 | { |
---|
[326] | 149 | if (pe1->flags != pe2->flags) return false; |
---|
| 150 | ParamEntry *e1 = pe1 + pe1->group, *e2 = pe2 + pe2->group; |
---|
| 151 | for (int i = 0; i < pe1->flags; i++, e1++, e2++) |
---|
[109] | 152 | { |
---|
[326] | 153 | if (strcmp(e1->id, e2->id)) return false; |
---|
| 154 | if (strcmp(e1->name, e2->name)) return false; |
---|
| 155 | if (strcmp(e1->type, e2->type)) return false; |
---|
| 156 | if (e1->offset != e2->offset) return false; |
---|
[109] | 157 | } |
---|
[326] | 158 | return true; |
---|
[109] | 159 | } |
---|
| 160 | |
---|
[326] | 161 | void ParamObject::p_new(void* obj, ExtValue *args, ExtValue *ret) |
---|
[109] | 162 | { |
---|
[326] | 163 | ParamObject *this_obj = (ParamObject*)obj; |
---|
| 164 | ParamObject *po = makeObject(this_obj->par.getParamTab()); |
---|
| 165 | ret->setObject(ExtObject(&this_obj->par, po)); |
---|
| 166 | po->par.setDefault(); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | ParamObject::ParamObject(int _numfields, ParamEntry *_tab) |
---|
| 170 | { |
---|
| 171 | numfields = _numfields; |
---|
| 172 | par.setParamTab(_tab); |
---|
| 173 | par.select(this); |
---|
| 174 | for (int i = 0; i < numfields; i++) |
---|
| 175 | new(&fields[i])ExtValue(); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | ParamObject::~ParamObject() |
---|
| 179 | { |
---|
| 180 | for (int i = 0; i < numfields; i++) |
---|
| 181 | fields[i].~ExtValue(); |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | ParamObject* ParamObject::makeObject(ParamEntry *tab) |
---|
| 185 | { |
---|
[396] | 186 | if (!tab) return NULL; |
---|
[792] | 187 | int n = tab->flags, used_fields = 0; |
---|
| 188 | for (ParamEntry *t = tab + tab->group; n > 0; n--, t++) |
---|
| 189 | if (strchr("dfsox", t->type[0])) |
---|
[396] | 190 | used_fields++; |
---|
| 191 | |
---|
[792] | 192 | if (used_fields == 0) return NULL; |
---|
[396] | 193 | ParamObject *obj = new(used_fields)ParamObject(used_fields, tab); // new(n): allocate n fields ; ParamObject(n,...): tell the object it has n fields |
---|
[326] | 194 | ExtValue *v = &obj->fields[0]; |
---|
[792] | 195 | n = tab->flags; |
---|
| 196 | for (ParamEntry *t = tab + tab->group; n > 0; n--, t++) |
---|
[396] | 197 | switch (*t->type) |
---|
[109] | 198 | { |
---|
[326] | 199 | case 'd': v->setInt(0); v++; break; |
---|
| 200 | case 'f': v->setDouble(0); v++; break; |
---|
| 201 | case 's': v->setString(SString::empty()); v++; break; |
---|
[490] | 202 | case 'o': |
---|
[792] | 203 | { |
---|
[490] | 204 | ExtObject new_obj; |
---|
| 205 | if (t->flags & PARAM_OBJECTSET) |
---|
[792] | 206 | { |
---|
| 207 | ParamInterface *cls = ExtValue::findDeserializableClass(t->type + 1); |
---|
| 208 | if (cls) |
---|
[490] | 209 | { |
---|
[792] | 210 | int new_fun = cls->findId("new"); |
---|
| 211 | if (new_fun >= 0) |
---|
[490] | 212 | { |
---|
[792] | 213 | ExtValue dummy, new_value; |
---|
[490] | 214 | cls->call(new_fun, &dummy, &new_value); |
---|
[792] | 215 | new_obj = new_value.getObject(); |
---|
[490] | 216 | } |
---|
| 217 | } |
---|
[792] | 218 | } |
---|
[490] | 219 | v->setObject(new_obj); |
---|
| 220 | v++; |
---|
[792] | 221 | } |
---|
[490] | 222 | break; |
---|
[326] | 223 | case 'x': v++; break; |
---|
[109] | 224 | } |
---|
[326] | 225 | return obj; |
---|
[109] | 226 | } |
---|
| 227 | |
---|
[326] | 228 | void ParamObject::operator=(const ParamObject& src) |
---|
[109] | 229 | { |
---|
[326] | 230 | const ExtValue *s = &src.fields[0]; |
---|
| 231 | ExtValue *d = &fields[0]; |
---|
| 232 | int n = min(numfields, src.numfields); |
---|
| 233 | for (int i = 0; i < n; i++, d++, s++) |
---|
| 234 | *d = *s; |
---|
[109] | 235 | } |
---|
| 236 | |
---|
[326] | 237 | ParamObject* ParamObject::clone() |
---|
[109] | 238 | { |
---|
[326] | 239 | ParamObject *c = new(numfields)ParamObject(numfields, par.getParamTab()); |
---|
| 240 | *c = *this; |
---|
| 241 | return c; |
---|
| 242 | } |
---|
[109] | 243 | |
---|
[326] | 244 | void ParamObject::copyObject(void* dst, void* src) |
---|
| 245 | { |
---|
| 246 | if ((!dst) || (!src)) return; |
---|
| 247 | ParamObject *s = (ParamObject*)src; |
---|
| 248 | ParamObject *d = (ParamObject*)dst; |
---|
| 249 | *d = *s; |
---|
| 250 | } |
---|
[109] | 251 | |
---|
[326] | 252 | void* ParamObject::dupObject(void* src) |
---|
| 253 | { |
---|
| 254 | if (!src) return NULL; |
---|
| 255 | ParamObject *s = (ParamObject*)src; |
---|
| 256 | return s->clone(); |
---|
[109] | 257 | } |
---|
| 258 | |
---|
| 259 | void ParamObject::freeObject(void* obj) |
---|
| 260 | { |
---|
[326] | 261 | if (!obj) return; |
---|
| 262 | ParamObject *o = (ParamObject*)obj; |
---|
| 263 | delete o; |
---|
[109] | 264 | } |
---|