/TGame/TCommon/Gene/GeneFactoryImp.h

http://awoe.googlecode.com/ · C Header · 65 lines · 42 code · 15 blank · 8 comment · 1 complexity · 211a6bb8b40fc2788ed50dd440be45c9 MD5 · raw file

  1. #ifndef __GENE_FACTORY__IMP__
  2. #define __GENE_FACTORY__IMP__
  3. #include "GeneFactoryIf.h"
  4. class TCOM_API GeneFactoryImp: public IGeneFactory
  5. {
  6. public:
  7. GeneFactoryImp();
  8. virtual ~GeneFactoryImp();
  9. //
  10. // initialize the factory
  11. //
  12. virtual void initialize();
  13. //
  14. // gene create
  15. // require an gene identifier : group id + id
  16. // require an gene create dynamic data
  17. //
  18. virtual IGene* create(IGeneCreateData& createData );
  19. protected:
  20. class IGeneCreator
  21. {
  22. public:
  23. virtual ~IGeneCreator(){}
  24. virtual IGene* create() = 0;
  25. };
  26. template<class GENE>
  27. class GeneCreatorByTemplate : public IGeneCreator
  28. {
  29. virtual IGene* create()
  30. {
  31. return new GENE;
  32. }
  33. };
  34. typedef stdext::hash_map<int, IGeneCreator*> GeneClass2Creator;
  35. template<class GENE>
  36. void registerGene(const int& nCls)
  37. {
  38. GeneClass2Creator::iterator it = m_theCreators.find(nCls);
  39. if (it==m_theCreators.end())
  40. {
  41. m_theCreators.insert(make_pair(nCls, new GeneCreatorByTemplate<GENE>));
  42. }
  43. else
  44. {
  45. }
  46. }
  47. private:
  48. GeneClass2Creator m_theCreators;
  49. };
  50. #endif