class FactoryManager {
public:
template <typename ProductCreator, typename AbstractProduct>
void Register(const std::string& name) {
if (IsRegistered(name)) {
LOGE("failed to register %s, already registered.", name.c_str());
NBASSERT(0);
}
/*LDRA_NOANALYSIS*/
producers_[name] = []() -> AbstractProduct* {
return new ProductCreator();
};
/*LDRA_ANALYSIS*/
}
template <typename AbstractProduct>
AbstractProduct* CreateObject(const std::string& name) {
if (IsRegistered(name)) {
return reinterpret_cast<AbstractProduct*>(producers_[name]());
} else {
return nullptr;
}
}
bool IsRegistered(const std::string& name) {
return producers_.count(name) > 0;
}
private:
std::unordered_map<std::string, std::function<void* ()>> producers_;
DECLARE_SINGLETON(FactoryManager)
};
|