Changeset 246

Show
Ignore:
Timestamp:
01/23/09 14:57:26 (15 years ago)
Author:
smidl
Message:

[PATCH] UIfile class

Location:
bdm/config
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • bdm/config/CMakeLists.txt

    r244 r246  
     1        INCLUDE_DIRECTORIES(libconfig-1.3.1) 
     2        LINK_DIRECTORIES(libconfig-1.3.1/.libs) 
     3 
    14add_library(libui uibuilder.cpp uibuilder.h) 
    25EXEC(UIbuilder_test config++ libui) 
  • bdm/config/UIbuilder_test.cfg

    r244 r246  
    77test2: 
    88{ 
     9type= "internal"; 
     10path="int"; 
     11}; 
     12test3: 
     13{ 
    914type= "external"; 
    1015filename = "UIbuilder_test_ex.cfg"; 
    1116path="test2"; 
    1217}; 
     18int: 
     19{ 
     20type= "test2"; 
     21a= 1; 
     22S= "yeah!!!"; 
     23}; 
  • bdm/config/UIbuilder_test.cpp

    r244 r246  
    6565int main(){ 
    6666 
    67 Config C; 
    68 C.readFile("UIbuilder_test.cfg"); 
    69 Setting& S=C.getRoot();; 
     67UIFile UI("UIbuilder_test.cfg"); 
    7068 
    7169cls* Cls; 
    72 UIbuild(S["test"],&Cls); 
     70UIbuild(UI.lookup("test"),&Cls); 
    7371cls* Cls2; 
    74 UIbuild(S["test2"],&Cls2); 
     72UIbuild(UI.lookup("test2"),&Cls2); 
     73cls* Cls3; 
     74UIbuild(UI.lookup("test3"),&Cls3); 
    7575 
    7676Cls->print(); 
    7777Cls2->print(); 
     78Cls3->print(); 
    7879return 0; 
    7980} 
  • bdm/config/libconfig-1.3.1/libconfig.h++

    r245 r246  
    327327    inline unsigned int getSourceLine() const throw() 
    328328    { return(config_setting_source_line(_setting)); } 
     329 
     330        inline Setting& lookup(const char *path) const 
     331  throw(SettingNotFoundException) 
     332{ 
     333  config_setting_t *s = config_lookup(_setting->config, path); 
     334  if(! s) 
     335    throw SettingNotFoundException(path); 
     336 
     337  return(Setting::wrapSetting(s)); 
     338} 
    329339  }; 
    330340 
  • bdm/config/uibuilder.cpp

    r244 r246  
    1818         
    1919}; 
     20UIREGISTER(UIexternal); 
    2021 
    21 UIREGISTER(UIexternal); 
     22void UIinternal::build(Setting &S, void** result) const{ 
     23        try {    
     24                Setting* Stmp = &S; 
     25                do {Stmp=&(Stmp->getParent());} while (!Stmp->isRoot()); 
     26                Setting& intS=Stmp->lookup((const char*)S["path"]); 
     27                UIbuild(intS,result); 
     28        } 
     29        catch (...) { it_error("Internal field " + string(S.getPath()) + " not valid"); 
     30        } 
     31         
     32}; 
     33UIREGISTER(UIinternal); 
     34 
     35 
    2236//UIexternal* UIexternal_instance =new UIexternal(); 
  • bdm/config/uibuilder.h

    r243 r246  
    77using namespace libconfig; 
    88using namespace itpp; 
    9          
    10 class UI: public Setting{ 
     9using namespace std; 
     10 
     11#define CHECK_UITYPE(S,Type) it_assert_debug(S.getType()==Setting::Type, string("Wrong input path \"")+string(S.getPath())+string("\"")); 
     12#define UIREGISTER(UI) UI* UI##_global_instance = new UI(); 
     13 
     14////////// GLOBAL VAriables 
     15 
     16class UIbuilder; 
     17//! Internal structure mapping strings to UIBuilder objects 
     18typedef map<const string, const UIbuilder*> UImap; 
     19extern UImap __uimap__; 
     20 
     21class UIFile : public Config{ 
    1122        public: 
    12                 template<class Num_T> 
    13                 Vec<Num_T>& getvec(Setting& S){ 
    14                         itpp_assert_debug(S.getType()==Setting::TypeArray,"Wrong input on line:"+S.getSourceLine()); 
    15                         Vec<Num_T> tmp;  
    16                         tmp.set_size(S.getLength()); 
    17                         for (int =0;i<S.getLength();i++){tmp[i]=(Num_T)(S[i]);} 
     23        UIFile(const char * fname):Config(){ 
     24                try{Config::readFile(fname);}  
     25                catch (ParseException& P) { 
     26                        char msg[200]; 
     27                        sprintf(msg,"Error in file %s  on line %d.", fname, P.getLine()); 
     28                        it_error(msg); 
     29                }  
     30                catch (FileIOException f) {it_error("File " + string(fname) + " not found");}  
     31        } 
     32}; 
     33 
     34/*!\brief Builds computational object from a UserInfo structure 
     35 
     36Return value is a pointer to the created object (memory management issue?) 
     37*/ 
     38class UIbuilder { 
     39        protected: 
     40                static const UIbuilder* theinstance; 
     41        vec getvec ( Setting& S ) { 
     42                CHECK_UITYPE(S,TypeArray); 
     43                vec tmp; 
     44                tmp.set_size ( S.getLength() ); 
     45                for ( int i=0;i<S.getLength();i++ ) { 
     46                        switch (S[i].getType()) { 
     47                                case Setting::TypeFloat : 
     48                                        tmp[i]=double(S[i]);break; 
     49                                case Setting::TypeInt : 
     50                                        tmp[i]=int(S[i]);break; 
     51                                case Setting::TypeBoolean : 
     52                                        tmp[i]=bool(S[i]);break; 
     53                                default: it_error("libconfig error?"); 
     54                        }  
    1855                } 
    19 } 
     56                return tmp; 
     57        }; 
     58        vec getivec ( Setting& S ) { 
     59                CHECK_UITYPE(S,TypeArray); 
     60                vec tmp; 
     61                tmp.set_size ( S.getLength() ); 
     62                for ( int i=0;i<S.getLength();i++ ) { 
     63                        switch (S[i].getType()) { 
     64                                case Setting::TypeFloat : 
     65                                        tmp[i]=double(S[i]);break; 
     66                                case Setting::TypeInt : 
     67                                        tmp[i]=int(S[i]);break; 
     68                                case Setting::TypeBoolean : 
     69                                        tmp[i]=bool(S[i]);break; 
     70                                        default: it_error("libconfig error?"); 
     71                        }  
     72                } 
     73                return tmp; 
     74        }; 
     75        public: 
     76                UIbuilder(const string &typ){__uimap__.insert(make_pair(typ,this));} 
     77                virtual void build(Setting &S, void** result) const {it_error("Calling uibuilder basic" );}; 
     78}; 
     79 
     80class UIexternal:public UIbuilder{ 
     81        public: 
     82                UIexternal():UIbuilder("external"){} 
     83                void build(Setting &S, void** result) const; 
     84}; 
     85 
     86class UIinternal:public UIbuilder{ 
     87        public: 
     88                UIinternal():UIbuilder("internal"){} 
     89                void build(Setting &S, void** result) const; 
     90}; 
     91 
     92template<class T> 
     93void UIbuild(Setting &S, T** ret){ 
     94        CHECK_UITYPE(S,TypeGroup); 
     95        T* tmp; 
     96        it_assert_debug(S.exists("type"), string(S.getPath())+" is not a valid UI!"); 
     97                 
     98        const string typ=S["type"]; 
     99        UImap::const_iterator iter = __uimap__.find( typ ); 
     100        if( iter == __uimap__.end()) { 
     101                it_error("UI of type \"" + typ + "\" is not registered!"); 
     102        } 
     103        else {const UIbuilder* is= iter->second; is->build(S,reinterpret_cast<void**>(&tmp));} 
     104        // make assignment 
     105        *ret=tmp; 
     106}; 
     107 
     108 
     109Setting& UILoad(const char* fname); 
    20110 
    21111#endif UIBUILD