33 lines
787 B
C++
33 lines
787 B
C++
#include "ObjectFactory.h"
|
|
|
|
namespace uLib {
|
|
|
|
ObjectFactory& ObjectFactory::Instance() {
|
|
static ObjectFactory instance;
|
|
return instance;
|
|
}
|
|
|
|
void ObjectFactory::Register(const std::string& className, FactoryFunction func) {
|
|
if (m_factoryMap.find(className) == m_factoryMap.end()) {
|
|
m_factoryMap[className] = func;
|
|
}
|
|
}
|
|
|
|
Object* ObjectFactory::Create(const std::string& className) {
|
|
auto it = m_factoryMap.find(className);
|
|
if (it != m_factoryMap.end()) {
|
|
return (it->second)();
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
std::vector<std::string> ObjectFactory::GetRegisteredClasses() const {
|
|
std::vector<std::string> classes;
|
|
for (auto const& [name, func] : m_factoryMap) {
|
|
classes.push_back(name);
|
|
}
|
|
return classes;
|
|
}
|
|
|
|
} // namespace uLib
|