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
39using std::string;
40
41CBaseParameter::CBaseParameter(const string &strName, const CTypeElement *pTypeElement)
42    : base(strName, pTypeElement)
43{
44}
45
46// XML configuration settings parsing/composing
47bool CBaseParameter::serializeXmlSettings(
48    CXmlElement &xmlConfigurationSettingsElementContent,
49    CConfigurationAccessContext &configurationAccessContext) const
50{
51    // Handle access
52    if (!configurationAccessContext.serializeOut()) {
53
54        // Write to blackboard
55        if (!doSetValue(xmlConfigurationSettingsElementContent.getTextContent(),
56                        getOffset() - configurationAccessContext.getBaseOffset(),
57                        configurationAccessContext)) {
58
59            appendParameterPathToError(configurationAccessContext);
60            return false;
61        }
62    } else {
63
64        // Get string value
65        string strValue;
66
67        doGetValue(strValue, getOffset() - configurationAccessContext.getBaseOffset(),
68                   configurationAccessContext);
69
70        // Populate value into xml text node
71        xmlConfigurationSettingsElementContent.setTextContent(strValue);
72    }
73
74    // Done
75    return base::serializeXmlSettings(xmlConfigurationSettingsElementContent,
76                                      configurationAccessContext);
77}
78
79// Dump
80string CBaseParameter::logValue(CParameterAccessContext &context) const
81{
82    // Dump value
83    string output;
84    doGetValue(output, getOffset(), context);
85    return output;
86}
87
88// Check element is a parameter
89bool CBaseParameter::isParameter() const
90{
91    return true;
92}
93
94bool CBaseParameter::access(bool & /*bValue*/, bool /*bSet*/,
95                            CParameterAccessContext &parameterAccessContext) const
96{
97    parameterAccessContext.setError("Unsupported conversion");
98    return false;
99}
100bool CBaseParameter::access(std::vector<bool> & /*abValues*/, bool /*bSet*/,
101                            CParameterAccessContext &parameterAccessContext) const
102{
103    parameterAccessContext.setError("Unsupported conversion");
104    return false;
105}
106
107bool CBaseParameter::access(uint32_t & /*bValue*/, bool /*bSet*/,
108                            CParameterAccessContext &parameterAccessContext) const
109{
110    parameterAccessContext.setError("Unsupported conversion");
111    return false;
112}
113bool CBaseParameter::access(std::vector<uint32_t> & /*abValues*/, bool /*bSet*/,
114                            CParameterAccessContext &parameterAccessContext) const
115{
116    parameterAccessContext.setError("Unsupported conversion");
117    return false;
118}
119
120bool CBaseParameter::access(int32_t & /*bValue*/, bool /*bSet*/,
121                            CParameterAccessContext &parameterAccessContext) const
122{
123    parameterAccessContext.setError("Unsupported conversion");
124    return false;
125}
126bool CBaseParameter::access(std::vector<int32_t> & /*abValues*/, bool /*bSet*/,
127                            CParameterAccessContext &parameterAccessContext) const
128{
129    parameterAccessContext.setError("Unsupported conversion");
130    return false;
131}
132
133bool CBaseParameter::access(double & /*bValue*/, bool /*bSet*/,
134                            CParameterAccessContext &parameterAccessContext) const
135{
136    parameterAccessContext.setError("Unsupported conversion");
137    return false;
138}
139bool CBaseParameter::access(std::vector<double> & /*abValues*/, bool /*bSet*/,
140                            CParameterAccessContext &parameterAccessContext) const
141{
142    parameterAccessContext.setError("Unsupported conversion");
143    return false;
144}
145
146// String Access
147bool CBaseParameter::access(string &strValue, bool bSet,
148                            CParameterAccessContext &parameterAccessContext) const
149{
150    if (bSet) {
151
152        // Set Value
153        if (!doSetValue(strValue, getOffset() - parameterAccessContext.getBaseOffset(),
154                        parameterAccessContext)) {
155
156            appendParameterPathToError(parameterAccessContext);
157            return false;
158        }
159        // Synchronize
160        if (!sync(parameterAccessContext)) {
161
162            appendParameterPathToError(parameterAccessContext);
163            return false;
164        }
165
166    } else {
167        // Get Value
168        doGetValue(strValue, getOffset() - parameterAccessContext.getBaseOffset(),
169                   parameterAccessContext);
170    }
171
172    return true;
173}
174
175bool CBaseParameter::access(std::vector<string> & /*astrValues*/, bool /*bSet*/,
176                            CParameterAccessContext & /*ctx*/) const
177{
178    // Generic string array access to scalar parameter must have been filtered out before
179    assert(0);
180
181    return false;
182}
183
184// Parameter Access
185bool CBaseParameter::accessValue(CPathNavigator &pathNavigator, string &strValue, bool bSet,
186                                 CParameterAccessContext &parameterAccessContext) const
187{
188    // Check path validity
189    if (!checkPathExhausted(pathNavigator, parameterAccessContext)) {
190
191        return false;
192    }
193
194    return access(strValue, bSet, parameterAccessContext);
195}
196
197void CBaseParameter::structureToXml(CXmlElement &xmlElement,
198                                    CXmlSerializingContext &serializingContext) const
199{
200
201    // Delegate to type element
202    getTypeElement()->toXml(xmlElement, serializingContext);
203}
204
205void CBaseParameter::appendParameterPathToError(
206    CParameterAccessContext &parameterAccessContext) const
207{
208    parameterAccessContext.appendToError(" " + getPath());
209}
210