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 | | }; |
| 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 | }; |
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 | | }; |
| 42 | const vec getvec ( Setting& S ) const { |
| 43 | CHECK_UITYPE ( S,TypeArray ); |
| 44 | vec tmp; |
| 45 | tmp.set_size ( S.getLength() ); |
| 46 | for ( int i=0;i<S.getLength();i++ ) { |
| 47 | switch ( S[i].getType() ) { |
| 48 | case Setting::TypeFloat : |
| 49 | tmp[i]=double ( S[i] );break; |
| 50 | case Setting::TypeInt : |
| 51 | tmp[i]=int ( S[i] );break; |
| 52 | case Setting::TypeBoolean : |
| 53 | tmp[i]=bool ( S[i] );break; |
| 54 | default: it_error ( "libconfig error?" ); |
| 55 | } |
| 56 | } |
| 57 | return tmp; |
| 58 | }; |
| 59 | const vec getivec ( Setting& S ) const { |
| 60 | CHECK_UITYPE ( S,TypeArray ); |
| 61 | vec tmp; |
| 62 | tmp.set_size ( S.getLength() ); |
| 63 | for ( int i=0;i<S.getLength();i++ ) { |
| 64 | switch ( S[i].getType() ) { |
| 65 | case Setting::TypeFloat : |
| 66 | tmp[i]=double ( S[i] );break; |
| 67 | case Setting::TypeInt : |
| 68 | tmp[i]=int ( S[i] );break; |
| 69 | case Setting::TypeBoolean : |
| 70 | tmp[i]=bool ( S[i] );break; |
| 71 | default: it_error ( "libconfig error?" ); |
| 72 | } |
| 73 | } |
| 74 | return tmp; |
| 75 | }; |
97 | | template<class T> |
98 | | void UIbuild(Setting &S, T* &ret){ |
99 | | CHECK_UITYPE(S,TypeGroup); |
100 | | // Check if field "type" is present, if not it is not a valid UI |
101 | | it_assert_debug(S.exists("type"), string(S.getPath())+" is not a valid UI!"); |
102 | | |
103 | | const string typ=S["type"]; |
104 | | // Find "type" in list of registred UI builders |
105 | | UImap::const_iterator iter = __uimap__.find( typ ); |
106 | | if( iter == __uimap__.end()) { |
107 | | it_error("UI of type \"" + typ + "\" is not registered!"); |
108 | | } |
109 | | |
110 | | //BUILD the result |
111 | | ret = dynamic_cast<T*>(iter->second->build(S)); |
112 | | }; |
| 99 | template<class T> |
| 100 | void UIbuild ( Setting &S, T* &ret ) { |
| 101 | CHECK_UITYPE ( S,TypeGroup ); |
| 102 | // Check if field "type" is present, if not it is not a valid UI |
| 103 | it_assert_debug ( S.exists ( "type" ), string ( S.getPath() ) +" is not a valid UI!" ); |
114 | | //! Auxiliary function allowing recursivity in S |
115 | | template<class T> |
116 | | void UIcall(Setting &S, void (*func)(Setting&, T), T Tmp ){ |
117 | | CHECK_UITYPE(S,TypeGroup); |
118 | | // Check if field "type" is present, if not it is not a valid UI |
119 | | it_assert_debug(S.exists("type"), string(S.getPath())+" is not a valid UI!"); |
120 | | |
121 | | const string typ=S["type"]; |
122 | | if(typ=="internal"){ |
123 | | try { |
124 | | Setting* Stmp = &S; |
125 | | do {Stmp=&(Stmp->getParent());} while (!Stmp->isRoot()); |
126 | | Setting& intS=Stmp->lookup((const char*)S["path"]); |
127 | | func(intS, Tmp); // <======== calling UIset |
| 105 | const string typ=S["type"]; |
| 106 | // Find "type" in list of registred UI builders |
| 107 | UImap::const_iterator iter = __uimap__.find ( typ ); |
| 108 | if ( iter == __uimap__.end() ) { |
| 109 | it_error ( "UI of type \"" + typ + "\" is not registered!" ); |
| 110 | } |
| 111 | |
| 112 | //BUILD the result |
| 113 | try { |
| 114 | ret = dynamic_cast<T*> ( iter->second->build ( S ) ); |
| 115 | } |
| 116 | catch ( SettingTypeException e ) { |
| 117 | UI_DBG(S,""); |
| 118 | it_error ( "Setting " +string ( e.getPath() ) +" is of incorrect Type" );} |
| 119 | catch ( SettingNotFoundException e ) { |
| 120 | UI_DBG(S,""); |
| 121 | it_error ( "Setting " + string ( e.getPath() ) +" was not found" );} |
| 122 | }; |
| 123 | |
| 124 | //! Auxiliary function allowing recursivity in S (too complex, remove?) |
| 125 | template<class T> |
| 126 | void UIcall ( Setting &S, void ( *func ) ( Setting&, T ), T Tmp ) { |
| 127 | CHECK_UITYPE ( S,TypeGroup ); |
| 128 | // Check if field "type" is present, if not it is not a valid UI |
| 129 | it_assert_debug ( S.exists ( "type" ), string ( S.getPath() ) +" is not a valid UI!" ); |
| 130 | |
| 131 | const string typ=S["type"]; |
| 132 | if ( typ=="internal" ) { |
| 133 | try { |
| 134 | Setting* Stmp = &S; |
| 135 | do {Stmp=& ( Stmp->getParent() );} |
| 136 | while ( !Stmp->isRoot() ); |
| 137 | Setting& intS=Stmp->lookup ( ( const char* ) S["path"] ); |
| 138 | func ( intS, Tmp ); // <======== calling func |
| 139 | return; |
| 140 | } |
| 141 | catch ( ... ) { |
| 142 | it_error ( "Internal field " + string ( S.getPath() ) + " not valid" ); |
| 143 | } |
| 144 | } |
| 145 | if ( typ=="external" ) { |
| 146 | UIFile C(S["filename"]); |
| 147 | try { |
| 148 | func ( C.lookup ( ( const char* ) S["path"] ), Tmp ); |
| 149 | } |
| 150 | catch ( ... ) { |
| 151 | it_error ( "External field " + string ( S.getPath() ) + " not valid" ); |
| 152 | } |