| David Wagner | b76c9d6 | 2014-02-05 18:30:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011-2014, Intel Corporation |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without modification, |
| 6 | * are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, this |
| 9 | * list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | * this list of conditions and the following disclaimer in the documentation and/or |
| 13 | * other materials provided with the distribution. |
| 14 | * |
| 15 | * 3. Neither the name of the copyright holder nor the names of its contributors |
| 16 | * may be used to endorse or promote products derived from this software without |
| 17 | * specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| 23 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 26 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 29 | */ |
| 30 | #include "DomainConfiguration.h" |
| 31 | #include "AreaConfiguration.h" |
| 32 | #include "ConfigurableElement.h" |
| 33 | #include "CompoundRule.h" |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 34 | #include "Subsystem.h" |
| 35 | #include "XmlDomainSerializingContext.h" |
| 36 | #include "ConfigurationAccessContext.h" |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 37 | #include <assert.h> |
| Patrick Benavoli | 0bd5054 | 2011-11-29 11:10:27 +0100 | [diff] [blame] | 38 | #include "RuleParser.h" |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 39 | |
| 40 | #define base CBinarySerializableElement |
| 41 | |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame^] | 42 | using std::string; |
| 43 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 44 | CDomainConfiguration::CDomainConfiguration(const string& strName) : base(strName) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | CDomainConfiguration::~CDomainConfiguration() |
| 49 | { |
| 50 | AreaConfigurationListIterator it; |
| 51 | |
| 52 | for (it = _areaConfigurationList.begin(); it != _areaConfigurationList.end(); ++it) { |
| 53 | |
| 54 | delete *it; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Class kind |
| 59 | string CDomainConfiguration::getKind() const |
| 60 | { |
| 61 | return "Configuration"; |
| 62 | } |
| 63 | |
| 64 | // Child dynamic creation |
| 65 | bool CDomainConfiguration::childrenAreDynamic() const |
| 66 | { |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | // XML configuration settings parsing |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 71 | bool CDomainConfiguration::parseSettings(CXmlElement& xmlConfigurationSettingsElement, CXmlSerializingContext& serializingContext) |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 72 | { |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 73 | // Actual XML context |
| 74 | CXmlDomainSerializingContext& xmlDomainSerializingContext = static_cast<CXmlDomainSerializingContext&>(serializingContext); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 75 | |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 76 | // Take care of configurable elements / area configurations ranks |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame^] | 77 | std::list<CAreaConfiguration*> areaConfigurationList; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 78 | |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 79 | // Parse configurable element's configuration settings |
| 80 | CXmlElement::CChildIterator it(xmlConfigurationSettingsElement); |
| 81 | |
| 82 | CXmlElement xmlConfigurableElementSettingsElement; |
| 83 | |
| 84 | while (it.next(xmlConfigurableElementSettingsElement)) { |
| 85 | |
| 86 | // Retrieve area configuration |
| 87 | string strConfigurableElementPath = xmlConfigurableElementSettingsElement.getAttributeString("Path"); |
| 88 | |
| 89 | CAreaConfiguration* pAreaConfiguration = findAreaConfiguration(strConfigurableElementPath); |
| 90 | |
| 91 | if (!pAreaConfiguration) { |
| 92 | |
| 93 | xmlDomainSerializingContext.setError("Configurable Element " + strConfigurableElementPath + " referred to by Configuration " + getPath() + " not associated to Domain"); |
| 94 | |
| 95 | return false; |
| 96 | } |
| 97 | // Ranks |
| 98 | areaConfigurationList.push_back(pAreaConfiguration); |
| 99 | |
| 100 | // Parse |
| 101 | if (!serializeConfigurableElementSettings(pAreaConfiguration, xmlConfigurableElementSettingsElement, xmlDomainSerializingContext, false)) { |
| 102 | |
| 103 | return false; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // Reorder area configurations according to XML content |
| 108 | reorderAreaConfigurations(areaConfigurationList); |
| 109 | |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | // XML configuration settings composing |
| 114 | void CDomainConfiguration::composeSettings(CXmlElement& xmlConfigurationSettingsElement, CXmlSerializingContext& serializingContext) const |
| 115 | { |
| 116 | // Go through all are configurations |
| 117 | AreaConfigurationListIterator it; |
| 118 | |
| 119 | for (it = _orderedAreaConfigurationList.begin(); it != _orderedAreaConfigurationList.end(); ++it) { |
| 120 | |
| 121 | const CAreaConfiguration* pAreaConfiguration = *it; |
| 122 | |
| 123 | // Retrieve configurable element |
| 124 | const CConfigurableElement* pConfigurableElement = pAreaConfiguration->getConfigurableElement(); |
| 125 | |
| 126 | // Create configurable element child element |
| 127 | CXmlElement xmlConfigurableElementSettingsElement; |
| 128 | |
| 129 | xmlConfigurationSettingsElement.createChild(xmlConfigurableElementSettingsElement, "ConfigurableElement"); |
| 130 | |
| 131 | // Set Path attribute |
| 132 | xmlConfigurableElementSettingsElement.setAttributeString("Path", pConfigurableElement->getPath()); |
| 133 | |
| 134 | // Delegate composing to area configuration |
| 135 | ((CDomainConfiguration&)(*this)).serializeConfigurableElementSettings((CAreaConfiguration*)pAreaConfiguration, xmlConfigurableElementSettingsElement, serializingContext, true); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // Serialize one configuration for one configurable element |
| 140 | bool CDomainConfiguration::serializeConfigurableElementSettings(CAreaConfiguration* pAreaConfiguration, CXmlElement& xmlConfigurableElementSettingsElement, CXmlSerializingContext& serializingContext, bool bSerializeOut) |
| 141 | { |
| 142 | // Actual XML context |
| 143 | CXmlDomainSerializingContext& xmlDomainSerializingContext = static_cast<CXmlDomainSerializingContext&>(serializingContext); |
| 144 | |
| 145 | // Configurable Element |
| 146 | const CConfigurableElement* pConfigurableElement = pAreaConfiguration->getConfigurableElement(); |
| 147 | |
| 148 | // Element content |
| 149 | CXmlElement xmlConfigurableElementSettingsElementContent; |
| 150 | |
| 151 | // Deal with element itself |
| 152 | if (!bSerializeOut) { |
| 153 | |
| 154 | // Check structure |
| 155 | if (xmlConfigurableElementSettingsElement.getNbChildElements() != 1) { |
| 156 | |
| 157 | // Structure error |
| 158 | serializingContext.setError("Struture error encountered while parsing settings of " + pConfigurableElement->getKind() + " " + pConfigurableElement->getName() + " in Configuration " + getPath()); |
| 159 | |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | // Check name and kind |
| 164 | if (!xmlConfigurableElementSettingsElement.getChildElement(pConfigurableElement->getKind(), pConfigurableElement->getName(), xmlConfigurableElementSettingsElementContent)) { |
| 165 | |
| 166 | serializingContext.setError("Couldn't find settings for " + pConfigurableElement->getKind() + " " + pConfigurableElement->getName() + " for Configuration " + getPath()); |
| 167 | |
| 168 | return false; |
| 169 | } |
| 170 | } else { |
| 171 | |
| 172 | // Create child XML element |
| 173 | xmlConfigurableElementSettingsElement.createChild(xmlConfigurableElementSettingsElementContent, pConfigurableElement->getKind()); |
| 174 | |
| 175 | // Set Name |
| 176 | xmlConfigurableElementSettingsElementContent.setNameAttribute(pConfigurableElement->getName()); |
| 177 | } |
| 178 | |
| 179 | // Change context type to parameter settings access |
| 180 | string strError; |
| 181 | |
| 182 | // Create configuration access context |
| 183 | CConfigurationAccessContext configurationAccessContext(strError, bSerializeOut); |
| 184 | |
| 185 | // Provide current value space |
| 186 | configurationAccessContext.setValueSpaceRaw(xmlDomainSerializingContext.valueSpaceIsRaw()); |
| 187 | |
| 188 | // Provide current output raw format |
| 189 | configurationAccessContext.setOutputRawFormat(xmlDomainSerializingContext.outputRawFormatIsHex()); |
| 190 | |
| 191 | // Get subsystem |
| 192 | const CSubsystem* pSubsystem = pConfigurableElement->getBelongingSubsystem(); |
| 193 | |
| 194 | if (pSubsystem && pSubsystem != pConfigurableElement) { |
| 195 | |
| 196 | // Element is a descendant of subsystem |
| 197 | |
| 198 | // Deal with Endianness |
| 199 | configurationAccessContext.setBigEndianSubsystem(pSubsystem->isBigEndian()); |
| 200 | } |
| 201 | |
| 202 | // Have domain configuration parse settings for configurable element |
| 203 | if (!pAreaConfiguration->serializeXmlSettings(xmlConfigurableElementSettingsElementContent, configurationAccessContext)) { |
| 204 | |
| 205 | // Forward error |
| 206 | xmlDomainSerializingContext.setError(strError); |
| 207 | |
| 208 | return false; |
| 209 | } |
| 210 | return true; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // Configurable Elements association |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 214 | void CDomainConfiguration::addConfigurableElement(const CConfigurableElement* pConfigurableElement, const CSyncerSet* pSyncerSet) |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 215 | { |
| Frédéric Boisnard | 9620e44 | 2012-05-30 16:15:02 +0200 | [diff] [blame] | 216 | CAreaConfiguration* pAreaConfiguration = pConfigurableElement->createAreaConfiguration(pSyncerSet); |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 217 | |
| 218 | _areaConfigurationList.push_back(pAreaConfiguration); |
| 219 | _orderedAreaConfigurationList.push_back(pAreaConfiguration); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void CDomainConfiguration::removeConfigurableElement(const CConfigurableElement* pConfigurableElement) |
| 223 | { |
| 224 | CAreaConfiguration* pAreaConfigurationToRemove = getAreaConfiguration(pConfigurableElement); |
| 225 | |
| 226 | _areaConfigurationList.remove(pAreaConfigurationToRemove); |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 227 | _orderedAreaConfigurationList.remove(pAreaConfigurationToRemove); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 228 | |
| 229 | delete pAreaConfigurationToRemove; |
| 230 | } |
| 231 | |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 232 | // Sequence management |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame^] | 233 | bool CDomainConfiguration::setElementSequence(const std::vector<string>& astrNewElementSequence, string& strError) |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 234 | { |
| 235 | // Build a new list of AreaConfiguration objects |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame^] | 236 | std::list<CAreaConfiguration*> areaConfigurationList; |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 237 | |
| 238 | uint32_t uiConfigurableElement; |
| 239 | |
| 240 | for (uiConfigurableElement = 0; uiConfigurableElement < astrNewElementSequence.size(); uiConfigurableElement++) { |
| 241 | |
| 242 | string strConfigurableElementPath = astrNewElementSequence[uiConfigurableElement]; |
| 243 | |
| 244 | CAreaConfiguration* pAreaConfiguration = findAreaConfiguration(strConfigurableElementPath); |
| 245 | |
| 246 | if (!pAreaConfiguration) { |
| 247 | |
| 248 | strError = "Element " + strConfigurableElementPath + " not found in domain"; |
| 249 | |
| 250 | return false; |
| 251 | } |
| 252 | // Check not already present in the list |
| 253 | if (findAreaConfiguration(strConfigurableElementPath, areaConfigurationList)) { |
| 254 | |
| 255 | strError = "Element " + strConfigurableElementPath + " provided more than once"; |
| 256 | |
| 257 | return false; |
| 258 | } |
| 259 | |
| 260 | // Store new ordered area configuration |
| 261 | areaConfigurationList.push_back(pAreaConfiguration); |
| 262 | } |
| 263 | |
| 264 | // Reorder area configurations according to given path list |
| 265 | reorderAreaConfigurations(areaConfigurationList); |
| 266 | |
| 267 | return true; |
| 268 | } |
| 269 | |
| 270 | void CDomainConfiguration::getElementSequence(string& strResult) const |
| 271 | { |
| 272 | strResult = "\n"; |
| 273 | |
| 274 | AreaConfigurationListIterator it; |
| 275 | |
| 276 | // List configurable element paths out of ordered area configuration list |
| 277 | for (it = _orderedAreaConfigurationList.begin(); it != _orderedAreaConfigurationList.end(); ++it) { |
| 278 | |
| 279 | const CAreaConfiguration* pAreaConfiguration = *it; |
| 280 | |
| 281 | const CConfigurableElement* pConfigurableElement = pAreaConfiguration->getConfigurableElement(); |
| 282 | |
| 283 | strResult += pConfigurableElement->getPath() + "\n"; |
| 284 | } |
| 285 | } |
| 286 | |
| Patrick Benavoli | 0bd5054 | 2011-11-29 11:10:27 +0100 | [diff] [blame] | 287 | // Application rule |
| 288 | bool CDomainConfiguration::setApplicationRule(const string& strApplicationRule, const CSelectionCriteriaDefinition* pSelectionCriteriaDefinition, string& strError) |
| 289 | { |
| 290 | // Parser |
| 291 | CRuleParser ruleParser(strApplicationRule, pSelectionCriteriaDefinition); |
| 292 | |
| 293 | // Attempt to parse it |
| 294 | if (!ruleParser.parse(NULL, strError)) { |
| 295 | |
| 296 | return false; |
| 297 | } |
| 298 | // Replace compound rule |
| 299 | setRule(ruleParser.grabRootRule()); |
| 300 | |
| 301 | return true; |
| 302 | } |
| 303 | |
| 304 | void CDomainConfiguration::clearApplicationRule() |
| 305 | { |
| 306 | // Replace compound rule |
| 307 | setRule(NULL); |
| 308 | } |
| 309 | |
| 310 | void CDomainConfiguration::getApplicationRule(string& strResult) const |
| 311 | { |
| 312 | // Rule |
| 313 | const CCompoundRule* pRule = getRule(); |
| 314 | |
| 315 | if (pRule) { |
| 316 | // Start clear |
| 317 | strResult.clear(); |
| 318 | |
| 319 | // Dump rule |
| 320 | pRule->dump(strResult); |
| 321 | |
| 322 | } else { |
| 323 | |
| 324 | strResult = "<none>"; |
| 325 | } |
| 326 | } |
| 327 | |
| Frédéric Boisnard | e42dacd | 2013-02-25 15:56:56 +0100 | [diff] [blame] | 328 | /** |
| 329 | * Get the Configuration Blackboard. |
| 330 | * |
| 331 | * Fetch the Configuration Blackboard related to the ConfigurableElement given in parameter. This |
| 332 | * Element is used to retrieve the correct AreaConfiguration where the Blackboard is stored. |
| 333 | * |
| 334 | * @param[in] pConfigurableElement A pointer to a ConfigurableElement that is part of the |
| 335 | * Domain. This must have been checked previously, as an |
| 336 | * assertion is performed. |
| 337 | * |
| 338 | * return Pointer to the Blackboard of the Configuration. |
| 339 | */ |
| 340 | CParameterBlackboard* CDomainConfiguration::getBlackboard(const CConfigurableElement* pConfigurableElement) const |
| 341 | { |
| 342 | AreaConfigurationListIterator it; |
| 343 | |
| 344 | for (it = _areaConfigurationList.begin(); it != _areaConfigurationList.end(); ++it) { |
| 345 | |
| 346 | CAreaConfiguration* pAreaConfiguration = *it; |
| 347 | |
| 348 | // Check if the Element is associated with the Domain |
| 349 | if (pAreaConfiguration->getConfigurableElement() == pConfigurableElement) { |
| 350 | |
| 351 | return &pAreaConfiguration->getBlackboard(); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | assert(0); |
| 356 | return NULL; |
| 357 | } |
| 358 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 359 | // Save data from current |
| 360 | void CDomainConfiguration::save(const CParameterBlackboard* pMainBlackboard) |
| 361 | { |
| 362 | AreaConfigurationListIterator it; |
| 363 | |
| 364 | // Just propagate to areas |
| 365 | for (it = _areaConfigurationList.begin(); it != _areaConfigurationList.end(); ++it) { |
| 366 | |
| 367 | CAreaConfiguration* pAreaConfiguration = *it; |
| 368 | |
| 369 | pAreaConfiguration->save(pMainBlackboard); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | // Apply data to current |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame^] | 374 | bool CDomainConfiguration::restore(CParameterBlackboard* pMainBlackboard, bool bSync, std::list<string>* plstrError) const |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 375 | { |
| Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 376 | bool bSuccess = true; |
| 377 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 378 | AreaConfigurationListIterator it; |
| 379 | |
| 380 | // Just propagate to areas |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 381 | for (it = _orderedAreaConfigurationList.begin(); it != _orderedAreaConfigurationList.end(); ++it) { |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 382 | |
| 383 | const CAreaConfiguration* pAreaConfiguration = *it; |
| 384 | |
| Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 385 | bSuccess &= pAreaConfiguration->restore(pMainBlackboard, bSync, plstrError); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 386 | } |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 387 | |
| Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 388 | return bSuccess; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | // Ensure validity for configurable element area configuration |
| 392 | void CDomainConfiguration::validate(const CConfigurableElement* pConfigurableElement, const CParameterBlackboard* pMainBlackboard) |
| 393 | { |
| 394 | CAreaConfiguration* pAreaConfigurationToValidate = getAreaConfiguration(pConfigurableElement); |
| 395 | |
| 396 | // Delegate |
| 397 | pAreaConfigurationToValidate->validate(pMainBlackboard); |
| 398 | } |
| 399 | |
| 400 | // Ensure validity of all area configurations |
| 401 | void CDomainConfiguration::validate(const CParameterBlackboard* pMainBlackboard) |
| 402 | { |
| 403 | AreaConfigurationListIterator it; |
| 404 | |
| 405 | for (it = _areaConfigurationList.begin(); it != _areaConfigurationList.end(); ++it) { |
| 406 | |
| 407 | CAreaConfiguration* pAreaConfiguration = *it; |
| 408 | |
| 409 | pAreaConfiguration->validate(pMainBlackboard); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | // Return configuration validity for given configurable element |
| 414 | bool CDomainConfiguration::isValid(const CConfigurableElement* pConfigurableElement) const |
| 415 | { |
| 416 | // Get child configurable elemnt's area configuration |
| 417 | CAreaConfiguration* pAreaConfiguration = getAreaConfiguration(pConfigurableElement); |
| 418 | |
| 419 | assert(pAreaConfiguration); |
| 420 | |
| 421 | return pAreaConfiguration->isValid(); |
| 422 | } |
| 423 | |
| 424 | // Ensure validity of configurable element's area configuration by copying in from a valid one |
| 425 | void CDomainConfiguration::validateAgainst(const CDomainConfiguration* pValidDomainConfiguration, const CConfigurableElement* pConfigurableElement) |
| 426 | { |
| 427 | // Retrieve related area configurations |
| 428 | CAreaConfiguration* pAreaConfigurationToValidate = getAreaConfiguration(pConfigurableElement); |
| 429 | const CAreaConfiguration* pAreaConfigurationToValidateAgainst = pValidDomainConfiguration->getAreaConfiguration(pConfigurableElement); |
| 430 | |
| 431 | // Delegate to area |
| 432 | pAreaConfigurationToValidate->validateAgainst(pAreaConfigurationToValidateAgainst); |
| 433 | } |
| 434 | |
| 435 | // Ensure validity of all configurable element's area configuration by copying in from a valid ones |
| 436 | void CDomainConfiguration::validateAgainst(const CDomainConfiguration* pValidDomainConfiguration) |
| 437 | { |
| 438 | // Copy in configuration data from against domain |
| 439 | AreaConfigurationListIterator it, itAgainst; |
| 440 | |
| 441 | for (it = _areaConfigurationList.begin(), itAgainst = pValidDomainConfiguration->_areaConfigurationList.begin(); it != _areaConfigurationList.end(); ++it, ++itAgainst) { |
| 442 | |
| 443 | CAreaConfiguration* pAreaConfigurationToValidate = *it; |
| 444 | const CAreaConfiguration* pAreaConfigurationToValidateAgainst = *itAgainst; |
| 445 | |
| 446 | // Delegate to area |
| 447 | pAreaConfigurationToValidate->validateAgainst(pAreaConfigurationToValidateAgainst); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | // Dynamic data application |
| 452 | bool CDomainConfiguration::isApplicable() const |
| 453 | { |
| 454 | const CCompoundRule* pRule = getRule(); |
| 455 | |
| 456 | return pRule && pRule->matches(); |
| 457 | } |
| 458 | |
| 459 | // Merge existing configurations to given configurable element ones |
| 460 | void CDomainConfiguration::merge(CConfigurableElement* pToConfigurableElement, CConfigurableElement* pFromConfigurableElement) |
| 461 | { |
| 462 | // Retrieve related area configurations |
| 463 | CAreaConfiguration* pAreaConfigurationToMergeTo = getAreaConfiguration(pToConfigurableElement); |
| 464 | const CAreaConfiguration* pAreaConfigurationToMergeFrom = getAreaConfiguration(pFromConfigurableElement); |
| 465 | |
| 466 | // Do the merge |
| Frédéric Boisnard | 9620e44 | 2012-05-30 16:15:02 +0200 | [diff] [blame] | 467 | pAreaConfigurationToMergeFrom->copyToOuter(pAreaConfigurationToMergeTo); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | // Domain splitting |
| 471 | void CDomainConfiguration::split(CConfigurableElement* pFromConfigurableElement) |
| 472 | { |
| 473 | // Retrieve related area configuration |
| 474 | const CAreaConfiguration* pAreaConfigurationToSplitFrom = getAreaConfiguration(pFromConfigurableElement); |
| 475 | |
| 476 | // Go through children areas to copy configuration data to them |
| 477 | uint32_t uiNbConfigurableElementChildren = pFromConfigurableElement->getNbChildren(); |
| 478 | uint32_t uiChild; |
| 479 | |
| 480 | for (uiChild = 0; uiChild < uiNbConfigurableElementChildren; uiChild++) { |
| 481 | |
| 482 | CConfigurableElement* pToChildConfigurableElement = static_cast<CConfigurableElement*>(pFromConfigurableElement->getChild(uiChild)); |
| 483 | |
| 484 | // Get child configurable elemnt's area configuration |
| 485 | CAreaConfiguration* pChildAreaConfiguration = getAreaConfiguration(pToChildConfigurableElement); |
| 486 | |
| 487 | // Do the copy |
| Frédéric Boisnard | 9620e44 | 2012-05-30 16:15:02 +0200 | [diff] [blame] | 488 | pChildAreaConfiguration->copyFromOuter(pAreaConfigurationToSplitFrom); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
| 492 | // AreaConfiguration retrieval from configurable element |
| 493 | CAreaConfiguration* CDomainConfiguration::getAreaConfiguration(const CConfigurableElement* pConfigurableElement) const |
| 494 | { |
| 495 | AreaConfigurationListIterator it; |
| 496 | |
| 497 | for (it = _areaConfigurationList.begin(); it != _areaConfigurationList.end(); ++it) { |
| 498 | |
| 499 | CAreaConfiguration* pAreaConfiguration = *it; |
| 500 | |
| 501 | if (pAreaConfiguration->getConfigurableElement() == pConfigurableElement) { |
| 502 | |
| 503 | return pAreaConfiguration; |
| 504 | } |
| 505 | } |
| 506 | // Not found? |
| 507 | assert(0); |
| 508 | |
| 509 | return NULL; |
| 510 | } |
| 511 | |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 512 | // AreaConfiguration retrieval from present area configurations |
| 513 | CAreaConfiguration* CDomainConfiguration::findAreaConfiguration(const string& strConfigurableElementPath) const |
| 514 | { |
| Patrick Benavoli | 4bed921 | 2011-10-27 14:18:00 +0200 | [diff] [blame] | 515 | return findAreaConfiguration(strConfigurableElementPath, _areaConfigurationList); |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | // AreaConfiguration retrieval from given area configuration list |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame^] | 519 | CAreaConfiguration* CDomainConfiguration::findAreaConfiguration(const string& strConfigurableElementPath, const std::list<CAreaConfiguration*>& areaConfigurationList) const |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 520 | { |
| 521 | AreaConfigurationListIterator it; |
| 522 | |
| 523 | for (it = areaConfigurationList.begin(); it != areaConfigurationList.end(); ++it) { |
| 524 | |
| 525 | CAreaConfiguration* pAreaConfiguration = *it; |
| 526 | |
| 527 | if (pAreaConfiguration->getConfigurableElement()->getPath() == strConfigurableElementPath) { |
| 528 | |
| 529 | return pAreaConfiguration; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | // Not found |
| 534 | return NULL; |
| 535 | } |
| 536 | |
| 537 | // Area configuration ordering |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame^] | 538 | void CDomainConfiguration::reorderAreaConfigurations(const std::list<CAreaConfiguration*>& areaConfigurationList) |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 539 | { |
| 540 | // Ensure elements in provided list appear first and ordered the same way in internal one |
| 541 | |
| 542 | // Remove all elements present in the provided list from the internal one |
| 543 | AreaConfigurationListIterator it; |
| 544 | |
| 545 | for (it = areaConfigurationList.begin(); it != areaConfigurationList.end(); ++it) { |
| 546 | |
| 547 | _orderedAreaConfigurationList.remove(*it); |
| 548 | } |
| 549 | |
| 550 | // Prepended provided elements into internal list |
| 551 | _orderedAreaConfigurationList.insert(_orderedAreaConfigurationList.begin(), areaConfigurationList.begin(), areaConfigurationList.end()); |
| 552 | } |
| 553 | |
| 554 | // Find area configuration rank from regular list: for ordered list maintainance |
| 555 | uint32_t CDomainConfiguration::getAreaConfigurationRank(const CAreaConfiguration* pAreaConfiguration) const |
| 556 | { |
| 557 | uint32_t uiAreaConfigurationRank; |
| 558 | AreaConfigurationListIterator it; |
| 559 | |
| 560 | // Propagate request to areas |
| 561 | for (it = _areaConfigurationList.begin(), uiAreaConfigurationRank = 0; it != _areaConfigurationList.end(); ++it, ++uiAreaConfigurationRank) { |
| 562 | |
| 563 | if (*it == pAreaConfiguration) { |
| 564 | |
| 565 | return uiAreaConfigurationRank; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | assert(0); |
| 570 | |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | // Find area configuration from regular list based on rank: for ordered list maintainance |
| 575 | CAreaConfiguration* CDomainConfiguration::getAreaConfiguration(uint32_t uiAreaConfigurationRank) const |
| 576 | { |
| 577 | AreaConfigurationListIterator it; |
| 578 | uint32_t uiCurrentAreaConfigurationRank; |
| 579 | |
| 580 | // Propagate request to areas |
| 581 | for (it = _areaConfigurationList.begin(), uiCurrentAreaConfigurationRank = 0; it != _areaConfigurationList.end(); ++it, ++uiCurrentAreaConfigurationRank) { |
| 582 | |
| 583 | if (uiCurrentAreaConfigurationRank == uiAreaConfigurationRank) { |
| 584 | |
| 585 | return *it; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | assert(0); |
| 590 | |
| 591 | return NULL; |
| 592 | } |
| 593 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 594 | // Rule |
| 595 | const CCompoundRule* CDomainConfiguration::getRule() const |
| 596 | { |
| 597 | if (getNbChildren()) { |
| 598 | // Rule created |
| 599 | return static_cast<const CCompoundRule*>(getChild(ECompoundRule)); |
| 600 | } |
| 601 | return NULL; |
| 602 | } |
| 603 | |
| Patrick Benavoli | 0bd5054 | 2011-11-29 11:10:27 +0100 | [diff] [blame] | 604 | CCompoundRule* CDomainConfiguration::getRule() |
| 605 | { |
| 606 | if (getNbChildren()) { |
| 607 | // Rule created |
| 608 | return static_cast<CCompoundRule*>(getChild(ECompoundRule)); |
| 609 | } |
| 610 | return NULL; |
| 611 | } |
| 612 | |
| 613 | void CDomainConfiguration::setRule(CCompoundRule* pRule) |
| 614 | { |
| 615 | CCompoundRule* pOldRule = getRule(); |
| 616 | |
| 617 | if (pOldRule) { |
| 618 | // Remove previous rule |
| 619 | removeChild(pOldRule); |
| 620 | |
| 621 | delete pOldRule; |
| 622 | } |
| 623 | |
| 624 | // Set new one |
| 625 | if (pRule) { |
| 626 | // Chain |
| 627 | addChild(pRule); |
| 628 | } |
| 629 | } |
| 630 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 631 | // Serialization |
| 632 | void CDomainConfiguration::binarySerialize(CBinaryStream& binaryStream) |
| 633 | { |
| 634 | AreaConfigurationListIterator it; |
| 635 | |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 636 | // Area configurations order |
| 637 | if (binaryStream.isOut()) { |
| 638 | |
| 639 | for (it = _orderedAreaConfigurationList.begin(); it != _orderedAreaConfigurationList.end(); ++it) { |
| 640 | |
| 641 | // Get rank |
| 642 | uint32_t uiAreaConfigurationRank = getAreaConfigurationRank(*it); |
| 643 | |
| 644 | // Store it |
| 645 | binaryStream.write((const uint8_t*)&uiAreaConfigurationRank, sizeof(uiAreaConfigurationRank)); |
| 646 | } |
| 647 | } else { |
| 648 | |
| 649 | // Empty ordered list first |
| 650 | _orderedAreaConfigurationList.resize(0); |
| 651 | |
| 652 | uint32_t uiAreaConfiguration; |
| 653 | |
| 654 | for (uiAreaConfiguration = 0; uiAreaConfiguration < _areaConfigurationList.size(); uiAreaConfiguration++) { |
| 655 | |
| 656 | // Get rank |
| 657 | uint32_t uiAreaConfigurationRank; |
| 658 | |
| 659 | binaryStream.read((uint8_t*)&uiAreaConfigurationRank, sizeof(uiAreaConfigurationRank)); |
| 660 | |
| 661 | _orderedAreaConfigurationList.push_back(getAreaConfiguration(uiAreaConfigurationRank)); |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | // Propagate to areas |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 666 | for (it = _areaConfigurationList.begin(); it != _areaConfigurationList.end(); ++it) { |
| 667 | |
| 668 | CAreaConfiguration* pAreaConfiguration = *it; |
| 669 | |
| 670 | pAreaConfiguration->serialize(binaryStream); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | // Data size |
| 675 | uint32_t CDomainConfiguration::getDataSize() const |
| 676 | { |
| Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 677 | uint32_t uiDataSize; |
| 678 | |
| 679 | // Add necessary size to store area configurations order |
| 680 | uiDataSize = _areaConfigurationList.size() * sizeof(uint32_t); |
| 681 | |
| 682 | // Propagate request to areas |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 683 | AreaConfigurationListIterator it; |
| 684 | |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 685 | for (it = _areaConfigurationList.begin(); it != _areaConfigurationList.end(); ++it) { |
| 686 | |
| 687 | const CAreaConfiguration* pAreaConfiguration = *it; |
| 688 | |
| 689 | uiDataSize += pAreaConfiguration->getSize(); |
| 690 | } |
| 691 | return uiDataSize; |
| 692 | } |