Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.93 KB | None | 0 0
  1. #ifndef CGENERICFACTORY_H
  2. #define CGENERICFACTORY_H
  3.  
  4. #include <string>
  5. #include <vector>
  6. #include "IGenericCreator.h"
  7.  
  8. template<class FType, class Ident = std::string, class Container = std::vector<IGenericCreator<FType, Ident>* > >
  9. class CGenericFactory
  10. {
  11.     public:
  12.         typedef IGenericCreator<FType, Ident> Creator;
  13.  
  14.         CGenericFactory(){}
  15.         virtual ~CGenericFactory()
  16.         {
  17.             for (unsigned int i=0;i<Creators.size();++i)
  18.             {
  19.                 Creators[i]->drop();
  20.             }
  21.         }
  22.  
  23.         template<class FTypeIn>
  24.         void registerTypePointer(const Ident& name)
  25.         {
  26.             for (unsigned int i=0;i<Creators.size();++i)
  27.             {
  28.                 if (Creators[i]->getIdent() == name)
  29.                 {
  30.                     Creators[i]->drop();
  31.                     Creators[i] = new CGenericCreatorPointer<FTypeIn, FType, Ident>(name);
  32.                     Creators[i]->grab();
  33.                     return;
  34.                 }
  35.             }
  36.             Creator* c = new CGenericCreatorPointer<FTypeIn, FType, Ident>(name);
  37.             c->grab();
  38.             Creators.push_back(c);
  39.         }
  40.  
  41.         void registerType(IGenericCreator<FType, Ident>* creator)
  42.         {
  43.             if (!creator)
  44.                 return;
  45.  
  46.             creator->grab();
  47.  
  48.             for (unsigned int i=0;i<Creators.size();++i)
  49.             {
  50.                 if (Creators[i]->getIdent() == creator->getIdent())
  51.                 {
  52.                     Creators[i]->drop();
  53.                     Creators[i] = creator;
  54.                     return;
  55.                 }
  56.             }
  57.             Creators.push_back(creator);
  58.         }
  59.  
  60.         FType create(const unsigned int& id) const
  61.         {
  62.             Creator* c = getCreatorById(id);
  63.             if (c)
  64.                 return c->create();
  65.             return FType();
  66.         }
  67.  
  68.         FType create(const unsigned int& id)
  69.         {
  70.             Creator* c = getCreatorById(id);
  71.             if (c)
  72.                 return c->create();
  73.             return FType();
  74.         }
  75.  
  76.         FType create(const Ident& ident) const
  77.         {
  78.             Creator* c = getCreator(ident);
  79.             if (c)
  80.                 return c->create();
  81.             return FType();
  82.         }
  83.  
  84.         FType create(const Ident& ident)
  85.         {
  86.             Creator* c = getCreator(ident);
  87.             if (c)
  88.                 return c->create();
  89.             return FType();
  90.         }
  91.  
  92.         bool isCreateAble(const Ident& ident) const
  93.         {
  94.             Creator* c = getCreator(ident);
  95.             if (c)
  96.                 return true;
  97.             return false;
  98.         }
  99.  
  100.         bool isCreateAble(const unsigned int& id)
  101.         {
  102.             Creator* c = getCreatorById(id);
  103.             if (c)
  104.                 return true;
  105.             return false;
  106.         }
  107.  
  108.         unsigned int getCreateAbleCount(void) const
  109.         {
  110.             return Creators.size();
  111.         }
  112.  
  113.         Creator* getCreatorById(const unsigned int& id) const
  114.         {
  115.             if (id < Creators.size())
  116.                 return Creators[id];
  117.             return 0;
  118.         }
  119.  
  120.         Creator* getCreatorById(const unsigned int& id)
  121.         {
  122.             if (id < Creators.size())
  123.                 return Creators[id];
  124.             return 0;
  125.         }
  126.  
  127.         Creator* getCreator(const Ident& ident) const
  128.         {
  129.             for (unsigned int i=0;i<Creators.size();++i)
  130.             {
  131.                 if (Creators[i]->checkIdent(ident))
  132.                     return Creators[i];
  133.             }
  134.             return 0;
  135.         }
  136.  
  137.         Creator* getCreator(const Ident& ident)
  138.         {
  139.             for (unsigned int i=0;i<Creators.size();++i)
  140.             {
  141.                 if (Creators[i]->checkIdent(ident))
  142.                     return Creators[i];
  143.             }
  144.             return 0;
  145.         }
  146.  
  147.         Ident getCreatorIdentById(const unsigned int& id) const
  148.         {
  149.             Creator* c = getCreatorById(id);
  150.             if (c)
  151.                 return c->getIdent();
  152.             return Ident();
  153.         }
  154.  
  155.         Ident getCreatorIdentById(const unsigned int& id)
  156.         {
  157.             Creator* c = getCreatorById(id);
  158.             if (c)
  159.                 return c->getIdent();
  160.             return Ident();
  161.         }
  162.  
  163.         int getCreatorIdByIdent(const Ident& ident) const
  164.         {
  165.             for (unsigned int i=0;i<Creators.size();++i)
  166.             {
  167.                 if (Creators[i]->checkIdent(ident))
  168.                     return i;
  169.             }
  170.             return -1;
  171.         }
  172.  
  173.         int getCreatorIdByIdent(const Ident& ident)
  174.         {
  175.             for (unsigned int i=0;i<Creators.size();++i)
  176.             {
  177.                 if (Creators[i]->checkIdent(ident))
  178.                     return i;
  179.             }
  180.             return -1;
  181.         }
  182.     protected:
  183.         Container Creators;
  184.     private:
  185. };
  186.  
  187. #endif // CGENERICFACTORY_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement