Merge branch 'master' into simulator
[platform/upstream/iotivity.git] / service / simulator / src / client-controller / 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(SimulatorResourceModel::Attribute &attribute)
24 {
25     m_name.assign(attribute.getName());
26     m_type = attribute.getValueType();
27
28     if (!attribute.getValueType())
29         attribute.getRange(m_min, m_max);
30     if (m_max > 0)
31     {
32         m_hasRange = true;
33     }
34
35     if (!m_type)
36         m_rangeIndex = m_min;
37
38     m_allowedValues = attribute.getAllowedValues();
39     if (0 != m_allowedValues.size())
40     {
41         m_hasAllowedValue = true;
42     }
43     m_nextAllowedValueIndex = 0;
44     m_prevAllowedValueIndex = m_allowedValues.size();
45 }
46
47 AttributeGenerator::~AttributeGenerator()
48 {
49     m_rangeIndex = 0;
50     m_min = 0;
51     m_max = 0;
52     m_nextAllowedValueIndex = 0;
53     m_prevAllowedValueIndex = 0;
54     m_type = 0;
55 }
56
57 bool AttributeGenerator::hasNext()
58 {
59     if (m_hasRange && m_rangeIndex <= m_max && !m_type)
60     {
61         return true;
62     }
63
64     if (m_hasAllowedValue && m_nextAllowedValueIndex < m_allowedValues.size())
65     {
66         return true;
67     }
68
69     return false;
70 }
71
72 AttributeSP AttributeGenerator::next()
73 {
74     AttributeSP attr = std::make_shared<SimulatorResourceModel::Attribute>(m_name);
75
76     if (!attr)
77         return nullptr;
78
79     if (m_hasRange && !m_type)
80     {
81         attr->setName(m_name);
82         attr->setValue(m_rangeIndex++);
83         return attr;
84     }
85
86     if (m_hasAllowedValue)
87     {
88         switch (m_type)
89         {
90             case 1:
91                 {
92                     attr->setValue(m_allowedValues[m_nextAllowedValueIndex++]);
93                 }
94                 break;
95
96             case 3:
97                 {
98                     attr->setValue(m_allowedValues[m_nextAllowedValueIndex++]);
99                 }
100                 break;
101         }
102
103         return attr;
104     }
105 }
106
107 AttributeSP AttributeGenerator::previous()
108 {
109     AttributeSP attr = std::make_shared<SimulatorResourceModel::Attribute>(m_name);
110
111     if (!attr)
112         return nullptr;
113
114     if (m_hasRange && !m_type)
115     {
116         attr->setValue(m_rangeIndex - 1);
117         return attr;
118     }
119
120     if (m_hasAllowedValue)
121     {
122         switch (m_type)
123         {
124             case 1:
125                 {
126                     attr->setValue(m_allowedValues[m_prevAllowedValueIndex - 1]);
127                 }
128                 break;
129
130             case 3:
131                 {
132                     attr->setValue(m_allowedValues[m_prevAllowedValueIndex - 1]);
133                 }
134                 break;
135         }
136
137         return attr;
138     }
139 }
140