Skip to content

Instantly share code, notes, and snippets.

@cluelessperson
Created July 22, 2020 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cluelessperson/7d019144923643e4e78a85a98e5b40b8 to your computer and use it in GitHub Desktop.
Save cluelessperson/7d019144923643e4e78a85a98e5b40b8 to your computer and use it in GitHub Desktop.
#include "registry.h"
template<typename Type>
void Registry::registerType(){
registry[Type::name] = [](){return std::unique_ptr<RegistryType>(new Type);};
};
std::unique_ptr<RegistryType> Registry::create(const std::string &name){
auto it = registry.find(name);
if(it != registry.end()){
return it->second();
} else {
return std::unique_ptr<RegistryType>();
}
};
#ifndef REGISTRY_H
#define REGISTRY_H
#include "unordered_map"
#include "memory"
#include "registry.h"
class RegistryType {
public:
const char* name;
};
class Registry {
public:
std::unordered_map<std::string, std::unique_ptr<RegistryType>(*)()> registry;
template<typename Type>
void registerType();
std::unique_ptr<RegistryType> create(const std::string &name);
Registry();
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment