Fix for SVACE and Klocwork issues.
[platform/upstream/iotivity.git] / service / simulator / src / common / attribute_generator.cpp
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #include "attribute_generator.h"
22
23 AttributeGenerator::AttributeGenerator(const SimulatorResourceAttribute &attribute)
24 {
25     m_name = attribute.getName();
26     if (attribute.getProperty())
27     {
28         m_valueGen = AttributeValueGenFactory::create(attribute.getProperty());
29     }
30 }
31
32 AttributeGenerator::AttributeGenerator(const std::string &name,
33                                        const std::shared_ptr<AttributeProperty> &property)
34     :   m_name(name)
35 {
36     if (property)
37     {
38         m_valueGen = AttributeValueGenFactory::create(property);
39     }
40 }
41
42 bool AttributeGenerator::hasNext()
43 {
44     if (m_valueGen)
45         return m_valueGen->hasNext();
46     return false;
47 }
48
49 bool AttributeGenerator::next(SimulatorResourceAttribute &attribute)
50 {
51     if (!hasNext())
52         return false;
53
54     if (m_valueGen)
55     {
56         attribute.setName(m_name);
57         attribute.setValue(m_valueGen->next());
58         return true;
59     }
60
61     return false;
62 }
63
64 SimulatorResourceAttribute AttributeGenerator::current()
65 {
66     if (m_valueGen)
67     {
68         return SimulatorResourceAttribute(m_name, m_valueGen->value());
69     }
70
71     return SimulatorResourceAttribute(m_name);
72 }
73
74 void AttributeGenerator::reset()
75 {
76     if (m_valueGen)
77         m_valueGen->reset();
78 }
79
80 AttributeCombinationGen::AttributeCombinationGen(
81     const std::vector<SimulatorResourceAttribute> &attributes)
82 {
83     for (auto &attr : attributes)
84     {
85         AttributeGenerator attrGen(attr);
86         m_attrGenList.push_back(attr);
87         m_resModel.add(attr.getName(), attr.getValue());
88     }
89
90     m_index = -1;
91 }
92
93 bool AttributeCombinationGen::next(SimulatorResourceModel &resModel)
94 {
95     if (!m_attrGenList.size())
96     {
97         return false;
98     }
99
100     std::lock_guard<std::mutex> lock(m_lock);
101
102     // This block will execute for only first time
103     if (-1 == m_index)
104     {
105         for (size_t index = 0; index < m_attrGenList.size(); index++)
106         {
107             // Add the attribute on resource model
108             updateAttributeInModel(index);
109         }
110
111         m_index = m_attrGenList.size() - 1;
112         resModel = m_resModel;
113         return true;
114     }
115
116     // Get the next attribute from statck top element
117     if (m_attrGenList[m_index].hasNext())
118     {
119         updateAttributeInModel(m_index);
120         resModel = m_resModel;
121     }
122     else
123     {
124         for (int index = m_index; index >= 0; index--)
125         {
126             if (!m_attrGenList[index].hasNext())
127             {
128                 if (!index)
129                     return false;
130
131                 m_attrGenList[index].reset();
132                 updateAttributeInModel(index);
133             }
134             else
135             {
136                 updateAttributeInModel(index);
137                 break;
138             }
139         }
140
141         resModel = m_resModel;
142     }
143
144     return true;
145 }
146
147 void AttributeCombinationGen::updateAttributeInModel(int index)
148 {
149     SimulatorResourceAttribute attribute;
150     if (m_attrGenList[index].next(attribute))
151         m_resModel.update(attribute.getName(), attribute.getValue());
152 }