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.
29 */
30 #include "BaseParameter.h"
31 #include "ParameterType.h"
32 #include "ParameterAccessContext.h"
33 #include "ConfigurationAccessContext.h"
34 #include "ParameterBlackboard.h"
35 #include <assert.h>
36
37 #define base CInstanceConfigurableElement
38
39 using std::string;
40
CBaseParameter(const string & strName,const CTypeElement * pTypeElement)41 CBaseParameter::CBaseParameter(const string& strName, const CTypeElement* pTypeElement) : base(strName, pTypeElement)
42 {
43 }
44
45 // XML configuration settings parsing/composing
serializeXmlSettings(CXmlElement & xmlConfigurationSettingsElementContent,CConfigurationAccessContext & configurationAccessContext) const46 bool CBaseParameter::serializeXmlSettings(CXmlElement& xmlConfigurationSettingsElementContent, CConfigurationAccessContext& configurationAccessContext) const
47 {
48 // Handle access
49 if (!configurationAccessContext.serializeOut()) {
50
51 // Write to blackboard
52 if (!doSetValue(xmlConfigurationSettingsElementContent.getTextContent(), getOffset() - configurationAccessContext.getBaseOffset(), configurationAccessContext)) {
53
54 appendParameterPathToError(configurationAccessContext);
55 return false;
56 }
57 } else {
58
59 // Get string value
60 string strValue;
61
62 doGetValue(strValue, getOffset() - configurationAccessContext.getBaseOffset(), configurationAccessContext);
63
64 // Populate value into xml text node
65 xmlConfigurationSettingsElementContent.setTextContent(strValue);
66 }
67
68 // Done
69 return true;
70 }
71
72 // Dump
logValue(string & strValue,CErrorContext & errorContext) const73 void CBaseParameter::logValue(string& strValue, CErrorContext& errorContext) const
74 {
75 // Parameter context
76 CParameterAccessContext& parameterAccessContext = static_cast<CParameterAccessContext&>(errorContext);
77
78 // Dump value
79 doGetValue(strValue, getOffset(), parameterAccessContext);
80 }
81
82 // Check element is a parameter
isParameter() const83 bool CBaseParameter::isParameter() const
84 {
85 return true;
86 }
87
88 /// Value access
89 // Boolean access
accessAsBoolean(bool & bValue,bool bSet,CParameterAccessContext & parameterAccessContext) const90 bool CBaseParameter::accessAsBoolean(bool& bValue, bool bSet, CParameterAccessContext& parameterAccessContext) const
91 {
92 (void)bValue;
93 (void)bSet;
94
95 parameterAccessContext.setError("Unsupported conversion");
96
97 return false;
98 }
99
accessAsBooleanArray(std::vector<bool> & abValues,bool bSet,CParameterAccessContext & parameterAccessContext) const100 bool CBaseParameter::accessAsBooleanArray(std::vector<bool>& abValues, bool bSet, CParameterAccessContext& parameterAccessContext) const
101 {
102 (void)abValues;
103 (void)bSet;
104
105 parameterAccessContext.setError("Unsupported conversion");
106
107 return false;
108 }
109
110 // Integer Access
accessAsInteger(uint32_t & uiValue,bool bSet,CParameterAccessContext & parameterAccessContext) const111 bool CBaseParameter::accessAsInteger(uint32_t& uiValue, bool bSet, CParameterAccessContext& parameterAccessContext) const
112 {
113 (void)uiValue;
114 (void)bSet;
115
116 parameterAccessContext.setError("Unsupported conversion");
117
118 return false;
119 }
120
accessAsIntegerArray(std::vector<uint32_t> & auiValues,bool bSet,CParameterAccessContext & parameterAccessContext) const121 bool CBaseParameter::accessAsIntegerArray(std::vector<uint32_t>& auiValues, bool bSet, CParameterAccessContext& parameterAccessContext) const
122 {
123 (void)auiValues;
124 (void)bSet;
125
126 parameterAccessContext.setError("Unsupported conversion");
127
128 return false;
129 }
130
131 // Signed Integer Access
accessAsSignedInteger(int32_t & iValue,bool bSet,CParameterAccessContext & parameterAccessContext) const132 bool CBaseParameter::accessAsSignedInteger(int32_t& iValue, bool bSet, CParameterAccessContext& parameterAccessContext) const
133 {
134 (void)iValue;
135 (void)bSet;
136
137 parameterAccessContext.setError("Unsupported conversion");
138
139 return false;
140 }
141
accessAsSignedIntegerArray(std::vector<int32_t> & aiValues,bool bSet,CParameterAccessContext & parameterAccessContext) const142 bool CBaseParameter::accessAsSignedIntegerArray(std::vector<int32_t>& aiValues, bool bSet, CParameterAccessContext& parameterAccessContext) const
143 {
144 (void)aiValues;
145 (void)bSet;
146
147 parameterAccessContext.setError("Unsupported conversion");
148
149 return false;
150 }
151
152 // Double Access
accessAsDouble(double & dValue,bool bSet,CParameterAccessContext & parameterAccessContext) const153 bool CBaseParameter::accessAsDouble(double& dValue, bool bSet, CParameterAccessContext& parameterAccessContext) const
154 {
155 (void)dValue;
156 (void)bSet;
157
158 parameterAccessContext.setError("Unsupported conversion");
159
160 return false;
161 }
162
accessAsDoubleArray(std::vector<double> & adValues,bool bSet,CParameterAccessContext & parameterAccessContext) const163 bool CBaseParameter::accessAsDoubleArray(std::vector<double>& adValues, bool bSet, CParameterAccessContext& parameterAccessContext) const
164 {
165 (void)adValues;
166 (void)bSet;
167
168 parameterAccessContext.setError("Unsupported conversion");
169
170 return false;
171 }
172
173 // String Access
accessAsString(string & strValue,bool bSet,CParameterAccessContext & parameterAccessContext) const174 bool CBaseParameter::accessAsString(string& strValue, bool bSet, CParameterAccessContext& parameterAccessContext) const
175 {
176 if (bSet) {
177
178 // Set Value
179 if (!doSetValue(strValue, getOffset() - parameterAccessContext.getBaseOffset(), parameterAccessContext)) {
180
181 appendParameterPathToError(parameterAccessContext);
182 return false;
183 }
184 // Synchronize
185 if (!sync(parameterAccessContext)) {
186
187 appendParameterPathToError(parameterAccessContext);
188 return false;
189 }
190
191 } else {
192 // Get Value
193 doGetValue(strValue, getOffset() - parameterAccessContext.getBaseOffset(), parameterAccessContext);
194 }
195
196 return true;
197 }
198
accessAsStringArray(std::vector<string> & astrValues,bool bSet,CParameterAccessContext & parameterAccessContext) const199 bool CBaseParameter::accessAsStringArray(std::vector<string>& astrValues, bool bSet, CParameterAccessContext& parameterAccessContext) const
200 {
201 (void)astrValues;
202 (void)bSet;
203 (void)parameterAccessContext;
204
205 // Generic string array access to scalar parameter must have been filtered out before
206 assert(0);
207
208 return false;
209 }
210
211 // Parameter Access
accessValue(CPathNavigator & pathNavigator,string & strValue,bool bSet,CParameterAccessContext & parameterAccessContext) const212 bool CBaseParameter::accessValue(CPathNavigator& pathNavigator, string& strValue, bool bSet, CParameterAccessContext& parameterAccessContext) const
213 {
214 // Check path validity
215 if (!checkPathExhausted(pathNavigator, parameterAccessContext)) {
216
217 return false;
218 }
219
220 return accessAsString(strValue, bSet, parameterAccessContext);
221 }
222
toXml(CXmlElement & xmlElement,CXmlSerializingContext & serializingContext) const223 void CBaseParameter::toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const
224 {
225
226 // Delegate to type element
227 getTypeElement()->toXml(xmlElement, serializingContext);
228 }
229
230
appendParameterPathToError(CParameterAccessContext & parameterAccessContext) const231 void CBaseParameter::appendParameterPathToError(CParameterAccessContext& parameterAccessContext)
232 const
233 {
234 parameterAccessContext.appendToError(" " + getPath());
235 }
236