00001 /* 00002 * This file is part of openMask © INRIA, CNRS, Universite de Rennes 1 1993-2002, thereinafter the Software 00003 * 00004 * The Software has been developped within the Siames Project. 00005 * INRIA, the University of Rennes 1 and CNRS jointly hold intellectual property rights 00006 * 00007 * The Software has been registered with the Agence pour la Protection des 00008 * Programmes (APP) under registration number IDDN.FR.001.510008.00.S.P.2001.000.41200 00009 * 00010 * This file may be distributed under the terms of the Q Public License 00011 * version 1.0 as defined by Trolltech AS of Norway and appearing in the file 00012 * LICENSE.QPL included in the packaging of this file. 00013 * 00014 * Licensees holding valid specific licenses issued by INRIA, CNRS or Université de Rennes 1 00015 * for the software may use this file in accordance with that specific license 00016 * 00017 */ 00018 00019 #include <sstream> 00020 00021 #include "OMKTracer.h" 00022 #include "OMKMultipleConfigurationParameter.h" 00023 #include "OMKBaseType.h" 00024 #include "OMKtokens.h" 00025 #include "string" 00026 00027 #include "stdio.h" 00028 #include "OMKgenericKernelParser.h" 00029 #include "OMKKernelIstreamLexer.h" 00030 #include "OMKDLGLexer.h" 00031 #include "ATokenBuffer.h" 00032 #include "ATokPtr.h" 00033 using namespace std ; 00034 using namespace OMK ; 00035 00036 00037 MultipleConfigurationParameter::MultipleConfigurationParameter(const MultipleConfigurationParameter & orig ) : 00038 ConfigurationParameterDescriptor ( orig ), 00039 _subDescriptors () 00040 { 00041 if (orig._associatedStringValid) 00042 { 00043 _associatedStringValid = true ; 00044 _associatedString= orig._associatedString ; 00045 } 00046 else 00047 { 00048 _associatedStringValid = false ; 00049 } 00050 00051 //reconstruct the data containers after having duplicated the subdescriptors 00052 FastAccessContainer::const_iterator j ; 00053 for (DataContainerType::const_iterator i = orig._subDescriptors.begin() ; 00054 i != orig._subDescriptors.end() ; 00055 ++i) 00056 { 00057 ConfigurationParameterDescriptor * subdescriptorCopy = (*i).second->clone() ; 00058 _subDescriptors.push_back( DataContainerType::value_type( (*i).first, 00059 subdescriptorCopy ) 00060 ) ; 00061 00062 j = orig._hashtable.find ((*i).first ) ; 00063 if ( j != orig._hashtable.end() ) 00064 { 00065 _hashtable.insert ( FastAccessContainer::value_type ( (*i).first, 00066 subdescriptorCopy ) ) ; 00067 } 00068 } 00069 } 00070 00071 00072 ConfigurationParameterDescriptor * MultipleConfigurationParameter::clone() const 00073 { 00074 return new MultipleConfigurationParameter( *this ) ; 00075 } 00076 00077 00078 00079 00080 MultipleConfigurationParameter::MultipleConfigurationParameter( ) : 00081 ConfigurationParameterDescriptor (), 00082 _subDescriptors (), 00083 _associatedStringValid ( false ) 00084 { 00085 00086 } 00087 00088 MultipleConfigurationParameter::~MultipleConfigurationParameter() 00089 { 00090 DataContainerType::iterator i ; 00091 for ( i = _subDescriptors.begin() ; 00092 i != _subDescriptors.end() ; 00093 i++ ) 00094 { 00095 OMTRACEID("Deleting", i->first << " Config* "); 00096 if ( i->second) 00097 delete i->second ; 00098 } 00099 } 00100 00101 00102 00103 const std::string & MultipleConfigurationParameter::getAssociatedString () const 00104 { 00105 if ( ! _associatedStringValid ) 00106 { 00107 string emptyString ; 00108 int numberOfSubItems = getNumberOfSubItems() ; 00109 _associatedString = "{ \n" ; 00110 DataContainerType::const_iterator i = _subDescriptors.begin() ; 00111 for ( int j = 0 ; 00112 j < numberOfSubItems - 1; 00113 ++j ) 00114 { 00115 //indent the subdescriptors 00116 _associatedString += " " ; 00117 if ( i-> first != emptyString ) 00118 { 00119 _associatedString += i-> first ; 00120 _associatedString += " = " ; 00121 } 00122 _associatedString += i->second->getAssociatedString() ; 00123 _associatedString += ",\n" ; 00124 i++ ; 00125 } 00126 //print the last item 00127 _associatedString += " " ; 00128 if ( i-> first != emptyString ) 00129 { 00130 _associatedString += i-> first ; 00131 _associatedString += " = " ; 00132 } 00133 _associatedString += i->second->getAssociatedString() ; 00134 _associatedString += "\n" ; 00135 00136 00137 _associatedString += "}" ; 00138 } 00139 _associatedStringValid = true ; 00140 return _associatedString ; 00141 } 00142 00143 00144 void MultipleConfigurationParameter::printToStream (ostream & out, int offset ) const 00145 { 00146 std::string emptyString ; 00147 int numberOfSubItems = getNumberOfSubItems() ; 00148 00149 DataContainerType::const_iterator i = _subDescriptors.begin() ; 00150 if ( i != _subDescriptors.end () ) 00151 { 00152 bool inList ; 00153 if ( i->first == emptyString ) 00154 { 00155 if ( ! _hashtable.empty() ) 00156 { 00157 out<<endl; 00158 printTabToStream(out,offset) ; 00159 out << "{" ; 00160 out<<endl; 00161 printTabToStream(out,offset+2) ; 00162 } 00163 out << "[ " ; 00164 inList = true ; 00165 } 00166 else 00167 { 00168 out<<endl; 00169 printTabToStream(out,offset) ; 00170 out << "{" ; 00171 inList = false ; 00172 } 00173 00174 for ( int j = 0 ; 00175 j != numberOfSubItems ; 00176 ++j ) 00177 { 00178 //at each step, if not printing a list of unamed values, new line has just been inserted 00179 if ( inList ) 00180 { 00181 if ( i-> first != emptyString ) 00182 { 00183 // the list was finished by the preceeding value 00184 out << "]" ; 00185 inList = false ; 00186 out<<endl; 00187 printTabToStream (out, offset + 2 ) ; 00188 out<< i->first <<" "; 00189 i->second->printToStream (out, offset + 2 ) ; 00190 } 00191 else 00192 { 00193 i->second->printToStream (out, offset + 4 ) ; 00194 out<<" "; 00195 } 00196 } 00197 else 00198 { 00199 if ( i-> first == emptyString ) 00200 { 00201 //starting a list 00202 out << "[ " ; 00203 inList = true ; 00204 i->second->printToStream (out, offset + 4 ) ; 00205 out<<" "; 00206 } 00207 else 00208 { 00209 out << endl ; 00210 printTabToStream (out, offset + 2 ) ; 00211 out<< i->first <<" " ; 00212 i->second->printToStream (out, offset + 4 ) ; 00213 } 00214 } 00215 i++ ; 00216 } 00217 if (inList) 00218 { 00219 out<<"]"; 00220 if ( ! _hashtable.empty() ) 00221 { 00222 out<<endl; 00223 printTabToStream (out, offset ) ; 00224 out<<"}"; 00225 } 00226 } 00227 else 00228 { 00229 out<<endl; 00230 printTabToStream (out, offset ) ; 00231 out<<"}"; 00232 } 00233 } 00234 else 00235 { 00236 out<<" - undefined - "; 00237 } 00238 } 00239 00240 00241 00242 const ConfigurationParameterDescriptor * MultipleConfigurationParameter::getSubDescriptorByName (const std::string & descriptorName ) const 00243 { 00244 ConfigurationParameterDescriptor * result = NULL ; 00245 FastAccessContainer::const_iterator i ; 00246 i = _hashtable.find (descriptorName) ; 00247 if ( i != _hashtable.end() ) 00248 { 00249 result = i->second ; 00250 OMASSERT( result != NULL ) ; 00251 } 00252 else 00253 { 00254 result = NULL ; 00255 } 00256 return result ; 00257 } 00258 00259 00260 ConfigurationParameterDescriptor * MultipleConfigurationParameter::getSubDescriptorByName (const std::string & descriptorName ) 00261 { 00262 ConfigurationParameterDescriptor * result = NULL ; 00263 FastAccessContainer::const_iterator i ; 00264 i = _hashtable.find (descriptorName) ; 00265 if ( i != _hashtable.end() ) 00266 { 00267 result = i->second ; 00268 OMASSERT( result != NULL ) ; 00269 } 00270 else 00271 { 00272 result = NULL ; 00273 } 00274 return result ; 00275 } 00276 00277 00278 00279 const ConfigurationParameterDescriptor * MultipleConfigurationParameter::getSubDescriptorByPosition (int descriptorPosition) const 00280 { 00281 if (((unsigned)descriptorPosition) >= _subDescriptors.size() ) 00282 { 00283 return NULL ; 00284 } 00285 else 00286 { 00287 OMASSERT( _subDescriptors[descriptorPosition].second != NULL ) ; 00288 return _subDescriptors[descriptorPosition].second ; 00289 } 00290 } 00291 00292 const std::string & MultipleConfigurationParameter::getNameOfSubDescriptor (int descriptorPosition) const 00293 { 00294 OMASSERT( ((unsigned)descriptorPosition) < _subDescriptors.size() ) ; 00295 return _subDescriptors[descriptorPosition].first ; 00296 } 00297 00298 00299 ConfigurationParameterDescriptor * MultipleConfigurationParameter::getSubDescriptorByPosition (int descriptorPosition) 00300 { 00301 if ( ((unsigned)descriptorPosition) >= _subDescriptors.size() ) 00302 { 00303 return NULL ; 00304 } 00305 else 00306 { 00307 OMASSERT( _subDescriptors[descriptorPosition].second != NULL ) ; 00308 return _subDescriptors[descriptorPosition].second ; 00309 } 00310 } 00311 00312 00313 00314 int MultipleConfigurationParameter::getNumberOfSubItems () const 00315 { 00316 return _subDescriptors.size() ; 00317 } 00318 00319 00320 00321 int MultipleConfigurationParameter::appendSubDescriptor(ConfigurationParameterDescriptor * subDescriptor ) 00322 { 00323 OMASSERT( subDescriptor != NULL ) ; 00324 _subDescriptors.push_back ( DataContainedType ( std::string() ,subDescriptor ) ) ; 00325 return _subDescriptors.size() - 1 ; 00326 } 00327 00328 00329 00330 int MultipleConfigurationParameter::appendSubDescriptorNamed (const std::string & descriptorName, ConfigurationParameterDescriptor * subDescriptor ) 00331 { 00332 OMASSERT( subDescriptor != NULL ) ; 00333 pair<FastAccessContainer::iterator ,bool > result = _hashtable.insert( FastAccessContainer::value_type(descriptorName, subDescriptor) ) ; 00334 // only add the sub descriptor if no subsdescriptor with the same name was allready added 00335 if (result.second) 00336 { 00337 _subDescriptors.push_back ( DataContainedType ( descriptorName, subDescriptor ) ) ; 00338 } 00339 else 00340 { 00341 replaceSubDescriptorNamed ( descriptorName, subDescriptor ) ; 00342 } 00343 return _subDescriptors.size() - 1 ; 00344 } 00345 00346 00347 int MultipleConfigurationParameter::appendSubDescriptorsOf (const MultipleConfigurationParameter & otherDescriptor) 00348 { 00349 if ( _hashtable.size() == 0 ) 00350 { 00351 *this = otherDescriptor ; 00352 } 00353 else 00354 { 00355 std::string emptyString ; 00356 for ( DataContainerType::const_iterator i = otherDescriptor._subDescriptors.begin() ; 00357 i != otherDescriptor._subDescriptors.end() ; 00358 ++i) 00359 { 00360 //_subDescriptors.push_back ( *i ) ; 00361 if ( (*i).first != emptyString ) 00362 { 00363 ConfigurationParameterDescriptor * existingDescriptor = getSubDescriptorByName ((*i).first) ; 00364 if ( existingDescriptor == NULL ) 00365 { 00366 ConfigurationParameterDescriptor * newDescriptor = (*i).second->clone() ; 00367 _hashtable.insert( FastAccessContainer::value_type ( (*i).first , newDescriptor ) ) ; 00368 _subDescriptors.push_back( DataContainedType((*i).first,newDescriptor) ) ; 00369 } 00370 else 00371 { 00372 ostringstream warningMessage ; 00373 warningMessage << "While merging two MultipleConfigurationParameter: " 00374 <<"2 subdescriptors named "<<(*i).first<<" found: "; 00375 00376 MultipleConfigurationParameter * originalDescriptor ; 00377 originalDescriptor = dynamic_cast<MultipleConfigurationParameter * > (existingDescriptor) ; 00378 MultipleConfigurationParameter * appendedDescriptor ; 00379 appendedDescriptor = dynamic_cast<MultipleConfigurationParameter * > ((*i).second) ; 00380 if ( (originalDescriptor != NULL) && 00381 (appendedDescriptor != NULL) ) 00382 { 00383 if ( originalDescriptor->_hashtable.size() == 0 ) 00384 { 00385 //it is a list of values, replace 00386 replaceSubDescriptorNamed ((*i).first, appendedDescriptor) ; 00387 } 00388 else 00389 { 00390 originalDescriptor->appendSubDescriptorsOf ( *appendedDescriptor ) ; 00391 } 00392 } 00393 else 00394 { 00395 _hashtable.erase ( (*i).first ) ; 00396 00397 //find (*i).first in the vector 00398 DataContainerType::iterator j = _subDescriptors.begin() ; 00399 00400 //(*i).first should be present in the vector 00401 OMASSERT( j != _subDescriptors.end() ); 00402 while ( (*j).first != (*i).first ) 00403 { 00404 ++j ; 00405 OMASSERT( j != _subDescriptors.end() ); 00406 } 00407 00408 ConfigurationParameterDescriptor * newDescriptor = (*i).second->clone() ; 00409 00410 _hashtable.insert( FastAccessContainer::value_type ( (*i).first , newDescriptor ) ) ; 00411 00412 (*j).second = newDescriptor ; 00413 00414 warningMessage<<"Replaced original value: that was "<<existingDescriptor->getAssociatedString() ; 00415 OMASSERT( existingDescriptor != NULL ) ; 00416 delete existingDescriptor ; 00417 } 00418 OMTRACEID( OMK_DEBUG_OMK_OBJ, warningMessage.str() ) ; 00419 } 00420 } 00421 } 00422 } 00423 return _subDescriptors.size() - 1 ; 00424 } 00425 00426 00427 00428 int MultipleConfigurationParameter::replaceSubDescriptorNamed (const std::string & descriptorName, ConfigurationParameterDescriptor * subDescriptor) 00429 { 00430 FastAccessContainer::iterator i ; 00431 i = _hashtable.find (descriptorName) ; 00432 if ( i == _hashtable.end() ) 00433 { 00434 return appendSubDescriptorNamed ( descriptorName, subDescriptor) ; 00435 } 00436 else 00437 { 00438 int result = 0 ; 00439 00440 //find descriptorName in the vector 00441 DataContainerType::iterator j = _subDescriptors.begin() ; 00442 00443 //It should be present 00444 OMASSERT( j != _subDescriptors.end() ); 00445 00446 while ( (*j).first != descriptorName ) 00447 { 00448 ++j ; 00449 ++result ; 00450 //descriptorName should be present 00451 OMASSERT( j != _subDescriptors.end() ); 00452 } 00453 00454 //delete the old descriptor 00455 delete (*i).second ; 00456 00457 //replace it 00458 (*i).second = subDescriptor ; 00459 (*j).second = subDescriptor ; 00460 00461 OMASSERT(_subDescriptors[result].second == subDescriptor) ; 00462 return result ; 00463 } 00464 } 00465 00466 00467 int MultipleConfigurationParameter::replaceSubDescriptor (int position, ConfigurationParameterDescriptor * subDescriptor) 00468 { 00469 OMASSERT( ((unsigned)position) < _subDescriptors.size() ) ; 00470 std::string descriptorName = _subDescriptors[position].first ; 00471 std::string emptyString ; 00472 ConfigurationParameterDescriptor * replacedDescription = _subDescriptors[position].second ; 00473 00474 if ( descriptorName != emptyString ) 00475 { 00476 FastAccessContainer::iterator i ; 00477 i = _hashtable.find (descriptorName) ; 00478 00479 //the descriptor should be present in the hash table 00480 OMASSERT( i != _hashtable.end() ) ; 00481 (*i).second = subDescriptor ; 00482 } 00483 delete replacedDescription ; 00484 _subDescriptors[position].second = subDescriptor ; 00485 return position ; 00486 } 00487 00488 00489 00490 void MultipleConfigurationParameter::pack ( OutgoingSynchronisationMessage & out) const 00491 { 00492 ostringstream char_out ; 00493 printToStream ( char_out, 0 ) ; 00494 out << char_out.str() ; 00495 } 00496 00497 void MultipleConfigurationParameter::unpack ( IncomingSynchronisationMessage & in) 00498 { 00499 _hashtable.clear () ; 00500 _subDescriptors.clear () ; 00501 00502 string messageBuffer ; 00503 in >> messageBuffer ; 00504 istringstream message ( messageBuffer ) ; 00505 extract (message ) ; 00506 00507 // //this doesn't work, because a IncomingSynchronisationMessage dosn't known the size of what has to be extracted 00508 // KernelIstreamLexer<IncomingSynchronisationMessage> inputStream (in) ; 00509 // DLGLexer scan( & inputStream ) ; 00510 // ANTLRTokenBuffer pipe ( & scan ) ; 00511 // ANTLRTokenPtr aToken = new ANTLRToken() ; 00512 // scan.setToken(mytoken(aToken)) ; 00513 // genericKernelParser extractor ( &pipe ) ; 00514 // extractor.init() ; 00515 // extractor.multipleValue ( this ) ; 00516 } 00517 00518 void MultipleConfigurationParameter::insertInStream (ostream & out) const 00519 { 00520 printToStream ( out, 0 ) ; 00521 } 00522 00523 void MultipleConfigurationParameter::extract (istream & in) 00524 { 00525 _hashtable.clear () ; 00526 _subDescriptors.clear () ; 00527 00528 KernelIstreamLexer<istream> inputStream (in) ; 00529 DLGLexer scan( & inputStream ) ; 00530 ANTLRTokenBuffer pipe ( & scan ) ; 00531 ANTLRTokenPtr aToken = new ANTLRToken() ; 00532 scan.setToken(mytoken(aToken)) ; 00533 genericKernelParser extractor ( &pipe ) ; 00534 extractor.init() ; 00535 extractor.multipleValue ( this ) ; 00536 }
Documentation generated on Mon Jun 9 11:45:56 2008 |
Generated with doxygen by Dimitri van Heesch , 1997-2007 |