-
Notifications
You must be signed in to change notification settings - Fork 0
/
SumValue.h
49 lines (41 loc) · 1.43 KB
/
SumValue.h
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
#ifndef KALKY_SUMVALUE_H
#define KALKY_SUMVALUE_H
#include "BaseValue.h"
namespace kalky {
class SumValue : public BaseValue {
public:
void AddValueToSum(BaseValue* value) {
_sumValues.push_back(value);
_autoDestroyConnections.emplace_back(
value->OnValueDeleted.connect([this](kalky::BaseValue *value) {
auto valIter = _sumValues.begin();
while(valIter != _sumValues.end()) {
if(*valIter == value) {
_sumValues.erase(valIter);
SetValueDirty();
return;
}
++valIter;
}
}));
// when anything changes in the summed values, we are dirty as well...
_autoDestroyConnections.emplace_back(
value->OnValueChanged.connect([this](kalky::BaseValue *) {
SetValueDirty();
}));
SetValueDirty();
}
protected:
std::vector<BaseValue*> _sumValues;
void RecalculateCachedValue() override {
float sum = 0;
for(auto& value : _sumValues) {
sum += value->GetValue();
}
CacheValue(sum);
}
private:
std::vector<fteng::connection> _autoDestroyConnections;
};
}
#endif //KALKY_SUMVALUE_H