61daf5e2d95ef52f34791361923bc9114e953ba7
[platform/upstream/iotivity.git] / service / simulator / ramlparser / raml / jsonSchemaParser / Properties.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 /**\r
22  * @file   Properties.h\r
23  *\r
24  * @brief   This file provides data Model for Json Schema Properties.\r
25  */\r
26 \r
27 #ifndef PROPERTIES_H_\r
28 #define PROPERTIES_H_\r
29 \r
30 #include <string>\r
31 #include <vector>\r
32 #include <map>\r
33 #include <boost/variant.hpp>\r
34 #include <boost/lexical_cast.hpp>\r
35 #include <limits>\r
36 #include "Items.h"\r
37 #include "AllowedValues.h"\r
38 #include "cJSON.h"\r
39 #include <memory>\r
40 \r
41 namespace RAML\r
42 {\r
43     /**\r
44      * @class   Properties\r
45      * @brief   This class provides data Model for Json Schema Properties.\r
46      */\r
47     class Properties\r
48     {\r
49         public:\r
50             /**\r
51                   * Constructor of Properties.\r
52                   */\r
53             Properties(): m_min(INT_MAX), m_max(INT_MAX), m_multipleOf(INT_MAX), m_updateInterval(0),\r
54                 m_unique(false), m_additionalItems(false) {}\r
55 \r
56             /**\r
57                   * Constructor of Properties.\r
58                   *\r
59                   * @param name - Properties name as string.\r
60                   */\r
61             Properties(const std::string &name) : m_name(name), m_min(INT_MAX), m_max(INT_MAX),\r
62                 m_multipleOf(INT_MAX), m_updateInterval(0), m_unique(false), m_additionalItems(false) {}\r
63 \r
64             /**\r
65                  * This method is for getting Name from Properties.\r
66                  *\r
67                  * @return Properties name as string\r
68                  */\r
69             inline std::string getName(void) const\r
70             {\r
71                 return m_name;\r
72             }\r
73 \r
74             /**\r
75                  * This method is for setting name to Properties\r
76                  *\r
77                  * @param name - Properties name as string.\r
78                  */\r
79             inline void setName(const std::string &name)\r
80             {\r
81                 m_name = name;\r
82             }\r
83 \r
84             /**\r
85                  * This method is for getting Value from Properties.\r
86                  *\r
87                  * @return Properties Value\r
88                  */\r
89             template <typename T>\r
90             T getValue() const\r
91             {\r
92                 return boost::get<T>(m_value);\r
93             }\r
94 \r
95             /**\r
96                  * This method is for getting Value from Properties.\r
97                  *\r
98                  * @return Properties Value\r
99                  */\r
100             ValueVariant &getValue()\r
101             {\r
102                 return m_value;\r
103             }\r
104 \r
105             /**\r
106                  * This method is for getting ValueVariant type from Properties.\r
107                  *\r
108                  * @return Properties Value type as Int\r
109                  */\r
110             int getValueType() const\r
111             {\r
112                 return m_value.which();\r
113             }\r
114 \r
115             /**\r
116                  * This method is for getting ValueVariant type from Properties.\r
117                  *\r
118                  * @return Properties VariantType type\r
119                  */\r
120             VariantType getVariantType() const\r
121             {\r
122                 if (m_value.which() == 3)\r
123                     return VariantType::STRING;\r
124                 else if (m_value.which() == 2)\r
125                     return VariantType::BOOL;\r
126                 else if (m_value.which() == 1)\r
127                     return VariantType::DOUBLE;\r
128                 else\r
129                     return VariantType::INT;\r
130             }\r
131 \r
132             /**\r
133                  * This method is for getting Value type as Integer from Properties.\r
134                  *\r
135                  * @return Properties Value type as Integer\r
136                  */\r
137             int getValueInt()\r
138             {\r
139                 return boost::lexical_cast<int> (m_value);\r
140             }\r
141 \r
142             /**\r
143                  * This method is for getting Value type as String from Properties.\r
144                  *\r
145                  * @return Properties Value type as String\r
146                  */\r
147             std::string getValueString()\r
148             {\r
149                 return boost::lexical_cast<std::string> (m_value);\r
150             }\r
151 \r
152             /**\r
153                  * This method is for getting Value type as double from Properties.\r
154                  *\r
155                  * @return Properties Value type as double\r
156                  */\r
157             double getValueDouble()\r
158             {\r
159                 return boost::lexical_cast<double> (m_value);\r
160             }\r
161 \r
162             /**\r
163                  * This method is for getting Value type as bool from Properties.\r
164                  *\r
165                  * @return Properties Value type as bool\r
166                  */\r
167             bool getValueBool()\r
168             {\r
169                 return boost::lexical_cast<bool> (m_value);\r
170             }\r
171 \r
172             /**\r
173                  * This method is for setting Value to Properties\r
174                  *\r
175                  * @param value - Properties Value.\r
176                  */\r
177             template <typename T>\r
178             void setValue(const T &value)\r
179             {\r
180                 m_value = value;\r
181             }\r
182 \r
183             /**\r
184                  * This method is for getting Range from Properties.\r
185                  *\r
186                  * @param min - reference to hold Minimum value of Properties.\r
187                  * @param max -  reference to hold Maximum value of Properties.\r
188                  * @param multipleOf -  reference to hold multipleOf value of Properties.\r
189                  */\r
190             inline void getRange(int &min, int &max, int &multipleOf) const\r
191             {\r
192                 min = m_min;\r
193                 max = m_max;\r
194                 multipleOf = m_multipleOf;\r
195             }\r
196 \r
197             /**\r
198                  * This method is for setting Minimum to Properties\r
199                  *\r
200                  * @param min - Minimum value of Properties.\r
201                  */\r
202             inline void setMin(const int &min)\r
203             {\r
204                 m_min = min;\r
205             }\r
206 \r
207             /**\r
208                  * This method is for setting Maximum to Properties\r
209                  *\r
210                  * @param max - Maximum value of Properties.\r
211                  */\r
212             inline void setMax(const int &max)\r
213             {\r
214                 m_max = max;\r
215             }\r
216 \r
217             /**\r
218                  * This method is for setting multipleOf to Properties\r
219                  *\r
220                  * @param multipleOf - multipleOf value of Properties.\r
221                  */\r
222             inline void setMultipleOf(const int &multipleOf)\r
223             {\r
224                 m_multipleOf = multipleOf;\r
225             }\r
226 \r
227             /**\r
228                  * This method is for setting AllowedValues to Properties\r
229                  *\r
230                  * @param values - list of AllowedValues of Properties.\r
231                  */\r
232             template <typename T>\r
233             bool setAllowedValues(const std::vector<T> &values)\r
234             {\r
235                 ValueVariant temp = values.at(0);\r
236                 if (temp.which() != m_value.which())\r
237                 {\r
238                     return false;\r
239                 }\r
240 \r
241                 m_allowedValues.addValues(values);\r
242                 return true;\r
243             }\r
244 \r
245             /**\r
246                  * This method is for getting size of AllowedValues from Properties.\r
247                  *\r
248                  * @return  size of AllowedValues\r
249                  */\r
250             inline int getAllowedValuesSize() const\r
251             {\r
252                 return m_allowedValues.size();\r
253             }\r
254 \r
255             /**\r
256                  * This method is for getting AllowedValues from Properties.\r
257                  *\r
258                  * @return list of AllowedValues of Properties.\r
259                  */\r
260             inline std::vector<ValueVariant> getAllowedValues()\r
261             {\r
262                 return m_allowedValues.getValues();\r
263             }\r
264 \r
265             /**\r
266                  * This method is for getting AllowedValues as integer from Properties.\r
267                  *\r
268                  * @return list of AllowedValues as integer\r
269                  */\r
270             inline std::vector<int> getAllowedValuesInt()\r
271             {\r
272                 return m_allowedValues.getValuesInt();\r
273             }\r
274 \r
275             /**\r
276                  * This method is for getting AllowedValues as String from Properties.\r
277                  *\r
278                  * @return list of AllowedValues as String\r
279                  */\r
280             inline std::vector<std::string> getAllowedValuesString()\r
281             {\r
282                 return m_allowedValues.getValuesString();\r
283             }\r
284 \r
285             /**\r
286                  * This method is for getting AllowedValues as Double from Properties.\r
287                  *\r
288                  * @return list of AllowedValues as Double\r
289                  */\r
290             inline std::vector<double> getAllowedValuesDouble()\r
291             {\r
292                 return m_allowedValues.getValuesDouble();\r
293             }\r
294 \r
295             /**\r
296                  * This method is for getting AllowedValues as Bool from Properties.\r
297                  *\r
298                  * @return list of AllowedValues as Bool\r
299                  */\r
300             inline std::vector<bool> getAllowedValuesBool()\r
301             {\r
302                 return m_allowedValues.getValuesBool();\r
303             }\r
304 \r
305             /**\r
306                  * This method is for setting Description to Properties\r
307                  *\r
308                  * @param description - Description as string.\r
309                  */\r
310             inline void setDescription(const std::string &description)\r
311             {\r
312                 m_description = description;\r
313             }\r
314 \r
315             /**\r
316                  * This method is for getting Description from Properties.\r
317                  *\r
318                  * @return Description as string\r
319                  */\r
320             inline std::string getDescription()\r
321             {\r
322                 return m_description;\r
323             }\r
324 \r
325             /**\r
326                  * This method is for getting UpdateFrequency from Properties.\r
327                  *\r
328                  * @return UpdateFrequency as int\r
329                  */\r
330             inline int getUpdateFrequencyTime()\r
331             {\r
332                 return m_updateInterval;\r
333             }\r
334 \r
335             /**\r
336                  * This method is for setting UpdateFrequency to Properties\r
337                  *\r
338                  * @param interval - UpdateFrequency as int.\r
339                  */\r
340             inline void setUpdateFrequencyTime(int interval)\r
341             {\r
342                 m_updateInterval = interval;\r
343             }\r
344 \r
345             /**\r
346                  * This method is for setting Type to Properties\r
347                  *\r
348                  * @param type - Type as string.\r
349                  */\r
350             void setType(const std::string &type)\r
351             {\r
352                 m_type = type;\r
353             }\r
354 \r
355             /**\r
356                  * This method is for getting Type from Properties.\r
357                  *\r
358                  * @return Type as string\r
359                  */\r
360             std::string getType()\r
361             {\r
362                 return m_type;\r
363             }\r
364 \r
365             /**\r
366                  * This method is for setting Pattern to Properties\r
367                  *\r
368                  * @param pattern - Pattern as string.\r
369                  */\r
370             void setPattern(const std::string &pattern)\r
371             {\r
372                 m_pattern = pattern;\r
373             }\r
374 \r
375 \r
376             /**\r
377                  * This method is for getting Pattern from Properties.\r
378                  *\r
379                  * @return Pattern as string\r
380                  */\r
381             std::string getPattern()\r
382             {\r
383                 return m_pattern;\r
384             }\r
385 \r
386             /**\r
387                  * This method is for setting Format to Properties\r
388                  *\r
389                  * @param format - Format as string.\r
390                  */\r
391             void setFormat(const std::string &format)\r
392             {\r
393                 m_format = format;\r
394             }\r
395 \r
396             /**\r
397                  * This method is for getting Format from Properties.\r
398                  *\r
399                  * @return Format as string\r
400                  */\r
401             std::string getFormat()\r
402             {\r
403                 return m_format;\r
404             }\r
405 \r
406             /**\r
407                  * This method is for setting Items to Properties\r
408                  *\r
409                  * @param item - pointer to Items\r
410                  */\r
411             void setItem(const ItemsPtr &item)\r
412             {\r
413                 m_items.push_back(item);\r
414             }\r
415 \r
416             /**\r
417                  * This method is for getting Items from Properties.\r
418                  *\r
419                  * @return list of pointer to Items\r
420                  */\r
421             std::vector<ItemsPtr> const &getItems() const\r
422             {\r
423                 return m_items;\r
424             }\r
425 \r
426             /**\r
427                  * This method is for setting Unique to Properties\r
428                  *\r
429                  * @param value - Unique as bool\r
430                  */\r
431             void setUnique( int value)\r
432             {\r
433                 if (value == cJSON_True) m_unique = true;\r
434                 else m_unique = false;\r
435             }\r
436 \r
437             /**\r
438                  * This method is for getting isUnique from Properties.\r
439                  *\r
440                  * @return isUnique as bool\r
441                  */\r
442             bool getUnique()\r
443             {\r
444                 return m_unique;\r
445             }\r
446 \r
447             /**\r
448                  * This method is for setting AdditionalItems to Properties\r
449                  *\r
450                  * @param value - AdditionalItems as bool\r
451                  */\r
452             void setAdditionalItems(int value)\r
453             {\r
454                 if (value == cJSON_True) m_additionalItems = true;\r
455                 else m_additionalItems = false;\r
456             }\r
457 \r
458             /**\r
459                  * This method is for getting AdditionalItems from Properties.\r
460                  *\r
461                  * @return AdditionalItems as bool\r
462                  */\r
463             bool getAdditionalItems()\r
464             {\r
465                 return m_additionalItems;\r
466             }\r
467         private:\r
468             std::string m_name;\r
469             ValueVariant m_value;\r
470             int m_min;\r
471             int m_max;\r
472             int m_multipleOf;\r
473             AllowedValues m_allowedValues;\r
474             int m_updateInterval;\r
475             std::string m_type;\r
476             std::string m_pattern;\r
477             std::string m_format;\r
478             std::string m_description;\r
479             bool m_unique;\r
480             bool m_additionalItems;\r
481             std::vector<ItemsPtr > m_items;\r
482     };\r
483 \r
484     /** PropertiesPtr - shared Ptr to Properties.*/\r
485     typedef std::shared_ptr<Properties> PropertiesPtr;\r
486 \r
487 }\r
488 #endif\r