1 /*
2 * Copyright (c) 2011-2015, 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 "TypeElement.h"
31 #include "MappingData.h"
32 #include "Tokenizer.h"
33 #include "InstanceConfigurableElement.h"
34 #include "Utility.h"
35 #include <list>
36 #include <assert.h>
37
38 #define base CElement
39
CTypeElement(const std::string & strName)40 CTypeElement::CTypeElement(const std::string &strName) : base(strName)
41 {
42 }
43
~CTypeElement()44 CTypeElement::~CTypeElement()
45 {
46 delete _pMappingData;
47 }
48
isScalar() const49 bool CTypeElement::isScalar() const
50 {
51 return !_arrayLength;
52 }
53
getArrayLength() const54 size_t CTypeElement::getArrayLength() const
55 {
56 return _arrayLength;
57 }
58
toPlainInteger(int iSizeOptimizedData) const59 int CTypeElement::toPlainInteger(int iSizeOptimizedData) const
60 {
61 return iSizeOptimizedData;
62 }
63
getMappingData(const std::string & strKey,const std::string * & pStrValue) const64 bool CTypeElement::getMappingData(const std::string &strKey, const std::string *&pStrValue) const
65 {
66 if (_pMappingData) {
67
68 return _pMappingData->getValue(strKey, pStrValue);
69 }
70 return false;
71 }
72
hasMappingData() const73 bool CTypeElement::hasMappingData() const
74 {
75 return !!_pMappingData;
76 }
77
78 // Element properties
showProperties(std::string & strResult) const79 void CTypeElement::showProperties(std::string &strResult) const
80 {
81 // The description attribute may be found in the type and not from instance.
82 showDescriptionProperty(strResult);
83 // Prevent base from being called from the Parameter Type context as
84 // it would lead to duplicate the name attribute (duplicated in the type and instance
85 // which have a common base Element)
86 }
87
populate(CElement * pElement) const88 void CTypeElement::populate(CElement *pElement) const
89 {
90 // Populate children
91 size_t uiChild;
92 size_t uiNbChildren = getNbChildren();
93
94 for (uiChild = 0; uiChild < uiNbChildren; uiChild++) {
95
96 const CTypeElement *pChildTypeElement =
97 static_cast<const CTypeElement *>(getChild(uiChild));
98
99 CInstanceConfigurableElement *pInstanceConfigurableChildElement =
100 pChildTypeElement->instantiate();
101
102 // Affiliate
103 pElement->addChild(pInstanceConfigurableChildElement);
104 }
105 }
106
fromXml(const CXmlElement & xmlElement,CXmlSerializingContext & serializingContext)107 bool CTypeElement::fromXml(const CXmlElement &xmlElement,
108 CXmlSerializingContext &serializingContext)
109 {
110 // Array Length attribute
111 xmlElement.getAttribute("ArrayLength", _arrayLength);
112 // Manage mapping attribute
113 std::string rawMapping;
114 if (xmlElement.getAttribute("Mapping", rawMapping) && !rawMapping.empty()) {
115
116 std::string error;
117 if (!getMappingData()->init(rawMapping, error)) {
118
119 serializingContext.setError("Invalid Mapping data from XML element '" +
120 xmlElement.getPath() + "': " + error);
121 return false;
122 }
123 }
124 return base::fromXml(xmlElement, serializingContext);
125 }
126
instantiate() const127 CInstanceConfigurableElement *CTypeElement::instantiate() const
128 {
129 CInstanceConfigurableElement *pInstanceConfigurableElement = doInstantiate();
130
131 // Populate
132 populate(pInstanceConfigurableElement);
133
134 return pInstanceConfigurableElement;
135 }
136
getMappingData()137 CMappingData *CTypeElement::getMappingData()
138 {
139 if (!_pMappingData) {
140
141 _pMappingData = new CMappingData;
142 }
143 return _pMappingData;
144 }
145
getFormattedMapping(const CTypeElement * predecessor) const146 std::string CTypeElement::getFormattedMapping(const CTypeElement *predecessor) const
147 {
148 std::list<std::string> mappings;
149 std::string mapping;
150
151 // Try predecessor type first, then myself (in order to have higher-level
152 // mappings displayed first)
153 if (predecessor) {
154 mapping = predecessor->getFormattedMapping();
155 if (not mapping.empty()) {
156 mappings.push_back(mapping);
157 }
158 }
159
160 // Explicitly call the root implementation instead of calling it virtually
161 // (otherwise, it will infinitely recurse).
162 mapping = CTypeElement::getFormattedMapping();
163 if (not mapping.empty()) {
164 mappings.push_back(mapping);
165 }
166
167 return utility::asString(mappings, ", ");
168 }
169
getFormattedMapping() const170 std::string CTypeElement::getFormattedMapping() const
171 {
172 if (_pMappingData) {
173
174 return _pMappingData->asString();
175 }
176 return "";
177 }
178
179 // From IXmlSource
toXml(CXmlElement & xmlElement,CXmlSerializingContext & serializingContext) const180 void CTypeElement::toXml(CXmlElement &xmlElement, CXmlSerializingContext &serializingContext) const
181 {
182 if (!isScalar()) {
183
184 xmlElement.setAttribute("ArrayLength", getArrayLength());
185 }
186
187 base::toXml(xmlElement, serializingContext);
188 }
189