QBaseModuleFactory.hh
00001 #ifndef _Q_BASE_MODULE_FACTORY_HH_
00002 #define _Q_BASE_MODULE_FACTORY_HH_
00003
00011 class QSequence;
00012
00013
00014 template<class T> class QModFactory {
00015 public:
00016 QModFactory<T>(const std::string& name) {fName = name; fCurrentSequence = NULL;}
00017 const std::string& GetName() const { return fName; }
00018 virtual ~QModFactory<T>() {};
00019 void SetOccurrence(T* mod) { mod->SetOccurrence(fOccurrence); }
00020 private:
00021 T* Create(QSequence* s) {
00022 if( s != fCurrentSequence ) {
00023 fCurrentSequence = s;
00024 fOccurrence = 0;
00025 } else {
00026 fOccurrence++;
00027 }
00028
00029 return GetObject(s);
00030 }
00031 virtual T* GetObject(QSequence* s) = 0;
00032
00033 std::string fName;
00034 int fOccurrence;
00035
00036 QSequence* fCurrentSequence;
00037 friend class QGeneralFactory;
00038 };
00039
00040 #define DECLARE_MODULE_FACTORY(clazz,modtype) \
00041 class clazz ## Factory : public QModFactory<modtype>{\
00042 public:\
00043 clazz ## Factory() : QModFactory<modtype>(#clazz) {}\
00044 modtype *GetObject(QSequence* s) {\
00045 modtype* obj = new clazz(s);\
00046 SetOccurrence(obj);\
00047 return obj;\
00048 }\
00049 };
00050
00051 #define REGISTER_MOD(clazz,modtype) \
00052 DECLARE_MODULE_FACTORY(clazz,modtype);\
00053 extern "C" void registerInFactory ## clazz() {\
00054 QModFactory<modtype>* pt = new clazz ## Factory;\
00055 QGeneralFactory::GetInstance().Register(pt);\
00056 }
00057
00058 #endif