TypeElement.cpp revision d9526499d6ab53b7d13d1434f748f6f2161c2e0a
1b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner/*
2b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * Copyright (c) 2011-2014, Intel Corporation
3b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * All rights reserved.
4b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner *
5b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * Redistribution and use in source and binary forms, with or without modification,
6b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * are permitted provided that the following conditions are met:
7b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner *
8b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * 1. Redistributions of source code must retain the above copyright notice, this
9b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * list of conditions and the following disclaimer.
10b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner *
11b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * 2. Redistributions in binary form must reproduce the above copyright notice,
12b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * this list of conditions and the following disclaimer in the documentation and/or
13b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * other materials provided with the distribution.
14b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner *
15b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * 3. Neither the name of the copyright holder nor the names of its contributors
16b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * may be used to endorse or promote products derived from this software without
17b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * specific prior written permission.
18b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner *
19b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28b76c9d6de717a9a1cfd94e7a8eca7ee4a2035cd7David Wagner * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2968a912857707864bbaaff9808717813105072a6ePatrick Benavoli */
3068a912857707864bbaaff9808717813105072a6ePatrick Benavoli#include "TypeElement.h"
3168a912857707864bbaaff9808717813105072a6ePatrick Benavoli#include "MappingData.h"
3268a912857707864bbaaff9808717813105072a6ePatrick Benavoli#include "Tokenizer.h"
3368a912857707864bbaaff9808717813105072a6ePatrick Benavoli#include "InstanceConfigurableElement.h"
3468a912857707864bbaaff9808717813105072a6ePatrick Benavoli#include <assert.h>
3568a912857707864bbaaff9808717813105072a6ePatrick Benavoli
3668a912857707864bbaaff9808717813105072a6ePatrick Benavoli#define base CElement
3768a912857707864bbaaff9808717813105072a6ePatrick Benavoli
38d9526499d6ab53b7d13d1434f748f6f2161c2e0aSebastien GonzalveCTypeElement::CTypeElement(const std::string& strName) : base(strName), _uiArrayLength(0), _pMappingData(NULL)
3968a912857707864bbaaff9808717813105072a6ePatrick Benavoli{
4068a912857707864bbaaff9808717813105072a6ePatrick Benavoli}
4168a912857707864bbaaff9808717813105072a6ePatrick Benavoli
4268a912857707864bbaaff9808717813105072a6ePatrick BenavoliCTypeElement::~CTypeElement()
4368a912857707864bbaaff9808717813105072a6ePatrick Benavoli{
4468a912857707864bbaaff9808717813105072a6ePatrick Benavoli    delete _pMappingData;
4568a912857707864bbaaff9808717813105072a6ePatrick Benavoli}
4668a912857707864bbaaff9808717813105072a6ePatrick Benavoli
4768a912857707864bbaaff9808717813105072a6ePatrick Benavolibool CTypeElement::isScalar() const
4868a912857707864bbaaff9808717813105072a6ePatrick Benavoli{
4968a912857707864bbaaff9808717813105072a6ePatrick Benavoli    return !_uiArrayLength;
5068a912857707864bbaaff9808717813105072a6ePatrick Benavoli}
5168a912857707864bbaaff9808717813105072a6ePatrick Benavoli
5268a912857707864bbaaff9808717813105072a6ePatrick Benavoliuint32_t CTypeElement::getArrayLength() const
5368a912857707864bbaaff9808717813105072a6ePatrick Benavoli{
5468a912857707864bbaaff9808717813105072a6ePatrick Benavoli    return _uiArrayLength;
5568a912857707864bbaaff9808717813105072a6ePatrick Benavoli}
5668a912857707864bbaaff9808717813105072a6ePatrick Benavoli
57a6b01d2049a137932f303cdee554d8d2703d7af7Guillaume Denneulinint CTypeElement::toPlainInteger(int iSizeOptimizedData) const
58a6b01d2049a137932f303cdee554d8d2703d7af7Guillaume Denneulin{
59a6b01d2049a137932f303cdee554d8d2703d7af7Guillaume Denneulin    return iSizeOptimizedData;
60a6b01d2049a137932f303cdee554d8d2703d7af7Guillaume Denneulin}
61a6b01d2049a137932f303cdee554d8d2703d7af7Guillaume Denneulin
62d9526499d6ab53b7d13d1434f748f6f2161c2e0aSebastien Gonzalvebool CTypeElement::getMappingData(const std::string& strKey, const std::string*& pStrValue) const
6368a912857707864bbaaff9808717813105072a6ePatrick Benavoli{
6468a912857707864bbaaff9808717813105072a6ePatrick Benavoli    if (_pMappingData) {
6568a912857707864bbaaff9808717813105072a6ePatrick Benavoli
666ba361d96bc2581667b3400f87ff89fae6449e1fPatrick Benavoli        return _pMappingData->getValue(strKey, pStrValue);
6768a912857707864bbaaff9808717813105072a6ePatrick Benavoli    }
6868a912857707864bbaaff9808717813105072a6ePatrick Benavoli    return false;
6968a912857707864bbaaff9808717813105072a6ePatrick Benavoli}
7068a912857707864bbaaff9808717813105072a6ePatrick Benavoli
7168a912857707864bbaaff9808717813105072a6ePatrick Benavolibool CTypeElement::hasMappingData() const
7268a912857707864bbaaff9808717813105072a6ePatrick Benavoli{
7368a912857707864bbaaff9808717813105072a6ePatrick Benavoli    return !!_pMappingData;
7468a912857707864bbaaff9808717813105072a6ePatrick Benavoli}
7568a912857707864bbaaff9808717813105072a6ePatrick Benavoli
762ecf900ad8c30ce9f8e81f57977a1a80a6f6d8afPatrick Benavoli// Element properties
77d9526499d6ab53b7d13d1434f748f6f2161c2e0aSebastien Gonzalvevoid CTypeElement::showProperties(std::string& strResult) const
782ecf900ad8c30ce9f8e81f57977a1a80a6f6d8afPatrick Benavoli{
792ecf900ad8c30ce9f8e81f57977a1a80a6f6d8afPatrick Benavoli    (void)strResult;
802ecf900ad8c30ce9f8e81f57977a1a80a6f6d8afPatrick Benavoli    // Prevent base from being called in that context!
812ecf900ad8c30ce9f8e81f57977a1a80a6f6d8afPatrick Benavoli}
822ecf900ad8c30ce9f8e81f57977a1a80a6f6d8afPatrick Benavoli
8368a912857707864bbaaff9808717813105072a6ePatrick Benavolivoid CTypeElement::populate(CElement* pElement) const
8468a912857707864bbaaff9808717813105072a6ePatrick Benavoli{
8568a912857707864bbaaff9808717813105072a6ePatrick Benavoli    // Populate children
8668a912857707864bbaaff9808717813105072a6ePatrick Benavoli    uint32_t uiChild;
8768a912857707864bbaaff9808717813105072a6ePatrick Benavoli    uint32_t uiNbChildren = getNbChildren();
8868a912857707864bbaaff9808717813105072a6ePatrick Benavoli
8968a912857707864bbaaff9808717813105072a6ePatrick Benavoli    for (uiChild = 0; uiChild < uiNbChildren; uiChild++) {
9068a912857707864bbaaff9808717813105072a6ePatrick Benavoli
9168a912857707864bbaaff9808717813105072a6ePatrick Benavoli        const CTypeElement* pChildTypeElement = static_cast<const CTypeElement*>(getChild(uiChild));
9268a912857707864bbaaff9808717813105072a6ePatrick Benavoli
9368a912857707864bbaaff9808717813105072a6ePatrick Benavoli        CInstanceConfigurableElement* pInstanceConfigurableChildElement = pChildTypeElement->instantiate();
9468a912857707864bbaaff9808717813105072a6ePatrick Benavoli
9568a912857707864bbaaff9808717813105072a6ePatrick Benavoli        // Affiliate
9668a912857707864bbaaff9808717813105072a6ePatrick Benavoli        pElement->addChild(pInstanceConfigurableChildElement);
9768a912857707864bbaaff9808717813105072a6ePatrick Benavoli    }
9868a912857707864bbaaff9808717813105072a6ePatrick Benavoli}
9968a912857707864bbaaff9808717813105072a6ePatrick Benavoli
10068a912857707864bbaaff9808717813105072a6ePatrick Benavolibool CTypeElement::fromXml(const CXmlElement& xmlElement, CXmlSerializingContext& serializingContext)
10168a912857707864bbaaff9808717813105072a6ePatrick Benavoli{
10268a912857707864bbaaff9808717813105072a6ePatrick Benavoli    // Array Length attribute
10368a912857707864bbaaff9808717813105072a6ePatrick Benavoli    if (xmlElement.hasAttribute("ArrayLength")) {
10468a912857707864bbaaff9808717813105072a6ePatrick Benavoli
10568a912857707864bbaaff9808717813105072a6ePatrick Benavoli        _uiArrayLength = xmlElement.getAttributeInteger("ArrayLength");
10668a912857707864bbaaff9808717813105072a6ePatrick Benavoli    } else {
10768a912857707864bbaaff9808717813105072a6ePatrick Benavoli        _uiArrayLength = 0; // Scalar
10868a912857707864bbaaff9808717813105072a6ePatrick Benavoli    }
10968a912857707864bbaaff9808717813105072a6ePatrick Benavoli    // Manage mapping attribute
11068a912857707864bbaaff9808717813105072a6ePatrick Benavoli    if (xmlElement.hasAttribute("Mapping")) {
11168a912857707864bbaaff9808717813105072a6ePatrick Benavoli
11268a912857707864bbaaff9808717813105072a6ePatrick Benavoli        if (!getMappingData()->fromXml(xmlElement, serializingContext)) {
11368a912857707864bbaaff9808717813105072a6ePatrick Benavoli
11468a912857707864bbaaff9808717813105072a6ePatrick Benavoli            return false;
11568a912857707864bbaaff9808717813105072a6ePatrick Benavoli        }
11668a912857707864bbaaff9808717813105072a6ePatrick Benavoli    }
11768a912857707864bbaaff9808717813105072a6ePatrick Benavoli    return base::fromXml(xmlElement, serializingContext);
11868a912857707864bbaaff9808717813105072a6ePatrick Benavoli}
11968a912857707864bbaaff9808717813105072a6ePatrick Benavoli
12068a912857707864bbaaff9808717813105072a6ePatrick BenavoliCInstanceConfigurableElement* CTypeElement::instantiate() const
12168a912857707864bbaaff9808717813105072a6ePatrick Benavoli{
12268a912857707864bbaaff9808717813105072a6ePatrick Benavoli    CInstanceConfigurableElement* pInstanceConfigurableElement = doInstantiate();
12368a912857707864bbaaff9808717813105072a6ePatrick Benavoli
12468a912857707864bbaaff9808717813105072a6ePatrick Benavoli    // Populate
12568a912857707864bbaaff9808717813105072a6ePatrick Benavoli    populate(pInstanceConfigurableElement);
12668a912857707864bbaaff9808717813105072a6ePatrick Benavoli
12768a912857707864bbaaff9808717813105072a6ePatrick Benavoli    return pInstanceConfigurableElement;
12868a912857707864bbaaff9808717813105072a6ePatrick Benavoli}
12968a912857707864bbaaff9808717813105072a6ePatrick Benavoli
13068a912857707864bbaaff9808717813105072a6ePatrick BenavoliCMappingData* CTypeElement::getMappingData()
13168a912857707864bbaaff9808717813105072a6ePatrick Benavoli{
13268a912857707864bbaaff9808717813105072a6ePatrick Benavoli    if (!_pMappingData) {
13368a912857707864bbaaff9808717813105072a6ePatrick Benavoli
13468a912857707864bbaaff9808717813105072a6ePatrick Benavoli        _pMappingData = new CMappingData;
13568a912857707864bbaaff9808717813105072a6ePatrick Benavoli    }
13668a912857707864bbaaff9808717813105072a6ePatrick Benavoli    return _pMappingData;
13768a912857707864bbaaff9808717813105072a6ePatrick Benavoli}
138326a31df0dd401283de6170ed09bcf605f61ef7dGeorges-Henri Baron
139d9526499d6ab53b7d13d1434f748f6f2161c2e0aSebastien Gonzalvestd::string CTypeElement::getFormattedMapping() const
140390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard{
141390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard    if (_pMappingData) {
142390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard
143390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard        return _pMappingData->asString();
144390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard    }
145390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard    return "";
146390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard}
147390b36d8129d3ece769c8542d9d3d3895ab13fbbFrederic Boisnard
148326a31df0dd401283de6170ed09bcf605f61ef7dGeorges-Henri Baron// From IXmlSource
149326a31df0dd401283de6170ed09bcf605f61ef7dGeorges-Henri Baronvoid CTypeElement::toXml(CXmlElement& xmlElement, CXmlSerializingContext& serializingContext) const
150326a31df0dd401283de6170ed09bcf605f61ef7dGeorges-Henri Baron{
151326a31df0dd401283de6170ed09bcf605f61ef7dGeorges-Henri Baron    if (!isScalar()) {
152326a31df0dd401283de6170ed09bcf605f61ef7dGeorges-Henri Baron
153326a31df0dd401283de6170ed09bcf605f61ef7dGeorges-Henri Baron        xmlElement.setAttributeInteger("ArrayLength", getArrayLength());
154326a31df0dd401283de6170ed09bcf605f61ef7dGeorges-Henri Baron    }
155326a31df0dd401283de6170ed09bcf605f61ef7dGeorges-Henri Baron
156326a31df0dd401283de6170ed09bcf605f61ef7dGeorges-Henri Baron    base::toXml(xmlElement, serializingContext);
157326a31df0dd401283de6170ed09bcf605f61ef7dGeorges-Henri Baron}
158