[254] | 1 | #ifndef UIBUILD |
---|
| 2 | #define UIBUILD |
---|
| 3 | |
---|
[243] | 4 | #include <itpp/itbase.h> |
---|
[254] | 5 | #include "stat/libBM.h" |
---|
[256] | 6 | #include "libconfig/libconfig.h++" |
---|
[243] | 7 | |
---|
[260] | 8 | namespace bdm { |
---|
[243] | 9 | |
---|
[281] | 10 | using namespace libconfig; |
---|
| 11 | using namespace std; |
---|
[246] | 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 | |
---|
[281] | 16 | //!Standard catches for UIs, use as: catch UICATCH |
---|
| 17 | #define UICATCH ( SettingTypeException e ) {it_error ( "Setting " +string ( e.getPath() ) +" is of incorrect Type" );} catch ( SettingNotFoundException e ) {it_error ( "Setting " + string ( e.getPath() ) +" was not found" );} |
---|
[271] | 18 | |
---|
[246] | 19 | ////////// GLOBAL VAriables |
---|
| 20 | |
---|
[281] | 21 | class UIbuilder; |
---|
[246] | 22 | //! Internal structure mapping strings to UIBuilder objects |
---|
[281] | 23 | typedef map<const string, const UIbuilder*> UImap; |
---|
| 24 | extern UImap __uimap__; |
---|
[246] | 25 | |
---|
[281] | 26 | class UIFile : public Config { |
---|
| 27 | public: |
---|
| 28 | UIFile ( const char * fname ) :Config() { |
---|
| 29 | try{Config::readFile ( fname );} |
---|
| 30 | catch ( FileIOException f ) {it_error ( "File " + string ( fname ) + " not found" );} |
---|
| 31 | catch ( ParseException& P ) { |
---|
| 32 | char msg[200]; |
---|
| 33 | sprintf ( msg,"Error in file %s on line %d.", fname, P.getLine() ); |
---|
| 34 | it_error ( msg ); |
---|
[260] | 35 | } |
---|
[281] | 36 | } |
---|
| 37 | }; |
---|
[246] | 38 | |
---|
[281] | 39 | //! \name elem Elementary build functions |
---|
| 40 | //!@{ |
---|
[246] | 41 | |
---|
[281] | 42 | //! construct itpp::vec from Setting of type Array |
---|
| 43 | inline 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 | }; |
---|
[243] | 60 | |
---|
[281] | 61 | //! construct itpp::mat from Setting of type Array, number of columns must be given |
---|
| 62 | inline mat getmat ( Setting& S , int ncols ) { |
---|
| 63 | CHECK_UITYPE ( S,TypeArray ); |
---|
| 64 | mat tmp; |
---|
| 65 | int nrows=S.getLength() /ncols; |
---|
| 66 | int r=0,c=0; |
---|
| 67 | tmp.set_size ( nrows, ncols ); |
---|
| 68 | // Build matrix row-wise |
---|
| 69 | for ( int i=0;i<S.getLength();i++ ) { |
---|
| 70 | switch ( S[i].getType() ) { |
---|
| 71 | case Setting::TypeFloat : |
---|
| 72 | tmp ( r,c ) =double ( S[i] );break; |
---|
| 73 | case Setting::TypeInt : |
---|
| 74 | tmp ( r,c ) =int ( S[i] );break; |
---|
| 75 | case Setting::TypeBoolean : |
---|
| 76 | tmp ( r,c ) =bool ( S[i] );break; |
---|
| 77 | default: it_error ( "libconfig error?" ); |
---|
| 78 | } |
---|
| 79 | c++; if ( c==ncols ) {c=0;r++;} |
---|
| 80 | } |
---|
| 81 | return tmp; |
---|
| 82 | }; |
---|
[246] | 83 | |
---|
[281] | 84 | //! construct itpp::ivec from Setting of type Array |
---|
[246] | 85 | |
---|
[281] | 86 | inline ivec getivec ( Setting& S ) { |
---|
| 87 | CHECK_UITYPE ( S,TypeArray ); |
---|
| 88 | ivec tmp; |
---|
| 89 | tmp.set_size ( S.getLength() ); |
---|
| 90 | for ( int i=0;i<S.getLength();i++ ) { |
---|
| 91 | switch ( S[i].getType() ) { |
---|
| 92 | case Setting::TypeFloat : |
---|
| 93 | tmp[i]=double ( S[i] );break; |
---|
| 94 | case Setting::TypeInt : |
---|
| 95 | tmp[i]=int ( S[i] );break; |
---|
| 96 | case Setting::TypeBoolean : |
---|
| 97 | tmp[i]=bool ( S[i] );break; |
---|
| 98 | default: it_error ( "libconfig error?" ); |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | return tmp; |
---|
| 102 | }; |
---|
[260] | 103 | |
---|
[281] | 104 | //! construct itpp::Array<string> from Setting of type Array |
---|
| 105 | |
---|
| 106 | inline Array<string> get_as ( Setting& S ) { |
---|
| 107 | CHECK_UITYPE ( S,TypeArray ); |
---|
| 108 | Array<string> tmp; |
---|
| 109 | tmp.set_size ( S.getLength() ); |
---|
| 110 | for ( int i=0;i<S.getLength();i++ ) {tmp ( i ) = ( const char* ) S[i];} |
---|
| 111 | return tmp; |
---|
| 112 | }; |
---|
| 113 | |
---|
| 114 | //!@} |
---|
| 115 | |
---|
| 116 | /*!\brief Builds computational object from a UserInfo structure |
---|
| 117 | |
---|
| 118 | Return value is a pointer to the created object (memory management issue?) |
---|
| 119 | */ |
---|
| 120 | class UIbuilder { |
---|
| 121 | protected: |
---|
| 122 | public: |
---|
| 123 | //!Constructor needs to be run only once macro UIREGISTER |
---|
| 124 | UIbuilder ( const string &typ ) {__uimap__.insert ( make_pair ( typ,this ) );} |
---|
| 125 | //! Function building the computational object |
---|
| 126 | virtual bdmroot* build ( Setting &S ) const =0; |
---|
[325] | 127 | //! Virtual destructor for future use; |
---|
| 128 | virtual ~UIbuilder(){}; |
---|
[281] | 129 | }; |
---|
| 130 | |
---|
| 131 | /*! Recursive build of objects defined in external file |
---|
| 132 | |
---|
| 133 | \code |
---|
| 134 | {type="external"; |
---|
| 135 | filename="my_file.cfg"; // name of file from which to read |
---|
| 136 | path="system.profile.[0]"; // Path in the external file |
---|
| 137 | }; |
---|
| 138 | \endcode |
---|
| 139 | */ |
---|
| 140 | class UIexternal:public UIbuilder { |
---|
| 141 | public: |
---|
| 142 | UIexternal() :UIbuilder ( "external" ) {} |
---|
| 143 | bdmroot* build ( Setting &S ) const; |
---|
| 144 | }; |
---|
| 145 | |
---|
| 146 | /*! Recursive build of objects defined in the same file |
---|
| 147 | |
---|
| 148 | \code |
---|
| 149 | {type="internal"; |
---|
| 150 | path="system.profile.[0]"; // Path from the root |
---|
| 151 | }; |
---|
| 152 | \endcode |
---|
| 153 | */ |
---|
| 154 | class UIinternal:public UIbuilder { |
---|
| 155 | public: |
---|
| 156 | UIinternal() :UIbuilder ( "internal" ) {} |
---|
| 157 | bdmroot* build ( Setting &S ) const; |
---|
| 158 | }; |
---|
| 159 | |
---|
| 160 | //! [Debugging] Print values in current S to cout |
---|
| 161 | void UI_DBG ( Setting &S, const string &spc ); |
---|
| 162 | |
---|
[257] | 163 | //! Prototype of a UI builder. Return value is by the second argument since it type checking via \c dynamic_cast. |
---|
[281] | 164 | template<class T> |
---|
| 165 | void UIbuild ( Setting &S, T* &ret ) { |
---|
| 166 | CHECK_UITYPE ( S,TypeGroup ); |
---|
| 167 | // Check if field "type" is present, if not it is not a valid UI |
---|
| 168 | it_assert_debug ( S.exists ( "type" ), string ( S.getPath() ) +" is not a valid UI!" ); |
---|
[246] | 169 | |
---|
[281] | 170 | const string typ=S["type"]; |
---|
| 171 | // Find "type" in list of registred UI builders |
---|
| 172 | UImap::const_iterator iter = __uimap__.find ( typ ); |
---|
| 173 | if ( iter == __uimap__.end() ) { |
---|
| 174 | it_error ( "UI of type \"" + typ + "\" is not registered!" ); |
---|
| 175 | } |
---|
[260] | 176 | |
---|
[281] | 177 | //BUILD the result |
---|
| 178 | try { |
---|
| 179 | ret = dynamic_cast<T*> ( iter->second->build ( S ) ); |
---|
| 180 | } |
---|
| 181 | catch UICATCH |
---|
| 182 | }; |
---|
[260] | 183 | |
---|
| 184 | //! Auxiliary function allowing recursivity in S (too complex, remove?) |
---|
[281] | 185 | template<class T> |
---|
| 186 | void UIcall ( Setting &S, void ( *func ) ( Setting&, T ), T Tmp ) { |
---|
| 187 | CHECK_UITYPE ( S,TypeGroup ); |
---|
| 188 | // Check if field "type" is present, if not it is not a valid UI |
---|
| 189 | it_assert_debug ( S.exists ( "type" ), string ( S.getPath() ) +" is not a valid UI!" ); |
---|
[260] | 190 | |
---|
[281] | 191 | const string typ=S["type"]; |
---|
| 192 | if ( typ=="internal" ) { |
---|
| 193 | try { |
---|
| 194 | Setting* Stmp = &S; |
---|
| 195 | do {Stmp=& ( Stmp->getParent() );} |
---|
| 196 | while ( !Stmp->isRoot() ); |
---|
| 197 | Setting& intS=Stmp->lookup ( ( const char* ) S["path"] ); |
---|
| 198 | func ( intS, Tmp ); // <======== calling func |
---|
[260] | 199 | return; |
---|
[257] | 200 | } |
---|
[281] | 201 | catch ( ... ) { |
---|
| 202 | it_error ( "Internal field " + string ( S.getPath() ) + " not valid" ); |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | if ( typ=="external" ) { |
---|
| 206 | UIFile C ( S["filename"] ); |
---|
| 207 | try { |
---|
| 208 | func ( C.lookup ( ( const char* ) S["path"] ), Tmp ); |
---|
| 209 | } |
---|
| 210 | catch ( ... ) { |
---|
| 211 | it_error ( "External field " + string ( S.getPath() ) + " not valid" ); |
---|
| 212 | } |
---|
| 213 | return; |
---|
| 214 | } |
---|
[257] | 215 | |
---|
[281] | 216 | // v======================= calling final func |
---|
| 217 | func ( S, Tmp ); |
---|
| 218 | }; |
---|
[260] | 219 | |
---|
[254] | 220 | } |
---|
[260] | 221 | #endif //UIBUILD |
---|