| 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. |
|---|
| 4 | |
|---|
| 5 | #include <frams/param/paramobj.h> |
|---|
| 6 | #include <frams/util/extvalue.h> |
|---|
| 7 | #include <common/nonstd_stl.h> |
|---|
| 8 | |
|---|
| 9 | static const char* maybedup(bool dup, const char* src) |
|---|
| 10 | { |
|---|
| 11 | return dup ? (src ? strdup(src) : 0) : src; |
|---|
| 12 | } |
|---|
| 13 | static void maybefree(void* mem) |
|---|
| 14 | { |
|---|
| 15 | if (mem) free(mem); |
|---|
| 16 | } |
|---|
| 17 | |
|---|
| 18 | int ParamObject::firstFieldOffset() |
|---|
| 19 | { |
|---|
| 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, |
|---|
| 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) |
|---|
| 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(); |
|---|
| 35 | if (stripproc || flagsexclude_tab) |
|---|
| 36 | for (int i = firstprop; i < pi->getPropCount(); i++) |
|---|
| 37 | { |
|---|
| 38 | const char*t = pi->type(i); |
|---|
| 39 | if ((!stripproc) || (strchr("dfsox", *t))) |
|---|
| 40 | if ((!flagsexclude_tab) || (!(pi->flags(i)&flagsexclude_tab))) |
|---|
| 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)); |
|---|
| 46 | t->group = (paInt)gcount; |
|---|
| 47 | t->flags = (paInt)count; |
|---|
| 48 | t->name = maybedup(dupentries, rename ? rename : pi->getName()); |
|---|
| 49 | t->type = maybedup(dupentries, pi->getDescription()); |
|---|
| 50 | for (i = 0; i < gcount; i++) |
|---|
| 51 | { |
|---|
| 52 | t->id = maybedup(dupentries, pi->grname(i)); |
|---|
| 53 | t->offset = 0; |
|---|
| 54 | t++; |
|---|
| 55 | } |
|---|
| 56 | n = 1; |
|---|
| 57 | offset = firstFieldOffset(); |
|---|
| 58 | if (addnew) |
|---|
| 59 | { |
|---|
| 60 | t->id = maybedup(dupentries, "new"); |
|---|
| 61 | t->name = maybedup(dupentries, "create new object"); |
|---|
| 62 | SString tmp = SString::sprintf("p o%s()", pi->getName()); |
|---|
| 63 | t->type = maybedup(dupentries, tmp.c_str()); |
|---|
| 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++; |
|---|
| 71 | } |
|---|
| 72 | for (i = firstprop; i < pi->getPropCount(); i++) |
|---|
| 73 | { |
|---|
| 74 | const char* type=pi->type(i); |
|---|
| 75 | if ((!stripproc) || (strchr("dfsox", type[0]))) |
|---|
| 76 | { |
|---|
| 77 | paInt flag=pi->flags(i); |
|---|
| 78 | int tmp_offset; |
|---|
| 79 | if ((!flagsexclude_data) || (!(flag&flagsexclude_data))) |
|---|
| 80 | { |
|---|
| 81 | if (type[0]=='p') |
|---|
| 82 | tmp_offset=0; |
|---|
| 83 | else |
|---|
| 84 | { |
|---|
| 85 | tmp_offset = offset; |
|---|
| 86 | if (type[0] != 'x') tmp_offset += (((char*)&ex.data[0]) - ((char*)&ex)); |
|---|
| 87 | offset += sizeof(ExtValue); |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | if ((!flagsexclude_tab) || (!(flag&flagsexclude_tab))) |
|---|
| 92 | { |
|---|
| 93 | t->offset=tmp_offset; |
|---|
| 94 | t->group = (paInt)(stripgroups ? 0 : pi->group(i)); |
|---|
| 95 | t->flags = (paInt)flag; |
|---|
| 96 | if (readonly_into_userreadonly && (t->flags & PARAM_READONLY)) |
|---|
| 97 | t->flags=(t->flags & ~PARAM_READONLY) | PARAM_USERREADONLY; |
|---|
| 98 | t->fun1 = 0; |
|---|
| 99 | t->fun2 = 0; |
|---|
| 100 | t->id = maybedup(dupentries, pi->id(i)); |
|---|
| 101 | t->name = maybedup(dupentries, pi->name(i)); |
|---|
| 102 | t->type = maybedup(dupentries, type); |
|---|
| 103 | t->help = maybedup(dupentries, pi->help(i)); |
|---|
| 104 | t++; n++; |
|---|
| 105 | if (n > count) break; |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | t->id = 0; t->group = 0; t->flags = dupentries ? MUTPARAM_ALLOCENTRY : 0; |
|---|
| 110 | return tab; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 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 | |
|---|
| 126 | void ParamObject::freeParamTab(ParamEntry *pe) |
|---|
| 127 | { |
|---|
| 128 | if (paramTabAllocatedString(pe)) |
|---|
| 129 | { |
|---|
| 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++) |
|---|
| 137 | { |
|---|
| 138 | maybefree((void*)e->id); |
|---|
| 139 | maybefree((void*)e->name); |
|---|
| 140 | maybefree((void*)e->type); |
|---|
| 141 | maybefree((void*)e->help); |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | free(pe); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | bool ParamObject::paramTabEqual(ParamEntry *pe1, ParamEntry *pe2) |
|---|
| 148 | { |
|---|
| 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++) |
|---|
| 152 | { |
|---|
| 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; |
|---|
| 157 | } |
|---|
| 158 | return true; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | void ParamObject::p_new(void* obj, ExtValue *args, ExtValue *ret) |
|---|
| 162 | { |
|---|
| 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 | { |
|---|
| 186 | if (!tab) return NULL; |
|---|
| 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])) |
|---|
| 190 | used_fields++; |
|---|
| 191 | |
|---|
| 192 | if (used_fields==0) return NULL; |
|---|
| 193 | ParamObject *obj = new(used_fields)ParamObject(used_fields, tab); // new(n): allocate n fields ; ParamObject(n,...): tell the object it has n fields |
|---|
| 194 | ExtValue *v = &obj->fields[0]; |
|---|
| 195 | n=tab->flags; |
|---|
| 196 | for (ParamEntry *t=tab+tab->group; n > 0; n--, t++) |
|---|
| 197 | switch (*t->type) |
|---|
| 198 | { |
|---|
| 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; |
|---|
| 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; |
|---|
| 223 | case 'x': v++; break; |
|---|
| 224 | } |
|---|
| 225 | return obj; |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | void ParamObject::operator=(const ParamObject& src) |
|---|
| 229 | { |
|---|
| 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; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | ParamObject* ParamObject::clone() |
|---|
| 238 | { |
|---|
| 239 | ParamObject *c = new(numfields)ParamObject(numfields, par.getParamTab()); |
|---|
| 240 | *c = *this; |
|---|
| 241 | return c; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 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 | } |
|---|
| 251 | |
|---|
| 252 | void* ParamObject::dupObject(void* src) |
|---|
| 253 | { |
|---|
| 254 | if (!src) return NULL; |
|---|
| 255 | ParamObject *s = (ParamObject*)src; |
|---|
| 256 | return s->clone(); |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | void ParamObject::freeObject(void* obj) |
|---|
| 260 | { |
|---|
| 261 | if (!obj) return; |
|---|
| 262 | ParamObject *o = (ParamObject*)obj; |
|---|
| 263 | delete o; |
|---|
| 264 | } |
|---|