Created
July 22, 2020 06:51
-
-
Save cluelessperson/7d019144923643e4e78a85a98e5b40b8 to your computer and use it in GitHub Desktop.
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 "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>(); | |
} | |
}; |
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
#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