diff options
author | Patrick Benavoli <patrickx.benavoli@intel.com> | 2011-11-20 15:46:41 +0100 |
---|---|---|
committer | David Wagner <david.wagner@intel.com> | 2014-02-10 17:15:00 +0100 |
commit | 065264a93ce9c63b6a5c95e985188ee33ba587d3 (patch) | |
tree | 01897cd4ed658e6a41f21061146098d0385734fd /parameter/ArrayParameter.cpp | |
parent | 6ccab9d382c08323fb1f000d859a696f05719c92 (diff) | |
download | external_parameter-framework-065264a93ce9c63b6a5c95e985188ee33ba587d3.zip external_parameter-framework-065264a93ce9c63b6a5c95e985188ee33ba587d3.tar.gz external_parameter-framework-065264a93ce9c63b6a5c95e985188ee33ba587d3.tar.bz2 |
PFW: Type safe dynamic parameter access
BZ: 15065
Replaced high level string based parameter access interface with typed ones.
Now hosting platforms that want to control parameters must instantiate a
CParameterHandle object out of the desired parameter path.
CParameterHandle object may be used to access any kind of parameters, whatever
its internal type, whether it's an array or not.
Note that non rogue parameters offer a read access only. Any attempt to write
them will fail.
CParameterHandle objects offer the following kind of parameter accessing
interfaces:
- Boolean
- Integer (signed or unsigned)
- Double
- String
Note that those interfaces are available for scalar as well as for array
parameters.
Not all parameter types support all access kinds. Naturally, array parameters
are only accessed via array interfaces while scalar parameters are managed
through scalar interfaces.
Here's a list of parameter types that may be controlled through each kind of
access interface:
- Boolean access: boolean, bit (bit size must be one);
- Integer access: integer (sign must match), boolean (unsigned access only,
value <= 1), enumerations;
- Double access: for now only fixed points (soon integers will support them
also through platform adaptation objects)
- String access: all parameter types
In addition, cleaned up parameter access related code so as to make it more
generic and reusable.
Changed version to 2.0.0
Change-Id: Ib80868cdb773e90962e48f1f38d2ff0029189815
Signed-off-by: Patrick Benavoli <patrickx.benavoli@intel.com>
Reviewed-on: http://android.intel.com:8080/25406
Reviewed-by: Barthes, FabienX <fabienx.barthes@intel.com>
Tested-by: Barthes, FabienX <fabienx.barthes@intel.com>
Reviewed-by: buildbot <buildbot@intel.com>
Tested-by: buildbot <buildbot@intel.com>
Diffstat (limited to 'parameter/ArrayParameter.cpp')
-rw-r--r-- | parameter/ArrayParameter.cpp | 230 |
1 files changed, 176 insertions, 54 deletions
diff --git a/parameter/ArrayParameter.cpp b/parameter/ArrayParameter.cpp index 3877e79..0021a20 100644 --- a/parameter/ArrayParameter.cpp +++ b/parameter/ArrayParameter.cpp @@ -35,6 +35,7 @@ #include "ParameterAccessContext.h" #include "ConfigurationAccessContext.h" #include "ParameterBlackboard.h" +#include <assert.h> #define base CParameter @@ -95,80 +96,91 @@ bool CArrayParameter::serializeXmlSettings(CXmlElement& xmlConfigurationSettings } // User set/get -bool CArrayParameter::setValue(CPathNavigator& pathNavigator, const string& strValue, CParameterAccessContext& parameterContext) const +bool CArrayParameter::accessValue(CPathNavigator& pathNavigator, string& strValue, bool bSet, CParameterAccessContext& parameterAccessContext) const { - uint32_t uiStartIndex; + uint32_t uiIndex; - if (!getIndex(pathNavigator, uiStartIndex, parameterContext)) { + if (!getIndex(pathNavigator, uiIndex, parameterAccessContext)) { return false; } - // Check for dynamic access - if (!checkForDynamicAccess(parameterContext)) { + if (bSet) { + // Set + if (uiIndex == (uint32_t)-1) { - return false; - } + // No index provided, start with 0 + uiIndex = 0; + } - if (uiStartIndex == (uint32_t)-1) { + // Actually set values + if (!setValues(uiIndex, 0, strValue, parameterAccessContext)) { - // No index provided, start with 0 - uiStartIndex = 0; - } + return false; + } - // Actually set values - if (!setValues(uiStartIndex, 0, strValue, parameterContext)) { + // Synchronize + if (parameterAccessContext.getAutoSync() && !sync(parameterAccessContext)) { - return false; - } + // Append parameter path to error + parameterAccessContext.appendToError(" " + getPath()); - // Synchronize - if (parameterContext.getAutoSync() && !sync(parameterContext)) { + return false; + } + } else { + // Get + if (uiIndex == (uint32_t)-1) { - // Append parameter path to error - parameterContext.appendToError(" " + getPath()); + // Whole array requested + getValues(0, strValue, parameterAccessContext); - return false; + } else { + // Scalar requested + doGetValue(strValue, getOffset() + uiIndex * getSize(), parameterAccessContext); + } } return true; } -bool CArrayParameter::getValue(CPathNavigator& pathNavigator, string& strValue, CParameterAccessContext& parameterContext) const +// Boolean +bool CArrayParameter::accessAsBooleanArray(vector<bool>& abValues, bool bSet, CParameterAccessContext& parameterAccessContext) const { - uint32_t uiIndex; - - if (!getIndex(pathNavigator, uiIndex, parameterContext)) { - - return false; - } - - // Check for dynamic access - if (!checkForDynamicAccess(parameterContext)) { - - return false; - } + return accessValues(abValues, bSet, parameterAccessContext); +} - if (uiIndex == (uint32_t)-1) { +// Integer +bool CArrayParameter::accessAsIntegerArray(vector<uint32_t>& auiValues, bool bSet, CParameterAccessContext& parameterAccessContext) const +{ + return accessValues(auiValues, bSet, parameterAccessContext); +} - // Whole array requested - getValues(0, strValue, parameterContext); +// Signed Integer Access +bool CArrayParameter::accessAsSignedIntegerArray(vector<int32_t>& aiValues, bool bSet, CParameterAccessContext& parameterAccessContext) const +{ + return accessValues(aiValues, bSet, parameterAccessContext); +} - } else { - // Scalar requested - doGetValue(strValue, getOffset() + uiIndex * getSize(), parameterContext); - } +// Double Access +bool CArrayParameter::accessAsDoubleArray(vector<double>& adValues, bool bSet, CParameterAccessContext& parameterAccessContext) const +{ + return accessValues(adValues, bSet, parameterAccessContext); +} - return true; +// String Access +bool CArrayParameter::accessAsStringArray(vector<string>& astrValues, bool bSet, CParameterAccessContext& parameterAccessContext) const +{ + return accessValues(astrValues, bSet, parameterAccessContext); } +// Dump void CArrayParameter::logValue(string& strValue, CErrorContext& errorContext) const { // Parameter context - CParameterAccessContext& parameterContext = static_cast<CParameterAccessContext&>(errorContext); + CParameterAccessContext& parameterAccessContext = static_cast<CParameterAccessContext&>(errorContext); // Dump values - getValues(0, strValue, parameterContext); + getValues(0, strValue, parameterAccessContext); } // Used for simulation and virtual subsystems @@ -197,7 +209,7 @@ void CArrayParameter::setDefaultValues(CParameterAccessContext& parameterAccessC } // Index from path -bool CArrayParameter::getIndex(CPathNavigator& pathNavigator, uint32_t& uiIndex, CParameterAccessContext& parameterContext) const +bool CArrayParameter::getIndex(CPathNavigator& pathNavigator, uint32_t& uiIndex, CParameterAccessContext& parameterAccessContext) const { uiIndex = (uint32_t)-1; @@ -212,7 +224,7 @@ bool CArrayParameter::getIndex(CPathNavigator& pathNavigator, uint32_t& uiIndex, if (!iss) { - parameterContext.setError("Expected numerical expression as last item in " + pathNavigator.getCurrentPath()); + parameterAccessContext.setError("Expected numerical expression as last item in " + pathNavigator.getCurrentPath()); return false; } @@ -222,7 +234,7 @@ bool CArrayParameter::getIndex(CPathNavigator& pathNavigator, uint32_t& uiIndex, oss << "Provided index out of range (max is " << getArrayLength() - 1 << ")"; - parameterContext.setError(oss.str()); + parameterAccessContext.setError(oss.str()); return false; } @@ -233,7 +245,7 @@ bool CArrayParameter::getIndex(CPathNavigator& pathNavigator, uint32_t& uiIndex, if (pStrChildName) { // Should be leaf element - parameterContext.setError("Path not found: " + pathNavigator.getCurrentPath()); + parameterAccessContext.setError("Path not found: " + pathNavigator.getCurrentPath()); return false; } @@ -243,7 +255,7 @@ bool CArrayParameter::getIndex(CPathNavigator& pathNavigator, uint32_t& uiIndex, } // Common set value processing -bool CArrayParameter::setValues(uint32_t uiStartIndex, uint32_t uiBaseOffset, const string& strValue, CParameterAccessContext& parameterContext) const +bool CArrayParameter::setValues(uint32_t uiStartIndex, uint32_t uiBaseOffset, const string& strValue, CParameterAccessContext& parameterAccessContext) const { // Deal with value(s) Tokenizer tok(strValue, DEFAULT_DELIMITER + ","); @@ -252,10 +264,10 @@ bool CArrayParameter::setValues(uint32_t uiStartIndex, uint32_t uiBaseOffset, co uint32_t uiNbValues = astrValues.size(); // Check number of provided values - if (uiNbValues + uiStartIndex > getArrayLength()) { + if (uiNbValues + uiStartIndex > getArrayLength()) { // Out of bounds - parameterContext.setError("Too many values provided"); + parameterAccessContext.setError("Too many values provided"); return false; } @@ -267,10 +279,10 @@ bool CArrayParameter::setValues(uint32_t uiStartIndex, uint32_t uiBaseOffset, co for (uiValueIndex = 0; uiValueIndex < uiNbValues; uiValueIndex++) { - if (!doSetValue(astrValues[uiValueIndex], uiOffset, parameterContext)) { + if (!doSetValue(astrValues[uiValueIndex], uiOffset, parameterAccessContext)) { // Append parameter path to error - parameterContext.appendToError(" " + getPath() + "/" + toString(uiValueIndex + uiStartIndex)); + parameterAccessContext.appendToError(" " + getPath() + "/" + toString(uiValueIndex + uiStartIndex)); return false; } @@ -281,7 +293,7 @@ bool CArrayParameter::setValues(uint32_t uiStartIndex, uint32_t uiBaseOffset, co } // Common get value processing -void CArrayParameter::getValues(uint32_t uiBaseOffset, string& strValues, CParameterAccessContext& parameterContext) const +void CArrayParameter::getValues(uint32_t uiBaseOffset, string& strValues, CParameterAccessContext& parameterAccessContext) const { uint32_t uiValueIndex; uint32_t uiSize = getSize(); @@ -295,7 +307,7 @@ void CArrayParameter::getValues(uint32_t uiBaseOffset, string& strValues, CParam for (uiValueIndex = 0; uiValueIndex < uiArrayLength; uiValueIndex++) { string strReadValue; - doGetValue(strReadValue, uiOffset, parameterContext); + doGetValue(strReadValue, uiOffset, parameterAccessContext); if (!bFirst) { @@ -310,3 +322,113 @@ void CArrayParameter::getValues(uint32_t uiBaseOffset, string& strValues, CParam uiOffset += uiSize; } } + +// Generic Access +template <typename type> +bool CArrayParameter::accessValues(vector<type>& values, bool bSet, CParameterAccessContext& parameterAccessContext) const +{ + bool bSuccess; + + if (bSet) { + + if (setValues(values, parameterAccessContext)) { + + // Synchronize + bSuccess = sync(parameterAccessContext); + } else { + + bSuccess = false; + } + } else { + + bSuccess = getValues(values, parameterAccessContext); + } + if (!bSuccess) { + + // Append parameter path to error + parameterAccessContext.appendToError(" " + getPath()); + } + return bSuccess; +} + +template <typename type> +bool CArrayParameter::setValues(const vector<type>& values, CParameterAccessContext& parameterAccessContext) const +{ + uint32_t uiNbValues = getArrayLength(); + uint32_t uiValueIndex; + uint32_t uiSize = getSize(); + uint32_t uiOffset = getOffset(); + + assert(values.size() == uiNbValues); + + // Process + for (uiValueIndex = 0; uiValueIndex < uiNbValues; uiValueIndex++) { + + if (!doSet(values[uiValueIndex], uiOffset, parameterAccessContext)) { + + return false; + } + + uiOffset += uiSize; + } + + return true; +} + +template <typename type> +bool CArrayParameter::getValues(vector<type>& values, CParameterAccessContext& parameterAccessContext) const +{ + uint32_t uiNbValues = getArrayLength(); + uint32_t uiValueIndex; + uint32_t uiSize = getSize(); + uint32_t uiOffset = getOffset(); + + values.clear(); + + for (uiValueIndex = 0; uiValueIndex < uiNbValues; uiValueIndex++) { + type readValue; + + if (!doGet(readValue, uiOffset, parameterAccessContext)) { + + return false; + } + + values.push_back(readValue); + + uiOffset += uiSize; + } + return true; +} + +template <typename type> +bool CArrayParameter::doSet(type value, uint32_t uiOffset, CParameterAccessContext& parameterAccessContext) const +{ + uint32_t uiData; + + if (!static_cast<const CParameterType*>(getTypeElement())->toBlackboard(value, uiData, parameterAccessContext)) { + + return false; + } + // Write blackboard + CParameterBlackboard* pBlackboard = parameterAccessContext.getParameterBlackboard(); + + // Beware this code works on little endian architectures only! + pBlackboard->writeInteger(&uiData, getSize(), uiOffset, parameterAccessContext.isBigEndianSubsystem()); + + return true; +} + +template <typename type> +bool CArrayParameter::doGet(type& value, uint32_t uiOffset, CParameterAccessContext& parameterAccessContext) const +{ + uint32_t uiData = 0; + + // Read blackboard + const CParameterBlackboard* pBlackboard = parameterAccessContext.getParameterBlackboard(); + + // Beware this code works on little endian architectures only! + pBlackboard->readInteger(&uiData, getSize(), uiOffset, parameterAccessContext.isBigEndianSubsystem()); + + return static_cast<const CParameterType*>(getTypeElement())->fromBlackboard(value, uiData, parameterAccessContext); +} + |