Refactored factory and component system.

This commit is contained in:
2014-03-07 01:08:54 +01:00
parent 18197bf21d
commit e65db077d5
30 changed files with 313 additions and 284 deletions
+35
View File
@@ -0,0 +1,35 @@
#ifndef ComponentFactory_h__
#define ComponentFactory_h__
#include <string>
#include <memory>
#include <functional>
#include <map>
template <typename T>
class Factory
{
public:
void Register(std::string name, std::function<T(void)> factoryFunction)
{
m_FactoryFunctions[name] = factoryFunction;
}
T Create(std::string name)
{
auto it = m_FactoryFunctions.find(name);
if (it != m_FactoryFunctions.end()) {
return it->second();
} else {
return nullptr;
}
}
private:
std::map<std::string, std::function<T(void)>> m_FactoryFunctions;
};
struct Component;
class ComponentFactory : public Factory<Component*> { };
#endif // ComponentFactory_h__