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 "ConfigurableElementAggregator.h"
31 #include "ConfigurableElement.h"
32 
CConfigurableElementAggregator(std::list<const CConfigurableElement * > & aggregateList,MatchesAggregationCriterion pfnMatchesAggregationCriterion)33 CConfigurableElementAggregator::CConfigurableElementAggregator(std::list<const CConfigurableElement*>& aggregateList, MatchesAggregationCriterion pfnMatchesAggregationCriterion)
34     : _aggregateList(aggregateList), _pfnMatchesAggregationCriterion(pfnMatchesAggregationCriterion)
35 {
36 }
37 
38 // Aggregate
aggegate(const CConfigurableElement * pConfigurableElement)39 void CConfigurableElementAggregator::aggegate(const CConfigurableElement* pConfigurableElement)
40 {
41     doAggregate(pConfigurableElement, _aggregateList);
42 }
43 
44 // Recursive aggregate
doAggregate(const CConfigurableElement * pConfigurableElement,std::list<const CConfigurableElement * > & aggregateList)45 bool CConfigurableElementAggregator::doAggregate(const CConfigurableElement* pConfigurableElement, std::list<const CConfigurableElement*>& aggregateList)
46 {
47     if (!(pConfigurableElement->*_pfnMatchesAggregationCriterion)()) {
48 
49         // Not a candidate for aggregation
50         return false;
51     }
52     // Check children
53     std::list<const CConfigurableElement*> childAggregateElementList;
54 
55     size_t uiIndex;
56     size_t uiNbChildren = pConfigurableElement->getNbChildren();
57     size_t uiNbMatchingChildren = 0;
58 
59     for (uiIndex = 0; uiIndex < uiNbChildren; uiIndex++) {
60 
61         const CConfigurableElement* pChildConfigurableElement = static_cast<const CConfigurableElement*>(pConfigurableElement->getChild(uiIndex));
62 
63         uiNbMatchingChildren += doAggregate(pChildConfigurableElement, childAggregateElementList);
64     }
65 
66     if (uiNbMatchingChildren == uiNbChildren) {
67 
68         // All children match => self is a match
69         aggregateList.push_back(pConfigurableElement);
70 
71         return true;
72     } else {
73         // Add children if any
74         aggregateList.insert(aggregateList.end(), childAggregateElementList.begin(), childAggregateElementList.end());
75 
76         return false;
77     }
78 }
79