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

View File

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

View File

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

View File

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