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) |
---|
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 | if ((!stripproc) || (strchr("dfsox", *pi->type(i)))) |
---|
72 | { |
---|
73 | if ((!flagsexclude) || (!(pi->flags(i)&flagsexclude))) |
---|
74 | { |
---|
75 | t->offset = offset; |
---|
76 | if (*pi->type(i) != 'x') t->offset += (((char*)&ex.data[0]) - ((char*)&ex)); |
---|
77 | t->group = (short)(stripgroups ? 0 : pi->group(i)); |
---|
78 | t->flags = (short)pi->flags(i); |
---|
79 | t->fun1 = 0; |
---|
80 | t->fun2 = 0; |
---|
81 | t->id = maybedup(dupentries, pi->id(i)); |
---|
82 | t->name = maybedup(dupentries, pi->name(i)); |
---|
83 | t->type = maybedup(dupentries, pi->type(i)); |
---|
84 | t->help = maybedup(dupentries, pi->help(i)); |
---|
85 | t++; n++; offset += sizeof(ExtValue); |
---|
86 | if (n > count) break; |
---|
87 | } |
---|
88 | } |
---|
89 | } |
---|
90 | t->id = 0; t->group = 0; t->flags = dupentries ? MUTPARAM_ALLOCENTRY : 0; |
---|
91 | return tab; |
---|
92 | } |
---|
93 | |
---|
94 | void ParamObject::setParamTabText(ParamEntry *pe, const char* &ptr, const char* txt) |
---|
95 | { |
---|
96 | if (!paramTabAllocatedString(pe)) |
---|
97 | return; |
---|
98 | maybefree((char*)ptr); |
---|
99 | ptr = maybedup(true, txt); |
---|
100 | } |
---|
101 | |
---|
102 | bool ParamObject::paramTabAllocatedString(ParamEntry *pe) |
---|
103 | { |
---|
104 | return (pe[pe->flags + pe->group].flags & MUTPARAM_ALLOCENTRY) ? true : false; |
---|
105 | } |
---|
106 | |
---|
107 | void ParamObject::freeParamTab(ParamEntry *pe) |
---|
108 | { |
---|
109 | if (paramTabAllocatedString(pe)) |
---|
110 | { |
---|
111 | int i; |
---|
112 | ParamEntry *e; |
---|
113 | maybefree((void*)pe->name); |
---|
114 | maybefree((void*)pe->type); |
---|
115 | for (i = 0, e = pe; i < pe->group; i++, e++) |
---|
116 | maybefree((void*)e->id); |
---|
117 | for (i = pe->group, e = pe + i; i < pe->group + pe->flags; i++, e++) |
---|
118 | { |
---|
119 | maybefree((void*)e->id); |
---|
120 | maybefree((void*)e->name); |
---|
121 | maybefree((void*)e->type); |
---|
122 | maybefree((void*)e->help); |
---|
123 | } |
---|
124 | } |
---|
125 | free(pe); |
---|
126 | } |
---|
127 | |
---|
128 | bool ParamObject::paramTabEqual(ParamEntry *pe1, ParamEntry *pe2) |
---|
129 | { |
---|
130 | if (pe1->flags != pe2->flags) return false; |
---|
131 | ParamEntry *e1 = pe1 + pe1->group, *e2 = pe2 + pe2->group; |
---|
132 | for (int i = 0; i < pe1->flags; i++, e1++, e2++) |
---|
133 | { |
---|
134 | if (strcmp(e1->id, e2->id)) return false; |
---|
135 | if (strcmp(e1->name, e2->name)) return false; |
---|
136 | if (strcmp(e1->type, e2->type)) return false; |
---|
137 | if (e1->offset != e2->offset) return false; |
---|
138 | } |
---|
139 | return true; |
---|
140 | } |
---|
141 | |
---|
142 | void ParamObject::p_new(void* obj, ExtValue *args, ExtValue *ret) |
---|
143 | { |
---|
144 | ParamObject *this_obj = (ParamObject*)obj; |
---|
145 | ParamObject *po = makeObject(this_obj->par.getParamTab()); |
---|
146 | ret->setObject(ExtObject(&this_obj->par, po)); |
---|
147 | po->par.setDefault(); |
---|
148 | } |
---|
149 | |
---|
150 | ParamObject::ParamObject(int _numfields, ParamEntry *_tab) |
---|
151 | { |
---|
152 | numfields = _numfields; |
---|
153 | par.setParamTab(_tab); |
---|
154 | par.select(this); |
---|
155 | for (int i = 0; i < numfields; i++) |
---|
156 | new(&fields[i])ExtValue(); |
---|
157 | } |
---|
158 | |
---|
159 | ParamObject::~ParamObject() |
---|
160 | { |
---|
161 | for (int i = 0; i < numfields; i++) |
---|
162 | fields[i].~ExtValue(); |
---|
163 | } |
---|
164 | |
---|
165 | ParamObject* ParamObject::makeObject(ParamEntry *tab) |
---|
166 | { |
---|
167 | if (!tab) return 0; |
---|
168 | int n = tab->flags; |
---|
169 | if (!n) return 0; |
---|
170 | ParamObject *obj = new(n)ParamObject(n, tab); // new(n): allocate n fields ; ParamObject(n,...): tell the object it has n fields |
---|
171 | ExtValue *v = &obj->fields[0]; |
---|
172 | tab += tab->group; |
---|
173 | for (; n > 0; n--, tab++) |
---|
174 | switch (*tab->type) |
---|
175 | { |
---|
176 | case 'd': v->setInt(0); v++; break; |
---|
177 | case 'f': v->setDouble(0); v++; break; |
---|
178 | case 's': v->setString(SString::empty()); v++; break; |
---|
179 | case 'o': v->setObject(ExtObject()); v++; break; |
---|
180 | case 'x': v++; break; |
---|
181 | } |
---|
182 | return obj; |
---|
183 | } |
---|
184 | |
---|
185 | void ParamObject::operator=(const ParamObject& src) |
---|
186 | { |
---|
187 | const ExtValue *s = &src.fields[0]; |
---|
188 | ExtValue *d = &fields[0]; |
---|
189 | int n = min(numfields, src.numfields); |
---|
190 | for (int i = 0; i < n; i++, d++, s++) |
---|
191 | *d = *s; |
---|
192 | } |
---|
193 | |
---|
194 | ParamObject* ParamObject::clone() |
---|
195 | { |
---|
196 | ParamObject *c = new(numfields)ParamObject(numfields, par.getParamTab()); |
---|
197 | *c = *this; |
---|
198 | return c; |
---|
199 | } |
---|
200 | |
---|
201 | void ParamObject::copyObject(void* dst, void* src) |
---|
202 | { |
---|
203 | if ((!dst) || (!src)) return; |
---|
204 | ParamObject *s = (ParamObject*)src; |
---|
205 | ParamObject *d = (ParamObject*)dst; |
---|
206 | *d = *s; |
---|
207 | } |
---|
208 | |
---|
209 | void* ParamObject::dupObject(void* src) |
---|
210 | { |
---|
211 | if (!src) return NULL; |
---|
212 | ParamObject *s = (ParamObject*)src; |
---|
213 | return s->clone(); |
---|
214 | } |
---|
215 | |
---|
216 | void ParamObject::freeObject(void* obj) |
---|
217 | { |
---|
218 | if (!obj) return; |
---|
219 | ParamObject *o = (ParamObject*)obj; |
---|
220 | delete o; |
---|
221 | } |
---|