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