[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, |
---|
[396] | 25 | int firstprop, int maxprops, bool dupentries, int flagsexclude, bool addnew, const char* rename, bool readonly_into_userreadonly) |
---|
[326] | 26 | { |
---|
| 27 | ParamEntry *tab, *t; |
---|
| 28 | int i, n, offset; |
---|
| 29 | static ExtValue ex; |
---|
| 30 | int count = 0, gcount = 1; |
---|
| 31 | if (!stripgroups) gcount = pi->getGroupCount(); |
---|
| 32 | if (stripproc || flagsexclude) |
---|
| 33 | for (int i = firstprop; i < pi->getPropCount(); i++) |
---|
| 34 | { |
---|
| 35 | const char*t = pi->type(i); |
---|
| 36 | if ((!stripproc) || (strchr("dfsox", *t))) |
---|
| 37 | if ((!flagsexclude) || (!(pi->flags(i)&flagsexclude))) |
---|
| 38 | if (++count >= maxprops) break; |
---|
| 39 | } |
---|
| 40 | else count = pi->getPropCount() - firstprop; |
---|
| 41 | if (addnew) count++; |
---|
| 42 | t = tab = (ParamEntry*)malloc(sizeof(ParamEntry)*(count + gcount + 1)); |
---|
| 43 | t->group = (short)gcount; |
---|
| 44 | t->flags = (short)count; |
---|
| 45 | t->name = maybedup(dupentries, rename ? rename : pi->getName()); |
---|
| 46 | t->type = maybedup(dupentries, pi->getDescription()); |
---|
| 47 | for (i = 0; i < gcount; i++) |
---|
[109] | 48 | { |
---|
[326] | 49 | t->id = maybedup(dupentries, pi->grname(i)); |
---|
| 50 | t->offset = 0; |
---|
| 51 | t++; |
---|
[109] | 52 | } |
---|
[326] | 53 | n = 1; |
---|
| 54 | offset = firstFieldOffset(); |
---|
| 55 | if (addnew) |
---|
[109] | 56 | { |
---|
[326] | 57 | t->id = maybedup(dupentries, "new"); |
---|
| 58 | t->name = maybedup(dupentries, "create new object"); |
---|
| 59 | SString tmp = SString::sprintf("p o%s()", pi->getName()); |
---|
[348] | 60 | t->type = maybedup(dupentries, tmp.c_str()); |
---|
[326] | 61 | t->help = maybedup(dupentries, pi->help(i)); |
---|
| 62 | t->flags = 0; |
---|
| 63 | t->group = 0; |
---|
| 64 | t->offset = PARAM_ILLEGAL_OFFSET; |
---|
| 65 | t->fun1 = (void*)p_new; |
---|
| 66 | t->fun2 = 0; |
---|
| 67 | t++; |
---|
[109] | 68 | } |
---|
[326] | 69 | for (i = firstprop; i < pi->getPropCount(); i++) |
---|
[109] | 70 | { |
---|
[396] | 71 | const char* type=pi->type(i); |
---|
| 72 | if ((!stripproc) || (strchr("dfsox", type[0]))) |
---|
[109] | 73 | { |
---|
[326] | 74 | if ((!flagsexclude) || (!(pi->flags(i)&flagsexclude))) |
---|
[109] | 75 | { |
---|
[396] | 76 | if (type[0]=='p') |
---|
| 77 | t->offset=0; |
---|
| 78 | else |
---|
| 79 | { |
---|
| 80 | t->offset = offset; |
---|
| 81 | if (type[0] != 'x') t->offset += (((char*)&ex.data[0]) - ((char*)&ex)); |
---|
| 82 | offset += sizeof(ExtValue); |
---|
| 83 | } |
---|
[326] | 84 | t->group = (short)(stripgroups ? 0 : pi->group(i)); |
---|
| 85 | t->flags = (short)pi->flags(i); |
---|
[396] | 86 | if (readonly_into_userreadonly && (t->flags & PARAM_READONLY)) |
---|
| 87 | t->flags=(t->flags & ~PARAM_READONLY) | PARAM_USERREADONLY; |
---|
[326] | 88 | t->fun1 = 0; |
---|
| 89 | t->fun2 = 0; |
---|
| 90 | t->id = maybedup(dupentries, pi->id(i)); |
---|
| 91 | t->name = maybedup(dupentries, pi->name(i)); |
---|
[396] | 92 | t->type = maybedup(dupentries, type); |
---|
[326] | 93 | t->help = maybedup(dupentries, pi->help(i)); |
---|
[396] | 94 | t++; n++; |
---|
[326] | 95 | if (n > count) break; |
---|
[109] | 96 | } |
---|
| 97 | } |
---|
| 98 | } |
---|
[326] | 99 | t->id = 0; t->group = 0; t->flags = dupentries ? MUTPARAM_ALLOCENTRY : 0; |
---|
| 100 | return tab; |
---|
[109] | 101 | } |
---|
| 102 | |
---|
[326] | 103 | void ParamObject::setParamTabText(ParamEntry *pe, const char* &ptr, const char* txt) |
---|
| 104 | { |
---|
| 105 | if (!paramTabAllocatedString(pe)) |
---|
| 106 | return; |
---|
| 107 | maybefree((char*)ptr); |
---|
| 108 | ptr = maybedup(true, txt); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | bool ParamObject::paramTabAllocatedString(ParamEntry *pe) |
---|
| 112 | { |
---|
| 113 | return (pe[pe->flags + pe->group].flags & MUTPARAM_ALLOCENTRY) ? true : false; |
---|
| 114 | } |
---|
| 115 | |
---|
[109] | 116 | void ParamObject::freeParamTab(ParamEntry *pe) |
---|
| 117 | { |
---|
[326] | 118 | if (paramTabAllocatedString(pe)) |
---|
[109] | 119 | { |
---|
[326] | 120 | int i; |
---|
| 121 | ParamEntry *e; |
---|
| 122 | maybefree((void*)pe->name); |
---|
| 123 | maybefree((void*)pe->type); |
---|
| 124 | for (i = 0, e = pe; i < pe->group; i++, e++) |
---|
| 125 | maybefree((void*)e->id); |
---|
| 126 | for (i = pe->group, e = pe + i; i < pe->group + pe->flags; i++, e++) |
---|
[109] | 127 | { |
---|
[326] | 128 | maybefree((void*)e->id); |
---|
| 129 | maybefree((void*)e->name); |
---|
| 130 | maybefree((void*)e->type); |
---|
| 131 | maybefree((void*)e->help); |
---|
[109] | 132 | } |
---|
| 133 | } |
---|
[326] | 134 | free(pe); |
---|
[109] | 135 | } |
---|
| 136 | |
---|
[326] | 137 | bool ParamObject::paramTabEqual(ParamEntry *pe1, ParamEntry *pe2) |
---|
[109] | 138 | { |
---|
[326] | 139 | if (pe1->flags != pe2->flags) return false; |
---|
| 140 | ParamEntry *e1 = pe1 + pe1->group, *e2 = pe2 + pe2->group; |
---|
| 141 | for (int i = 0; i < pe1->flags; i++, e1++, e2++) |
---|
[109] | 142 | { |
---|
[326] | 143 | if (strcmp(e1->id, e2->id)) return false; |
---|
| 144 | if (strcmp(e1->name, e2->name)) return false; |
---|
| 145 | if (strcmp(e1->type, e2->type)) return false; |
---|
| 146 | if (e1->offset != e2->offset) return false; |
---|
[109] | 147 | } |
---|
[326] | 148 | return true; |
---|
[109] | 149 | } |
---|
| 150 | |
---|
[326] | 151 | void ParamObject::p_new(void* obj, ExtValue *args, ExtValue *ret) |
---|
[109] | 152 | { |
---|
[326] | 153 | ParamObject *this_obj = (ParamObject*)obj; |
---|
| 154 | ParamObject *po = makeObject(this_obj->par.getParamTab()); |
---|
| 155 | ret->setObject(ExtObject(&this_obj->par, po)); |
---|
| 156 | po->par.setDefault(); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | ParamObject::ParamObject(int _numfields, ParamEntry *_tab) |
---|
| 160 | { |
---|
| 161 | numfields = _numfields; |
---|
| 162 | par.setParamTab(_tab); |
---|
| 163 | par.select(this); |
---|
| 164 | for (int i = 0; i < numfields; i++) |
---|
| 165 | new(&fields[i])ExtValue(); |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | ParamObject::~ParamObject() |
---|
| 169 | { |
---|
| 170 | for (int i = 0; i < numfields; i++) |
---|
| 171 | fields[i].~ExtValue(); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | ParamObject* ParamObject::makeObject(ParamEntry *tab) |
---|
| 175 | { |
---|
[396] | 176 | if (!tab) return NULL; |
---|
| 177 | int n=tab->flags, used_fields=0; |
---|
| 178 | for (ParamEntry *t=tab+tab->group; n > 0; n--, t++) |
---|
| 179 | if (strchr("dfsox",t->type[0])) |
---|
| 180 | used_fields++; |
---|
| 181 | |
---|
| 182 | if (used_fields==0) return NULL; |
---|
| 183 | ParamObject *obj = new(used_fields)ParamObject(used_fields, tab); // new(n): allocate n fields ; ParamObject(n,...): tell the object it has n fields |
---|
[326] | 184 | ExtValue *v = &obj->fields[0]; |
---|
[396] | 185 | n=tab->flags; |
---|
| 186 | for (ParamEntry *t=tab+tab->group; n > 0; n--, t++) |
---|
| 187 | switch (*t->type) |
---|
[109] | 188 | { |
---|
[326] | 189 | case 'd': v->setInt(0); v++; break; |
---|
| 190 | case 'f': v->setDouble(0); v++; break; |
---|
| 191 | case 's': v->setString(SString::empty()); v++; break; |
---|
| 192 | case 'o': v->setObject(ExtObject()); v++; break; |
---|
| 193 | case 'x': v++; break; |
---|
[109] | 194 | } |
---|
[326] | 195 | return obj; |
---|
[109] | 196 | } |
---|
| 197 | |
---|
[326] | 198 | void ParamObject::operator=(const ParamObject& src) |
---|
[109] | 199 | { |
---|
[326] | 200 | const ExtValue *s = &src.fields[0]; |
---|
| 201 | ExtValue *d = &fields[0]; |
---|
| 202 | int n = min(numfields, src.numfields); |
---|
| 203 | for (int i = 0; i < n; i++, d++, s++) |
---|
| 204 | *d = *s; |
---|
[109] | 205 | } |
---|
| 206 | |
---|
[326] | 207 | ParamObject* ParamObject::clone() |
---|
[109] | 208 | { |
---|
[326] | 209 | ParamObject *c = new(numfields)ParamObject(numfields, par.getParamTab()); |
---|
| 210 | *c = *this; |
---|
| 211 | return c; |
---|
| 212 | } |
---|
[109] | 213 | |
---|
[326] | 214 | void ParamObject::copyObject(void* dst, void* src) |
---|
| 215 | { |
---|
| 216 | if ((!dst) || (!src)) return; |
---|
| 217 | ParamObject *s = (ParamObject*)src; |
---|
| 218 | ParamObject *d = (ParamObject*)dst; |
---|
| 219 | *d = *s; |
---|
| 220 | } |
---|
[109] | 221 | |
---|
[326] | 222 | void* ParamObject::dupObject(void* src) |
---|
| 223 | { |
---|
| 224 | if (!src) return NULL; |
---|
| 225 | ParamObject *s = (ParamObject*)src; |
---|
| 226 | return s->clone(); |
---|
[109] | 227 | } |
---|
| 228 | |
---|
| 229 | void ParamObject::freeObject(void* obj) |
---|
| 230 | { |
---|
[326] | 231 | if (!obj) return; |
---|
| 232 | ParamObject *o = (ParamObject*)obj; |
---|
| 233 | delete o; |
---|
[109] | 234 | } |
---|