ZFACTROY source code

zhaozj2021-02-12  139

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 using std :: cout; using std :: end1; using st: string; typedef void * (* createfunarg0) (); typedef void * (* createfunarg1) (void *); typedef void * (* CreateFunArg2) (void *, void *); typedef void * (* CreateFunArg3) (void *, void *, void *); class RegisterInfo; class RegisterInfoArg0; class RegisterInfoArg1; class RegisterInfoArg2; class RegisterInfoArg3; #define RegisterClass (id , classname) / static classname * Create ## classname () {/ return new classname; /} / RegisterInfoArg0 mRegisterInfo ## classname (#classname, (CreateFunArg0) Create ## classname, id); / #define RegisterClassArg1 (id, classname , type1) / static classname * CreateArg1 ## classname (type1 arg1) {/ return new classname (arg1); /} / static classname * CreateShadowArg1 ## classname (void * arg1) {/ return CreateArg1 ## classname (* (( TYPE1 *) ARG1))); /} / registerinfoarg1 mregisterinfoarg1 ## ClassName (#classname, (CreateFunarg1) Createshadowarg1 ## Classna me, id); / #define RegisterClassArg2 (id, classname, type1, type2) / static classname * CreateArg2 ## classname (type1 arg1, type2 arg2) {/ return new classname (arg1, arg2); /} / static classname * CreateShadowArg2 ## classname (void * arg1, void * arg2) {/ return CreateArg2 ## classname (* ((type1 *) arg1), * ((type2 *) arg2)); /} / RegisterInfoArg2 mRegisterInfoArg2 ## classname (# Classname, (CreateFunarg2) Createshadowarg2 ## ClassName, ID);

/ #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 void * createclass (int classid, type arg1) {registerinfo * tmp = regarg1header-> find (classID); if (tmp == 0) Return 0; Else Return ((registerInfoarg1 *) TMP) -> CREATOR) ((void *) & arg1);}; Template void * createclass (int classid, type1 arg1, type2 arg2) {registerinfo * tmp = regarg2header-> Find (classID); if ( TMP == 0) RETURN 0; Else Return ((RegisterInfoarg2 *) TMP) -> CREATOR) (Void *) & arg1, (void * ) & Arg2);}; template void * CreateClass (int classId, TYPE1 arg1, TYPE2 arg2, TYPE3 arg3) {RegisterInfo * tmp = RegArg3Header-> Find (classId); if (tmp = = 0) RETURN 0; Else Return ((RegisterInfoarg3 *) TMP) -> CREATOR) (Void *) & arg1, (void *) & arg2, (void *) & arg3);}; ~ zFactory () {ClearList (Regarg0Header) ); ClearList (RegArg1Header); ClearList (RegArg2Header); ClearList (RegArg3Header);} private: ZFactory () {RegArg0Header = 0; RegArg1Header = 0; RegArg2Header = 0; RegArg3Header = 0;}; void ClearList (RegisterInfo * header); Static ZFActory * Minstance; RegisterInfo * regarg0Header;

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

转载请注明原文地址:https://www.9cbs.com/read-7553.html

New Post(0)