Created
February 13, 2016 19:21
-
-
Save lierdakil/14054928b7b3b04aa2d2 to your computer and use it in GitHub Desktop.
Source-level singleton plugins for C++
This file contains 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
struct registry { | |
static std::map<const char*,Plugin*> plugins; | |
registry(const char* name, Plugin* plugin) { | |
plugins[name]=plugin; | |
} | |
}; | |
#define REGISTER(name) \ | |
registry name ## _register(#name, new name()); | |
This file contains 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
#include "common.h" | |
std::map<const char*,Plugin*> registry::plugins; | |
int main(int argc, char *argv[]) | |
{ | |
... | |
for(auto &i : registry::plugins) { | |
... | |
} | |
... | |
} |
This file contains 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
#include "common.h" | |
class SomePlugin : public Plugin { | |
... | |
}; | |
REGISTER(SomePlugin) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment