When compiling the remote sensing Algorithm Toolbox, all the tools are configured in the XML file. in the toolbox, build a tree based on the xml configuration file, and then double-click different Tree nodes, the dialog box for different algorithms is displayed. The simplest way is to use conditional judgment statements such as if else or switch case. However, this is too inconvenient. Every time an algorithm is added, a conditional negotiation must be added to the branch, in popular words, it is too bad. So I want to solve this problem in a common way.
Because the algorithm dialog box is based on the cdialog of MFC, you can create the corresponding objects by using the Class Name of the algorithm dialog box, and then display the dialog box. Previously, we know that there is a reflection mode in Java and C #, that is, we can create a class object through the class name. But C ++ does not have this stuff, so I searched for the materials and finally implemented similar functions using the C ++ template. Below is the code:
/**/File registclassname. H * Image Data Processing dialog box reflection mode */# ifndef registclassname_h # define registclassname_h # include# Include# Include
Using namespace STD;/*** @ brief redefinition object */typedef cdialog * (* PF) ();/***/class classmap registclassname. H * @ brief class ing */class classmap {PRIVATE:/*** @ brief private constructor * to prevent this class from generating instance objects */classmap () {}/*** @ brief a hash table storing the build method with the class name and key */static mapstring, PF> m_classmap; public: /*** @ brief defines that a class must be registered * @ Param _ classname class name * @ Param _ createfun register function pointer */static void registclass (string _ classname, PF _ cr Eatefun) {If (m_classmap.find (_ classname )! = M_classmap.end () return; // if it has been registered, m_classmap [_ classname] = _ createfun;} is returned ;} /*** @ brief private constructor * @ Param _ classname class name */static cdialog * forname (string _ classname) {If (m_classmap.find (_ classname) = m_classmap.end () return NULL; // Class Object not found, return NULL else return (m_classmap [_ classname]) () ;}}; /*** @ brief a hash table storing the build method with the class name and key */_ declspec (selectany) mapstring, PF> classmap: m_classmap; /***/class delegatingobject registclassname. H * @ brief delegate template class */templatetypename T> class delegatingobject {public:/*** @ brief constructor * @ Param _ classname class name */delegatingobject (string _ classname) {classmap: registclass (_ classname, & (delegatingobject: Create);}/*** @ brief instance creation function */static cdialog * Create () {return static_castcdialog *> (New T) ;}};/*** @ brief registration class macro definition */# ifndef regist_class # define regist_class (X) delegatingobjectx> _ class _ # X (# X); # endif // registclassname_h
In this way, you only need to add the following sentence before the CPP file in the respective algorithm dialog box:
Regist_class (crastertransformdlg); // regist_class (cvectortransformdlg) for Grid file format conversion; // convert the Vector file format
Then follow the method below to call the algorithm, so that you do not need to write a large number of if or switch statements:
Bool showalgodialog (string strdlgname) {afx_manage_state (afxgetstaticmodulestate (); cdialog * pdlg = (classmap: forname (strdlgname); If (pdlg = NULL) {afxmessagebox ("this class is not registered. Please check it! ", Mb_ OK); Return false;} pdlg-> domodal (); Return true ;}