File indexing completed on 2023-10-25 09:56:04
0001 #ifndef LOGICFACTORY_H
0002 #define LOGICFACTORY_H 1
0003
0004
0005 #include <cstdlib>
0006 #include <string>
0007 #include <map>
0008 #include <memory>
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 template <class Ilogic, typename Identifier, typename LogicCreator = Ilogic* (*)()>
0020 class LogicFactory {
0021 public:
0022 bool Register(const Identifier& id, LogicCreator creator) {
0023 return m_associations.insert(typename std::map<Identifier, LogicCreator>::value_type(id, creator)).second;
0024 }
0025
0026 bool Unregister(const Identifier& id) { return m_associations.erase(id) == 1; }
0027
0028 std::unique_ptr<Ilogic> CreateObject(const Identifier& id) const {
0029 auto itr = m_associations.find(id);
0030
0031 if (itr != m_associations.end()) {
0032 return std::unique_ptr<Ilogic>{(itr->second)()};
0033 } else
0034 return nullptr;
0035 }
0036
0037 protected:
0038 private:
0039 typename std::map<Identifier, LogicCreator> m_associations;
0040 };
0041 #endif