22 | | typedef map<const string, const UIbuilder*> UImap; |
23 | | extern UImap __uimap__; |
24 | | |
25 | | class UIFile : public Config { |
26 | | public: |
27 | | UIFile ( const char * fname ) :Config() { |
28 | | try{Config::readFile ( fname );} |
29 | | catch ( FileIOException f ) {it_error ( "File " + string ( fname ) + " not found" );} |
30 | | catch ( ParseException& P ) { |
31 | | char msg[200]; |
32 | | sprintf ( msg,"Error in file %s on line %d.", fname, P.getLine() ); |
33 | | it_error ( msg ); |
34 | | } |
35 | | } |
36 | | }; |
37 | | |
38 | | /*!\brief Builds computational object from a UserInfo structure |
39 | | |
40 | | Return value is a pointer to the created object (memory management issue?) |
41 | | */ |
42 | | class UIbuilder { |
43 | | protected: |
44 | | const vec getvec ( Setting& S ) const { |
45 | | CHECK_UITYPE ( S,TypeArray ); |
46 | | vec tmp; |
47 | | tmp.set_size ( S.getLength() ); |
48 | | for ( int i=0;i<S.getLength();i++ ) { |
49 | | switch ( S[i].getType() ) { |
50 | | case Setting::TypeFloat : |
51 | | tmp[i]=double ( S[i] );break; |
52 | | case Setting::TypeInt : |
53 | | tmp[i]=int ( S[i] );break; |
54 | | case Setting::TypeBoolean : |
55 | | tmp[i]=bool ( S[i] );break; |
56 | | default: it_error ( "libconfig error?" ); |
57 | | } |
58 | | } |
59 | | return tmp; |
60 | | }; |
61 | | const mat getmat ( Setting& S , int ncols) const { |
62 | | CHECK_UITYPE ( S,TypeArray ); |
63 | | mat tmp; |
64 | | int nrows=S.getLength()/ncols; |
65 | | int r=0,c=0; |
66 | | tmp.set_size ( nrows, ncols ); |
67 | | // Build matrix row-wise |
68 | | for ( int i=0;i<S.getLength();i++ ) { |
69 | | switch ( S[i].getType() ) { |
70 | | case Setting::TypeFloat : |
71 | | tmp(r,c)=double ( S[i] );break; |
72 | | case Setting::TypeInt : |
73 | | tmp(r,c)=int ( S[i] );break; |
74 | | case Setting::TypeBoolean : |
75 | | tmp(r,c)=bool ( S[i] );break; |
76 | | default: it_error ( "libconfig error?" ); |
77 | | } |
78 | | c++; if (c==ncols) {c=0;r++;} |
79 | | } |
80 | | return tmp; |
81 | | }; |
82 | | const ivec getivec ( Setting& S ) const { |
83 | | CHECK_UITYPE ( S,TypeArray ); |
84 | | ivec tmp; |
85 | | tmp.set_size ( S.getLength() ); |
86 | | for ( int i=0;i<S.getLength();i++ ) { |
87 | | switch ( S[i].getType() ) { |
88 | | case Setting::TypeFloat : |
89 | | tmp[i]=double ( S[i] );break; |
90 | | case Setting::TypeInt : |
91 | | tmp[i]=int ( S[i] );break; |
92 | | case Setting::TypeBoolean : |
93 | | tmp[i]=bool ( S[i] );break; |
94 | | default: it_error ( "libconfig error?" ); |
95 | | } |
96 | | } |
97 | | return tmp; |
98 | | }; |
99 | | const Array<string> get_as ( Setting& S ) const { |
100 | | CHECK_UITYPE ( S,TypeArray ); |
101 | | Array<string> tmp; |
102 | | tmp.set_size ( S.getLength() ); |
103 | | for ( int i=0;i<S.getLength();i++ ) {tmp(i)=(const char*)S[i];} |
104 | | return tmp; |
105 | | }; |
106 | | public: |
107 | | //!Constructor needs to be run only once macro UIREGISTER |
108 | | UIbuilder ( const string &typ ) {__uimap__.insert ( make_pair ( typ,this ) );} |
109 | | //! Function building the computational object |
110 | | virtual bdmroot* build ( Setting &S ) const =0; |
111 | | }; |
112 | | |
113 | | class UIexternal:public UIbuilder { |
114 | | public: |
115 | | UIexternal() :UIbuilder ( "external" ) {} |
116 | | bdmroot* build ( Setting &S ) const; |
117 | | }; |
118 | | |
119 | | class UIinternal:public UIbuilder { |
120 | | public: |
121 | | UIinternal() :UIbuilder ( "internal" ) {} |
122 | | bdmroot* build ( Setting &S ) const; |
123 | | }; |
124 | | |
125 | | //! [Debugging] Print values in current S to cout |
126 | | void UI_DBG ( Setting &S, const string &spc ); |
| 23 | typedef map<const string, const UIbuilder*> UImap; |
| 24 | extern UImap __uimap__; |
| 25 | |
| 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 ); |
| 35 | } |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | //! \name elem Elementary build functions |
| 40 | //!@{ |
| 41 | |
| 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 | }; |
| 60 | |
| 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 | }; |
| 83 | |
| 84 | //! construct itpp::ivec from Setting of type Array |
| 85 | |
| 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 | }; |
| 103 | |
| 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; |
| 127 | }; |
| 128 | |
| 129 | /*! Recursive build of objects defined in external file |
| 130 | |
| 131 | \code |
| 132 | {type="external"; |
| 133 | filename="my_file.cfg"; // name of file from which to read |
| 134 | path="system.profile.[0]"; // Path in the external file |
| 135 | }; |
| 136 | \endcode |
| 137 | */ |
| 138 | class UIexternal:public UIbuilder { |
| 139 | public: |
| 140 | UIexternal() :UIbuilder ( "external" ) {} |
| 141 | bdmroot* build ( Setting &S ) const; |
| 142 | }; |
| 143 | |
| 144 | /*! Recursive build of objects defined in the same file |
| 145 | |
| 146 | \code |
| 147 | {type="internal"; |
| 148 | path="system.profile.[0]"; // Path from the root |
| 149 | }; |
| 150 | \endcode |
| 151 | */ |
| 152 | class UIinternal:public UIbuilder { |
| 153 | public: |
| 154 | UIinternal() :UIbuilder ( "internal" ) {} |
| 155 | bdmroot* build ( Setting &S ) const; |
| 156 | }; |
| 157 | |
| 158 | //! [Debugging] Print values in current S to cout |
| 159 | void UI_DBG ( Setting &S, const string &spc ); |
129 | | template<class T> |
130 | | void UIbuild ( Setting &S, T* &ret ) { |
131 | | CHECK_UITYPE ( S,TypeGroup ); |
132 | | // Check if field "type" is present, if not it is not a valid UI |
133 | | it_assert_debug ( S.exists ( "type" ), string ( S.getPath() ) +" is not a valid UI!" ); |
134 | | |
135 | | const string typ=S["type"]; |
136 | | // Find "type" in list of registred UI builders |
137 | | UImap::const_iterator iter = __uimap__.find ( typ ); |
138 | | if ( iter == __uimap__.end() ) { |
139 | | it_error ( "UI of type \"" + typ + "\" is not registered!" ); |
140 | | } |
141 | | |
142 | | //BUILD the result |
| 162 | template<class T> |
| 163 | void UIbuild ( Setting &S, T* &ret ) { |
| 164 | CHECK_UITYPE ( S,TypeGroup ); |
| 165 | // Check if field "type" is present, if not it is not a valid UI |
| 166 | it_assert_debug ( S.exists ( "type" ), string ( S.getPath() ) +" is not a valid UI!" ); |
| 167 | |
| 168 | const string typ=S["type"]; |
| 169 | // Find "type" in list of registred UI builders |
| 170 | UImap::const_iterator iter = __uimap__.find ( typ ); |
| 171 | if ( iter == __uimap__.end() ) { |
| 172 | it_error ( "UI of type \"" + typ + "\" is not registered!" ); |
| 173 | } |
| 174 | |
| 175 | //BUILD the result |
| 176 | try { |
| 177 | ret = dynamic_cast<T*> ( iter->second->build ( S ) ); |
| 178 | } |
| 179 | catch UICATCH |
| 180 | }; |
| 181 | |
| 182 | //! Auxiliary function allowing recursivity in S (too complex, remove?) |
| 183 | template<class T> |
| 184 | void UIcall ( Setting &S, void ( *func ) ( Setting&, T ), T Tmp ) { |
| 185 | CHECK_UITYPE ( S,TypeGroup ); |
| 186 | // Check if field "type" is present, if not it is not a valid UI |
| 187 | it_assert_debug ( S.exists ( "type" ), string ( S.getPath() ) +" is not a valid UI!" ); |
| 188 | |
| 189 | const string typ=S["type"]; |
| 190 | if ( typ=="internal" ) { |