| 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 "ArrayParameter.h" |
| 31 | #include <sstream> // for istringstream |
| 32 | #include "Tokenizer.h" |
| 33 | #include "ParameterType.h" |
| 34 | #include "ParameterAccessContext.h" |
| 35 | #include "ConfigurationAccessContext.h" |
| 36 | #include "ParameterBlackboard.h" |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 37 | #include <assert.h> |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 38 | |
| 39 | #define base CParameter |
| 40 | |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame] | 41 | using std::string; |
| 42 | |
| Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 43 | CArrayParameter::CArrayParameter(const string& strName, const CTypeElement* pTypeElement) : base(strName, pTypeElement) |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
| 47 | uint32_t CArrayParameter::getFootPrint() const |
| 48 | { |
| Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 49 | return getSize() * getArrayLength(); |
| 50 | } |
| 51 | |
| 52 | // Array length |
| 53 | uint32_t CArrayParameter::getArrayLength() const |
| 54 | { |
| 55 | return getTypeElement()->getArrayLength(); |
| 56 | } |
| 57 | |
| 58 | // Element properties |
| 59 | void CArrayParameter::showProperties(string& strResult) const |
| 60 | { |
| 61 | base::showProperties(strResult); |
| 62 | |
| 63 | // Array length |
| 64 | strResult += "Array length: "; |
| 65 | strResult += toString(getArrayLength()); |
| 66 | strResult += "\n"; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // XML configuration settings parsing |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 70 | bool CArrayParameter::serializeXmlSettings(CXmlElement& xmlConfigurationSettingsElementContent, CConfigurationAccessContext& configurationAccessContext) const |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 71 | { |
| 72 | // Check for value space |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 73 | handleValueSpaceAttribute(xmlConfigurationSettingsElementContent, configurationAccessContext); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 74 | |
| 75 | // Handle access |
| 76 | if (!configurationAccessContext.serializeOut()) { |
| 77 | |
| 78 | // Actually set values to blackboard |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 79 | if (!setValues(0, configurationAccessContext.getBaseOffset(), xmlConfigurationSettingsElementContent.getTextContent(), configurationAccessContext)) { |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 80 | |
| 81 | return false; |
| 82 | } |
| 83 | } else { |
| 84 | |
| 85 | // Get string value |
| 86 | string strValue; |
| 87 | |
| 88 | // Whole array requested |
| 89 | getValues(configurationAccessContext.getBaseOffset(), strValue, configurationAccessContext); |
| 90 | |
| 91 | // Populate value into xml text node |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 92 | xmlConfigurationSettingsElementContent.setTextContent(strValue); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // Done |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | // User set/get |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 100 | bool CArrayParameter::accessValue(CPathNavigator& pathNavigator, string& strValue, bool bSet, CParameterAccessContext& parameterAccessContext) const |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 101 | { |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 102 | uint32_t uiIndex; |
| 103 | |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 104 | if (!getIndex(pathNavigator, uiIndex, parameterAccessContext)) { |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 105 | |
| 106 | return false; |
| 107 | } |
| Patrick Benavoli | 4bed921 | 2011-10-27 14:18:00 +0200 | [diff] [blame] | 108 | |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 109 | if (bSet) { |
| 110 | // Set |
| 111 | if (uiIndex == (uint32_t)-1) { |
| Patrick Benavoli | 4bed921 | 2011-10-27 14:18:00 +0200 | [diff] [blame] | 112 | |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 113 | // No index provided, start with 0 |
| 114 | uiIndex = 0; |
| 115 | } |
| Patrick Benavoli | 4bed921 | 2011-10-27 14:18:00 +0200 | [diff] [blame] | 116 | |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 117 | // Actually set values |
| Frédéric Boisnard | 6bc17c0 | 2013-02-25 15:55:47 +0100 | [diff] [blame] | 118 | if (!setValues(uiIndex, parameterAccessContext.getBaseOffset(), strValue, parameterAccessContext)) { |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 119 | |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 120 | return false; |
| 121 | } |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 122 | |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 123 | // Synchronize |
| Francois Gaffie | 8ab7293 | 2014-04-07 13:46:05 +0200 | [diff] [blame] | 124 | if (!sync(parameterAccessContext)) { |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 125 | |
| Francois Gaffie | a1258f4 | 2014-04-07 13:47:48 +0200 | [diff] [blame] | 126 | appendParameterPathToError(parameterAccessContext); |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 127 | return false; |
| 128 | } |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 129 | } else { |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 130 | // Get |
| 131 | if (uiIndex == (uint32_t)-1) { |
| 132 | |
| 133 | // Whole array requested |
| Frédéric Boisnard | 6bc17c0 | 2013-02-25 15:55:47 +0100 | [diff] [blame] | 134 | getValues(parameterAccessContext.getBaseOffset(), strValue, parameterAccessContext); |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 135 | |
| 136 | } else { |
| 137 | // Scalar requested |
| 138 | doGetValue(strValue, getOffset() + uiIndex * getSize(), parameterAccessContext); |
| 139 | } |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | return true; |
| 143 | } |
| 144 | |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 145 | // Boolean |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame] | 146 | bool CArrayParameter::accessAsBooleanArray(std::vector<bool>& abValues, bool bSet, CParameterAccessContext& parameterAccessContext) const |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 147 | { |
| 148 | return accessValues(abValues, bSet, parameterAccessContext); |
| 149 | } |
| 150 | |
| 151 | // Integer |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame] | 152 | bool CArrayParameter::accessAsIntegerArray(std::vector<uint32_t>& auiValues, bool bSet, CParameterAccessContext& parameterAccessContext) const |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 153 | { |
| 154 | return accessValues(auiValues, bSet, parameterAccessContext); |
| 155 | } |
| 156 | |
| 157 | // Signed Integer Access |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame] | 158 | bool CArrayParameter::accessAsSignedIntegerArray(std::vector<int32_t>& aiValues, bool bSet, CParameterAccessContext& parameterAccessContext) const |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 159 | { |
| 160 | return accessValues(aiValues, bSet, parameterAccessContext); |
| 161 | } |
| 162 | |
| 163 | // Double Access |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame] | 164 | bool CArrayParameter::accessAsDoubleArray(std::vector<double>& adValues, bool bSet, CParameterAccessContext& parameterAccessContext) const |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 165 | { |
| 166 | return accessValues(adValues, bSet, parameterAccessContext); |
| 167 | } |
| 168 | |
| 169 | // String Access |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame] | 170 | bool CArrayParameter::accessAsStringArray(std::vector<string>& astrValues, bool bSet, CParameterAccessContext& parameterAccessContext) const |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 171 | { |
| 172 | return accessValues(astrValues, bSet, parameterAccessContext); |
| 173 | } |
| 174 | |
| 175 | // Dump |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 176 | void CArrayParameter::logValue(string& strValue, CErrorContext& errorContext) const |
| 177 | { |
| Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 178 | // Parameter context |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 179 | CParameterAccessContext& parameterAccessContext = static_cast<CParameterAccessContext&>(errorContext); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 180 | |
| 181 | // Dump values |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 182 | getValues(0, strValue, parameterAccessContext); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 183 | } |
| 184 | |
| Patrick Benavoli | 6ccab9d | 2011-11-10 23:21:01 +0100 | [diff] [blame] | 185 | // Used for simulation and virtual subsystems |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 186 | void CArrayParameter::setDefaultValues(CParameterAccessContext& parameterAccessContext) const |
| 187 | { |
| 188 | // Get default value from type |
| 189 | uint32_t uiDefaultValue = static_cast<const CParameterType*>(getTypeElement())->getDefaultValue(); |
| 190 | |
| 191 | // Write blackboard |
| 192 | CParameterBlackboard* pBlackboard = parameterAccessContext.getParameterBlackboard(); |
| 193 | |
| 194 | // Process |
| 195 | uint32_t uiValueIndex; |
| 196 | uint32_t uiSize = getSize(); |
| 197 | uint32_t uiOffset = getOffset(); |
| 198 | bool bSubsystemIsBigEndian = parameterAccessContext.isBigEndianSubsystem(); |
| Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 199 | uint32_t uiArrayLength = getArrayLength(); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 200 | |
| Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 201 | for (uiValueIndex = 0; uiValueIndex < uiArrayLength; uiValueIndex++) { |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 202 | |
| 203 | // Beware this code works on little endian architectures only! |
| Patrick Benavoli | 1352ae5 | 2011-10-21 16:48:04 +0200 | [diff] [blame] | 204 | pBlackboard->writeInteger(&uiDefaultValue, uiSize, uiOffset, bSubsystemIsBigEndian); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 205 | |
| 206 | uiOffset += uiSize; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // Index from path |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 211 | bool CArrayParameter::getIndex(CPathNavigator& pathNavigator, uint32_t& uiIndex, CParameterAccessContext& parameterAccessContext) const |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 212 | { |
| 213 | uiIndex = (uint32_t)-1; |
| 214 | |
| 215 | string* pStrChildName = pathNavigator.next(); |
| 216 | |
| 217 | if (pStrChildName) { |
| 218 | |
| 219 | // Check index is numeric |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame] | 220 | std::istringstream iss(*pStrChildName); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 221 | |
| 222 | iss >> uiIndex; |
| 223 | |
| 224 | if (!iss) { |
| 225 | |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 226 | parameterAccessContext.setError("Expected numerical expression as last item in " + pathNavigator.getCurrentPath()); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 227 | |
| 228 | return false; |
| 229 | } |
| 230 | |
| Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 231 | if (uiIndex >= getArrayLength()) { |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame] | 232 | std::ostringstream oss; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 233 | |
| Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 234 | oss << "Provided index out of range (max is " << getArrayLength() - 1 << ")"; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 235 | |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 236 | parameterAccessContext.setError(oss.str()); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 237 | |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | // Check no other item provided in path |
| 242 | pStrChildName = pathNavigator.next(); |
| 243 | |
| 244 | if (pStrChildName) { |
| 245 | |
| 246 | // Should be leaf element |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 247 | parameterAccessContext.setError("Path not found: " + pathNavigator.getCurrentPath()); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 248 | |
| 249 | return false; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | return true; |
| 254 | } |
| 255 | |
| 256 | // Common set value processing |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 257 | bool CArrayParameter::setValues(uint32_t uiStartIndex, uint32_t uiBaseOffset, const string& strValue, CParameterAccessContext& parameterAccessContext) const |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 258 | { |
| 259 | // Deal with value(s) |
| David Wagner | 99b3e85 | 2015-03-19 16:35:47 +0100 | [diff] [blame] | 260 | Tokenizer tok(strValue, Tokenizer::defaultDelimiters + ","); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 261 | |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame] | 262 | std::vector<string> astrValues = tok.split(); |
| Patrick Benavoli | 911844b | 2014-07-23 01:27:00 +0200 | [diff] [blame^] | 263 | size_t uiNbValues = astrValues.size(); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 264 | |
| 265 | // Check number of provided values |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 266 | if (uiNbValues + uiStartIndex > getArrayLength()) { |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 267 | |
| 268 | // Out of bounds |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 269 | parameterAccessContext.setError("Too many values provided"); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 270 | |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | // Process |
| 275 | uint32_t uiValueIndex; |
| 276 | uint32_t uiSize = getSize(); |
| 277 | uint32_t uiOffset = getOffset() + uiStartIndex * uiSize - uiBaseOffset; |
| 278 | |
| 279 | for (uiValueIndex = 0; uiValueIndex < uiNbValues; uiValueIndex++) { |
| 280 | |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 281 | if (!doSetValue(astrValues[uiValueIndex], uiOffset, parameterAccessContext)) { |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 282 | |
| 283 | // Append parameter path to error |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 284 | parameterAccessContext.appendToError(" " + getPath() + "/" + toString(uiValueIndex + uiStartIndex)); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 285 | |
| 286 | return false; |
| 287 | } |
| 288 | |
| 289 | uiOffset += uiSize; |
| 290 | } |
| 291 | return true; |
| 292 | } |
| 293 | |
| 294 | // Common get value processing |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 295 | void CArrayParameter::getValues(uint32_t uiBaseOffset, string& strValues, CParameterAccessContext& parameterAccessContext) const |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 296 | { |
| 297 | uint32_t uiValueIndex; |
| 298 | uint32_t uiSize = getSize(); |
| 299 | uint32_t uiOffset = getOffset() - uiBaseOffset; |
| Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 300 | uint32_t uiArrayLength = getArrayLength(); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 301 | |
| Patrick Benavoli | 4bed921 | 2011-10-27 14:18:00 +0200 | [diff] [blame] | 302 | strValues.clear(); |
| 303 | |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 304 | bool bFirst = true; |
| 305 | |
| Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 306 | for (uiValueIndex = 0; uiValueIndex < uiArrayLength; uiValueIndex++) { |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 307 | string strReadValue; |
| 308 | |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 309 | doGetValue(strReadValue, uiOffset, parameterAccessContext); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 310 | |
| Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 311 | if (!bFirst) { |
| 312 | |
| 313 | strValues += " "; |
| 314 | } else { |
| 315 | |
| 316 | bFirst = false; |
| 317 | } |
| 318 | |
| 319 | strValues += strReadValue; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 320 | |
| 321 | uiOffset += uiSize; |
| 322 | } |
| 323 | } |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 324 | |
| 325 | // Generic Access |
| 326 | template <typename type> |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame] | 327 | bool CArrayParameter::accessValues(std::vector<type>& values, bool bSet, CParameterAccessContext& parameterAccessContext) const |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 328 | { |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 329 | if (bSet) { |
| 330 | |
| Francois Gaffie | 8ab7293 | 2014-04-07 13:46:05 +0200 | [diff] [blame] | 331 | // Set Value |
| 332 | if (!setValues(values, parameterAccessContext)) { |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 333 | |
| Francois Gaffie | a1258f4 | 2014-04-07 13:47:48 +0200 | [diff] [blame] | 334 | appendParameterPathToError(parameterAccessContext); |
| Francois Gaffie | 8ab7293 | 2014-04-07 13:46:05 +0200 | [diff] [blame] | 335 | return false; |
| 336 | } |
| 337 | if (!sync(parameterAccessContext)) { |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 338 | |
| Francois Gaffie | a1258f4 | 2014-04-07 13:47:48 +0200 | [diff] [blame] | 339 | appendParameterPathToError(parameterAccessContext); |
| Francois Gaffie | 8ab7293 | 2014-04-07 13:46:05 +0200 | [diff] [blame] | 340 | return false; |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 341 | } |
| 342 | } else { |
| Francois Gaffie | 8ab7293 | 2014-04-07 13:46:05 +0200 | [diff] [blame] | 343 | // Get Value |
| 344 | if (!getValues(values, parameterAccessContext)) { |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 345 | |
| Francois Gaffie | a1258f4 | 2014-04-07 13:47:48 +0200 | [diff] [blame] | 346 | appendParameterPathToError(parameterAccessContext); |
| Francois Gaffie | 8ab7293 | 2014-04-07 13:46:05 +0200 | [diff] [blame] | 347 | return false; |
| 348 | } |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 349 | } |
| Francois Gaffie | 8ab7293 | 2014-04-07 13:46:05 +0200 | [diff] [blame] | 350 | return true; |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | template <typename type> |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame] | 354 | bool CArrayParameter::setValues(const std::vector<type>& values, CParameterAccessContext& parameterAccessContext) const |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 355 | { |
| 356 | uint32_t uiNbValues = getArrayLength(); |
| 357 | uint32_t uiValueIndex; |
| 358 | uint32_t uiSize = getSize(); |
| 359 | uint32_t uiOffset = getOffset(); |
| 360 | |
| 361 | assert(values.size() == uiNbValues); |
| 362 | |
| 363 | // Process |
| 364 | for (uiValueIndex = 0; uiValueIndex < uiNbValues; uiValueIndex++) { |
| 365 | |
| 366 | if (!doSet(values[uiValueIndex], uiOffset, parameterAccessContext)) { |
| 367 | |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | uiOffset += uiSize; |
| 372 | } |
| 373 | |
| 374 | return true; |
| 375 | } |
| 376 | |
| 377 | template <typename type> |
| Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame] | 378 | bool CArrayParameter::getValues(std::vector<type>& values, CParameterAccessContext& parameterAccessContext) const |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 379 | { |
| 380 | uint32_t uiNbValues = getArrayLength(); |
| 381 | uint32_t uiValueIndex; |
| 382 | uint32_t uiSize = getSize(); |
| 383 | uint32_t uiOffset = getOffset(); |
| 384 | |
| 385 | values.clear(); |
| 386 | |
| 387 | for (uiValueIndex = 0; uiValueIndex < uiNbValues; uiValueIndex++) { |
| 388 | type readValue; |
| 389 | |
| 390 | if (!doGet(readValue, uiOffset, parameterAccessContext)) { |
| 391 | |
| 392 | return false; |
| 393 | } |
| 394 | |
| 395 | values.push_back(readValue); |
| 396 | |
| 397 | uiOffset += uiSize; |
| 398 | } |
| 399 | return true; |
| 400 | } |
| 401 | |
| 402 | template <typename type> |
| 403 | bool CArrayParameter::doSet(type value, uint32_t uiOffset, CParameterAccessContext& parameterAccessContext) const |
| 404 | { |
| 405 | uint32_t uiData; |
| 406 | |
| 407 | if (!static_cast<const CParameterType*>(getTypeElement())->toBlackboard(value, uiData, parameterAccessContext)) { |
| 408 | |
| 409 | return false; |
| 410 | } |
| 411 | // Write blackboard |
| 412 | CParameterBlackboard* pBlackboard = parameterAccessContext.getParameterBlackboard(); |
| 413 | |
| 414 | // Beware this code works on little endian architectures only! |
| 415 | pBlackboard->writeInteger(&uiData, getSize(), uiOffset, parameterAccessContext.isBigEndianSubsystem()); |
| 416 | |
| 417 | return true; |
| 418 | } |
| 419 | |
| 420 | template <typename type> |
| 421 | bool CArrayParameter::doGet(type& value, uint32_t uiOffset, CParameterAccessContext& parameterAccessContext) const |
| 422 | { |
| 423 | uint32_t uiData = 0; |
| 424 | |
| 425 | // Read blackboard |
| 426 | const CParameterBlackboard* pBlackboard = parameterAccessContext.getParameterBlackboard(); |
| 427 | |
| 428 | // Beware this code works on little endian architectures only! |
| 429 | pBlackboard->readInteger(&uiData, getSize(), uiOffset, parameterAccessContext.isBigEndianSubsystem()); |
| 430 | |
| 431 | return static_cast<const CParameterType*>(getTypeElement())->fromBlackboard(value, uiData, parameterAccessContext); |
| 432 | } |
| 433 | |