diff --git a/src/common/Module.cpp b/src/common/Module.cpp index 8b025e72a..049da4052 100644 --- a/src/common/Module.cpp +++ b/src/common/Module.cpp @@ -58,6 +58,8 @@ namespace namespace love { +Module *Module::instances[] = {}; + Module::~Module() { ModuleRegistry ®istry = registryInstance(); @@ -72,6 +74,13 @@ Module::~Module() } } + // Same deal with Module::getModuleType(). + for (int i = 0; i < (int) M_MAX_ENUM; i++) + { + if (instances[i] == this) + instances[i] = nullptr; + } + freeEmptyRegistry(); } @@ -94,6 +103,16 @@ void Module::registerInstance(Module *instance) } registry.insert(make_pair(name, instance)); + + ModuleType moduletype = instance->getModuleType(); + + 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 +127,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..9ab6eb67c 100644 --- a/src/common/Module.h +++ b/src/common/Module.h @@ -34,11 +34,34 @@ class Module : public Object { public: - /** - * Destructor. - **/ + enum ModuleType + { + 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 + }; + virtual ~Module(); + /** + * Gets the base type of the module. + **/ + virtual ModuleType getModuleType() const = 0; + /** * Gets the name of the module. This is used in case of errors * and other messages. @@ -63,13 +86,19 @@ 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]; + } + +private: + + static Module *instances[M_MAX_ENUM]; }; // Module diff --git a/src/modules/audio/Audio.h b/src/modules/audio/Audio.h index 7ed9b4362..e02cb9700 100644 --- a/src/modules/audio/Audio.h +++ b/src/modules/audio/Audio.h @@ -64,11 +64,11 @@ public: static bool getConstant(const char *in, DistanceModel &out); static bool getConstant(DistanceModel in, const char *&out); - /** - * Destructor. - **/ virtual ~Audio() {} + // Implements Module. + virtual ModuleType getModuleType() const { return M_AUDIO; } + virtual Source *newSource(love::sound::Decoder *decoder) = 0; virtual Source *newSource(love::sound::SoundData *soundData) = 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