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 "TypeElement.h"
31 #include "MappingData.h"
32 #include "Tokenizer.h"
33 #include "InstanceConfigurableElement.h"
34 #include <assert.h>
35 
36 #define base CElement
37 
CTypeElement(const std::string & strName)38 CTypeElement::CTypeElement(const std::string& strName) : base(strName), _uiArrayLength(0), _pMappingData(NULL)
39 {
40 }
41 
~CTypeElement()42 CTypeElement::~CTypeElement()
43 {
44     delete _pMappingData;
45 }
46 
isScalar() const47 bool CTypeElement::isScalar() const
48 {
49     return !_uiArrayLength;
50 }
51 
getArrayLength() const52 uint32_t CTypeElement::getArrayLength() const
53 {
54     return _uiArrayLength;
55 }
56 
toPlainInteger(int iSizeOptimizedData) const57 int CTypeElement::toPlainInteger(int iSizeOptimizedData) const
58 {
59     return iSizeOptimizedData;
60 }
61 
getMappingData(const std::string & strKey,const std::string * & pStrValue) const62 bool CTypeElement::getMappingData(const std::string& strKey, const std::string*& pStrValue) const
63 {
64     if (_pMappingData) {
65 
66         return _pMappingData->getValue(strKey, pStrValue);
67     }
68     return false;
69 }
70 
hasMappingData() const71 bool CTypeElement::hasMappingData() const
72 {
73     return !!_pMappingData;
74 }
75 
76 // Element properties
showProperties(std::string & strResult) const77 void CTypeElement::showProperties(std::string& strResult) const
78 {
79     // The description attribute may be found in the type and not from instance.
80     showDescriptionProperty(strResult);
81     // Prevent base from being called from the Parameter Type context as
82     // it would lead to duplicate the name attribute (duplicated in the type and instance
83     // which have a common base Element)
84 }
85 
populate(CElement * pElement) const86 void CTypeElement::populate(CElement* pElement) const
87 {
88     // Populate children
89     size_t uiChild;
90     size_t uiNbChildren = getNbChildren();
91 
92     for (uiChild = 0; uiChild < uiNbChildren; uiChild++) {
93 
94         const CTypeElement* pChildTypeElement = static_cast<const CTypeElement*>(getChild(uiChild));
95 
96         CInstanceConfigurableElement* pInstanceConfigurableChildElement = pChildTypeElement->instantiate();
97 
98         // Affiliate
99         pElement->addChild(pInstanceConfigurableChildElement);
100     }
101 }
102 
fromXml(const CXmlElement & xmlElement,CXmlSerializingContext & serializingContext)103 bool CTypeElement::fromXml(const CXmlElement& xmlElement, CXmlSerializingContext& serializingContext)
104 {
105     // Array Length attribute
106     if (xmlElement.hasAttribute("ArrayLength")) {
107 
108         _uiArrayLength = xmlElement.getAttributeInteger("ArrayLength");
109     } else {
110         _uiArrayLength = 0; // Scalar
111     }
112     // Manage mapping attribute
113     if (xmlElement.hasAttribute("Mapping")) {
114 
115         if (!getMappingData()->fromXml(xmlElement, serializingContext)) {
116 
117             return false;
118         }
119     }
120     return base::fromXml(xmlElement, serializingContext);
121 }
122 
instantiate() const123 CInstanceConfigurableElement* CTypeElement::instantiate() const
124 {
125     CInstanceConfigurableElement* pInstanceConfigurableElement = doInstantiate();
126 
127     // Populate
128     populate(pInstanceConfigurableElement);
129 
130     return pInstanceConfigurableElement;
131 }
132 
getMappingData()133 CMappingData* CTypeElement::getMappingData()
134 {
135     if (!_pMappingData) {
136 
137         _pMappingData = new CMappingData;
138     }
139     return _pMappingData;
140 }
141 
getFormattedMapping() const142 std::string CTypeElement::getFormattedMapping() const
143 {
144     if (_pMappingData) {
145 
146         return _pMappingData->asString();
147     }
148     return "";
149 }
150 
151 // From IXmlSource
toXml(CXmlElement & xmlElement,CXmlSerializingContext & serializingContext) const152 void CTypeElement::toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const
153 {
154     if (!isScalar()) {
155 
156         xmlElement.setAttributeInteger("ArrayLength", getArrayLength());
157     }
158 
159     base::toXml(xmlElement, serializingContext);
160 }
161