"Basic" entity component system. Almost.

This commit is contained in:
2014-02-25 19:26:34 +01:00
parent ec426301a2
commit 369ff782f1
7 changed files with 184 additions and 2 deletions
+50
View File
@@ -0,0 +1,50 @@
#ifndef ComponentFactory_h__
#define ComponentFactory_h__
#include <string>
#include <functional>
#include <map>
#include "Component.h"
#define REGISTER_COMPONENT(NAME, TYPE) \
static ComponentRegistrar<TYPE> registrar(NAME);
template<class T>
class ComponentRegistrar
{
public:
ComponentRegistrar(std::string name)
{
ComponentFactory::Instance()->Register(name, [](void) -> std::shared_ptr<Component> { return new T() });
}
};
class ComponentFactory
{
public:
static ComponentFactory* Instance()
{
static ComponentFactory factory;
return &factory;
}
void Register(std::string name, std::function<std::shared_ptr<Component>(void)> factoryFunction)
{
m_FactoryFunctions[name] = factoryFunction;
}
std::shared_ptr<Component> 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<std::shared_ptr<Component>(void)>> m_FactoryFunctions;
};
#endif // ComponentFactory_h__