Use a macro support to dynamically create, and the passage of the parameters is created. Maybe ZFActory.h's #include is not pleasing, but there is no way, it seems that only this can be compiled under VC, BCB, and GCC. . . .
----------------- ZFActory.h --------------------- #ifndef zfactory_h # define zfactory_h #include < String> #include
/ #Define RegisterClassArg3 (id, classname, type1, type2, type3) / static classname * CreateArg3 ## classname (type1 arg1, type2 arg2, type3 arg3) {/ return new classname (arg1, arg2, arg3); /} / static ClassName * Createshadowarg3 ## ClassName (void * arg1, void * arg2, void * arg3) {/ return createrg3 ## classname (* ((Type1 *) arg1), * ((type2 *) arg2), * (((((type3 *) ) arg3)); /} / RegisterInfoArg3 mRegisterInfoArg3 ## classname (#classname, (CreateFunArg3) CreateShadowArg3 ## classname, id); / class ZFactory {public: static ZFactory * GetInstance (); void * ZFactory :: CreateClass (int classId ); Template
RegisterInfo * RegArg1Header; RegisterInfo * RegArg2Header; RegisterInfo * RegArg3Header; friend class RegisterInfo; friend class RegisterInfoArg0; friend class RegisterInfoArg1; friend class RegisterInfoArg2; friend class RegisterInfoArg3;}; # include "Register.h" #endif // ----- -------- ZFActory.cpp -----------------------
#include "zfactory.h"
ZFActory * zfactory :: minstance = 0;
Void * zfactory :: CreateClass (int classid) {registerinfo * tmp = regarg0Header-> Find (ClassID); if (tmp == 0) Return 0; Else Return ((RegisterInfoarg0 *) TMP) -> CREATOR) (); }
ZFactory * ZFactory :: GetInstance () {if (mInstance == 0) {mInstance = new ZFactory; mInstance-> RegArg0Header = new RegisterInfo (); mInstance-> RegArg1Header = new RegisterInfo (); mInstance-> RegArg2Header = new RegisterInfo ( MINSTANCE-> regarg3header = new registerinfo ();} Return MINSTANCE
Void Clearlist (registerinfo * tmp = header; while (tmp) {tmp = tmp-> next; delete tmp;}}
//--------------- Zregister.h---------------------
#ifndef register_h # Define register_h # include "zfactory.h"
Class registerinfo {public: registerinfo (): Name ("header"), Identify (0), Next (0) {}; registerinfo * find (int classid) {registerinfo * tmp = this; while (classid! = tmp-> Identify) TMP = TMP-> Next; RETURN TMP;} RegisterInfo * tail () {registerinfo * tmp = this; while (tmp-> next) TMP = TMP-> Next; returnit;} string name; int Identify; RegisterInfo * Next;
Protected: void init (string n, int i, registerinfo * header) {name = n; Identify = i; Next = 0; header-> tail () -> next = this;}};
class RegisterInfoArg0: public RegisterInfo {public: RegisterInfoArg0 (): RegisterInfo () {}; RegisterInfoArg0 :: RegisterInfoArg0 (string n, CreateFunArg0 c, int i): Creator (c) {Init (n, i, ZFactory :: GetInstance () -> regarg0Header;}
class RegisterInfoArg1: public RegisterInfo {public: RegisterInfoArg1 (): RegisterInfo () {}; RegisterInfoArg1 :: RegisterInfoArg1 (string n, CreateFunArg1 c, int i): Creator (c) {Init (n, i, ZFactory :: GetInstance () -> regarg1header;}
class RegisterInfoArg2: public RegisterInfo {public: RegisterInfoArg2 (): RegisterInfo () {}; RegisterInfoArg2 :: RegisterInfoArg2 (string n, CreateFunArg2 c, int i): Creator (c) {Init (n, i, ZFactory :: GetInstance () CreateFunarg2 Creat.
class RegisterInfoArg3: public RegisterInfo {public: RegisterInfoArg3 (): RegisterInfo () {}; RegisterInfoArg3 :: RegisterInfoArg3 (string n, CreateFunArg3 c, int i): Creator (c) {Init (n, i, ZFactory :: GetInstance () -> regarg3header;};
#ENDIF