blob: 01b71374e34c79051bd252e258d74859f0157e57 [file] [log] [blame]
David Wagnerb76c9d62014-02-05 18:30:24 +01001/*
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.
Patrick Benavoli68a91282011-08-31 11:23:23 +020029 */
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
Sebastien Gonzalved9526492014-02-20 22:28:03 +010038CTypeElement::CTypeElement(const std::string& strName) : base(strName), _uiArrayLength(0), _pMappingData(NULL)
Patrick Benavoli68a91282011-08-31 11:23:23 +020039{
40}
41
42CTypeElement::~CTypeElement()
43{
44 delete _pMappingData;
45}
46
47bool CTypeElement::isScalar() const
48{
49 return !_uiArrayLength;
50}
51
52uint32_t CTypeElement::getArrayLength() const
53{
54 return _uiArrayLength;
55}
56
Guillaume Denneulina6b01d22014-01-10 14:58:42 +010057int CTypeElement::toPlainInteger(int iSizeOptimizedData) const
58{
59 return iSizeOptimizedData;
60}
61
Sebastien Gonzalved9526492014-02-20 22:28:03 +010062bool CTypeElement::getMappingData(const std::string& strKey, const std::string*& pStrValue) const
Patrick Benavoli68a91282011-08-31 11:23:23 +020063{
64 if (_pMappingData) {
65
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020066 return _pMappingData->getValue(strKey, pStrValue);
Patrick Benavoli68a91282011-08-31 11:23:23 +020067 }
68 return false;
69}
70
71bool CTypeElement::hasMappingData() const
72{
73 return !!_pMappingData;
74}
75
Patrick Benavoli2ecf9002011-08-31 11:23:24 +020076// Element properties
Sebastien Gonzalved9526492014-02-20 22:28:03 +010077void CTypeElement::showProperties(std::string& strResult) const
Patrick Benavoli2ecf9002011-08-31 11:23:24 +020078{
Jean-Michel Trivia9be2d32015-07-15 15:37:57 -070079 // 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)
Patrick Benavoli2ecf9002011-08-31 11:23:24 +020084}
85
Patrick Benavoli68a91282011-08-31 11:23:23 +020086void CTypeElement::populate(CElement* pElement) const
87{
88 // Populate children
Patrick Benavoli911844b2014-07-23 01:27:00 +020089 size_t uiChild;
90 size_t uiNbChildren = getNbChildren();
Patrick Benavoli68a91282011-08-31 11:23:23 +020091
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
103bool 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
123CInstanceConfigurableElement* CTypeElement::instantiate() const
124{
125 CInstanceConfigurableElement* pInstanceConfigurableElement = doInstantiate();
126
127 // Populate
128 populate(pInstanceConfigurableElement);
129
130 return pInstanceConfigurableElement;
131}
132
133CMappingData* CTypeElement::getMappingData()
134{
135 if (!_pMappingData) {
136
137 _pMappingData = new CMappingData;
138 }
139 return _pMappingData;
140}
Georges-Henri Baron326a31d2012-06-28 12:05:09 +0200141
Sebastien Gonzalved9526492014-02-20 22:28:03 +0100142std::string CTypeElement::getFormattedMapping() const
Frederic Boisnard390b36d2013-05-23 15:28:31 +0200143{
144 if (_pMappingData) {
145
146 return _pMappingData->asString();
147 }
148 return "";
149}
150
Georges-Henri Baron326a31d2012-06-28 12:05:09 +0200151// From IXmlSource
152void 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}