1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2020 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 | t->help = maybedup(dupentries, pi->getLongName()); |
---|
51 | for (i = 0; i < gcount; i++) |
---|
52 | { |
---|
53 | t->id = maybedup(dupentries, pi->grname(i)); |
---|
54 | t->offset = 0; |
---|
55 | t++; |
---|
56 | } |
---|
57 | n = 1; |
---|
58 | offset = firstFieldOffset(); |
---|
59 | if (addnew) |
---|
60 | { |
---|
61 | t->id = maybedup(dupentries, "new"); |
---|
62 | t->name = maybedup(dupentries, "create new object"); |
---|
63 | SString tmp = SString::sprintf("p o%s()", pi->getName()); |
---|
64 | t->type = maybedup(dupentries, tmp.c_str()); |
---|
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++; |
---|
72 | } |
---|
73 | for (i = firstprop; i < pi->getPropCount(); i++) |
---|
74 | { |
---|
75 | const char* type = pi->type(i); |
---|
76 | if ((!stripproc) || (strchr("dfsox", type[0]))) |
---|
77 | { |
---|
78 | paInt flag = pi->flags(i); |
---|
79 | int tmp_offset; |
---|
80 | if ((!flagsexclude_data) || (!(flag&flagsexclude_data))) |
---|
81 | { |
---|
82 | if (type[0] == 'p') |
---|
83 | tmp_offset = 0; |
---|
84 | else |
---|
85 | { |
---|
86 | tmp_offset = offset; |
---|
87 | if (type[0] != 'x') tmp_offset += (((char*)&ex.data[0]) - ((char*)&ex)); |
---|
88 | offset += sizeof(ExtValue); |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | if ((!flagsexclude_tab) || (!(flag&flagsexclude_tab))) |
---|
93 | { |
---|
94 | t->offset = tmp_offset; |
---|
95 | t->group = (paInt)(stripgroups ? 0 : pi->group(i)); |
---|
96 | t->flags = (paInt)flag; |
---|
97 | if (readonly_into_userreadonly && (t->flags & PARAM_READONLY)) |
---|
98 | t->flags = (t->flags & ~PARAM_READONLY) | PARAM_USERREADONLY; |
---|
99 | t->fun1 = 0; |
---|
100 | t->fun2 = 0; |
---|
101 | t->id = maybedup(dupentries, pi->id(i)); |
---|
102 | t->name = maybedup(dupentries, pi->name(i)); |
---|
103 | t->type = maybedup(dupentries, type); |
---|
104 | t->help = maybedup(dupentries, pi->help(i)); |
---|
105 | t++; n++; |
---|
106 | if (n > count) break; |
---|
107 | } |
---|
108 | } |
---|
109 | } |
---|
110 | t->id = 0; t->group = 0; t->flags = dupentries ? MUTPARAM_ALLOCENTRY : 0; |
---|
111 | return tab; |
---|
112 | } |
---|
113 | |
---|
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 | |
---|
127 | void ParamObject::freeParamTab(ParamEntry *pe) |
---|
128 | { |
---|
129 | if (paramTabAllocatedString(pe)) |
---|
130 | { |
---|
131 | int i; |
---|
132 | ParamEntry *e; |
---|
133 | maybefree((void*)pe->name); |
---|
134 | maybefree((void*)pe->type); |
---|
135 | maybefree((void*)pe->help); |
---|
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++) |
---|
139 | { |
---|
140 | maybefree((void*)e->id); |
---|
141 | maybefree((void*)e->name); |
---|
142 | maybefree((void*)e->type); |
---|
143 | maybefree((void*)e->help); |
---|
144 | } |
---|
145 | } |
---|
146 | free(pe); |
---|
147 | } |
---|
148 | |
---|
149 | bool ParamObject::paramTabEqual(ParamEntry *pe1, ParamEntry *pe2) |
---|
150 | { |
---|
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++) |
---|
154 | { |
---|
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; |
---|
159 | } |
---|
160 | return true; |
---|
161 | } |
---|
162 | |
---|
163 | void ParamObject::p_new(void* obj, ExtValue *args, ExtValue *ret) |
---|
164 | { |
---|
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 | { |
---|
188 | if (!tab) return NULL; |
---|
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])) |
---|
192 | used_fields++; |
---|
193 | |
---|
194 | if (used_fields == 0) return NULL; |
---|
195 | ParamObject *obj = new(used_fields)ParamObject(used_fields, tab); // new(n): allocate n fields ; ParamObject(n,...): tell the object it has n fields |
---|
196 | ExtValue *v = &obj->fields[0]; |
---|
197 | n = tab->flags; |
---|
198 | for (ParamEntry *t = tab + tab->group; n > 0; n--, t++) |
---|
199 | switch (*t->type) |
---|
200 | { |
---|
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; |
---|
204 | case 'o': |
---|
205 | { |
---|
206 | ExtObject new_obj; |
---|
207 | if (t->flags & PARAM_OBJECTSET) |
---|
208 | { |
---|
209 | ParamInterface *cls = ExtValue::findDeserializableClass(t->type + 1); |
---|
210 | if (cls) |
---|
211 | { |
---|
212 | int new_fun = cls->findId("new"); |
---|
213 | if (new_fun >= 0) |
---|
214 | { |
---|
215 | ExtValue dummy, new_value; |
---|
216 | cls->call(new_fun, &dummy, &new_value); |
---|
217 | new_obj = new_value.getObject(); |
---|
218 | } |
---|
219 | } |
---|
220 | } |
---|
221 | v->setObject(new_obj); |
---|
222 | v++; |
---|
223 | } |
---|
224 | break; |
---|
225 | case 'x': v++; break; |
---|
226 | } |
---|
227 | return obj; |
---|
228 | } |
---|
229 | |
---|
230 | void ParamObject::operator=(const ParamObject& src) |
---|
231 | { |
---|
232 | const ExtValue *s = &src.fields[0]; |
---|
233 | ExtValue *d = &fields[0]; |
---|
234 | int n = min(numfields, src.numfields); |
---|
235 | for (int i = 0; i < n; i++, d++, s++) |
---|
236 | *d = *s; |
---|
237 | } |
---|
238 | |
---|
239 | ParamObject* ParamObject::clone() |
---|
240 | { |
---|
241 | ParamObject *c = new(numfields)ParamObject(numfields, par.getParamTab()); |
---|
242 | *c = *this; |
---|
243 | return c; |
---|
244 | } |
---|
245 | |
---|
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 | } |
---|
253 | |
---|
254 | void* ParamObject::dupObject(void* src) |
---|
255 | { |
---|
256 | if (!src) return NULL; |
---|
257 | ParamObject *s = (ParamObject*)src; |
---|
258 | return s->clone(); |
---|
259 | } |
---|
260 | |
---|
261 | void ParamObject::freeObject(void* obj) |
---|
262 | { |
---|
263 | if (!obj) return; |
---|
264 | ParamObject *o = (ParamObject*)obj; |
---|
265 | delete o; |
---|
266 | } |
---|