Performed refactoring and code cleanup on service provider plug-in.
[platform/upstream/iotivity.git] / service / simulator / ramlparser / raml / model / ResourceProperties.h
1 /******************************************************************\r
2  *\r
3  * Copyright 2015 Samsung Electronics All Rights Reserved.\r
4  *\r
5  *\r
6  *\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *              http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  *\r
19  ******************************************************************/\r
20 \r
21 #ifndef RESOURCE_PROPERTIES_H_\r
22 #define RESOURCE_PROPERTIES_H_\r
23 \r
24 #include <string>\r
25 #include <vector>\r
26 #include <map>\r
27 #include "cJSON.h"\r
28 #include <boost/variant.hpp>\r
29 #include <boost/lexical_cast.hpp>\r
30 \r
31 namespace RAML\r
32 {\r
33 \r
34     class ResourceProperties\r
35     {\r
36         public:\r
37             ResourceProperties() {}\r
38             ResourceProperties(cJSON *cjson) : m_cjson(cjson) { readJson();}\r
39 \r
40             class Attribute\r
41             {\r
42                 public:\r
43                     typedef boost::variant <\r
44                     int,\r
45                     double,\r
46                     std::string\r
47                     > ValueVariant;\r
48 \r
49                     Attribute() = default;\r
50                     Attribute(const std::string &attrName) : m_name(attrName) {}\r
51 \r
52                     inline std::string getName(void) const { return m_name; }\r
53                     inline void setName(const std::string &name) { m_name = name;}\r
54 \r
55                     template <typename T>\r
56                     T getValue() const\r
57                     {\r
58                         T val = T();\r
59                         return boost::get<T>(m_value);\r
60                     }\r
61 \r
62                     ValueVariant &getValue()\r
63                     {\r
64                         return m_value;\r
65                     }\r
66 \r
67                     int getValueType() const\r
68                     {\r
69                         return m_value.which();\r
70                     }\r
71                     int getValueInt()\r
72                     {\r
73                         return boost::lexical_cast<int> (m_value);\r
74                     }\r
75                     std::string getValueString()\r
76                     {\r
77                         return boost::lexical_cast<std::string> (m_value);\r
78                     }\r
79 \r
80                     template <typename T>\r
81                     void setValue(const T &value)\r
82                     {\r
83                         m_value = value;\r
84                     }\r
85 \r
86                     inline void getRange(int &min, int &max) const\r
87                     {\r
88                         min = m_min;\r
89                         max = m_max;\r
90                     }\r
91 \r
92                     inline void setRange(const int &min, const int &max)\r
93                     {\r
94                         m_min = min;\r
95                         m_max = max;\r
96                     }\r
97 \r
98                     template <typename T>\r
99                     bool setAllowedValues(const std::vector<T> &values)\r
100                     {\r
101                         ValueVariant temp = values.at(0);\r
102                         if (temp.which() != m_value.which())\r
103                         {\r
104                             return false;\r
105                         }\r
106 \r
107                         m_allowedValues.addValues(values);\r
108                         return true;\r
109                     }\r
110                     inline int getAllowedValuesSize() const\r
111                     {\r
112                         return m_allowedValues.size();\r
113                     }\r
114 \r
115                     inline std::vector<ValueVariant> getAllowedValues()\r
116                     {\r
117                         return m_allowedValues.getValues();\r
118                     }\r
119 \r
120                     int getUpdateFrequencyTime() {return m_updateInterval;}\r
121                     void setUpdateFrequencyTime(int interval) {m_updateInterval = interval;}\r
122 \r
123                 private:\r
124                     class AllowedValues\r
125                     {\r
126                         public:\r
127                             template <typename T>\r
128                             void addValue(const T &value)\r
129                             {\r
130                                 ValueVariant temp = value;\r
131                                 m_values.push_back(temp);\r
132                             }\r
133 \r
134                             template <typename T>\r
135                             void addValues(const std::vector<T> &values)\r
136                             {\r
137                                 for (auto value : values)\r
138                                 {\r
139                                     ValueVariant vValue = value;\r
140                                     m_values.push_back(vValue);\r
141                                 }\r
142                             }\r
143 \r
144                             inline ValueVariant &at(int index)\r
145                             {\r
146                                 return m_values.at(index);\r
147                             }\r
148                             inline int size() const\r
149                             {\r
150                                 return m_values.size();\r
151                             }\r
152 \r
153                             inline std::vector<ValueVariant> getValues()\r
154                             {\r
155                                 return m_values;\r
156                             }\r
157 \r
158                         private:\r
159                             std::vector<ValueVariant> m_values;\r
160                     };\r
161 \r
162                     std::string m_name;\r
163                     ValueVariant m_value;\r
164                     int m_max;\r
165                     int m_min;\r
166                     AllowedValues m_allowedValues;\r
167                     int m_updateInterval;\r
168             };\r
169             int size() const { return m_attributes.size(); }\r
170             inline bool getAttribute(const std::string &attrName, Attribute &value)\r
171             {\r
172                 if (m_attributes.end() != m_attributes.find(attrName))\r
173                 {\r
174                     value = m_attributes[attrName];\r
175                     return true;\r
176                 }\r
177 \r
178                 return false;\r
179             }\r
180 \r
181             inline std::map<std::string, Attribute> getAttributes() const\r
182             {\r
183                 return m_attributes;\r
184             }\r
185 \r
186 \r
187             template <typename T>\r
188             void addAttribute(const std::string &attrName, const T &attrValue)\r
189             {\r
190                 if (m_attributes.end() == m_attributes.find(attrName))\r
191                 {\r
192                     m_attributes[attrName] = Attribute(attrName);\r
193                     m_attributes[attrName].setValue(attrValue);\r
194                 }\r
195             }\r
196 \r
197         private:\r
198             inline void setRange(const std::string &attrName, const int min, const int max)\r
199             {\r
200                 if (m_attributes.end() != m_attributes.find(attrName))\r
201                     m_attributes[attrName].setRange(min, max);\r
202             }\r
203 \r
204 \r
205             template <typename T>\r
206             void setAllowedValues(const std::string &attrName, const std::vector<T> &values)\r
207             {\r
208                 if (m_attributes.end() != m_attributes.find(attrName))\r
209                     m_attributes[attrName].setAllowedValues(values);\r
210             }\r
211 \r
212             inline void setUpdateInterval(const std::string &attrName, int interval)\r
213             {\r
214                 if (m_attributes.end() != m_attributes.find(attrName))\r
215                     m_attributes[attrName].setUpdateFrequencyTime(interval);\r
216             }\r
217 \r
218 \r
219             inline void removeAttribute(const std::string &attrName)\r
220             {\r
221                 m_attributes.erase(attrName);\r
222                 return;\r
223             }\r
224             void readJson();\r
225 \r
226         public:\r
227             std::string getResoureType() const {return m_rt; }\r
228             std::string getInterface() const {return m_if; }\r
229 \r
230         private:\r
231             std::map<std::string, Attribute> m_attributes;\r
232             std::string m_rt;\r
233             std::string m_if;\r
234             cJSON *m_cjson;\r
235 \r
236     };\r
237 \r
238 }\r
239 #endif\r