| 9 | | |
| 10 | | class UI: public Setting{ |
| | 9 | using 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 | |
| | 16 | class UIbuilder; |
| | 17 | //! Internal structure mapping strings to UIBuilder objects |
| | 18 | typedef map<const string, const UIbuilder*> UImap; |
| | 19 | extern UImap __uimap__; |
| | 20 | |
| | 21 | class UIFile : public Config{ |
| 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 | |
| | 36 | Return value is a pointer to the created object (memory management issue?) |
| | 37 | */ |
| | 38 | class 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 | } |
| 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 | |
| | 80 | class UIexternal:public UIbuilder{ |
| | 81 | public: |
| | 82 | UIexternal():UIbuilder("external"){} |
| | 83 | void build(Setting &S, void** result) const; |
| | 84 | }; |
| | 85 | |
| | 86 | class UIinternal:public UIbuilder{ |
| | 87 | public: |
| | 88 | UIinternal():UIbuilder("internal"){} |
| | 89 | void build(Setting &S, void** result) const; |
| | 90 | }; |
| | 91 | |
| | 92 | template<class T> |
| | 93 | void 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 | |
| | 109 | Setting& UILoad(const char* fname); |