Last active
August 29, 2015 14:04
-
-
Save matthijskooijman/27a9a2f66c4a59111378 to your computer and use it in GitHub Desktop.
Fancy module loading stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Module { | |
}; | |
class ModuleInfoBase { | |
public: | |
const char *name; | |
virtual Module* load() const; | |
// For the modules linked list | |
ModuleInfoBase *next; | |
}; | |
template<class M> | |
class ModuleInfo : public ModuleInfoBase { | |
public: | |
ModuleInfo() { | |
registerModule(this); | |
} | |
virtual Module* load() const { | |
return new M(); | |
} | |
}; | |
ModuleInfoBase *modules = NULL; | |
void registerModule(ModuleInfoBase* info) { | |
info->next = modules; | |
modules = info; | |
} | |
Module* loadModule(const char* name) { | |
ModuleInfoBase *info = modules; | |
while (info) { | |
if (strcmp(name, info->name) == 0) | |
return info->load(); | |
} | |
} | |
class TacoModule : public Module { | |
}; | |
ModuleInfo<TacoModule> tacoInfo; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment