#ifndef ComponentFactory_h__ #define ComponentFactory_h__ #include #include #include #include struct Component; #define REGISTER_COMPONENT(NAME, TYPE) \ static ComponentRegistrar registrar(NAME); template class ComponentRegistrar { public: ComponentRegistrar(std::string name) { ComponentFactory::Instance()->Register(name, [](void) -> std::shared_ptr { return std::shared_ptr(new T()); }); } }; class ComponentFactory { public: static ComponentFactory* Instance() { static ComponentFactory factory; return &factory; } void Register(std::string name, std::function(void)> factoryFunction) { m_FactoryFunctions[name] = factoryFunction; } std::shared_ptr Create(std::string name) { auto it = m_FactoryFunctions.find(name); if (it != m_FactoryFunctions.end()) return it->second(); else return nullptr; } private: std::map(void)>> m_FactoryFunctions; }; #endif // ComponentFactory_h__