00001 #ifndef PSPARAMETERSACCESSOR_INL 00002 #define PSPARAMETERSACCESSOR_INL 00003 00004 #include "OMKParametersAccessor.h" 00005 #include "OMKAttribute.h" 00006 #include "OMKTracer.h" 00007 #include "OMKConfigurationParameterDescriptor.h" 00008 #include "OMKUniqueConfigurationParameter.h" 00009 #include "OMKMultipleConfigurationParameter.h" 00010 namespace OMK 00011 { 00013 00014 //------------------------------------------------------------------- 00017 template <> 00018 inline 00019 bool ParametersAccessor::getTxtValue( const std::string & textValue, std::string & value ) 00020 { 00021 value = textValue; 00022 return true; 00023 } 00024 00025 //------------------------------------------------------------------- 00028 template <> 00029 inline 00030 bool ParametersAccessor::setTxtValue( std::string& textValue, const std::string& value ) 00031 { 00032 textValue = value ; 00033 return true ; 00034 } 00035 00036 //------------------------------------------------------------------- 00039 template <> 00040 inline 00041 bool ParametersAccessor::getValue( const ConfigurationParameterDescriptor * nodeValue, 00042 const ConfigurationParameterDescriptor * & value, 00043 std::string& errorStr ) 00044 { 00045 errorStr = "The node is empty" ; 00046 // Get the value of the node 00047 value = nodeValue ; 00048 // if it is not null, return true 00049 return value != 0 ; 00050 } 00051 //------------------------------------------------------------------- 00054 template <> 00055 inline 00056 bool ParametersAccessor::getValue( const ConfigurationParameterDescriptor * nodeValue, 00057 ConfigurationParameterDescriptor * & value, 00058 std::string& errorStr ) 00059 { 00060 value = 0 ; 00061 errorStr = "Unable to get a no-const ConfigurationParameterDescriptor*" ; 00062 return false ; 00063 } 00065 //------------------------------------------------------------------- 00066 // Get a value for a T 00067 template < typename T > 00068 inline 00069 bool ParametersAccessor::getValue( const ConfigurationParameterDescriptor * nodeValue, 00070 T & value, 00071 std::string& errorStr ) 00072 { 00073 bool ok = ( nodeValue != NULL ) ; 00074 ok = ok && getTxtValue( nodeValue->getAssociatedString(), value ) ; 00075 if( !ok ) 00076 { 00077 errorStr = "Cannot read the value in string \"" ; 00078 errorStr += nodeValue ? nodeValue->getAssociatedString() : "empty" ; 00079 errorStr += "\"" ; 00080 } 00081 return ok ; 00082 } 00083 00084 //------------------------------------------------------------------- 00085 // convert the string in a T value 00086 template < typename T > 00087 inline 00088 bool ParametersAccessor::getTxtValue( const std::string & textValue, T & value ) 00089 { 00090 std::stringstream is; 00091 is << textValue; 00092 is >> value; 00093 return 0 != (void*)is; 00094 } 00095 00096 //------------------------------------------------------------------- 00097 template < typename T > 00098 inline 00099 bool ParametersAccessor::get( const ConfigurationParameterDescriptor * node, 00100 const std::string & name, 00101 T & value, 00102 SimulatedObject* object ) 00103 { 00104 std::string errorStr ; 00105 // Get the node for the value 00106 const ConfigurationParameterDescriptor * nodeValue = getNodeValue( node, name, errorStr ) ; 00107 // if it is good, get the value else display error 00108 return ( nodeValue && getValue( nodeValue, value, errorStr ) ) 00109 || displayError( name, "get", object, errorStr ) ; 00110 } 00111 00112 //------------------------------------------------------------------- 00113 template < typename T > 00114 inline 00115 bool ParametersAccessor::get( const ConfigurationParameterDescriptor * node, 00116 const std::string & name, 00117 T & value, 00118 const std::map< std::string, T >& mapping, 00119 SimulatedObject* object ) 00120 { 00121 std::string errorStr ; 00122 // Get the node for the value 00123 const ConfigurationParameterDescriptor * nodeValue = getNodeValue( node, name, errorStr ) ; 00124 // get the value as a string and convert it with the mapping 00125 std::string txt ; 00126 bool ok = false ; 00127 if( nodeValue && getValue( nodeValue, txt, errorStr ) ) 00128 { 00129 typename std::map< std::string, T >::const_iterator i = mapping.find( txt ) ; 00130 if( i != mapping.end() ) 00131 { 00132 value = i->second ; 00133 ok = true ; 00134 } 00135 else 00136 { 00137 errorStr = "Cannot read a valid enum value in string \"" ; 00138 errorStr += txt ; 00139 errorStr += "\"" ; 00140 } 00141 } 00142 // true if it is good else display error 00143 return ok || displayError( name, "get", object, errorStr ) ; 00144 00145 } 00146 00147 //------------------------------------------------------------------- 00148 template < typename T > 00149 inline 00150 bool ParametersAccessor::getValue( const ConfigurationParameterDescriptor * nodeValue, 00151 OMK::IAttributeBaseT< T > & attr, 00152 std::string& errorStr ) 00153 { 00154 T value ; 00155 bool ok = ( nodeValue != NULL ) ; 00156 ok = ok && getValue( nodeValue, value, errorStr ) ; 00157 if( ok ) 00158 { 00159 attr.set( value ) ; 00160 } 00161 else 00162 { 00163 errorStr = "Cannot read the value in string \"" ; 00164 errorStr += nodeValue ? nodeValue->getAssociatedString() : "empty" ; 00165 errorStr += "\"" ; 00166 } 00167 return ok ; 00168 } 00169 00170 //------------------------------------------------------------------- 00171 template < typename T > 00172 inline 00173 bool ParametersAccessor::getValue( const ConfigurationParameterDescriptor * nodeValue, 00174 std::vector< T > & values, 00175 std::string& errorStr ) 00176 { 00177 bool ok = ( nodeValue != NULL ) ; 00178 if( ok ) 00179 { 00180 for( int i = 0 ; ok && ( i < nodeValue->getNumberOfSubItems() ) ; i++ ) 00181 { 00182 T aValue; 00183 ok = ParametersAccessor::getValue( nodeValue->getSubDescriptorByPosition( i ), aValue, errorStr ) ; 00184 if( ok ) 00185 { 00186 values.push_back(aValue); 00187 } 00188 else 00189 { 00190 std::ostringstream oss ; 00191 oss << ", cannot get the parameter number " << i ; 00192 errorStr += oss.str() ; 00193 } 00194 } 00195 } 00196 return ok ; 00197 } 00198 00199 //------------------------------------------------------------------- 00200 template < typename T > 00201 inline 00202 bool ParametersAccessor::getValue( const ConfigurationParameterDescriptor * nodeValue, 00203 std::list< T > & values, 00204 std::string& errorStr ) 00205 { 00206 bool ok = ( nodeValue != NULL ) ; 00207 if( ok ) 00208 { 00209 for( int i = 0 ; ok && ( i < nodeValue->getNumberOfSubItems() ) ; i++ ) 00210 { 00211 T aValue; 00212 ok = ParametersAccessor::getValue( nodeValue->getSubDescriptorByPosition( i ), aValue, errorStr ) ; 00213 if( ok ) 00214 { 00215 values.push_back(aValue); 00216 } 00217 else 00218 { 00219 std::ostringstream oss ; 00220 oss << ", cannot get the parameter number " << i ; 00221 errorStr += oss.str() ; 00222 } 00223 } 00224 } 00225 return ok ; 00226 } 00227 00228 //------------------------------------------------------------------- 00229 template < typename TK, typename TD > 00230 inline 00231 bool ParametersAccessor::getValue( const ConfigurationParameterDescriptor * nodeValue, 00232 std::map< TK, TD > & values, 00233 std::string& errorStr ) 00234 { 00235 bool ok = ( nodeValue != NULL ) ; 00236 if( ok ) 00237 { 00238 for( int i = 0 ; ok && ( i < nodeValue->getNumberOfSubItems() ) ; i++ ) 00239 { 00240 std::pair< TK, TD > aValue; 00241 ok = ParametersAccessor::getValue( nodeValue->getSubDescriptorByPosition( i ), aValue, errorStr ) ; 00242 if( ok ) 00243 { 00244 values[ aValue.first ] = aValue.second ; 00245 } 00246 else 00247 { 00248 std::ostringstream oss ; 00249 oss << ", cannot get the parameter number " << i ; 00250 errorStr += oss.str() ; 00251 } 00252 } 00253 } 00254 return ok ; 00255 } 00256 00257 //------------------------------------------------------------------- 00258 template < typename T1, typename T2 > 00259 inline 00260 bool ParametersAccessor::getValue( const ConfigurationParameterDescriptor * nodeValue, 00261 std::pair< T1, T2 > & value, 00262 std::string& errorStr ) 00263 { 00264 bool ok( ( nodeValue != NULL ) && ( nodeValue->getNumberOfSubItems() == 2 ) ) ; 00265 if ( !ok && errorStr.empty() ) 00266 { 00267 errorStr = "Bad format of pair (need 2 arguments)" + nodeValue->getAssociatedString() ; 00268 } 00269 ok = ok && getValue( nodeValue->getSubDescriptorByPosition( 0 ), value.first, errorStr ) ; 00270 ok = ok && getValue( nodeValue->getSubDescriptorByPosition( 1 ), value.second, errorStr ) ; 00271 return ok ; 00272 } 00273 00274 00275 //------------------------------------------------------------------- 00276 template < typename T > 00277 inline 00278 ConfigurationParameterDescriptor * ParametersAccessor::set( 00279 ConfigurationParameterDescriptor *& node, 00280 const std::string& name, 00281 const T & value ) 00282 { 00283 std::string errorStr ; 00284 ConfigurationParameterDescriptor* newNode = setValue( value, errorStr ) ; 00285 if( newNode ) 00286 { 00287 if( !node ) node = new MultipleConfigurationParameter() ; 00288 node->appendSubDescriptorNamed( name, newNode ) ; 00289 } 00290 else 00291 { 00292 OMERROR( "Warning in ParametersAccessor::Set with parameter [" + name + "]" << std::endl 00293 << ">>> :-( " << errorStr ) ; 00294 } 00295 return newNode ; 00296 } 00297 00298 //------------------------------------------------------------------- 00299 template < typename T > 00300 ConfigurationParameterDescriptor * ParametersAccessor::set( 00301 ConfigurationParameterDescriptor *& node, 00302 const std::string& name, 00303 const T & value, 00304 const std::map< T, std::string >& mapping ) 00305 { 00306 bool ok = false ; 00307 std::string errorStr ; 00308 std::string txt ; 00309 typename std::map< T, std::string >::const_iterator i = mapping.find( value ) ; 00310 if( i != mapping.end() ) 00311 { 00312 txt = i->second ; 00313 ok = true ; 00314 } 00315 else 00316 { 00317 errorStr = "Cannot get a valid string value with the enum value #" ; 00318 errorStr += (int)value ; 00319 } 00320 00321 ConfigurationParameterDescriptor* newNode = ok ? setValue( value, errorStr ) : 0 ; 00322 if( newNode ) 00323 { 00324 if( !node ) node = new MultipleConfigurationParameter() ; 00325 node->appendSubDescriptorNamed( name, newNode ) ; 00326 } 00327 if( !ok ) 00328 { 00329 OMERROR( "Warning in ParametersAccessor::Set with parameter [" + name + "]" << std::endl 00330 << ">>> :-( " << errorStr ) ; 00331 } 00332 return newNode ; 00333 } 00334 00335 00336 //------------------------------------------------------------------- 00337 template < typename T > 00338 inline 00339 bool ParametersAccessor::setTxtValue( std::string& textValue, const T& value ) 00340 { 00341 std::stringstream os; 00342 os << value ; 00343 textValue = os.str() ; 00344 return 0 != (void*)os ; 00345 } 00346 00347 //------------------------------------------------------------------- 00348 template < typename T > 00349 inline 00350 ConfigurationParameterDescriptor *ParametersAccessor::setValue( const T& value, 00351 std::string& errorStr ) 00352 { 00353 ConfigurationParameterDescriptor *node = 0 ; 00354 std::string textValue ; 00355 if( setTxtValue( textValue, value ) ) 00356 { 00357 node = new UniqueConfigurationParameter( textValue ) ; 00358 } 00359 else 00360 { 00361 errorStr = "Unable to convert the value in a string" ; 00362 } 00363 return node ; 00364 } 00365 00366 //------------------------------------------------------------------- 00367 template < typename T > 00368 inline 00369 ConfigurationParameterDescriptor *ParametersAccessor::setValue( const OMK::IAttributeBaseT< T >& attr, 00370 std::string& errorStr ) 00371 { 00372 ConfigurationParameterDescriptor *node = 0 ; 00373 std::string textValue ; 00374 if( setTxtValue( textValue, attr.get() ) ) 00375 { 00376 node = new UniqueConfigurationParameter( textValue ) ; 00377 } 00378 else 00379 { 00380 errorStr = "Unable to convert the value of the attribut in a string" ; 00381 } 00382 return node ; 00383 } 00384 00385 //------------------------------------------------------------------- 00386 template < typename T > 00387 inline 00388 ConfigurationParameterDescriptor *ParametersAccessor::setValue( const std::vector< T >& values, 00389 std::string& errorStr ) 00390 { 00391 ConfigurationParameterDescriptor *node = new MultipleConfigurationParameter() ; 00392 for( unsigned int i = 0 ; i < values.size() ; i++ ) 00393 { 00394 ConfigurationParameterDescriptor *itemNode = setValue( values[ i ], errorStr ) ; 00395 if( itemNode ) 00396 { 00397 node->appendSubDescriptor( itemNode ) ; 00398 } 00399 else 00400 { 00401 errorStr += "\nUnable to set the value #" ; 00402 errorStr += i ; 00403 } 00404 } 00405 return node ; 00406 } 00407 00408 //------------------------------------------------------------------- 00409 template < typename T > 00410 inline 00411 ConfigurationParameterDescriptor *ParametersAccessor::setValue( const std::list< T >& values, 00412 std::string& errorStr ) 00413 { 00414 ConfigurationParameterDescriptor *node = new MultipleConfigurationParameter() ; 00415 for( typename std::list< T >::const_iterator i = values.begin() ; i != values.end() ; i++ ) 00416 { 00417 ConfigurationParameterDescriptor *itemNode = setValue( *i, errorStr ) ; 00418 if( itemNode ) 00419 { 00420 node->appendSubDescriptor( itemNode ) ; 00421 } 00422 else 00423 { 00424 errorStr += "\nUnable to set the value of the list" ; 00425 } 00426 } 00427 return node ; 00428 } 00429 00430 //------------------------------------------------------------------- 00431 template < typename TK, typename TD > 00432 inline 00433 ConfigurationParameterDescriptor *ParametersAccessor::setValue( const std::map< TK, TD >& values, 00434 std::string& errorStr ) 00435 { 00436 ConfigurationParameterDescriptor *node = new MultipleConfigurationParameter() ; 00437 for( typename std::map< TK, TD >::const_iterator i = values.begin() ; i != values.end() ; i++ ) 00438 { 00439 ConfigurationParameterDescriptor *keyNode = setValue( i->first, errorStr ) ; 00440 ConfigurationParameterDescriptor *dataNode = setValue( i->second, errorStr ) ; 00441 if( keyNode && dataNode ) 00442 { 00443 ConfigurationParameterDescriptor *itemNode = new MultipleConfigurationParameter() ; 00444 itemNode->appendSubDescriptor( keyNode ) ; 00445 itemNode->appendSubDescriptor( dataNode ) ; 00446 node->appendSubDescriptor( itemNode ) ; 00447 } 00448 else 00449 { 00450 errorStr += "\nUnable to set the value of the map" ; 00451 } 00452 } 00453 return node ; 00454 } 00455 00456 //------------------------------------------------------------------- 00457 template < typename T1, typename T2 > 00458 inline 00459 ConfigurationParameterDescriptor *ParametersAccessor::setValue( const std::pair< T1, T2 >& values, 00460 std::string& errorStr ) 00461 { 00462 ConfigurationParameterDescriptor *node = new MultipleConfigurationParameter (); 00463 ConfigurationParameterDescriptor *firstNode = setValue( values.first, errorStr ) ; 00464 ConfigurationParameterDescriptor *secondNode = setValue( values.second, errorStr ) ; 00465 if( firstNode && secondNode ) 00466 { 00467 node->appendSubDescriptor( firstNode ) ; 00468 node->appendSubDescriptor( secondNode ) ; 00469 } 00470 else 00471 { 00472 errorStr += "\nUnable to set the value of the pair" ; 00473 } 00474 return node ; 00475 } 00476 } // namespace OMK 00477 00478 #endif // PSPARAMETERSACCESSOR_INL
Documentation generated on Mon Jun 9 11:45:57 2008 |
Generated with doxygen by Dimitri van Heesch , 1997-2007 |