Rework ScriptAggregator so it only needs to be created once, and can then be reset.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-04-23 19:57:45 +02:00
parent 742d0c772c
commit 3a11bba913
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
4 changed files with 26 additions and 19 deletions

View File

@ -14,12 +14,18 @@ namespace CreatureLib::Battling {
List<ScriptWrapper> _scripts;
size_t _index = 0;
bool _isSetSet = false;
const List<Script*>* _setScripts;
size_t _setIndex;
const List<Script*>* _setScripts = nullptr;
size_t _setIndex = 0;
public:
ScriptAggregator(List<ScriptWrapper> scripts) : _scripts(std::move(scripts)){};
ScriptAggregator(){};
explicit ScriptAggregator(const List<ScriptWrapper>& scripts) : _scripts(scripts){};
void Reset() {
_index = 0;
_isSetSet = false;
_setIndex = 0;
}
bool HasNext() { return _index < _scripts.Count() || (_isSetSet && _setIndex < _setScripts->Count()); }
Script* GetNext() {
@ -38,7 +44,7 @@ namespace CreatureLib::Battling {
}
if (_index >= _scripts.Count())
return nullptr;
auto next = _scripts[_index];
auto& next = _scripts[_index];
_index++;
if (!next.IsSet()) {
auto scriptPtr = next.GetScript();
@ -54,7 +60,6 @@ namespace CreatureLib::Battling {
_setIndex = 0;
return GetNext();
}
throw NotReachableException();
}
};
}

View File

@ -1,6 +1,7 @@
#define HOOK(hookName, source, ...) \
{ \
auto aggregator = source->GetScriptIterator(); \
aggregator.Reset(); \
while (aggregator.HasNext()) { \
auto next = aggregator.GetNext(); \
if (next == nullptr) \

View File

@ -8,22 +8,21 @@
namespace CreatureLib::Battling {
class ScriptSource {
bool _areScriptsInitialized = false;
Arbutils::Collections::List<ScriptWrapper> _scripts;
ScriptAggregator _scripts;
protected:
virtual void GetActiveScripts(Arbutils::Collections::List<ScriptWrapper>& scripts) = 0;
void ResetActiveScripts() {
_areScriptsInitialized = false;
_scripts.Clear();
}
void ResetActiveScripts() { _areScriptsInitialized = false; }
public:
ScriptAggregator GetScriptIterator() {
const ScriptAggregator& GetScriptIterator() {
if (!_areScriptsInitialized) {
GetActiveScripts(_scripts);
List<ScriptWrapper> scripts;
GetActiveScripts(scripts);
_scripts = ScriptAggregator(scripts);
_areScriptsInitialized = true;
}
return ScriptAggregator(_scripts);
return _scripts;
}
};
}

View File

@ -7,18 +7,20 @@
namespace CreatureLib::Battling {
class ScriptWrapper {
std::variant<Script**, ScriptSet*> _value;
union {
Script** _script;
ScriptSet* _scriptSet;
};
bool _isSet;
public:
ScriptWrapper(Script** s) : _value(s), _isSet(false) {}
ScriptWrapper(ScriptSet* s) : _value(s), _isSet(true) {}
ScriptWrapper(Script** s) : _script(s), _isSet(false) {}
ScriptWrapper(ScriptSet* s) : _scriptSet(s), _isSet(true) {}
bool IsSet() const { return _isSet; }
Script** GetScript() const { return std::get<Script**>(_value); }
ScriptSet* GetScriptSet() const { return std::get<ScriptSet*>(_value); }
Script** GetScript() const { return _script; }
ScriptSet* GetScriptSet() const { return _scriptSet; }
};
}