David Wagner | b76c9d6 | 2014-02-05 18:30:24 +0100 | [diff] [blame] | 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. |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 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 | |
Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame] | 38 | CTypeElement::CTypeElement(const std::string& strName) : base(strName), _uiArrayLength(0), _pMappingData(NULL) |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 39 | { |
| 40 | } |
| 41 | |
| 42 | CTypeElement::~CTypeElement() |
| 43 | { |
| 44 | delete _pMappingData; |
| 45 | } |
| 46 | |
| 47 | bool CTypeElement::isScalar() const |
| 48 | { |
| 49 | return !_uiArrayLength; |
| 50 | } |
| 51 | |
| 52 | uint32_t CTypeElement::getArrayLength() const |
| 53 | { |
| 54 | return _uiArrayLength; |
| 55 | } |
| 56 | |
Guillaume Denneulin | a6b01d2 | 2014-01-10 14:58:42 +0100 | [diff] [blame] | 57 | int CTypeElement::toPlainInteger(int iSizeOptimizedData) const |
| 58 | { |
| 59 | return iSizeOptimizedData; |
| 60 | } |
| 61 | |
Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame] | 62 | bool CTypeElement::getMappingData(const std::string& strKey, const std::string*& pStrValue) const |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 63 | { |
| 64 | if (_pMappingData) { |
| 65 | |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 66 | return _pMappingData->getValue(strKey, pStrValue); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 67 | } |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | bool CTypeElement::hasMappingData() const |
| 72 | { |
| 73 | return !!_pMappingData; |
| 74 | } |
| 75 | |
Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 76 | // Element properties |
Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame] | 77 | void CTypeElement::showProperties(std::string& strResult) const |
Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 78 | { |
Jean-Michel Trivi | a9be2d3 | 2015-07-15 15:37:57 -0700 | [diff] [blame] | 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) |
Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 86 | void CTypeElement::populate(CElement* pElement) const |
| 87 | { |
| 88 | // Populate children |
Patrick Benavoli | 911844b | 2014-07-23 01:27:00 +0200 | [diff] [blame] | 89 | size_t uiChild; |
| 90 | size_t uiNbChildren = getNbChildren(); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 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 | |
| 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 | |
| 123 | CInstanceConfigurableElement* CTypeElement::instantiate() const |
| 124 | { |
| 125 | CInstanceConfigurableElement* pInstanceConfigurableElement = doInstantiate(); |
| 126 | |
| 127 | // Populate |
| 128 | populate(pInstanceConfigurableElement); |
| 129 | |
| 130 | return pInstanceConfigurableElement; |
| 131 | } |
| 132 | |
| 133 | CMappingData* CTypeElement::getMappingData() |
| 134 | { |
| 135 | if (!_pMappingData) { |
| 136 | |
| 137 | _pMappingData = new CMappingData; |
| 138 | } |
| 139 | return _pMappingData; |
| 140 | } |
Georges-Henri Baron | 326a31d | 2012-06-28 12:05:09 +0200 | [diff] [blame] | 141 | |
Sebastien Gonzalve | d952649 | 2014-02-20 22:28:03 +0100 | [diff] [blame] | 142 | std::string CTypeElement::getFormattedMapping() const |
Frederic Boisnard | 390b36d | 2013-05-23 15:28:31 +0200 | [diff] [blame] | 143 | { |
| 144 | if (_pMappingData) { |
| 145 | |
| 146 | return _pMappingData->asString(); |
| 147 | } |
| 148 | return ""; |
| 149 | } |
| 150 | |
Georges-Henri Baron | 326a31d | 2012-06-28 12:05:09 +0200 | [diff] [blame] | 151 | // From IXmlSource |
| 152 | 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 | } |