From 014b9a46e5d9874e0a2f5112370add64f4d82a80 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Mon, 21 Jul 2014 14:52:01 -0300 Subject: [PATCH] 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. --- src/common/Module.cpp | 40 +++-- src/common/Module.h | 50 +++++- src/modules/audio/Audio.cpp | 5 + src/modules/audio/Audio.h | 4 +- src/modules/audio/wrap_Audio.cpp | 56 +++--- src/modules/event/Event.cpp | 1 + src/modules/event/sdl/Event.cpp | 10 +- src/modules/event/sdl/wrap_Event.cpp | 17 +- src/modules/filesystem/physfs/Filesystem.cpp | 1 + src/modules/filesystem/wrap_Filesystem.cpp | 87 +++++----- src/modules/font/Font.h | 3 + src/modules/font/freetype/wrap_Font.cpp | 13 +- src/modules/graphics/Graphics.cpp | 5 + src/modules/graphics/Graphics.h | 1 + src/modules/graphics/opengl/wrap_Graphics.cpp | 159 +++++++++--------- src/modules/image/Image.h | 4 +- src/modules/image/wrap_Image.cpp | 11 +- src/modules/joystick/JoystickModule.h | 1 + src/modules/joystick/wrap_JoystickModule.cpp | 19 ++- src/modules/keyboard/Keyboard.cpp | 5 + src/modules/keyboard/Keyboard.h | 1 + src/modules/keyboard/wrap_Keyboard.cpp | 13 +- src/modules/math/MathModule.cpp | 2 + src/modules/mouse/Mouse.cpp | 5 + src/modules/mouse/Mouse.h | 1 + src/modules/mouse/wrap_Mouse.cpp | 35 ++-- src/modules/physics/box2d/Physics.cpp | 5 + src/modules/physics/box2d/Physics.h | 2 + src/modules/physics/box2d/wrap_Physics.cpp | 50 +++--- src/modules/sound/Sound.cpp | 5 + src/modules/sound/Sound.h | 4 +- src/modules/sound/wrap_Sound.cpp | 11 +- src/modules/system/System.cpp | 5 + src/modules/system/System.h | 1 + src/modules/system/wrap_System.cpp | 15 +- src/modules/thread/LuaThread.cpp | 2 +- src/modules/thread/ThreadModule.cpp | 5 + src/modules/thread/ThreadModule.h | 1 + src/modules/thread/wrap_ThreadModule.cpp | 11 +- src/modules/timer/Timer.h | 4 +- src/modules/timer/wrap_Timer.cpp | 17 +- src/modules/window/Window.cpp | 5 + src/modules/window/Window.h | 1 + src/modules/window/sdl/Window.cpp | 4 +- src/modules/window/wrap_Window.cpp | 51 +++--- 45 files changed, 432 insertions(+), 316 deletions(-) diff --git a/src/common/Module.cpp b/src/common/Module.cpp index 8b025e72a..4621ad0e3 100644 --- a/src/common/Module.cpp +++ b/src/common/Module.cpp @@ -58,6 +58,13 @@ namespace namespace love { +Module *Module::instances[] = {}; + +Module::Module() + : moduleType(M_INVALID) +{ +} + Module::~Module() { ModuleRegistry ®istry = 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 ®istry = registryInstance(); - - for (auto it = registry.begin(); it != registry.end(); ++it) - { - if (it->first.find(name) == 0) - return it->second; - } - - return nullptr; -} - } // love diff --git a/src/common/Module.h b/src/common/Module.h index feea22af8..a0468c280 100644 --- a/src/common/Module.h +++ b/src/common/Module.h @@ -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 + static T *getInstance(ModuleType type) + { + return (T *) instances[type]; + } + +protected: + + ModuleType moduleType; + +private: + + static Module *instances[M_MAX_ENUM]; }; // Module diff --git a/src/modules/audio/Audio.cpp b/src/modules/audio/Audio.cpp index e9240add5..fc5efe3f6 100644 --- a/src/modules/audio/Audio.cpp +++ b/src/modules/audio/Audio.cpp @@ -25,6 +25,11 @@ namespace love namespace audio { +Audio::Audio() +{ + moduleType = M_AUDIO; +} + StringMap::Entry Audio::distanceModelEntries[] = { {"none", Audio::DISTANCE_NONE}, diff --git a/src/modules/audio/Audio.h b/src/modules/audio/Audio.h index 7ed9b4362..24fed7d49 100644 --- a/src/modules/audio/Audio.h +++ b/src/modules/audio/Audio.h @@ -64,9 +64,7 @@ public: static bool getConstant(const char *in, DistanceModel &out); static bool getConstant(DistanceModel in, const char *&out); - /** - * Destructor. - **/ + Audio(); virtual ~Audio() {} virtual Source *newSource(love::sound::Decoder *decoder) = 0; diff --git a/src/modules/audio/wrap_Audio.cpp b/src/modules/audio/wrap_Audio.cpp index 3b3c48568..2e037260e 100644 --- a/src/modules/audio/wrap_Audio.cpp +++ b/src/modules/audio/wrap_Audio.cpp @@ -31,11 +31,11 @@ namespace love namespace audio { -static Audio *instance = 0; +#define instance() (Module::getInstance