Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:30:33

0001 #ifndef LOGICFACTORY_H
0002 #define LOGICFACTORY_H 1
0003 
0004 // Include files
0005 #include <cstdlib>
0006 #include <string>
0007 #include <map>
0008 #include <memory>
0009 /** @class LogicFactory LogicFactory.h
0010  *  
0011  *
0012  *  @author Andres Osorio
0013  *
0014  *  email: aosorio@uniandes.edu.co
0015  *
0016  *  @date   2008-10-11
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;  // handle error
0035   }
0036 
0037 protected:
0038 private:
0039   typename std::map<Identifier, LogicCreator> m_associations;
0040 };
0041 #endif  // LOGICFACTORY_H