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, bool addnew, const char* rename, bool readonly_into_userreadonly) |
---|
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++) |
---|
48 | { |
---|
49 | t->id = maybedup(dupentries, pi->grname(i)); |
---|
50 | t->offset = 0; |
---|
51 | t++; |
---|
52 | } |
---|
53 | n = 1; |
---|
54 | offset = firstFieldOffset(); |
---|
55 | if (addnew) |
---|
56 | { |
---|
57 | t->id = maybedup(dupentries, "new"); |
---|
58 | t->name = maybedup(dupentries, "create new object"); |
---|
59 | SString tmp = SString::sprintf("p o%s()", pi->getName()); |
---|
60 | t->type = maybedup(dupentries, tmp.c_str()); |
---|
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++; |
---|
68 | } |
---|
69 | for (i = firstprop; i < pi->getPropCount(); i++) |
---|
70 | { |
---|
71 | const char* type=pi->type(i); |
---|
72 | if ((!stripproc) || (strchr("dfsox", type[0]))) |
---|
73 | { |
---|
74 | if ((!flagsexclude) || (!(pi->flags(i)&flagsexclude))) |
---|
75 | { |
---|
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 | } |
---|
84 | t->group = (short)(stripgroups ? 0 : pi->group(i)); |
---|
85 | t->flags = (short)pi->flags(i); |
---|
86 | if (readonly_into_userreadonly && (t->flags & PARAM_READONLY)) |
---|
87 | t->flags=(t->flags & ~PARAM_READONLY) | PARAM_USERREADONLY; |
---|
88 | t->fun1 = 0; |
---|
89 | t->fun2 = 0; |
---|
90 | t->id = maybedup(dupentries, pi->id(i)); |
---|
91 | t->name = maybedup(dupentries, pi->name(i)); |
---|
92 | t->type = maybedup(dupentries, type); |
---|
93 | t->help = maybedup(dupentries, pi->help(i)); |
---|
94 | t++; n++; |
---|
95 | if (n > count) break; |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|
99 | t->id = 0; t->group = 0; t->flags = dupentries ? MUTPARAM_ALLOCENTRY : 0; |
---|
100 | return tab; |
---|
101 | } |
---|
102 | |
---|
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 | |
---|
116 | void ParamObject::freeParamTab(ParamEntry *pe) |
---|
117 | { |
---|
118 | if (paramTabAllocatedString(pe)) |
---|
119 | { |
---|
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++) |
---|
127 | { |
---|
128 | maybefree((void*)e->id); |
---|
129 | maybefree((void*)e->name); |
---|
130 | maybefree((void*)e->type); |
---|
131 | maybefree((void*)e->help); |
---|
132 | } |
---|
133 | } |
---|
134 | free(pe); |
---|
135 | } |
---|
136 | |
---|
137 | bool ParamObject::paramTabEqual(ParamEntry *pe1, ParamEntry *pe2) |
---|
138 | { |
---|
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++) |
---|
142 | { |
---|
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; |
---|
147 | } |
---|
148 | return true; |
---|
149 | } |
---|
150 | |
---|
151 | void ParamObject::p_new(void* obj, ExtValue *args, ExtValue *ret) |
---|
152 | { |
---|
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 | { |
---|
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 |
---|
184 | ExtValue *v = &obj->fields[0]; |
---|
185 | n=tab->flags; |
---|
186 | for (ParamEntry *t=tab+tab->group; n > 0; n--, t++) |
---|
187 | switch (*t->type) |
---|
188 | { |
---|
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; |
---|
194 | } |
---|
195 | return obj; |
---|
196 | } |
---|
197 | |
---|
198 | void ParamObject::operator=(const ParamObject& src) |
---|
199 | { |
---|
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; |
---|
205 | } |
---|
206 | |
---|
207 | ParamObject* ParamObject::clone() |
---|
208 | { |
---|
209 | ParamObject *c = new(numfields)ParamObject(numfields, par.getParamTab()); |
---|
210 | *c = *this; |
---|
211 | return c; |
---|
212 | } |
---|
213 | |
---|
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 | } |
---|
221 | |
---|
222 | void* ParamObject::dupObject(void* src) |
---|
223 | { |
---|
224 | if (!src) return NULL; |
---|
225 | ParamObject *s = (ParamObject*)src; |
---|
226 | return s->clone(); |
---|
227 | } |
---|
228 | |
---|
229 | void ParamObject::freeObject(void* obj) |
---|
230 | { |
---|
231 | if (!obj) return; |
---|
232 | ParamObject *o = (ParamObject*)obj; |
---|
233 | delete o; |
---|
234 | } |
---|