• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ComponentInstance.h"
31 #include "ComponentLibrary.h"
32 #include "ComponentType.h"
33 #include "Component.h"
34 #include "ParameterBlock.h" // for "array" instantiation
35 #include "XmlParameterSerializingContext.h"
36 
37 #define base CTypeElement
38 
CComponentInstance(const std::string & strName)39 CComponentInstance::CComponentInstance(const std::string &strName) : base(strName)
40 {
41 }
42 
getKind() const43 std::string CComponentInstance::getKind() const
44 {
45     return "ComponentInstance";
46 }
47 
getXmlElementName() const48 std::string CComponentInstance::getXmlElementName() const
49 {
50     // Once instantiated components are reflected as parameter blocks
51     // in XML documents
52     return "ParameterBlock";
53 }
54 
childrenAreDynamic() const55 bool CComponentInstance::childrenAreDynamic() const
56 {
57     return true;
58 }
59 
getMappingData(const std::string & strKey,const std::string * & pStrValue) const60 bool CComponentInstance::getMappingData(const std::string &strKey,
61                                         const std::string *&pStrValue) const
62 {
63     // Try myself first then associated component type
64     return base::getMappingData(strKey, pStrValue) ||
65            (_pComponentType && _pComponentType->getMappingData(strKey, pStrValue));
66 }
67 
hasMappingData() const68 bool CComponentInstance::hasMappingData() const
69 {
70     // Try myself first then extended component type
71     return base::hasMappingData() || (_pComponentType && _pComponentType->hasMappingData());
72 }
73 
getFormattedMapping() const74 std::string CComponentInstance::getFormattedMapping() const
75 {
76     return base::getFormattedMapping(_pComponentType);
77 }
78 
fromXml(const CXmlElement & xmlElement,CXmlSerializingContext & serializingContext)79 bool CComponentInstance::fromXml(const CXmlElement &xmlElement,
80                                  CXmlSerializingContext &serializingContext)
81 {
82     // Context
83     CXmlParameterSerializingContext &parameterBuildContext =
84         static_cast<CXmlParameterSerializingContext &>(serializingContext);
85 
86     const CComponentLibrary *pComponentLibrary = parameterBuildContext.getComponentLibrary();
87 
88     std::string strComponentType;
89     xmlElement.getAttribute("Type", strComponentType);
90 
91     _pComponentType = pComponentLibrary->getComponentType(strComponentType);
92 
93     if (!_pComponentType) {
94 
95         serializingContext.setError("Unable to create Component " + xmlElement.getPath() +
96                                     ". ComponentType " + strComponentType + " not found!");
97 
98         return false;
99     }
100     if (_pComponentType == getParent()) {
101 
102         serializingContext.setError("Recursive definition of " + _pComponentType->getName() +
103                                     " due to " + xmlElement.getPath() +
104                                     " referring to one of its own type.");
105 
106         return false;
107     }
108 
109     return base::fromXml(xmlElement, serializingContext);
110 }
111 
doInstantiate() const112 CInstanceConfigurableElement *CComponentInstance::doInstantiate() const
113 {
114     if (isScalar()) {
115         return new CComponent(getName(), this);
116     } else {
117         return new CParameterBlock(getName(), this);
118     }
119 }
120 
populate(CElement * pElement) const121 void CComponentInstance::populate(CElement *pElement) const
122 {
123     size_t arrayLength = getArrayLength();
124 
125     if (arrayLength != 0) {
126 
127         // Create child elements
128         for (size_t child = 0; child < arrayLength; child++) {
129 
130             CComponent *pChildComponent = new CComponent(std::to_string(child), this);
131 
132             pElement->addChild(pChildComponent);
133 
134             base::populate(pChildComponent);
135 
136             _pComponentType->populate(pChildComponent);
137         }
138     } else {
139         base::populate(pElement);
140 
141         _pComponentType->populate(static_cast<CComponent *>(pElement));
142     }
143 }
144