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