Make scriptset return scripts in order of insertion, unit tests for ScriptSource
This commit is contained in:
parent
cdd1ac992f
commit
fd16152121
|
@ -13,26 +13,26 @@ namespace CreatureLib::Battling{
|
||||||
std::vector<ScriptWrapper> _scripts;
|
std::vector<ScriptWrapper> _scripts;
|
||||||
int _index = 0;
|
int _index = 0;
|
||||||
bool _isSetSet = false;
|
bool _isSetSet = false;
|
||||||
std::__detail::_Node_const_iterator<std::pair<const std::string, Script *>, false, true> _setIterator;
|
const std::vector<Script*>* _setScripts;
|
||||||
std::__detail::_Node_const_iterator<std::pair<const std::string, Script *>, false, true> _setEnd;
|
int _setIndex;
|
||||||
public:
|
public:
|
||||||
ScriptAggregator(std::vector<ScriptWrapper> scripts) : _scripts(std::move(scripts)){
|
ScriptAggregator(std::vector<ScriptWrapper> scripts) : _scripts(std::move(scripts)){
|
||||||
};
|
};
|
||||||
|
|
||||||
bool HasNext(){
|
bool HasNext(){
|
||||||
return _index < _scripts.size() || (_isSetSet && _setIterator != _setEnd);
|
return _index < _scripts.size() || (_isSetSet && _setIndex < _setScripts->size());
|
||||||
}
|
}
|
||||||
|
|
||||||
Script* GetNext(){
|
Script* GetNext(){
|
||||||
// We can probably do this in a cleaner version once C++ 20 drops with Coroutine support.
|
// We can probably do this in a cleaner version once C++ 20 drops with Coroutine support.
|
||||||
if (_isSetSet){
|
if (_isSetSet){
|
||||||
if (_setIterator == _setEnd){
|
if (_setIndex >= _setScripts->size()){
|
||||||
_isSetSet = false;
|
_isSetSet = false;
|
||||||
return GetNext();
|
return GetNext();
|
||||||
}
|
}
|
||||||
auto s = _setIterator->second;
|
auto s = _setScripts->at(_setIndex);
|
||||||
_setIterator.operator++();
|
_setIndex++;
|
||||||
if (_setIterator == _setEnd){
|
if (_setIndex >= _setScripts->size()){
|
||||||
_isSetSet = false;
|
_isSetSet = false;
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
|
@ -51,10 +51,9 @@ namespace CreatureLib::Battling{
|
||||||
auto set = next.GetScriptSet();
|
auto set = next.GetScriptSet();
|
||||||
if (set->Count() == 0)
|
if (set->Count() == 0)
|
||||||
return GetNext();
|
return GetNext();
|
||||||
auto it = set->GetIterator();
|
_setScripts = set->GetIterator();
|
||||||
_setIterator = it->begin();
|
|
||||||
_setEnd = it->end();
|
|
||||||
_isSetSet = true;
|
_isSetSet = true;
|
||||||
|
_setIndex = 0;
|
||||||
return GetNext();
|
return GetNext();
|
||||||
}
|
}
|
||||||
throw NotReachableException();
|
throw NotReachableException();
|
||||||
|
|
|
@ -7,27 +7,32 @@
|
||||||
|
|
||||||
namespace CreatureLib::Battling{
|
namespace CreatureLib::Battling{
|
||||||
class ScriptSet{
|
class ScriptSet{
|
||||||
std::unordered_map<std::string, Script*> _scripts;
|
std::vector<Script*> _scripts;
|
||||||
|
std::unordered_map<std::string, size_t> _lookup;
|
||||||
public:
|
public:
|
||||||
void Add(Script* script){
|
void Add(Script* script){
|
||||||
auto f = _scripts.find(script->GetName());
|
auto f = _lookup.find(script->GetName());
|
||||||
if (f != _scripts.end()){
|
if (f != _lookup.end()){
|
||||||
f->second->Stack();
|
_scripts[f.operator*().second]->Stack();
|
||||||
}
|
return;
|
||||||
else{
|
|
||||||
_scripts.insert({script->GetName(), script});
|
|
||||||
}
|
}
|
||||||
|
_scripts.push_back(script);
|
||||||
|
_lookup.insert({script->GetName(), _scripts.size() - 1});
|
||||||
}
|
}
|
||||||
|
|
||||||
void Remove(const std::string& key){
|
void Remove(const std::string& key){
|
||||||
_scripts.erase(key);
|
auto find = _lookup.find(key);
|
||||||
|
if (find != _lookup.end()){
|
||||||
|
_scripts.erase(_scripts.begin() + find.operator*().second);
|
||||||
|
_lookup.erase(key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t Count() const{
|
size_t Count() const{
|
||||||
return _scripts.size();
|
return _scripts.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::unordered_map<std::string, Script *> * GetIterator() const{
|
const std::vector<Script *> * GetIterator() const{
|
||||||
return &_scripts;
|
return &_scripts;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -0,0 +1,104 @@
|
||||||
|
#ifdef TESTS_BUILD
|
||||||
|
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
#include "../../../extern/catch.hpp"
|
||||||
|
#include "../../../src/Battling/ScriptHandling/ScriptSource.hpp"
|
||||||
|
#include "../../../src/Battling/ScriptHandling/ScriptAggregator.hpp"
|
||||||
|
|
||||||
|
using namespace CreatureLib;
|
||||||
|
using namespace CreatureLib::Battling;
|
||||||
|
|
||||||
|
class TestScript : public Script{
|
||||||
|
public:
|
||||||
|
explicit TestScript(std::string name) : Script(std::move(name)){};
|
||||||
|
|
||||||
|
void TestMethod(int& runCount) {
|
||||||
|
runCount++;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class ScriptSourceWithScriptPtr : public ScriptSource{
|
||||||
|
public:
|
||||||
|
Script* ScriptPtr = nullptr;
|
||||||
|
protected:
|
||||||
|
void GetActiveScripts(std::vector<ScriptWrapper> &scripts) override {
|
||||||
|
scripts.emplace_back(&ScriptPtr);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class ScriptSourceWithScriptSet : public ScriptSource{
|
||||||
|
public:
|
||||||
|
ScriptSet Set;
|
||||||
|
protected:
|
||||||
|
void GetActiveScripts(std::vector<ScriptWrapper> &scripts) override {
|
||||||
|
scripts.emplace_back(&Set);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
TEST_CASE( "Script source with unset script ptr.", "[Battling, Scripting]" ) {
|
||||||
|
auto source = ScriptSourceWithScriptPtr();
|
||||||
|
auto scripts = source.GetScriptIterator();
|
||||||
|
auto first = scripts.GetNext();
|
||||||
|
CHECK(first == nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE( "Script source with script ptr being set.", "[Battling, Scripting]" ) {
|
||||||
|
auto source = ScriptSourceWithScriptPtr();
|
||||||
|
source.ScriptPtr = new Script("foobar");
|
||||||
|
auto scripts = source.GetScriptIterator();
|
||||||
|
auto first = scripts.GetNext();
|
||||||
|
CHECK(first != nullptr);
|
||||||
|
delete source.ScriptPtr;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE( "Script source with script ptr being set after first iteration.", "[Battling, Scripting]" ) {
|
||||||
|
auto source = ScriptSourceWithScriptPtr();
|
||||||
|
auto scripts = source.GetScriptIterator();
|
||||||
|
auto first = scripts.GetNext();
|
||||||
|
CHECK(first == nullptr);
|
||||||
|
source.ScriptPtr = new Script("foobar");
|
||||||
|
scripts = source.GetScriptIterator();
|
||||||
|
first = scripts.GetNext();
|
||||||
|
CHECK(first != nullptr);
|
||||||
|
delete source.ScriptPtr;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE( "Script source with empty script set.", "[Battling, Scripting]" ) {
|
||||||
|
auto source = ScriptSourceWithScriptSet();
|
||||||
|
auto scripts = source.GetScriptIterator();
|
||||||
|
auto first = scripts.GetNext();
|
||||||
|
CHECK(first == nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE( "Script source with single item script set.", "[Battling, Scripting]" ) {
|
||||||
|
auto source = ScriptSourceWithScriptSet();
|
||||||
|
auto s = new Script("foobar");
|
||||||
|
source.Set.Add(s);
|
||||||
|
auto scripts = source.GetScriptIterator();
|
||||||
|
auto first = scripts.GetNext();
|
||||||
|
CHECK(first != nullptr);
|
||||||
|
CHECK (first->GetName() == "foobar");
|
||||||
|
delete s;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE( "Script source with multiple item script set.", "[Battling, Scripting]" ) {
|
||||||
|
auto source = ScriptSourceWithScriptSet();
|
||||||
|
auto s = new Script("foobar");
|
||||||
|
auto s2 = new Script("foobar2");
|
||||||
|
source.Set.Add(s);
|
||||||
|
source.Set.Add(s2);
|
||||||
|
auto scripts = source.GetScriptIterator();
|
||||||
|
auto first = scripts.GetNext();
|
||||||
|
CHECK(first != nullptr);
|
||||||
|
CHECK (first->GetName() == "foobar");
|
||||||
|
auto second = scripts.GetNext();
|
||||||
|
CHECK(second != nullptr);
|
||||||
|
CHECK (second->GetName() == "foobar2");
|
||||||
|
delete s;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue