Guest User

Untitled

a guest
May 26th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #ifndef ModelFactory_H_INCLUDED
  2. #define ModelFactory_H_INCLUDED
  3.  
  4. #include <map>
  5.  
  6. template
  7.     <
  8.         typename AbstractModel,
  9.         typename IdentifierType,
  10.         typename ModelCreator
  11.     >
  12. class ModelFactory
  13. {
  14. private:
  15.     typedef std::map<IdentifierType,ModelCreator> AssociationMap;
  16.     AssociationMap assocMap;
  17.  
  18. public:
  19.     bool Register(const IdentifierType& id, ModelCreator creator)
  20.     {
  21.         return this->assocMap.insert(AssociationMap::value_type(id,creator)).second;
  22.     }
  23.  
  24.     bool Unregister(const IdentifierType& id)
  25.     {
  26.         return assocMap.erase(id) == 1;
  27.     }
  28.  
  29.     AbstractModel* create(const IdentifierType& id)
  30.     {
  31.         typename AssociationMap::const_iterator iter = assocMap.find(id);
  32.         if(iter != assocMap.end())
  33.         {
  34.             return (iter->second)();
  35.         }
  36.     }
  37. };
  38.  
  39. #endif
Add Comment
Please, Sign In to add comment