Skip to content

Instantly share code, notes, and snippets.

@lierdakil
Created February 13, 2016 19:21
Show Gist options
  • Save lierdakil/14054928b7b3b04aa2d2 to your computer and use it in GitHub Desktop.
Save lierdakil/14054928b7b3b04aa2d2 to your computer and use it in GitHub Desktop.
Source-level singleton plugins for C++
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());
#include "common.h"
std::map<const char*,Plugin*> registry::plugins;
int main(int argc, char *argv[])
{
...
for(auto &i : registry::plugins) {
...
}
...
}
#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