Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit bedf6aa

Browse files
committed
Merge pull request #293 from krocard/add-get-set-elementXML-commands
Add get and set element xml commands Get command returns current element values (from main blackboard) in XML format. Set command allows assigning a configurable element's settings directly in XML. Notes: - Tuning mode must be on - In case of failure, all the subelements that have been successfully written along the access keep their new value
2 parents 69b84e9 + 40135ec commit bedf6aa

18 files changed

+273
-58
lines changed

parameter/ArrayParameter.cpp

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -67,35 +67,6 @@ void CArrayParameter::showProperties(string& strResult) const
6767
strResult += "\n";
6868
}
6969

70-
// XML configuration settings parsing
71-
bool CArrayParameter::serializeXmlSettings(CXmlElement& xmlConfigurationSettingsElementContent, CConfigurationAccessContext& configurationAccessContext) const
72-
{
73-
// Check for value space
74-
handleValueSpaceAttribute(xmlConfigurationSettingsElementContent, configurationAccessContext);
75-
76-
// Handle access
77-
if (!configurationAccessContext.serializeOut()) {
78-
79-
// Actually set values to blackboard
80-
if (!setValues(0, configurationAccessContext.getBaseOffset(), xmlConfigurationSettingsElementContent.getTextContent(), configurationAccessContext)) {
81-
82-
return false;
83-
}
84-
} else {
85-
86-
// Get string value
87-
string strValue = getValues(
88-
configurationAccessContext.getBaseOffset(), // Whole array requested
89-
configurationAccessContext);
90-
91-
// Populate value into xml text node
92-
xmlConfigurationSettingsElementContent.setTextContent(strValue);
93-
}
94-
95-
// Done
96-
return true;
97-
}
98-
9970
// User set/get
10071
bool CArrayParameter::accessValue(CPathNavigator& pathNavigator, string& strValue, bool bSet, CParameterAccessContext& parameterAccessContext) const
10172
{
@@ -115,8 +86,8 @@ bool CArrayParameter::accessValue(CPathNavigator& pathNavigator, string& strValu
11586
}
11687

11788
// Actually set values
118-
if (!setValues(index, parameterAccessContext.getBaseOffset(), strValue, parameterAccessContext)) {
119-
89+
if (!setValues(index, getOffset() - parameterAccessContext.getBaseOffset(),
90+
strValue, parameterAccessContext)) {
12091
return false;
12192
}
12293

@@ -131,17 +102,32 @@ bool CArrayParameter::accessValue(CPathNavigator& pathNavigator, string& strValu
131102
if (index == (size_t)-1) {
132103

133104
// Whole array requested
134-
strValue = getValues(parameterAccessContext.getBaseOffset(), parameterAccessContext);
105+
strValue = getValues(getOffset() - parameterAccessContext.getBaseOffset(), parameterAccessContext);
135106

136107
} else {
137108
// Scalar requested
138-
doGetValue(strValue, getOffset() + index * getSize(), parameterAccessContext);
109+
CParameter::doGetValue(strValue, getOffset() + index * getSize(), parameterAccessContext);
139110
}
140111
}
141112

142113
return true;
143114
}
144115

116+
/// Actual parameter access
117+
// String access
118+
bool CArrayParameter::doSetValue(const string& value, size_t offset,
119+
CParameterAccessContext& parameterAccessContext) const
120+
{
121+
return setValues(0, offset, value, parameterAccessContext);
122+
}
123+
124+
void CArrayParameter::doGetValue(string& value, size_t offset,
125+
CParameterAccessContext& parameterAccessContext) const
126+
{
127+
// Whole array requested
128+
value = getValues(offset, parameterAccessContext);
129+
}
130+
145131
// Boolean
146132
bool CArrayParameter::access(std::vector<bool>& abValues, bool bSet, CParameterAccessContext& parameterAccessContext) const
147133
{
@@ -250,7 +236,7 @@ bool CArrayParameter::getIndex(CPathNavigator& pathNavigator, size_t& index, CPa
250236
}
251237

252238
// Common set value processing
253-
bool CArrayParameter::setValues(size_t uiStartIndex, size_t baseOffset, const string& strValue, CParameterAccessContext& parameterAccessContext) const
239+
bool CArrayParameter::setValues(size_t uiStartIndex, size_t offset, const string& strValue, CParameterAccessContext& parameterAccessContext) const
254240
{
255241
// Deal with value(s)
256242
Tokenizer tok(strValue, Tokenizer::defaultDelimiters + ",");
@@ -270,11 +256,11 @@ bool CArrayParameter::setValues(size_t uiStartIndex, size_t baseOffset, const st
270256
// Process
271257
size_t valueIndex;
272258
size_t size = getSize();
273-
size_t offset = getOffset() + uiStartIndex * size - baseOffset;
259+
size_t startOffset = offset + uiStartIndex * size;
274260

275261
for (valueIndex = 0; valueIndex < nbValues; valueIndex++) {
276262

277-
if (!doSetValue(astrValues[valueIndex], offset, parameterAccessContext)) {
263+
if (!doSet(astrValues[valueIndex], startOffset, parameterAccessContext)) {
278264

279265
// Append parameter path to error
280266
parameterAccessContext.appendToError(" " + getPath() + "/" +
@@ -289,10 +275,9 @@ bool CArrayParameter::setValues(size_t uiStartIndex, size_t baseOffset, const st
289275
}
290276

291277
// Common get value processing
292-
string CArrayParameter::getValues(size_t baseOffset, CParameterAccessContext& parameterAccessContext) const
278+
string CArrayParameter::getValues(size_t offset, CParameterAccessContext& parameterAccessContext) const
293279
{
294280
size_t size = getSize();
295-
size_t offset = getOffset() - baseOffset;
296281
size_t arrayLength = getArrayLength();
297282

298283
string output;
@@ -302,7 +287,7 @@ string CArrayParameter::getValues(size_t baseOffset, CParameterAccessContext& pa
302287
for (size_t valueIndex = 0; valueIndex < arrayLength; valueIndex++) {
303288
string strReadValue;
304289

305-
doGetValue(strReadValue, offset, parameterAccessContext);
290+
doGet(strReadValue, offset, parameterAccessContext);
306291

307292
if (!bFirst) {
308293

parameter/ArrayParameter.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ class CArrayParameter : public CParameter
3939
// Instantiation, allocation
4040
virtual size_t getFootPrint() const;
4141

42-
// XML configuration settings parsing
43-
virtual bool serializeXmlSettings(CXmlElement& xmlConfigurationSettingsElementContent, CConfigurationAccessContext& configurationAccessContext) const;
44-
4542
/// Value access
4643
using CBaseParameter::access;
4744
bool access(std::vector<bool>& abValues, bool bSet,
@@ -67,13 +64,24 @@ class CArrayParameter : public CParameter
6764
// Array length
6865
size_t getArrayLength() const;
6966
// Common set value processing
70-
bool setValues(size_t uiStartIndex, size_t baseOffset, const std::string& strValue, CParameterAccessContext& parameterAccessContext) const;
67+
bool setValues(size_t uiStartIndex, size_t offset, const std::string& strValue, CParameterAccessContext& parameterAccessContext) const;
7168
// Log / get values common
7269
std::string getValues(size_t baseOffset, CParameterAccessContext& parameterAccessContext) const;
7370
std::string logValue(CParameterAccessContext &context) const override;
7471
// Index retrieval from user set/get request
7572
bool getIndex(CPathNavigator& pathNavigator, size_t& index, CParameterAccessContext& parameterAccessContext) const;
7673

74+
/** Access whole array.
75+
*
76+
* @param[in] offset Offset of the array in the context blackboard.
77+
* @{
78+
*/
79+
bool doSetValue(const std::string& strValue, size_t offset,
80+
CParameterAccessContext& parameterAccessContext) const override;
81+
void doGetValue(std::string& strValue, size_t offset,
82+
CParameterAccessContext& parameterAccessContext) const override;
83+
/** @} */
84+
7785
/// Value access
7886
// Generic Access
7987
template <typename type>

parameter/BaseParameter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ bool CBaseParameter::accessValue(CPathNavigator& pathNavigator, string& strValue
174174
return access(strValue, bSet, parameterAccessContext);
175175
}
176176

177-
void CBaseParameter::toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const
177+
void CBaseParameter::structureToXml(CXmlElement &xmlElement, CXmlSerializingContext &serializingContext) const
178178
{
179179

180180
// Delegate to type element

parameter/BaseParameter.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ class CBaseParameter : public CInstanceConfigurableElement
7272
bool access(std::string& strValue, bool bSet, CParameterAccessContext& parameterAccessContext) const;
7373
virtual bool access(std::vector<std::string>& astrValues, bool bSet, CParameterAccessContext& parameterAccessContext) const;
7474

75-
// From IXmlSource
76-
virtual void toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const;
75+
void structureToXml(CXmlElement &xmlElement, CXmlSerializingContext &serializingContext) const override final;
7776

7877
protected:
7978
// Parameter Access

parameter/ConfigurableElement.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "AreaConfiguration.h"
3737
#include "Iterator.hpp"
3838
#include "Utility.h"
39+
#include "XmlParameterSerializingContext.h"
3940
#include <assert.h>
4041

4142
#define base CElement
@@ -48,6 +49,37 @@ CConfigurableElement::~CConfigurableElement()
4849
{
4950
}
5051

52+
bool CConfigurableElement::fromXml(const CXmlElement &xmlElement,
53+
CXmlSerializingContext &serializingContext)
54+
{
55+
auto &context = static_cast<CXmlParameterSerializingContext &>(serializingContext);
56+
auto &accessContext = context.getAccessContext();
57+
58+
if (accessContext.serializeSettings()) {
59+
// As serialization and deserialisation are handled through the *same* function
60+
// the (de)serialize object can not be const in `serializeXmlSettings` signature.
61+
// As a result a const_cast is unavoidable :(.
62+
// Fixme: split serializeXmlSettings in two functions (in and out) to avoid the `const_cast`
63+
return serializeXmlSettings(const_cast<CXmlElement&>(xmlElement),
64+
static_cast<CConfigurationAccessContext &>(accessContext));
65+
}
66+
return structureFromXml(xmlElement, serializingContext);
67+
}
68+
69+
void CConfigurableElement::toXml(CXmlElement &xmlElement,
70+
CXmlSerializingContext &serializingContext) const
71+
{
72+
auto &context = static_cast<CXmlParameterSerializingContext &>(serializingContext);
73+
auto &accessContext = context.getAccessContext();
74+
if (accessContext.serializeSettings()) {
75+
76+
serializeXmlSettings(xmlElement, static_cast<CConfigurationAccessContext &>(accessContext));
77+
} else {
78+
79+
structureToXml(xmlElement, serializingContext);
80+
}
81+
}
82+
5183
// XML configuration settings parsing
5284
bool CConfigurableElement::serializeXmlSettings(CXmlElement& xmlConfigurationSettingsElementContent, CConfigurationAccessContext& configurationAccessContext) const
5385
{

parameter/ConfigurableElement.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,36 @@ class PARAMETER_EXPORT CConfigurableElement : public CElement
145145

146146
// XML configuration settings parsing
147147
virtual bool serializeXmlSettings(CXmlElement& xmlConfigurationSettingsElementContent, CConfigurationAccessContext& configurationAccessContext) const;
148+
149+
bool fromXml(const CXmlElement &xmlElement,
150+
CXmlSerializingContext &serializingContext) override final;
151+
152+
void toXml(CXmlElement &xmlElement, CXmlSerializingContext &serializingContext) const override final;
153+
154+
/** Deserialize the structure from xml. */
155+
virtual bool structureFromXml(const CXmlElement &xmlElement, CXmlSerializingContext &serializingContext)
156+
{
157+
// Forward to Element::fromXml.
158+
// This is unfortunate as Element::fromXml will call back
159+
// fromXml on each children.
160+
// Thus on each non leaf node of the tree, the code will test if
161+
// the setting or the structure are to be serialized.
162+
// This test could be avoided by several ways including:
163+
// - split 2 roles fromXml in two function
164+
// 1) construct the elements
165+
// 2) recursive call on children
166+
// - dispatch in with a virtual method. This would not not remove
167+
// the branching rather hide it behind a virtual method override.
168+
return CElement::fromXml(xmlElement, serializingContext);
169+
}
170+
171+
/** Serialize the structure to xml. */
172+
virtual void structureToXml(CXmlElement &xmlElement, CXmlSerializingContext &serializingContext) const
173+
{
174+
// See structureFromXml implementation comment.
175+
CElement::toXml(xmlElement, serializingContext);
176+
}
177+
148178
protected:
149179
// Syncer (me or ascendant)
150180
virtual ISyncer* getSyncer() const;

parameter/ConfigurationAccessContext.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@
3333

3434
using std::string;
3535

36+
CConfigurationAccessContext::CConfigurationAccessContext(std::string& strError,
37+
CParameterBlackboard* pParameterBlackboard,
38+
bool bValueSpaceIsRaw,
39+
bool bOutputRawFormatIsHex,
40+
bool bSerializeOut) :
41+
base(strError, pParameterBlackboard, bValueSpaceIsRaw, bOutputRawFormatIsHex),
42+
_bSerializeOut(bSerializeOut)
43+
{
44+
}
45+
3646
CConfigurationAccessContext::CConfigurationAccessContext(string& strError, bool bSerializeOut) :
3747
base(strError),
3848
_bSerializeOut(bSerializeOut)

parameter/ConfigurationAccessContext.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,19 @@
3636
class CConfigurationAccessContext : public CParameterAccessContext
3737
{
3838
public:
39+
CConfigurationAccessContext(std::string& strError,
40+
CParameterBlackboard* pParameterBlackboard,
41+
bool bValueSpaceIsRaw,
42+
bool bOutputRawFormatIsHex,
43+
bool bSerializeOut);
44+
3945
CConfigurationAccessContext(std::string& strError, bool bSerializeOut);
4046

4147
// Serialization direction
4248
bool serializeOut() const;
4349

50+
bool serializeSettings() const override final { return true; }
51+
4452
private:
4553
// Serialization direction
4654
bool _bSerializeOut;

parameter/InstanceConfigurableElement.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,10 @@ bool CInstanceConfigurableElement::checkPathExhausted(CPathNavigator& pathNaviga
217217
return true;
218218
}
219219

220-
void CInstanceConfigurableElement::toXml(CXmlElement &xmlElement, CXmlSerializingContext &serializingContext) const
220+
void CInstanceConfigurableElement::structureToXml(CXmlElement &xmlElement,
221+
CXmlSerializingContext &serializingContext) const
221222
{
222-
base::toXml(xmlElement, serializingContext);
223+
base::structureToXml(xmlElement, serializingContext);
223224
// Since Description belongs to the Type of Element, delegate it to the type element.
224225
getTypeElement()->setXmlDescriptionAttribute(xmlElement);
225226
}

parameter/InstanceConfigurableElement.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class PARAMETER_EXPORT CInstanceConfigurableElement : public CConfigurableElemen
105105
virtual void getListOfElementsWithMapping(std::list<const CConfigurableElement*>&
106106
configurableElementPath) const;
107107

108-
virtual void toXml(CXmlElement &xmlElement, CXmlSerializingContext &serializingContext) const;
108+
virtual void structureToXml(CXmlElement &xmlElement, CXmlSerializingContext &serializingContext) const;
109109

110110
protected:
111111
// Syncer

0 commit comments

Comments
 (0)