C ++ general realization

xiaoxiao2021-03-06  67

// C general factory registration class // ----------------------------------- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#ifndef genericfactoryh # Define GenericFactoryh

#include #include

#define register_class (base_class, derived_class) / registerinpfactory register ## derived_class (# derived_class)

#define create_class (base_class, derived_class) / genericfactory :: instance (). Create (#derived_class)

/ / -------------------------------------------------------------------------------------------- --------------------------- USING std :: string; using std :: map;

Template class genericfactory {typedef manufacturedtype * (* basecreatefn) ();

Typedef st :: map fnregistry;

FnRegistry Registry;

GenericFactory () {}

GenericFactory (const genericfactory &) {} // does not implement

GenericFactory & Operator = (const genericfactory &) {} // does not implement

PUBLIC: static genericfactory & instance () {static genericfactory bf; return bf;}

Void regreatefn (const classidkey & classname, basecreatefn fn) {registry [classname] = fn;}

ManufacturedType * Create (const ClassIDKey & classname) const {ManufacturedType * theObject (0); FnRegistry :: const_iterator regEntry = registry.find (classname); if (! RegEntry = registry.end ()) {theObject = regEntry-> second () } Return theobject;}};

template class RegisterInpFactory {public: static AncestorType * CreateInstance () {return (new ManufacturedType);} RegisterInpFactory (const ClassIDKey & id) {GenericFactory :: Instance (). Regcreatefn (ID, CREATEINSTANCE);}}; # ENDIF

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

New Post(0)