Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame^] | 1 | /* <auto_header> |
| 2 | * <FILENAME> |
| 3 | * |
| 4 | * INTEL CONFIDENTIAL |
| 5 | * Copyright © 2011 Intel |
| 6 | * Corporation All Rights Reserved. |
| 7 | * |
| 8 | * The source code contained or described herein and all documents related to |
| 9 | * the source code ("Material") are owned by Intel Corporation or its suppliers |
| 10 | * or licensors. Title to the Material remains with Intel Corporation or its |
| 11 | * suppliers and licensors. The Material contains trade secrets and proprietary |
| 12 | * and confidential information of Intel or its suppliers and licensors. The |
| 13 | * Material is protected by worldwide copyright and trade secret laws and |
| 14 | * treaty provisions. No part of the Material may be used, copied, reproduced, |
| 15 | * modified, published, uploaded, posted, transmitted, distributed, or |
| 16 | * disclosed in any way without Intel’s prior express written permission. |
| 17 | * |
| 18 | * No license under any patent, copyright, trade secret or other intellectual |
| 19 | * property right is granted to or conferred upon you by disclosure or delivery |
| 20 | * of the Materials, either expressly, by implication, inducement, estoppel or |
| 21 | * otherwise. Any license under such intellectual property rights must be |
| 22 | * express and approved by Intel in writing. |
| 23 | * |
| 24 | * AUTHOR: Patrick Benavoli (patrickx.benavoli@intel.com) |
| 25 | * CREATED: 2011-06-01 |
| 26 | * UPDATED: 2011-07-27 |
| 27 | * |
| 28 | * |
| 29 | * </auto_header> |
| 30 | */ |
| 31 | #include "ArrayParameter.h" |
| 32 | #include <sstream> // for istringstream |
| 33 | #include "Tokenizer.h" |
| 34 | #include "ParameterType.h" |
| 35 | #include "ParameterAccessContext.h" |
| 36 | #include "ConfigurationAccessContext.h" |
| 37 | #include "ParameterBlackboard.h" |
| 38 | |
| 39 | #define base CParameter |
| 40 | |
| 41 | CArrayParameter::CArrayParameter(const string& strName, const CTypeElement* pTypeElement, uint32_t uiLength) : base(strName, pTypeElement), _uiLength(uiLength) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | uint32_t CArrayParameter::getFootPrint() const |
| 46 | { |
| 47 | return getSize() * _uiLength; |
| 48 | } |
| 49 | |
| 50 | // XML configuration settings parsing |
| 51 | bool CArrayParameter::serializeXmlSettings(CXmlElement& xmlConfigurableElementSettingsElement, CConfigurationAccessContext& configurationAccessContext) const |
| 52 | { |
| 53 | // Check for value space |
| 54 | handleValueSpaceAttribute(xmlConfigurableElementSettingsElement, configurationAccessContext); |
| 55 | |
| 56 | // Handle access |
| 57 | if (!configurationAccessContext.serializeOut()) { |
| 58 | |
| 59 | // Actually set values to blackboard |
| 60 | if (!setValues(0, configurationAccessContext.getBaseOffset(), xmlConfigurableElementSettingsElement.getTextContent(), configurationAccessContext)) { |
| 61 | |
| 62 | return false; |
| 63 | } |
| 64 | } else { |
| 65 | |
| 66 | // Get string value |
| 67 | string strValue; |
| 68 | |
| 69 | // Whole array requested |
| 70 | getValues(configurationAccessContext.getBaseOffset(), strValue, configurationAccessContext); |
| 71 | |
| 72 | // Populate value into xml text node |
| 73 | xmlConfigurableElementSettingsElement.setTextContent(strValue); |
| 74 | } |
| 75 | |
| 76 | // Done |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | // User set/get |
| 81 | bool CArrayParameter::setValue(CPathNavigator& pathNavigator, const string& strValue, CErrorContext& errorContext) const |
| 82 | { |
| 83 | CParameterAccessContext& parameterContext = static_cast<CParameterAccessContext&>(errorContext); |
| 84 | |
| 85 | uint32_t uiStartIndex; |
| 86 | |
| 87 | if (!getIndex(pathNavigator, uiStartIndex, parameterContext)) { |
| 88 | |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | if (uiStartIndex == (uint32_t)-1) { |
| 93 | |
| 94 | // No index provided, start with 0 |
| 95 | uiStartIndex = 0; |
| 96 | } |
| 97 | |
| 98 | // Actually set values |
| 99 | if (!setValues(uiStartIndex, 0, strValue, parameterContext)) { |
| 100 | |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | // Synchronize |
| 105 | if (parameterContext.getAutoSync() && !sync(parameterContext)) { |
| 106 | |
| 107 | // Append parameter path to error |
| 108 | errorContext.appendToError(" " + getPath()); |
| 109 | |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | bool CArrayParameter::getValue(CPathNavigator& pathNavigator, string& strValue, CErrorContext& errorContext) const |
| 117 | { |
| 118 | CParameterAccessContext& parameterContext = static_cast<CParameterAccessContext&>(errorContext); |
| 119 | uint32_t uiIndex; |
| 120 | |
| 121 | if (!getIndex(pathNavigator, uiIndex, parameterContext)) { |
| 122 | |
| 123 | return false; |
| 124 | } |
| 125 | if (uiIndex == (uint32_t)-1) { |
| 126 | |
| 127 | // Whole array requested |
| 128 | getValues(0, strValue, parameterContext); |
| 129 | |
| 130 | } else { |
| 131 | // Scalar requested |
| 132 | doGetValue(strValue, getOffset() + uiIndex * getSize(), parameterContext); |
| 133 | } |
| 134 | |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | void CArrayParameter::logValue(string& strValue, CErrorContext& errorContext) const |
| 139 | { |
| 140 | CParameterAccessContext& parameterContext = static_cast<CParameterAccessContext&>(errorContext); |
| 141 | |
| 142 | // Dump values |
| 143 | getValues(0, strValue, parameterContext); |
| 144 | |
| 145 | // Prepend unit if any |
| 146 | prependUnit(strValue); |
| 147 | } |
| 148 | |
| 149 | // Used for simulation only |
| 150 | void CArrayParameter::setDefaultValues(CParameterAccessContext& parameterAccessContext) const |
| 151 | { |
| 152 | // Get default value from type |
| 153 | uint32_t uiDefaultValue = static_cast<const CParameterType*>(getTypeElement())->getDefaultValue(); |
| 154 | |
| 155 | // Write blackboard |
| 156 | CParameterBlackboard* pBlackboard = parameterAccessContext.getParameterBlackboard(); |
| 157 | |
| 158 | // Process |
| 159 | uint32_t uiValueIndex; |
| 160 | uint32_t uiSize = getSize(); |
| 161 | uint32_t uiOffset = getOffset(); |
| 162 | bool bSubsystemIsBigEndian = parameterAccessContext.isBigEndianSubsystem(); |
| 163 | |
| 164 | for (uiValueIndex = 0; uiValueIndex < _uiLength; uiValueIndex++) { |
| 165 | |
| 166 | // Beware this code works on little endian architectures only! |
| 167 | pBlackboard->write(&uiDefaultValue, uiSize, uiOffset, bSubsystemIsBigEndian); |
| 168 | |
| 169 | uiOffset += uiSize; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // Index from path |
| 174 | bool CArrayParameter::getIndex(CPathNavigator& pathNavigator, uint32_t& uiIndex, CErrorContext& errorContext) const |
| 175 | { |
| 176 | uiIndex = (uint32_t)-1; |
| 177 | |
| 178 | string* pStrChildName = pathNavigator.next(); |
| 179 | |
| 180 | if (pStrChildName) { |
| 181 | |
| 182 | // Check index is numeric |
| 183 | istringstream iss(*pStrChildName); |
| 184 | |
| 185 | iss >> uiIndex; |
| 186 | |
| 187 | if (!iss) { |
| 188 | |
| 189 | errorContext.setError("Expected numerical expression as last item in " + pathNavigator.getCurrentPath()); |
| 190 | |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | if (uiIndex >= _uiLength) { |
| 195 | ostringstream oss; |
| 196 | |
| 197 | oss << "Provided index out of range (max is " << _uiLength - 1 << ")"; |
| 198 | |
| 199 | errorContext.setError(oss.str()); |
| 200 | |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | // Check no other item provided in path |
| 205 | pStrChildName = pathNavigator.next(); |
| 206 | |
| 207 | if (pStrChildName) { |
| 208 | |
| 209 | // Should be leaf element |
| 210 | errorContext.setError("Path not found: " + pathNavigator.getCurrentPath()); |
| 211 | |
| 212 | return false; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | // Common set value processing |
| 220 | bool CArrayParameter::setValues(uint32_t uiStartIndex, uint32_t uiBaseOffset, const string& strValue, CParameterAccessContext& parameterContext) const |
| 221 | { |
| 222 | // Deal with value(s) |
| 223 | Tokenizer tok(strValue); |
| 224 | |
| 225 | vector<string> astrValues = tok.split(); |
| 226 | uint32_t uiNbValues = astrValues.size(); |
| 227 | |
| 228 | // Check number of provided values |
| 229 | if (uiNbValues + uiStartIndex > _uiLength) { |
| 230 | |
| 231 | // Out of bounds |
| 232 | parameterContext.setError("Too many values provided"); |
| 233 | |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | // Process |
| 238 | uint32_t uiValueIndex; |
| 239 | uint32_t uiSize = getSize(); |
| 240 | uint32_t uiOffset = getOffset() + uiStartIndex * uiSize - uiBaseOffset; |
| 241 | |
| 242 | for (uiValueIndex = 0; uiValueIndex < uiNbValues; uiValueIndex++) { |
| 243 | |
| 244 | if (!doSetValue(astrValues[uiValueIndex], uiOffset, parameterContext)) { |
| 245 | |
| 246 | // Append parameter path to error |
| 247 | parameterContext.appendToError(" " + getPath() + "/" + getIndexAsString(uiValueIndex + uiStartIndex)); |
| 248 | |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | uiOffset += uiSize; |
| 253 | } |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | // Common get value processing |
| 258 | void CArrayParameter::getValues(uint32_t uiBaseOffset, string& strValues, CParameterAccessContext& parameterContext) const |
| 259 | { |
| 260 | uint32_t uiValueIndex; |
| 261 | uint32_t uiSize = getSize(); |
| 262 | uint32_t uiOffset = getOffset() - uiBaseOffset; |
| 263 | |
| 264 | for (uiValueIndex = 0; uiValueIndex < _uiLength; uiValueIndex++) { |
| 265 | string strReadValue; |
| 266 | |
| 267 | doGetValue(strReadValue, uiOffset, parameterContext); |
| 268 | |
| 269 | strValues += strReadValue + " "; |
| 270 | |
| 271 | uiOffset += uiSize; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | string CArrayParameter::getIndexAsString(uint32_t uiIndex) |
| 276 | { |
| 277 | ostringstream strStream; |
| 278 | |
| 279 | strStream << uiIndex; |
| 280 | |
| 281 | return strStream.str(); |
| 282 | } |