Reworks script aggregator. Cleans up API and code, and now handles scripts being removed from a set while we're iterating.
All checks were successful
continuous-integration/drone/push Build is passing

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
2021-04-18 12:50:48 +02:00
parent 13df99a6cc
commit be10b3515c
4 changed files with 83 additions and 80 deletions

View File

@@ -9,73 +9,54 @@ namespace CreatureLib::Battling {
class ScriptAggregator {
const ScriptWrapper* _scripts;
i32 _size;
i32 _index = 0;
i32 _setIndex = 0;
i32 _index = -1;
i32 _setIndex = -1;
inline void IncrementToNextNotNull(bool initialIncrement = true) {
if (_scripts[_index].IsSet()) {
inline bool IncrementToNextNotNull() {
if (_index != -1 && _scripts[_index].IsSet()) {
_setIndex++;
if (_setIndex >= (i32)_scripts[_index].GetScriptSet()->Count()) {
_setIndex = -1;
} else {
return;
return true;
}
}
if (initialIncrement)
_index++;
while (HasNext()) {
for (_index++; _index < _size; ++_index) {
if (_scripts[_index].HasValue()) {
if (_scripts[_index].IsSet()) {
_setIndex = -1;
_setIndex = 0;
}
return;
return true;
}
_index++;
}
return false;
}
public:
ScriptAggregator(){};
explicit ScriptAggregator(const ArbUt::List<ScriptWrapper>& scripts)
: _scripts(scripts.RawData()), _size(scripts.Count()) {
Reset();
};
: _scripts(scripts.RawData()), _size(scripts.Count()){};
inline void Reset() {
_index = 0;
if (_size > 0) {
if (_scripts[_index].IsSet()) {
_setIndex = -1;
}
IncrementToNextNotNull(false);
}
_index = -1;
_setIndex = -1;
}
inline bool HasNext() { return _index < _size; }
std::optional<ArbUt::BorrowedPtr<BattleScript>> GetNextNotNull() {
while (HasNext()) {
auto s = GetNext();
return s;
bool GetNext(ArbUt::BorrowedPtr<BattleScript>& out) {
if (!IncrementToNextNotNull()) {
return false;
}
return {};
}
ArbUt::BorrowedPtr<BattleScript> GetNext() {
auto& current = _scripts[_index];
if (!current.IsSet()) {
auto s = current.GetScript();
IncrementToNextNotNull();
return (*s);
out = *s;
return true;
} else {
auto& set = current.GetScriptSet()->GetIterator();
if (_setIndex == -1) {
_setIndex = 0;
}
auto v = set[_setIndex];
IncrementToNextNotNull();
return v;
out = v;
return true;
}
}
};

View File

@@ -2,8 +2,8 @@
{ \
try { \
auto aggregator = source->GetScriptIterator(); \
while (aggregator.HasNext()) { \
auto next = aggregator.GetNext(); \
ArbUt::BorrowedPtr<CreatureLib::Battling::BattleScript> next = (CreatureLib::Battling::BattleScript*)1; \
while (aggregator.GetNext(next)) { \
try { \
next->hookName(__VA_ARGS__); \
} catch (const std::exception& e) { \