Merge branch 'upstream' into tizen
[platform/upstream/iotivity.git] / service / simulator / src / common / attribute_value_generator.h
1 /******************************************************************
2  *
3  * Copyright 2016 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 #ifndef SIMULATOR_ATTRIBUTE_VALUE_GENERATOR_H_
22 #define SIMULATOR_ATTRIBUTE_VALUE_GENERATOR_H_
23
24 #include "simulator_resource_model_schema.h"
25
26 class AttributeValueGen
27 {
28     public:
29         virtual bool hasNext() = 0;
30         virtual AttributeValueVariant next() = 0;
31         virtual AttributeValueVariant value() = 0;
32         virtual void reset() = 0;
33 };
34
35 template <typename TYPE>
36 class RangeValueGen : public AttributeValueGen
37 {
38     private:
39         TYPE m_min;
40         TYPE m_max;
41         TYPE m_cur;
42
43     public:
44         RangeValueGen(TYPE min, TYPE max) : m_min(min), m_max(max), m_cur(min) {}
45
46         bool hasNext()
47         {
48             return (m_cur <= m_max);
49         }
50
51         AttributeValueVariant next()
52         {
53             TYPE value = m_cur;
54             m_cur++;
55             return value;
56         }
57
58         AttributeValueVariant value()
59         {
60             return m_cur - 1;
61         }
62
63         void reset()
64         {
65             m_cur = m_min;
66         }
67 };
68
69 template <typename TYPE>
70 class ValuesSetGen : public AttributeValueGen
71 {
72     private:
73         std::vector<TYPE> m_values;
74         size_t m_index;
75
76     public:
77         ValuesSetGen(const std::vector<TYPE> &values) : m_values(values), m_index(0) {}
78
79         bool hasNext()
80         {
81             return (m_index < m_values.size());
82         }
83
84         AttributeValueVariant next()
85         {
86             return m_values[m_index++];
87         }
88
89         AttributeValueVariant value()
90         {
91             return m_values[m_index - 1];
92         }
93
94         void reset()
95         {
96             m_index = 0;
97         }
98 };
99
100 class AttributeValueGenFactory
101 {
102     public:
103         static std::unique_ptr<AttributeValueGen> create(
104             const std::shared_ptr<AttributeProperty> &property);
105 };
106
107 #endif