Reset on initialization in ScriptAggregator, fixes segfault when calling Reset on an empty Aggregator.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
parent
33f796eff8
commit
9f091308b0
|
@ -32,17 +32,23 @@ namespace CreatureLib::Battling {
|
|||
}
|
||||
|
||||
public:
|
||||
ScriptAggregator(){};
|
||||
explicit ScriptAggregator(const ArbUt::List<ScriptWrapper>& scripts)
|
||||
: _scripts(scripts.RawData()), _size(scripts.Count()){};
|
||||
ScriptAggregator() {};
|
||||
|
||||
explicit ScriptAggregator(const ArbUt::List<ScriptWrapper> &scripts)
|
||||
: _scripts(scripts.RawData()), _size(scripts.Count()) {
|
||||
Reset();
|
||||
};
|
||||
|
||||
inline void Reset() {
|
||||
_index = 0;
|
||||
if (_size > 0) {
|
||||
if (_scripts[_index].IsSet()) {
|
||||
_setIndex = -1;
|
||||
}
|
||||
IncrementToNextNotNull(false);
|
||||
}
|
||||
}
|
||||
|
||||
inline bool HasNext() { return _index < _size; }
|
||||
|
||||
std::optional<ArbUt::BorrowedPtr<BattleScript>> GetNextNotNull() {
|
||||
|
@ -54,13 +60,13 @@ namespace CreatureLib::Battling {
|
|||
}
|
||||
|
||||
ArbUt::BorrowedPtr<BattleScript> GetNext() {
|
||||
auto& current = _scripts[_index];
|
||||
auto ¤t = _scripts[_index];
|
||||
if (!current.IsSet()) {
|
||||
auto s = current.GetScript();
|
||||
IncrementToNextNotNull();
|
||||
return (*s);
|
||||
} else {
|
||||
auto& set = current.GetScriptSet()->GetIterator();
|
||||
auto &set = current.GetScriptSet()->GetIterator();
|
||||
auto v = set[_setIndex];
|
||||
IncrementToNextNotNull();
|
||||
return v;
|
||||
|
|
|
@ -12,18 +12,20 @@ private:
|
|||
ArbUt::StringView _name;
|
||||
|
||||
public:
|
||||
explicit TestScript(const ArbUt::StringView& name) : _name(name){};
|
||||
const ArbUt::StringView& GetName() const noexcept override { return _name; }
|
||||
explicit TestScript(const ArbUt::StringView &name) : _name(name) {};
|
||||
|
||||
void TestMethod(int& runCount) { runCount++; }
|
||||
BattleScript* Clone() override { return new TestScript(_name); }
|
||||
const ArbUt::StringView &GetName() const noexcept override { return _name; }
|
||||
|
||||
void TestMethod(int &runCount) { runCount++; }
|
||||
|
||||
BattleScript *Clone() override { return new TestScript(_name); }
|
||||
};
|
||||
|
||||
TEST_CASE("Script Aggregator properly iterates containing script.") {
|
||||
TEST_CASE ("Script Aggregator properly iterates containing script.") {
|
||||
auto script = std::make_unique<TestScript>("test");
|
||||
auto ran = 0;
|
||||
auto vec = ArbUt::List<ScriptWrapper>{
|
||||
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script))};
|
||||
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript> *>(&script))};
|
||||
auto aggr = ScriptAggregator(vec);
|
||||
while (aggr.HasNext()) {
|
||||
auto next = aggr.GetNext();
|
||||
|
@ -32,15 +34,15 @@ TEST_CASE("Script Aggregator properly iterates containing script.") {
|
|||
CHECK(ran == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("Script Aggregator properly iterates multiple scripts.") {
|
||||
TEST_CASE ("Script Aggregator properly iterates multiple scripts.") {
|
||||
auto script = std::make_unique<TestScript>("test");
|
||||
auto script2 = std::make_unique<TestScript>("test2");
|
||||
auto script3 = std::make_unique<TestScript>("test3");
|
||||
auto ran = 0;
|
||||
auto vec = ArbUt::List<ScriptWrapper>{
|
||||
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script)),
|
||||
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script2)),
|
||||
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script3))};
|
||||
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript> *>(&script)),
|
||||
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript> *>(&script2)),
|
||||
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript> *>(&script3))};
|
||||
auto aggr = ScriptAggregator(vec);
|
||||
while (aggr.HasNext()) {
|
||||
auto next = aggr.GetNext();
|
||||
|
@ -49,10 +51,10 @@ TEST_CASE("Script Aggregator properly iterates multiple scripts.") {
|
|||
CHECK(ran == 3);
|
||||
}
|
||||
|
||||
TEST_CASE("Script Aggregator properly iterates Script Set.") {
|
||||
BattleScript* script = new TestScript("test");
|
||||
BattleScript* script2 = new TestScript("test2");
|
||||
BattleScript* script3 = new TestScript("test3");
|
||||
TEST_CASE ("Script Aggregator properly iterates Script Set.") {
|
||||
BattleScript *script = new TestScript("test");
|
||||
BattleScript *script2 = new TestScript("test2");
|
||||
BattleScript *script3 = new TestScript("test3");
|
||||
auto ran = 0;
|
||||
auto set = ScriptSet();
|
||||
set.Add(script);
|
||||
|
@ -67,17 +69,17 @@ TEST_CASE("Script Aggregator properly iterates Script Set.") {
|
|||
CHECK(ran == 3);
|
||||
}
|
||||
|
||||
TEST_CASE("Script Aggregator properly iterates data of Script Set and Script.") {
|
||||
TEST_CASE ("Script Aggregator properly iterates data of Script Set and Script.") {
|
||||
auto script = std::make_unique<TestScript>("test");
|
||||
BattleScript* script2 = new TestScript("test2");
|
||||
BattleScript* script3 = new TestScript("test3");
|
||||
BattleScript *script2 = new TestScript("test2");
|
||||
BattleScript *script3 = new TestScript("test3");
|
||||
auto ran = 0;
|
||||
auto set = ScriptSet();
|
||||
set.Add(script2);
|
||||
set.Add(script3);
|
||||
auto vec = ArbUt::List<ScriptWrapper>{
|
||||
ScriptWrapper::FromSet(&set),
|
||||
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script))};
|
||||
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript> *>(&script))};
|
||||
auto aggr = ScriptAggregator(vec);
|
||||
while (aggr.HasNext()) {
|
||||
auto next = aggr.GetNextNotNull();
|
||||
|
@ -86,16 +88,17 @@ TEST_CASE("Script Aggregator properly iterates data of Script Set and Script.")
|
|||
CHECK(ran == 3);
|
||||
}
|
||||
|
||||
TEST_CASE("Script Aggregator properly iterates data of Script and Script Set.") {
|
||||
TEST_CASE ("Script Aggregator properly iterates data of Script and Script Set.") {
|
||||
auto script = std::make_unique<TestScript>("test");
|
||||
BattleScript* script2 = new TestScript("test2");
|
||||
BattleScript* script3 = new TestScript("test3");
|
||||
BattleScript *script2 = new TestScript("test2");
|
||||
BattleScript *script3 = new TestScript("test3");
|
||||
auto ran = 0;
|
||||
auto set = ScriptSet();
|
||||
set.Add(script2);
|
||||
set.Add(script3);
|
||||
auto vec =
|
||||
ArbUt::List<ScriptWrapper>{ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script)),
|
||||
ArbUt::List<ScriptWrapper>{
|
||||
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript> *>(&script)),
|
||||
ScriptWrapper::FromSet(&set)};
|
||||
auto aggr = ScriptAggregator(vec);
|
||||
while (aggr.HasNext()) {
|
||||
|
@ -105,19 +108,19 @@ TEST_CASE("Script Aggregator properly iterates data of Script and Script Set.")
|
|||
CHECK(ran == 3);
|
||||
}
|
||||
|
||||
TEST_CASE("Script Aggregator properly iterates data of Script, Script Set and Script.") {
|
||||
TEST_CASE ("Script Aggregator properly iterates data of Script, Script Set and Script.") {
|
||||
auto script = std::make_unique<TestScript>("test");
|
||||
BattleScript* script2 = new TestScript("test2");
|
||||
BattleScript* script3 = new TestScript("test3");
|
||||
BattleScript *script2 = new TestScript("test2");
|
||||
BattleScript *script3 = new TestScript("test3");
|
||||
auto script4 = std::make_unique<TestScript>("test4");
|
||||
auto ran = 0;
|
||||
auto set = ScriptSet();
|
||||
set.Add(script2);
|
||||
set.Add(script3);
|
||||
auto vec = ArbUt::List<ScriptWrapper>{
|
||||
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script)),
|
||||
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript> *>(&script)),
|
||||
ScriptWrapper::FromSet(&set),
|
||||
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript>*>(&script4))};
|
||||
ScriptWrapper::FromScript(reinterpret_cast<std::unique_ptr<BattleScript> *>(&script4))};
|
||||
auto aggr = ScriptAggregator(vec);
|
||||
while (aggr.HasNext()) {
|
||||
auto next = aggr.GetNextNotNull();
|
||||
|
@ -126,7 +129,7 @@ TEST_CASE("Script Aggregator properly iterates data of Script, Script Set and Sc
|
|||
CHECK(ran == 4);
|
||||
}
|
||||
|
||||
TEST_CASE("Script Aggregator properly iterates when empty.") {
|
||||
TEST_CASE ("Script Aggregator properly iterates when empty.") {
|
||||
auto ran = 0;
|
||||
auto vec = ArbUt::List<ScriptWrapper>{};
|
||||
auto aggr = ScriptAggregator(vec);
|
||||
|
@ -136,7 +139,7 @@ TEST_CASE("Script Aggregator properly iterates when empty.") {
|
|||
CHECK(ran == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("Script Aggregator properly iterates empty Script Set.") {
|
||||
TEST_CASE ("Script Aggregator properly iterates empty Script Set.") {
|
||||
auto ran = 0;
|
||||
auto set = ScriptSet();
|
||||
auto vec = ArbUt::List<ScriptWrapper>{ScriptWrapper::FromSet(&set)};
|
||||
|
|
Loading…
Reference in New Issue