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
+41 -9
View File
@@ -34,11 +34,33 @@ class Module : public Object
{
public:
/**
* Destructor.
**/
enum ModuleType
{
M_INVALID = 0,
M_AUDIO,
M_EVENT,
M_FILESYSTEM,
M_FONT,
M_GRAPHICS,
M_IMAGE,
M_JOYSTICK,
M_KEYBOARD,
M_MATH,
M_MOUSE,
M_PHYSICS,
M_SOUND,
M_SYSTEM,
M_THREAD,
M_TIMER,
M_WINDOW,
M_MAX_ENUM
};
Module();
virtual ~Module();
ModuleType getModuleType() const;
/**
* Gets the name of the module. This is used in case of errors
* and other messages.
@@ -63,13 +85,23 @@ public:
static Module *getInstance(const std::string &name);
/**
* Find the first module instance from the internal registry whose name
* starts with the supplied name. May return NULL if module is not
* registered or the supplied name is not part of any module name.
* @param name The partial name of the module.
* @return Module instance or NULL if the module is not registered.
* Retrieve module instance from the internal registry using the base
* module type. May return null if the module is not registered.
* @param type The base type of the module.
**/
static Module *findInstance(const std::string &name);
template <typename T>
static T *getInstance(ModuleType type)
{
return (T *) instances[type];
}
protected:
ModuleType moduleType;
private:
static Module *instances[M_MAX_ENUM];
}; // Module