00001 00002 /************************************************************************/ 00003 /* This file is part of openMask(c) INRIA, CNRS, Universite de Rennes 1 */ 00004 /* 1993-2002, thereinafter the Software */ 00005 /* */ 00006 /* The Software has been developped within the Siames Project. */ 00007 /* INRIA, the University of Rennes 1 and CNRS jointly hold intellectual */ 00008 /* property rights */ 00009 /* */ 00010 /* The Software has been registered with the Agence pour la Protection */ 00011 /* des Programmes (APP) under registration number */ 00012 /* IDDN.FR.001.510008.00.S.P.2001.000.41200 */ 00013 /* */ 00014 /* This file may be distributed under the terms of the Q Public License */ 00015 /* version 1.0 as defined by Trolltech AS of Norway and appearing in */ 00016 /* the file LICENSE.QPL included in the packaging of this file. */ 00017 /* */ 00018 /* Licensees holding valid specific licenses issued by INRIA, CNRS or */ 00019 /* Universite Rennes 1 for the software may use this file in */ 00020 /* acordance with that specific license */ 00021 /************************************************************************/ 00022 00023 #include "OMKExtensionManager.h" 00024 #include "OMKExtension.h" 00025 #include "OMKIAttribute.h" 00026 #include "OMKMultipleConfigurationParameter.h" 00027 #include "OMKParametersAccessor.inl" 00028 #include <vector> 00029 #include <string> 00030 #include <iostream> 00031 00032 namespace OMK 00033 { 00034 //----------------------------------------------------------------- 00035 00036 // Default constructor 00037 ExtensionManager::ExtensionManager() 00038 { 00039 } 00040 00041 //----------------------------------------------------------------- 00042 00043 // Destructor 00044 ExtensionManager::~ExtensionManager() 00045 { 00046 } 00047 //----------------------------------------------------------------- 00048 void ExtensionManager::createExtensionsFromParameters( ExtensibleSimulatedObject* owner, const ConfigurationParameterDescriptor * node ) 00049 { 00050 // Retrieve the extension fields to create the extensions which defined a "Class" 00051 if( node ) 00052 { 00053 int nbItems = node->getNumberOfSubItems() ; 00054 for( int i = 0 ; i < nbItems ; ++i ) 00055 { 00056 // Retrieves parameters sets for each extension 00057 Name id( static_cast<const MultipleConfigurationParameter *>( node )->getNameOfSubDescriptor( i ) ) ; 00058 const ConfigurationParameterDescriptor * extensionParam = node->getSubDescriptorByPosition( i ) ; 00059 // Try to retrieve the class id to create the extension 00060 std::string classId ; 00061 if( ParametersAccessor::get( extensionParam, "Class", classId ) ) 00062 { // ClassId is ok => Create the extension 00063 createAndAddExtension( owner, classId, id ) ; 00064 } 00065 } 00066 } 00067 } 00068 //----------------------------------------------------------------- 00069 00070 // finish 00071 void ExtensionManager::deleteExtensions() 00072 { 00073 // Destroy every extensions 00074 for( ExtensionStore::iterator i = _extensions.begin() ; i != _extensions.end() ; i++ ) 00075 { 00076 delete i->second ; 00077 } 00078 _extensions.clear() ; 00079 _orderedExtensionsForLoadParameters.clear() ; 00080 _orderedExtensionsForPreCompute.clear() ; 00081 _orderedExtensionsForPostCompute.clear() ; 00082 } 00083 //----------------------------------------------------------------- 00084 00085 // Initialisation 00086 void ExtensionManager::loadOrder( const ConfigurationParameterDescriptor* node, 00087 const std::string& name, 00088 ExtensionList& extensionsList ) 00089 { 00090 std::vector< Name > order ; 00091 if( ParametersAccessor::get( node, name, order ) ) 00092 { 00093 OMASSERTM( extensionsList.size() == order.size(), "The list \"" << name << "\" has not the good size" ) ; 00094 extensionsList.clear() ; 00095 for( size_t i = 0 ; i < order.size() ; i++ ) 00096 { 00097 ExtensionStore::const_iterator iterator = _extensions.find( order[ i ] ) ; 00098 OMASSERTM( iterator != _extensions.end(), "Cannot find the extension \"" << order[ i ] << "\"" ) ; 00099 extensionsList.push_back( iterator->second ) ; 00100 } 00101 } 00102 } 00103 00104 //----------------------------------------------------------------- 00105 00106 void ExtensionManager::preComputeExtension() 00107 { 00108 // Call the extensions which work on inputs parameters 00109 for( ExtensionList::iterator i = _orderedExtensionsForPreCompute.begin() ; i != _orderedExtensionsForPreCompute.end(); ) 00110 { 00111 // should pass to the next extension because maybe the current extension will be self destruct ! 00112 ExtensionList::iterator current = i ; 00113 i++ ; 00114 (*current)->preComputeParameters() ; 00115 } 00116 } 00117 //----------------------------------------------------------------- 00118 00119 void ExtensionManager::postComputeExtension() 00120 { 00121 // Call the extensions which work on outputs parameters 00122 for( ExtensionList::iterator i = _orderedExtensionsForPostCompute.begin() ; i != _orderedExtensionsForPostCompute.end(); ) 00123 { 00124 // should pass to the next extension because maybe the current extension will be self destruct ! 00125 ExtensionList::iterator current = i ; 00126 i++ ; 00127 (*current)->postComputeParameters() ; 00128 } 00129 } 00130 00131 //----------------------------------------------------------------- 00132 bool ExtensionManager::loadFromExtensionsParameters( const ConfigurationParameterDescriptor* extensionNode ) 00133 { 00134 bool ok = true ; 00135 if( extensionNode ) 00136 { 00137 // Retrieve the each extension fields to configure the extension 00138 for( ExtensionList::iterator i = _orderedExtensionsForLoadParameters.begin() ; i != _orderedExtensionsForLoadParameters.end(); i++ ) 00139 { 00140 const ConfigurationParameterDescriptor* subExtensionNode = 0 ; 00141 ParametersAccessor::get( extensionNode, (*i)->getId().getCString(), subExtensionNode ) ; 00142 // call the extension parameter loader 00143 ok = (*i)->loadExtensionParameters( subExtensionNode ) && ok ; 00144 } 00145 } 00146 00147 return ok ; 00148 } 00149 //----------------------------------------------------------------- 00150 bool ExtensionManager::loadFromObjectParameters( const ConfigurationParameterDescriptor* node ) 00151 { 00152 bool ok = true ; 00153 // Load object parameters for the extensions 00154 for( ExtensionList::iterator i = _orderedExtensionsForLoadParameters.begin() ; i != _orderedExtensionsForLoadParameters.end() ; ) 00155 { 00156 // should pass to the next extension because maybe the current extension will be self destruct ! 00157 ExtensionList::iterator current = i ; 00158 i++ ; 00159 ok = (*current)->loadObjectParameters( node ) && ok ; 00160 } 00161 00162 return ok ; 00163 } 00164 //----------------------------------------------------------------- 00165 void ExtensionManager::addExtension( Extension* extension ) 00166 { 00167 // return value 00168 std::string errorMsg ; 00169 bool ok = false ; 00170 if( extension ) 00171 { 00172 // Check for duplicate extension 00173 ExtensionStore::iterator i = _extensions.find( extension->getId() ); 00174 if( i == _extensions.end() ) 00175 { 00176 // It is a really new item, adds it in the main list 00177 _extensions[ extension->getId() ] = extension ; 00178 _orderedExtensionsForLoadParameters.push_back( extension ) ; 00179 _orderedExtensionsForPreCompute .push_back( extension ) ; 00180 _orderedExtensionsForPostCompute .push_back( extension ) ; 00181 ok = true ; 00182 } 00183 else 00184 { 00185 // Duplicate Item 00186 errorMsg = "The extension \"" ; 00187 errorMsg += extension->getId().getString() ; 00188 errorMsg += "\" is a duplicate one" ; 00189 } 00190 } 00191 else 00192 { 00193 errorMsg = "No extension to add" ; 00194 } 00195 // Unable to add the extension => error message 00196 if( !ok ) 00197 { 00198 OMTRACEID( OMK_DEBUG_OMK_EXT, 00199 "Error to add extension" << std::endl 00200 << ">>> :-( " << errorMsg << "." << std::endl ) ; 00201 } 00202 } 00203 //----------------------------------------------------------------- 00204 Extension* ExtensionManager::removeExtension( const Name& id ) 00205 { 00206 Extension* extension = 0 ; 00207 // look for the extension 00208 ExtensionStore::iterator i = _extensions.find( id ) ; 00209 if( i != _extensions.end() ) 00210 { 00211 extension = i->second ; 00212 // remove the extension 00213 _extensions.erase( i ) ; 00214 _orderedExtensionsForLoadParameters.remove( extension ) ; 00215 _orderedExtensionsForPreCompute.remove( extension ) ; 00216 _orderedExtensionsForPostCompute.remove( extension ) ; 00217 OMTRACEID( OMK_DEBUG_OMK_EXEC, "Information for " << debugMsg( extension, 0 ) << std::endl 00218 << ">>> :-) removed from object" ) ; 00219 } 00220 else 00221 { 00222 // unknow Item 00223 OMTRACEID( OMK_DEBUG_OMK_EXT, 00224 "Unable to remove the extension from" << std::endl 00225 << ">>> :-( The extension \"" << id << "\" is not an extension of this object." << std::endl ) ; 00226 } 00227 return extension ; 00228 } 00229 //----------------------------------------------------------------- 00230 Extension* ExtensionManager::getExtension( const Name& id ) 00231 { 00232 Extension* extension = 0 ; 00233 // look for the extension 00234 ExtensionStore::iterator i = _extensions.find( id ) ; 00235 if( i != _extensions.end() ) 00236 { 00237 extension = i->second ; 00238 } 00239 else 00240 { 00241 // unknow Item 00242 OMTRACEID( OMK_DEBUG_OMK_EXT, 00243 "Unable to find the extension" << std::endl 00244 << ">>> :-( The extension \"" << id << "\" is not an extension of this object." << std::endl ) ; 00245 } 00246 return extension ; 00247 } 00248 //----------------------------------------------------------------- 00249 Extension* ExtensionManager::createAndAddExtension( ExtensibleSimulatedObject* owner, const Name& classId, const Name& id ) 00250 { 00251 Extension *extension = ExtensionFactory::getInstance().create( classId )( owner, id, false ) ; 00252 if( extension ) 00253 { 00254 addExtension( extension ) ; 00255 OMTRACEID( OMK_DEBUG_OMK_EXEC, "Information for " << debugMsg( extension ) << std::endl 00256 << ">>> :-) Created" ) ; 00257 } 00258 else 00259 { 00260 OMTRACEID( OMK_DEBUG_OMK_EXT, 00261 "Error to create extension" << std::endl 00262 << ">>> :-( Unable to create the extension \"" << id << "\" with the class id \"" << classId << "\"." << std::endl ) ; 00263 } 00264 return extension ; 00265 } 00266 //----------------------------------------------------------------- 00267 00268 }// namespace OMK
Documentation generated on Mon Jun 9 11:45:56 2008 |
Generated with doxygen by Dimitri van Heesch , 1997-2007 |