Merge branch '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     :   m_name(attribute.getName()),
25         m_min(INT_MIN),
26         m_max(INT_MAX),
27         m_rangeIndex(-1),
28         m_nextAllowedValueIndex(0),
29         m_prevAllowedValueIndex(0),
30         m_hasRange(false),
31         m_hasAllowedValue(false)
32 {
33     if (attribute.getValueType() ==
34         SimulatorResourceModel::Attribute::ValueType::INTEGER)
35     {
36         attribute.getRange(m_min, m_max);
37         if (INT_MIN != m_min && INT_MAX != m_max)
38         {
39             m_hasRange = true;
40             m_rangeIndex = m_min;
41         }
42     }
43     else
44     {
45         m_allowedValues = attribute.getAllowedValues();
46         if (0 != m_allowedValues.size())
47         {
48             m_hasAllowedValue = true;
49         }
50         m_prevAllowedValueIndex = m_allowedValues.size();
51     }
52 }
53
54 bool AttributeGenerator::hasNext()
55 {
56     if (m_hasRange && m_rangeIndex <= m_max)
57     {
58         return true;
59     }
60
61     if (m_hasAllowedValue && m_nextAllowedValueIndex < m_allowedValues.size())
62     {
63         return true;
64     }
65
66     return false;
67 }
68
69 bool AttributeGenerator::next(SimulatorResourceModel::Attribute &attribute)
70 {
71     attribute.setName(m_name);
72
73     if (m_hasRange)
74     {
75         attribute.setValue(m_rangeIndex++);
76         return true;
77     }
78     else if (m_hasAllowedValue)
79     {
80         attribute.setValue(m_allowedValues[m_nextAllowedValueIndex++]);
81         return true;
82     }
83
84     return false;
85 }
86
87 bool AttributeGenerator::previous(SimulatorResourceModel::Attribute &attribute)
88 {
89     attribute.setName(m_name);
90
91     if (m_hasRange)
92     {
93         attribute.setValue(m_rangeIndex - 1);
94         return true;
95     }
96     else if (m_hasAllowedValue)
97     {
98         attribute.setValue(m_allowedValues[m_prevAllowedValueIndex - 1]);
99         return true;
100     }
101
102     return false;
103 }
104