Updated the Lua wrapper code for modules to account for cases where the module's instance is removed from memory completely and recreated during the program's lifetime.

This commit is contained in:
Alex Szpakowski
2014-07-21 14:52:01 -03:00
parent e87fbdcaaf
commit 014b9a46e5
45 changed files with 432 additions and 316 deletions
+27 -13
View File
@@ -58,6 +58,13 @@ namespace
namespace love
{
Module *Module::instances[] = {};
Module::Module()
: moduleType(M_INVALID)
{
}
Module::~Module()
{
ModuleRegistry &registry = registryInstance();
@@ -73,6 +80,14 @@ Module::~Module()
}
freeEmptyRegistry();
if (instances[moduleType] == this)
instances[moduleType] = nullptr;
}
Module::ModuleType Module::getModuleType() const
{
return moduleType;
}
void Module::registerInstance(Module *instance)
@@ -93,7 +108,19 @@ void Module::registerInstance(Module *instance)
throw Exception("Module %s already registered!", instance->getName());
}
ModuleType moduletype = instance->getModuleType();
if (moduletype == M_INVALID)
throw love::Exception("Module %s has an invalid base module type.", instance->getName());
registry.insert(make_pair(name, instance));
if (instances[moduletype] != nullptr)
{
printf("Warning: overwriting module instance %s with new instance %s\n",
instances[moduletype]->getName(), instance->getName());
}
instances[moduletype] = instance;
}
Module *Module::getInstance(const std::string &name)
@@ -108,17 +135,4 @@ Module *Module::getInstance(const std::string &name)
return it->second;
}
Module *Module::findInstance(const std::string &name)
{
ModuleRegistry &registry = registryInstance();
for (auto it = registry.begin(); it != registry.end(); ++it)
{
if (it->first.find(name) == 0)
return it->second;
}
return nullptr;
}
} // love