source: cpp/frams/param/paramtrans.cpp @ 729

Last change on this file since 729 was 729, checked in by Maciej Komosinski, 5 years ago

mutableparam_test added: demonstrates usage of ParamTransaction? and dynamic properties and groups

File size: 5.5 KB
Line 
1#include "paramtrans.h"
2#include <frams/util/extvalue.h>
3
4void TwoWayMap::printList(const SListTempl<int>& L)
5{
6for (int i=0;i<L.size();i++)
7        {
8        int x=L(i);
9        if (x>=0) printf("%d",x);
10        else printf("x");
11        }
12}
13
14void TwoWayMap::print()
15{
16printList(map);
17printf("/");
18printList(invmap);
19}
20
21void TwoWayMap::reset(int size)
22{
23map.clear(); invmap.clear();
24for (int i=0;i<size;i++)
25        {
26        map.set(i,i);
27        invmap.set(i,i);
28        }
29}
30
31/** @return -2 = out of bounds */
32int TwoWayMap::get(int pos)
33{
34if ((pos<0)||(pos>=map.size())) return -2;
35return map(pos);
36}
37
38/** @return -2 = out of bounds */
39int TwoWayMap::invget(int pos)
40{
41if ((pos<0)||(pos>=invmap.size())) return -2;
42return invmap(pos);
43}
44
45void TwoWayMap::insert(int i)
46{// map/invmap: 01234/01234 add(1) -> 0-1234/02345
47//             1--40-2/406-3 add(1) -> 1---40-2/507-4
48map.insert(i,-1);
49for (int p=i+1;p<map.size();p++)
50        {
51        int x=map(p);
52        if (x>=0)
53                invmap.set(x,p);
54        }
55}
56
57void TwoWayMap::remove(int i)
58{
59// map/invmap: 01234/01234 rem(1) -> 0234/0-123
60//             1--40-2/406-3 rem(1) -> 1-40-2/305-2
61int x=map(i);
62if (x>=0) invmap.set(x,-1);
63map.remove(i);
64for (int p=i;p<map.size();p++)
65        {
66        int x=map(p);
67        if (x>=0)
68                invmap.set(x,p);
69        }
70}
71
72///////////////////////////
73
74ParamTransaction::ParamTransaction(MutableParamInterface &mpi)
75        :par(mpi)
76{
77panode=par.onadd.add(STATRICKCALLBACK(this,&ParamTransaction::onPropAdd,0));
78pdnode=par.ondelete.add(STATRICKCALLBACK(this,&ParamTransaction::onPropDelete,0));
79pcnode=par.onchange.add(STATRICKCALLBACK(this,&ParamTransaction::onPropChange,0));
80ganode=par.ongroupadd.add(STATRICKCALLBACK(this,&ParamTransaction::onGroupAdd,0));
81gdnode=par.ongroupdelete.add(STATRICKCALLBACK(this,&ParamTransaction::onGroupDelete,0));
82gcnode=par.ongroupchange.add(STATRICKCALLBACK(this,&ParamTransaction::onGroupChange,0));
83reset();
84}
85
86ParamTransaction::~ParamTransaction()
87{
88par.onadd.removeNode(panode);
89par.ondelete.removeNode(pdnode);
90par.onchange.removeNode(pcnode);
91par.ongroupadd.removeNode(ganode);
92par.ongroupdelete.removeNode(gdnode);
93par.ongroupchange.removeNode(gcnode);
94}
95
96void ParamTransaction::reset()
97{
98resetMaps();
99changed=false;
100grchanged=false;
101}
102
103void ParamTransaction::onPropAdd(void* data,intptr_t i)
104{
105propmap.insert((int)i);
106changed=true;
107}
108
109void ParamTransaction::onPropDelete(void* data,intptr_t i)
110{
111propmap.remove((int)i);
112changed=true;
113}
114
115void ParamTransaction::onGroupAdd(void* data,intptr_t i)
116{
117groupmap.insert((int)i);
118grchanged=true;
119}
120
121void ParamTransaction::onGroupDelete(void* data,intptr_t i)
122{
123groupmap.remove((int)i);
124grchanged=true;
125}
126
127void ParamTransaction::onPropChange(void* data,intptr_t i)
128{
129changed=true;
130}
131
132void ParamTransaction::onGroupChange(void* data,intptr_t i)
133{
134grchanged=true;
135}
136
137void ParamTransaction::resetMaps()
138{
139propmap.reset(par.getPropCount());
140groupmap.reset(par.getGroupCount());
141}
142
143int ParamTransaction::propertyPosition(int prop)
144{return propmap.invget(prop);}
145
146int ParamTransaction::groupPosition(int group)
147{return groupmap.invget(group);}
148
149const char * ParamTransaction::id(int i)
150{
151i=propertyPosition(i);
152if (i==-2) return 0;
153if (i>=0) return par.id(i); else return "?";
154}
155
156const char * ParamTransaction::name(int i)
157{
158i=propertyPosition(i);
159if (i==-2) return 0;
160if (i>=0) return par.name(i); else return "?";
161}
162
163const char * ParamTransaction::type(int i)
164{
165i=propertyPosition(i);
166if (i==-2) return 0;
167if (i>=0) return par.type(i); else return "?";
168}
169
170const char * ParamTransaction::help(int i)
171{
172i=propertyPosition(i);
173return (i>=0)?par.help(i):0;
174}
175
176int ParamTransaction::flags(int i)
177{
178i=propertyPosition(i);
179return (i>=0)?par.flags(i):0;
180}
181
182int ParamTransaction::group(int i)
183{
184i=propertyPosition(i);
185return (i>=0)?par.group(i):0;
186}
187
188const char * ParamTransaction::grname(int i)
189{
190i=groupPosition(i);
191if (i==-2) return 0;
192return (i>=0)?par.grname(i):"?";
193}
194
195int ParamTransaction::grmember(int g, int i)
196{
197g=groupPosition(g);
198if (g<0) return -9999;
199for(int p=0;p<getPropCount();p++)
200        if (group(p)==g)
201                if (!i--)
202                        return p;
203return -9999;
204}
205
206void ParamTransaction::call(int i, ExtValue *a, ExtValue *b)
207{
208i=propertyPosition(i);
209if (i>=0) par.call(i,a,b);
210}
211
212SString ParamTransaction::getString(int i)
213{
214i=propertyPosition(i);
215return (i>=0)?par.getString(i):SString();
216}
217
218paInt ParamTransaction::getInt(int i)
219{
220i=propertyPosition(i);
221return (i>=0)?par.getInt(i):0;
222}
223
224double ParamTransaction::getDouble(int i)
225{
226i=propertyPosition(i);
227return (i>=0)?par.getDouble(i):0;
228}
229
230ExtObject ParamTransaction::getObject(int i)
231{
232i=propertyPosition(i);
233return (i>=0)?par.getObject(i):ExtObject();
234}
235
236ExtValue ParamTransaction::getExtValue(int i)
237{
238i=propertyPosition(i);
239return (i>=0)?par.getExtValue(i):ExtValue();
240}
241
242int ParamTransaction::setInt(int i, paInt v)
243{
244i=propertyPosition(i);
245return (i>=0)?par.setInt(i,v):PSET_NOPROPERTY;
246}
247
248int ParamTransaction::setDouble(int i, double v)
249{
250i=propertyPosition(i);
251return (i>=0)?par.setDouble(i,v):PSET_NOPROPERTY;
252}
253
254int ParamTransaction::setString(int i, const SString &v)
255{
256i=propertyPosition(i);
257return (i>=0)?par.setString(i,v):PSET_NOPROPERTY;
258}
259
260int ParamTransaction::setObject(int i, const ExtObject &v)
261{
262i=propertyPosition(i);
263return (i>=0)?par.setObject(i,v):PSET_NOPROPERTY;
264}
265
266int ParamTransaction::setExtValue(int i, const ExtValue &v)
267{
268i=propertyPosition(i);
269return (i>=0)?par.setExtValue(i,v):PSET_NOPROPERTY;
270}
Note: See TracBrowser for help on using the repository browser.