Rework ScriptIterator to jump to first value on reset.
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
This commit is contained in:
parent
9e7607338f
commit
fddf2cabab
|
@ -14,6 +14,25 @@ namespace CreatureLib::Battling {
|
||||||
size_t _index = 0;
|
size_t _index = 0;
|
||||||
size_t _setIndex = 0;
|
size_t _setIndex = 0;
|
||||||
|
|
||||||
|
inline void IncrementToNextNotNull(bool initialIncrement = true) {
|
||||||
|
if (_scripts[_index].IsSet()) {
|
||||||
|
_setIndex++;
|
||||||
|
if (_setIndex >= _scripts[_index].GetScriptSet()->Count()) {
|
||||||
|
_setIndex = 0;
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (initialIncrement)
|
||||||
|
_index++;
|
||||||
|
while (HasNext()) {
|
||||||
|
if (_scripts[_index].HasValue()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ScriptAggregator(){};
|
ScriptAggregator(){};
|
||||||
explicit ScriptAggregator(const ArbUt::List<ScriptWrapper>& scripts)
|
explicit ScriptAggregator(const ArbUt::List<ScriptWrapper>& scripts)
|
||||||
|
@ -21,11 +40,12 @@ namespace CreatureLib::Battling {
|
||||||
|
|
||||||
inline void Reset() {
|
inline void Reset() {
|
||||||
_index = 0;
|
_index = 0;
|
||||||
_setIndex = 0;
|
_setIndex = -1;
|
||||||
|
IncrementToNextNotNull(false);
|
||||||
}
|
}
|
||||||
inline bool HasNext() { return _index < _size; }
|
inline bool HasNext() { return _index < _size; }
|
||||||
|
|
||||||
Script* GetNextNotNull() {
|
ArbUt::BorrowedPtr<Script> GetNextNotNull() {
|
||||||
while (HasNext()) {
|
while (HasNext()) {
|
||||||
auto s = GetNext();
|
auto s = GetNext();
|
||||||
if (s != nullptr) {
|
if (s != nullptr) {
|
||||||
|
@ -35,28 +55,17 @@ namespace CreatureLib::Battling {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
Script* GetNext() {
|
ArbUt::BorrowedPtr<Script> GetNext() {
|
||||||
auto& current = _scripts[_index];
|
auto& current = _scripts[_index];
|
||||||
if (!current.IsSet()) {
|
if (!current.IsSet()) {
|
||||||
_index++;
|
|
||||||
auto s = current.GetScript();
|
auto s = current.GetScript();
|
||||||
if (s == nullptr)
|
IncrementToNextNotNull();
|
||||||
return nullptr;
|
return (*s);
|
||||||
return (*s).get();
|
|
||||||
} else {
|
} else {
|
||||||
auto& set = current.GetScriptSet()->GetIterator();
|
auto& set = current.GetScriptSet()->GetIterator();
|
||||||
auto count = set.Count();
|
auto v = set[_setIndex];
|
||||||
if (_setIndex >= count) {
|
IncrementToNextNotNull();
|
||||||
_index++;
|
return v;
|
||||||
_setIndex = 0;
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
auto v = set[_setIndex++];
|
|
||||||
if (_setIndex >= count) {
|
|
||||||
_index++;
|
|
||||||
_setIndex = 0;
|
|
||||||
}
|
|
||||||
return v.GetRaw();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -68,7 +68,7 @@ namespace CreatureLib::Battling {
|
||||||
|
|
||||||
inline size_t Count() const { return _scripts.Count(); }
|
inline size_t Count() const { return _scripts.Count(); }
|
||||||
|
|
||||||
const ArbUt::UniquePtrList<Script>& GetIterator() const { return _scripts; }
|
inline const ArbUt::UniquePtrList<Script>& GetIterator() const { return _scripts; }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,10 +21,17 @@ namespace CreatureLib::Battling {
|
||||||
static inline ScriptWrapper FromScript(std::unique_ptr<Script>* s) { return ScriptWrapper(s, false); }
|
static inline ScriptWrapper FromScript(std::unique_ptr<Script>* s) { return ScriptWrapper(s, false); }
|
||||||
static inline ScriptWrapper FromSet(ScriptSet* s) { return ScriptWrapper(s, true); }
|
static inline ScriptWrapper FromSet(ScriptSet* s) { return ScriptWrapper(s, true); }
|
||||||
|
|
||||||
bool IsSet() const { return _isSet; }
|
inline bool IsSet() const noexcept { return _isSet; }
|
||||||
|
|
||||||
inline const std::unique_ptr<Script>* GetScript() const { return _script; }
|
inline const std::unique_ptr<Script>* GetScript() const noexcept { return _script; }
|
||||||
inline const ScriptSet* GetScriptSet() const { return _scriptSet; }
|
inline const ScriptSet* GetScriptSet() const noexcept { return _scriptSet; }
|
||||||
|
|
||||||
|
inline bool HasValue() const noexcept {
|
||||||
|
if (_isSet)
|
||||||
|
return _scriptSet->Count() > 0;
|
||||||
|
else
|
||||||
|
return *_script != nullptr;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,7 @@ TEST_CASE("Script Aggregator properly iterates containing script.", "[Battling,
|
||||||
while (aggr.HasNext()) {
|
while (aggr.HasNext()) {
|
||||||
auto next = aggr.GetNext();
|
auto next = aggr.GetNext();
|
||||||
REQUIRE(next != nullptr);
|
REQUIRE(next != nullptr);
|
||||||
dynamic_cast<TestScript*>(next)->TestMethod(ran);
|
next.As<TestScript>()->TestMethod(ran);
|
||||||
}
|
}
|
||||||
CHECK(ran == 1);
|
CHECK(ran == 1);
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ TEST_CASE("Script Aggregator properly iterates multiple scripts.", "[Battling, S
|
||||||
while (aggr.HasNext()) {
|
while (aggr.HasNext()) {
|
||||||
auto next = aggr.GetNext();
|
auto next = aggr.GetNext();
|
||||||
REQUIRE(next != nullptr);
|
REQUIRE(next != nullptr);
|
||||||
dynamic_cast<TestScript*>(next)->TestMethod(ran);
|
next.As<TestScript>()->TestMethod(ran);
|
||||||
}
|
}
|
||||||
CHECK(ran == 3);
|
CHECK(ran == 3);
|
||||||
}
|
}
|
||||||
|
@ -64,7 +64,7 @@ TEST_CASE("Script Aggregator properly iterates Script Set.", "[Battling, Scripti
|
||||||
while (aggr.HasNext()) {
|
while (aggr.HasNext()) {
|
||||||
auto next = aggr.GetNextNotNull();
|
auto next = aggr.GetNextNotNull();
|
||||||
REQUIRE(next != nullptr);
|
REQUIRE(next != nullptr);
|
||||||
dynamic_cast<TestScript*>(next)->TestMethod(ran);
|
next.As<TestScript>()->TestMethod(ran);
|
||||||
}
|
}
|
||||||
CHECK(ran == 3);
|
CHECK(ran == 3);
|
||||||
}
|
}
|
||||||
|
@ -83,7 +83,7 @@ TEST_CASE("Script Aggregator properly iterates data of Script Set and Script.",
|
||||||
while (aggr.HasNext()) {
|
while (aggr.HasNext()) {
|
||||||
auto next = aggr.GetNextNotNull();
|
auto next = aggr.GetNextNotNull();
|
||||||
REQUIRE(next != nullptr);
|
REQUIRE(next != nullptr);
|
||||||
dynamic_cast<TestScript*>(next)->TestMethod(ran);
|
next.As<TestScript>()->TestMethod(ran);
|
||||||
}
|
}
|
||||||
CHECK(ran == 3);
|
CHECK(ran == 3);
|
||||||
}
|
}
|
||||||
|
@ -102,7 +102,7 @@ TEST_CASE("Script Aggregator properly iterates data of Script and Script Set.",
|
||||||
while (aggr.HasNext()) {
|
while (aggr.HasNext()) {
|
||||||
auto next = aggr.GetNextNotNull();
|
auto next = aggr.GetNextNotNull();
|
||||||
REQUIRE(next != nullptr);
|
REQUIRE(next != nullptr);
|
||||||
dynamic_cast<TestScript*>(next)->TestMethod(ran);
|
next.As<TestScript>()->TestMethod(ran);
|
||||||
}
|
}
|
||||||
CHECK(ran == 3);
|
CHECK(ran == 3);
|
||||||
}
|
}
|
||||||
|
@ -123,7 +123,7 @@ TEST_CASE("Script Aggregator properly iterates data of Script, Script Set and Sc
|
||||||
while (aggr.HasNext()) {
|
while (aggr.HasNext()) {
|
||||||
auto next = aggr.GetNextNotNull();
|
auto next = aggr.GetNextNotNull();
|
||||||
REQUIRE(next != nullptr);
|
REQUIRE(next != nullptr);
|
||||||
dynamic_cast<TestScript*>(next)->TestMethod(ran);
|
next.As<TestScript>()->TestMethod(ran);
|
||||||
}
|
}
|
||||||
CHECK(ran == 4);
|
CHECK(ran == 4);
|
||||||
}
|
}
|
||||||
|
@ -143,12 +143,11 @@ TEST_CASE("Script Aggregator properly iterates empty Script Set.", "[Battling, S
|
||||||
auto set = ScriptSet();
|
auto set = ScriptSet();
|
||||||
auto vec = ArbUt::List<ScriptWrapper>{ScriptWrapper::FromSet(&set)};
|
auto vec = ArbUt::List<ScriptWrapper>{ScriptWrapper::FromSet(&set)};
|
||||||
auto aggr = ScriptAggregator(vec);
|
auto aggr = ScriptAggregator(vec);
|
||||||
|
aggr.Reset();
|
||||||
while (aggr.HasNext()) {
|
while (aggr.HasNext()) {
|
||||||
auto next = aggr.GetNext();
|
|
||||||
REQUIRE(next == nullptr);
|
|
||||||
ran++;
|
ran++;
|
||||||
}
|
}
|
||||||
CHECK(ran == 1);
|
CHECK(ran == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -59,19 +59,18 @@ TEST_CASE("Script source with script ptr being set.", "[Battling, Scripting]") {
|
||||||
TEST_CASE("Script source with script ptr being set after first iteration.", "[Battling, Scripting]") {
|
TEST_CASE("Script source with script ptr being set after first iteration.", "[Battling, Scripting]") {
|
||||||
auto source = ScriptSourceWithScriptPtr();
|
auto source = ScriptSourceWithScriptPtr();
|
||||||
auto scripts = source.GetScriptIterator();
|
auto scripts = source.GetScriptIterator();
|
||||||
auto first = scripts.GetNext();
|
REQUIRE_FALSE(scripts.HasNext());
|
||||||
CHECK(first == nullptr);
|
|
||||||
source.ScriptPtr = std::make_unique<TestScript>("foobar");
|
source.ScriptPtr = std::make_unique<TestScript>("foobar");
|
||||||
scripts = source.GetScriptIterator();
|
scripts = source.GetScriptIterator();
|
||||||
first = scripts.GetNext();
|
auto first = scripts.GetNext();
|
||||||
CHECK(first != nullptr);
|
CHECK(first != nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Script source with empty script set.", "[Battling, Scripting]") {
|
TEST_CASE("Script source with empty script set.", "[Battling, Scripting]") {
|
||||||
auto source = ScriptSourceWithScriptSet();
|
auto source = ScriptSourceWithScriptSet();
|
||||||
auto scripts = source.GetScriptIterator();
|
auto scripts = source.GetScriptIterator();
|
||||||
auto first = scripts.GetNext();
|
scripts.Reset();
|
||||||
CHECK(first == nullptr);
|
REQUIRE_FALSE(scripts.HasNext());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Script source with single item script set.", "[Battling, Scripting]") {
|
TEST_CASE("Script source with single item script set.", "[Battling, Scripting]") {
|
||||||
|
|
Loading…
Reference in New Issue