forked from apoch/formula-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PropertyBag.cpp
472 lines (356 loc) · 10.8 KB
/
PropertyBag.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#include "Pch.h"
#include "Interfaces.h"
#include "Formula.h"
#include "Actions.h"
#include "TokenPool.h"
#include "PropertyBag.h"
#include "EventHandler.h"
#include "Scriptable.h"
#include "ScriptWorld.h"
#include "Functions.h"
void SimplePropertyBag::MaintainSorted() {
std::sort(std::begin(m_bag), std::end(m_bag), [](const BagPair & l, const BagPair & r) {
return l.first < r.first;
});
}
void SimplePropertyBag::Set(unsigned token, const Result & value) {
auto iter = std::remove_if(std::begin(m_bag), std::end(m_bag), [token](const std::pair<unsigned, ValueT> & item) {
return item.first == token;
});
m_bag.erase(iter, m_bag.end());
m_bag.emplace_back(token, value.payload.num.value);
MaintainSorted();
}
Result SimplePropertyBag::ResolveNumber(const IFormulaContext & context, unsigned scope, unsigned token) const {
Result ret;
ret.code = RESULT_CODE_MISSING_DEFINITION;
ret.payload.num.value = 0.0;
if(scope != 0) {
if(&context != this)
return context.ResolveNumber(context, scope, token);
return ret;
}
auto iter = std::lower_bound(std::begin(m_bag), std::end(m_bag), token, [](const BagPair & l, unsigned token) {
return l.first < token;
});
if(iter != m_bag.end() && iter->first == token) {
ret.code = RESULT_CODE_OK;
ret.payload.num.value = iter->second;
}
return ret;
}
ListResult SimplePropertyBag::ResolveList(const IFormulaContext & context, unsigned scope, unsigned token) const {
ref(context);
ref(scope);
ref(token);
ListResult ret;
ret.code = RESULT_CODE_MISSING_DEFINITION;
return ret;
}
void FormulaPropertyBag::Clear() {
m_bag.clear();
}
void FormulaPropertyBag::Flatten (SimplePropertyBag * bag, const ScopedPropertyBag * scopes) const {
for (auto & pair : m_bag) {
Result result = pair.second.Evaluate(scopes);
if (result.code != RESULT_CODE_OK)
assert(false);
bag->Set(pair.first, result);
}
}
void FormulaPropertyBag::MaintainSorted () {
std::sort(std::begin(m_bag), std::end(m_bag), [](const BagPair & l, const BagPair & r) {
return l.first < r.first;
});
}
void FormulaPropertyBag::Set (unsigned token, Formula && formula) {
for (auto & pair : m_bag) {
if (pair.first == token) {
pair.second = std::move(formula);
return;
}
}
m_bag.emplace_back(token, formula);
MaintainSorted();
}
void FormulaPropertyBag::Set (unsigned token, const Formula & formula) {
for (auto & pair : m_bag) {
if (pair.first == token) {
pair.second = formula;
return;
}
}
m_bag.emplace_back(token, formula);
MaintainSorted();
}
Result FormulaPropertyBag::ResolveNumber (const IFormulaContext & context, unsigned scope, unsigned token) const {
if (scope == 0) {
for (const auto & pair : m_bag) {
if (pair.first == token) {
return pair.second.Evaluate(&context);
}
}
}
Result ret;
ret.code = RESULT_CODE_MISSING_DEFINITION;
ret.payload.num.value = 0.0;
return ret;
}
ListResult FormulaPropertyBag::ResolveList (const IFormulaContext & context, unsigned scope, unsigned token) const {
ref(context);
ref(scope);
ref(token);
ListResult ret;
ret.code = RESULT_CODE_MISSING_DEFINITION;
return ret;
}
void FormulaPropertyBag::Set (unsigned token, const Result & value) {
Formula f;
if (value.type == RESULT_TYPE_SCALAR)
f.Push(value.payload.num.value);
else if (value.type == RESULT_TYPE_VECTOR2) {
f.Push(value.payload.num.value);
f.Push(value.payload.num.value2);
f.Push(GetFunctionEvaluatorMakeVector());
}
Set(token, std::move(f));
}
void ScopeResolver::AddScope (unsigned token, const IFormulaContext & context) {
m_bag.emplace_back(token, &context);
}
const IFormulaContext * ScopeResolver::GetScope (unsigned token) const {
for (const auto & pair : m_bag) {
if (pair.first == token)
return pair.second;
}
return nullptr;
}
void ScopeResolver::Clear () {
m_bag.clear();
}
ScopedPropertyBag::ScopedPropertyBag (ScriptWorld * world) {
m_thisBag = &m_builtInBag;
m_bindingBag = nullptr;
m_world = world;
}
ScopedPropertyBag::ScopedPropertyBag (ScopedPropertyBag && other) {
m_bindingBag = nullptr;
std::swap(m_lists, other.m_lists);
std::swap(m_builtInBag, other.m_builtInBag);
std::swap(m_resolver, other.m_resolver);
std::swap(m_bindingBag, other.m_bindingBag);
std::swap(m_world, other.m_world);
m_thisBag = &m_builtInBag;
if (other.m_thisBag != &other.m_builtInBag)
m_thisBag = other.m_thisBag;
}
ScopedPropertyBag::~ScopedPropertyBag () {
for (auto & pair : m_lists) {
for (auto & member : pair.second) {
member->OnListMembershipRemoved(pair.first, this);
}
}
delete m_bindingBag;
}
void ScopedPropertyBag::Clear () {
m_lists.clear();
m_builtInBag.Clear();
m_resolver.Clear();
m_thisBag = &m_builtInBag;
delete m_bindingBag;
m_bindingBag = nullptr;
}
void ScopedPropertyBag::InstantiateFrom(const ScopedPropertyBag & other) {
m_resolver = other.m_resolver;
m_builtInBag = other.m_builtInBag;
m_thisBag = (other.m_thisBag == &other.m_builtInBag ? &m_builtInBag : other.m_thisBag);
}
void ScopedPropertyBag::ListAddEntry(unsigned listToken, Scriptable * entry) {
m_lists[listToken].push_back(entry);
entry->OnListMembershipAdded(listToken, this);
}
void ScopedPropertyBag::ListRemoveEntry(unsigned listToken, const Scriptable & entry) {
auto iter = m_lists.find(listToken);
if (iter == m_lists.end())
return;
auto memberiter = std::find(std::begin(iter->second), std::end(iter->second), &entry);
if (memberiter == std::end(iter->second))
return;
iter->second.erase(memberiter);
}
const IFormulaContext * ScopedPropertyBag::ResolveContext (unsigned scope) const {
if (scope == 0)
return this;
if (m_world) {
TextPropertyBag * magic = m_world->GetMagicBag(scope);
if (magic)
return magic;
Scriptable * target = m_world->GetScriptable(scope);
if (target)
return &target->GetScopes();
}
return m_resolver.GetScope(scope);
}
Result ScopedPropertyBag::ResolveNumber (const IFormulaContext & originalContext, unsigned scope, unsigned token) const {
if (m_world && scope) {
TextPropertyBag * magic = m_world->GetMagicBag(scope);
if (magic)
return magic->ResolveNumber(originalContext, scope, token);
Scriptable * target = m_world->GetScriptable(scope);
if (target)
return target->GetScopes().ResolveNumber(originalContext, 0, token);
}
auto resolved = scope ? m_resolver.GetScope(scope) : m_thisBag;
if (resolved == nullptr) {
Result ret;
ret.code = RESULT_CODE_MISSING_DEFINITION;
ret.payload.num.value = 0.0;
return ret;
}
Result ret = resolved->ResolveNumber(originalContext, 0, token);
if (ret.code != RESULT_CODE_OK) {
if (m_bindingBag)
return m_bindingBag->ResolveNumber(originalContext, 0, token);
}
return ret;
}
ListResult ScopedPropertyBag::ResolveList (const IFormulaContext & context, unsigned scope, unsigned token) const {
ListResult ret;
auto iter = m_lists.find(scope);
if (iter != m_lists.end()) {
ret.code = RESULT_CODE_OK;
if (!iter->second.empty()) {
ret.values.reserve(iter->second.size());
for (const auto & scriptable : iter->second) {
Result result = scriptable->GetScopes().ResolveNumber(context, 0, token);
if (result.code != RESULT_CODE_OK) {
ret.code = result.code;
ret.values.clear();
break;
}
ret.values.push_back(result.payload.num.value);
}
}
return ret;
}
ret.code = RESULT_CODE_MISSING_DEFINITION;
return ret;
}
void ScopedPropertyBag::SetFormula (unsigned token, const Formula & formula) {
m_thisBag->Set(token, formula);
}
void ScopedPropertyBag::SetProperties (FormulaPropertyBag * refbag) {
m_thisBag = refbag;
}
void ScopedPropertyBag::SetBindings (const BindingPropertyBag & refBag) {
if (m_bindingBag)
*m_bindingBag = refBag;
else
m_bindingBag = new BindingPropertyBag(refBag);
}
void ScopedPropertyBag::SetNamedBinding (unsigned token, Scriptable * binding) {
m_namedBindings.emplace_back(token, binding);
}
Scriptable * ScopedPropertyBag::GetNamedBinding (unsigned token) const {
for (const auto & pair : m_namedBindings) {
if (pair.first == token)
return pair.second;
}
return nullptr;
}
void ScopedPropertyBag::PopulateNamedBindings (ScopedPropertyBag * other) const {
other->m_namedBindings = m_namedBindings;
}
void ScopedPropertyBag::Set (unsigned token, const Result & value) {
m_thisBag->Set(token, value);
}
bool ScopedPropertyBag::ResolveToken (unsigned scope, unsigned token, std::string * out) const {
if (m_world && scope) {
TextPropertyBag * magic = m_world->GetMagicBag(scope);
if (magic) {
*out = magic->GetLine(token);
return true;
}
}
if (!m_resolver.GetScope(scope))
return false;
return m_resolver.GetScope(scope)->ResolveToken(0, token, out);
}
BindingPropertyBag::BindingPropertyBag(const Scriptable * scriptable)
: m_scriptable(scriptable)
{
}
Result BindingPropertyBag::ResolveNumber(const IFormulaContext & context, unsigned scope, unsigned token) const {
return m_scriptable->ResolveBinding(context, scope, token);
}
ListResult BindingPropertyBag::ResolveList(const IFormulaContext & context, unsigned scope, unsigned token) const {
ref(context);
ref(scope);
ref(token);
ListResult ret;
ret.code = RESULT_CODE_MISSING_DEFINITION;
return ret;
}
TextPropertyBag::TextPropertyBag (unsigned scope) :
m_scope(scope)
{
}
void TextPropertyBag::AddLine(unsigned token, const char * str) {
m_bag[token] = str;
}
const char * TextPropertyBag::GetLine(unsigned token) const {
auto iter = m_bag.find(token);
if (iter == m_bag.end())
return nullptr;
return iter->second.c_str();
}
Result TextPropertyBag::ResolveNumber(const IFormulaContext & context, unsigned scope, unsigned token) const {
ref(context);
ref(scope);
Result ret;
ret.type = RESULT_TYPE_TOKEN;
ret.code = RESULT_CODE_OK;
ret.payload.txt.token = token;
ret.payload.txt.scope = m_scope;
return ret;
}
ListResult TextPropertyBag::ResolveList(const IFormulaContext & context, unsigned scope, unsigned token) const {
ref(context);
ref(scope);
ref(token);
ListResult ret;
ret.code = RESULT_CODE_MISSING_DEFINITION;
return ret;
}
void TokenPropertyBag::Set (unsigned token, const Result & value) {
m_bag[token] = value;
}
Result TokenPropertyBag::ResolveNumber (const IFormulaContext & context, unsigned scope, unsigned token) const {
ref(context);
ref(scope);
Result ret;
auto iter = m_bag.find(token);
if (iter == m_bag.end()) {
ret.code = RESULT_CODE_MISSING_DEFINITION;
ret.payload.num.value = 0.0;
return ret;
}
ret = iter->second;
return ret;
}
ListResult TokenPropertyBag::ResolveList (const IFormulaContext & context, unsigned scope, unsigned token) const {
ref(context);
ref(scope);
ref(token);
ListResult ret;
ret.code = RESULT_CODE_MISSING_DEFINITION;
return ret;
}
unsigned TokenPropertyBag::AddToken (const std::string & token) {
return m_pool.AddToken(token);
}
bool TokenPropertyBag::ResolveToken (unsigned scope, unsigned token, std::string * out) const {
ref(scope);
*out = m_pool.GetStringFromToken(token);
return true;
}