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