Changeset 257

Show
Ignore:
Timestamp:
01/28/09 10:01:19 (15 years ago)
Author:
smidl
Message:

All objects have a virtual predecessor. This allows type checking in UI, see

Files:
7 modified

Legend:

Unmodified
Added
Removed
  • bdm/stat/libBM.h

    r256 r257  
    2626 
    2727//! Root class of BDM objects 
    28 class bdmroot{}; 
     28class bdmroot{ 
     29        virtual void print(){} 
     30}; 
    2931 
    3032//! Structure of RV (used internally), i.e. expanded RVs 
  • bdm/stat/loggers.h

    r254 r257  
    2626This class abstracts logging of results from implementation. This class replaces direct logging of results (e.g. to files or to global variables) by calling methods of a logger. Specializations of this abstract class for specific storage method are designed. 
    2727*/ 
    28 class logger { 
     28class logger : public bdmroot{ 
    2929protected: 
    3030        //! RVs of all logged variables.  
  • bdm/uibuilder.cpp

    r256 r257  
    55UImap __uimap__; 
    66 
    7 void UIexternal::build(Setting &S, bdmroot* &result) const{ 
     7bdmroot* UIexternal::build(Setting &S) const{ 
    88        Config C; 
     9        bdmroot* tmp; 
    910        try{ 
    1011                C.readFile((const char*)S["filename"]); 
     
    1415        try {    
    1516                Setting& remS=C.lookup((const char*)S["path"]); 
    16                 UIbuild(remS,result); 
     17                UIbuild(remS,tmp); 
    1718        } 
    1819        catch (...) { it_error("External field " + string(S.getPath()) + " not valid"); 
    1920        } 
    20          
     21        return tmp; 
    2122}; 
    2223UIREGISTER(UIexternal); 
    2324 
    24 void UIinternal::build(Setting &S, bdmroot* &result) const{ 
     25bdmroot* UIinternal::build(Setting &S) const{ 
     26        bdmroot* tmp; 
    2527        try {    
    2628                Setting* Stmp = &S; 
    2729                do {Stmp=&(Stmp->getParent());} while (!Stmp->isRoot()); 
    2830                Setting& intS=Stmp->lookup((const char*)S["path"]); 
    29                 UIbuild(intS,result); 
     31                UIbuild(intS,tmp); 
    3032        } 
    3133        catch (...) { it_error("Internal field " + string(S.getPath()) + " not valid"); 
    3234        } 
    33          
     35        return tmp; 
    3436}; 
    3537UIREGISTER(UIinternal); 
  • bdm/uibuilder.h

    r256 r257  
    7676        }; 
    7777        public: 
    78                 //!Constructor needs to be run only once via REGISTERUI 
     78                //!Constructor needs to be run only once macro UIREGISTER 
    7979                UIbuilder(const string &typ){__uimap__.insert(make_pair(typ,this));} 
    8080                //! Function building the computational object 
    81                 virtual void build(Setting &S, bdmroot* &result) const =0; 
     81                virtual bdmroot* build(Setting &S) const =0; 
    8282}; 
    8383 
     
    8585        public: 
    8686                UIexternal():UIbuilder("external"){} 
    87                 void build(Setting &S, bdmroot* &result) const; 
     87                bdmroot* build(Setting &S) const; 
    8888}; 
    8989 
     
    9191        public: 
    9292                UIinternal():UIbuilder("internal"){} 
    93                 void build(Setting &S, bdmroot* &result) const; 
     93                bdmroot* build(Setting &S) const; 
    9494}; 
    9595 
    96 //! Prototype of a UI builder. Return value is by the second argument since it allows some type of type checking. 
     96//! Prototype of a UI builder. Return value is by the second argument since it type checking via \c dynamic_cast. 
    9797template<class T> 
    9898void UIbuild(Setting &S, T* &ret){ 
    9999        CHECK_UITYPE(S,TypeGroup); 
    100         T* tmp; 
    101100        // Check if field "type" is present, if not it is not a valid UI 
    102101        it_assert_debug(S.exists("type"), string(S.getPath())+" is not a valid UI!"); 
    103102                 
    104         const string typ=S["type"]; 
    105          
     103        const string typ=S["type"];      
    106104        // Find "type" in list of registred UI builders 
    107105        UImap::const_iterator iter = __uimap__.find( typ ); 
    108106        if( iter == __uimap__.end()) { 
    109                 cout << "UI error" << endl; 
    110107                it_error("UI of type \"" + typ + "\" is not registered!"); 
    111108        } 
    112109         
    113         const UIbuilder* is= iter->second;  
    114110        //BUILD the result 
    115         is->build(S,(bdmroot*&)(tmp)); 
    116         // make assignment 
    117         ret=tmp; 
     111        ret = dynamic_cast<T*>(iter->second->build(S)); 
     112}; 
     113 
     114//! Auxiliary function allowing recursivity in S 
     115template<class T> 
     116void UIcall(Setting &S, void (*func)(Setting&, T), T Tmp ){ 
     117        CHECK_UITYPE(S,TypeGroup); 
     118        // Check if field "type" is present, if not it is not a valid UI 
     119        it_assert_debug(S.exists("type"), string(S.getPath())+" is not a valid UI!"); 
     120                 
     121        const string typ=S["type"];      
     122        if(typ=="internal"){ 
     123                try {    
     124                        Setting* Stmp = &S; 
     125                        do {Stmp=&(Stmp->getParent());} while (!Stmp->isRoot()); 
     126                        Setting& intS=Stmp->lookup((const char*)S["path"]); 
     127                        func(intS, Tmp); // <======== calling UIset 
     128                        return; 
     129                } 
     130                catch (...) { it_error("Internal field " + string(S.getPath()) + " not valid"); 
     131                } 
     132        } 
     133        if(typ=="extern"){ 
     134                Config C; 
     135                bdmroot* tmp; 
     136                try{C.readFile((const char*)S["filename"]);} catch (...){ 
     137                        it_error("File " + string((const char*)S["filename"]) + " not found or broken"); 
     138                } 
     139                try {func(C.lookup((const char*)S["path"]), Tmp);} catch (...) { //<=========== calling UIset 
     140                        it_error("External field " + string(S.getPath()) + " not valid"); 
     141                } 
     142                return; 
     143        } 
     144         
     145        // v======================= calling final set 
     146        func(S, Tmp); 
    118147}; 
    119148 
  • pmsm/CMakeLists.txt

    r250 r257  
    3030EXEC (mpf_load pmsmsim) 
    3131 
    32 #EXEC (sim pmsmsim config++) 
     32EXEC (sim pmsmsim) 
    3333EXEC (sim_var pmsmsim) 
    3434EXEC (sim_var_arx pmsmsim) 
  • pmsm/sim.cpp

    r254 r257  
    1111*/ 
    1212 
    13 #include <config/uibuilder.h> 
     13#include <uibuilder.h> 
    1414#include <stat/libDS.h> 
    1515#include "pmsmDS.h" 
    1616 
     17#include <stat/loggers_ui.h> 
     18 
    1719using namespace bdm; 
    1820int main() { 
    19         UIFile F("sim.bdm"); 
     21        UIFile F("sim.txt"); 
    2022 
    2123        logger* L;  
    22         UIbuild(F.lookup("logger"),&L); 
     24        UIbuild(F.lookup("logger"),L); 
    2325        pmsmDS * DS; 
    24         UIbuild(F.lookup("system"),&DS); 
     26        UIbuild(F.lookup("system"),DS); 
    2527        int Ndat; 
    2628        F.lookupValue("ndat",Ndat); 
     
    3638         
    3739        L->finalize();  
     40         
     41        delete L; 
     42        delete DS; 
    3843        return 0; 
    3944} 
  • tests/UI/UIbuilder_test.cpp

    r256 r257  
    2121        public: 
    2222        UItest():UIbuilder("test"){} 
    23         void build(Setting &S, bdmroot* &ret) const{ 
     23        bdmroot* build(Setting &S) const{ 
    2424                try{ 
    2525                        int a=S["a"]; 
     
    2727                        S.lookupValue("S",St); 
    2828                        cls* tmp = new cls(a,St); 
    29                         ret=tmp; 
     29                        return tmp; 
    3030                } 
    3131                catch (...){ 
     
    3838        public: 
    3939        UItest2():UIbuilder("test2"){} 
    40         void build(Setting &S, bdmroot* &ret) const{ 
     40        bdmroot* build(Setting &S) const{ 
    4141                try{ 
    4242                        int a=S["a"]; 
    4343                        string St; 
    4444                        S.lookupValue("S",St); 
    45                         cls* tmp = new cls2(a,St); 
    46                         ret=tmp; 
     45                        return  new cls2(a,St); 
    4746                } 
    4847                catch (...){