Merge "Merge branch 'master' into easysetup" into easysetup
[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),\r
54                 m_multipleOf(INT_MAX), 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),\r
63                 m_unique(false), m_additionalItems(false) {}\r
64 \r
65             /**\r
66                  * This method is for getting Name from Properties.\r
67                  *\r
68                  * @return Properties name as string\r
69                  */\r
70             inline std::string getName(void) const\r
71             {\r
72                 return m_name;\r
73             }\r
74 \r
75             /**\r
76                  * This method is for setting name to Properties\r
77                  *\r
78                  * @param name - Properties name as string.\r
79                  */\r
80             inline void setName(const std::string &name)\r
81             {\r
82                 m_name = name;\r
83             }\r
84 \r
85             /**\r
86                  * This method is for getting Value from Properties.\r
87                  *\r
88                  * @return Properties Value\r
89                  */\r
90             template <typename T>\r
91             T getValue() const\r
92             {\r
93                 return boost::get<T>(m_value);\r
94             }\r
95 \r
96             /**\r
97                  * This method is for getting Value from Properties.\r
98                  *\r
99                  * @return Properties Value\r
100                  */\r
101             ValueVariant &getValue()\r
102             {\r
103                 return m_value;\r
104             }\r
105 \r
106             /**\r
107                  * This method is for getting ValueVariant type from Properties.\r
108                  *\r
109                  * @return Properties Value type as Int\r
110                  */\r
111             int getValueType() const\r
112             {\r
113                 return m_value.which();\r
114             }\r
115 \r
116             /**\r
117                  * This method is for getting ValueVariant type from Properties.\r
118                  *\r
119                  * @return Properties VariantType type\r
120                  */\r
121             VariantType getVariantType() const\r
122             {\r
123                 if (m_value.which() == 3)\r
124                     return VariantType::STRING;\r
125                 else if (m_value.which() == 2)\r
126                     return VariantType::BOOL;\r
127                 else if (m_value.which() == 1)\r
128                     return VariantType::DOUBLE;\r
129                 else\r
130                     return VariantType::INT;\r
131             }\r
132 \r
133             /**\r
134                  * This method is for getting Value type as Integer from Properties.\r
135                  *\r
136                  * @return Properties Value type as Integer\r
137                  */\r
138             int getValueInt()\r
139             {\r
140                 return boost::lexical_cast<int> (m_value);\r
141             }\r
142 \r
143             /**\r
144                  * This method is for getting Value type as String from Properties.\r
145                  *\r
146                  * @return Properties Value type as String\r
147                  */\r
148             std::string getValueString()\r
149             {\r
150                 return boost::lexical_cast<std::string> (m_value);\r
151             }\r
152 \r
153             /**\r
154                  * This method is for getting Value type as double from Properties.\r
155                  *\r
156                  * @return Properties Value type as double\r
157                  */\r
158             double getValueDouble()\r
159             {\r
160                 return boost::lexical_cast<double> (m_value);\r
161             }\r
162 \r
163             /**\r
164                  * This method is for getting Value type as bool from Properties.\r
165                  *\r
166                  * @return Properties Value type as bool\r
167                  */\r
168             bool getValueBool()\r
169             {\r
170                 return boost::lexical_cast<bool> (m_value);\r
171             }\r
172 \r
173             /**\r
174                  * This method is for setting Value to Properties\r
175                  *\r
176                  * @param value - Properties Value.\r
177                  */\r
178             template <typename T>\r
179             void setValue(const T &value)\r
180             {\r
181                 m_value = value;\r
182             }\r
183 \r
184             /**\r
185                  * This method is for getting Range from Properties.\r
186                  *\r
187                  * @param min - reference to hold Minimum value of Properties.\r
188                  * @param max -  reference to hold Maximum value of Properties.\r
189                  * @param multipleOf -  reference to hold multipleOf value of Properties.\r
190                  */\r
191             inline void getRange(double &min, double &max, int &multipleOf) const\r
192             {\r
193                 min = m_min;\r
194                 max = m_max;\r
195                 multipleOf = m_multipleOf;\r
196             }\r
197 \r
198             /**\r
199                  * This method is for setting Minimum to Properties.\r
200                  *\r
201                  * @param min - Minimum value of Properties.\r
202                  */\r
203             inline void setMin(double min)\r
204             {\r
205                 m_min = min;\r
206             }\r
207 \r
208             /**\r
209                  * This method is for setting Maximum to Properties\r
210                  *\r
211                  * @param max - Maximum value of Properties.\r
212                  */\r
213             inline void setMax(double max)\r
214             {\r
215                 m_max = max;\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 setting Type to Properties\r
327                  *\r
328                  * @param type - Type as string.\r
329                  */\r
330             void setType(const std::string &type)\r
331             {\r
332                 m_type = type;\r
333             }\r
334 \r
335             /**\r
336                  * This method is for getting Type from Properties.\r
337                  *\r
338                  * @return Type as string\r
339                  */\r
340             std::string getType()\r
341             {\r
342                 return m_type;\r
343             }\r
344 \r
345             /**\r
346                  * This method is for setting Pattern to Properties\r
347                  *\r
348                  * @param pattern - Pattern as string.\r
349                  */\r
350             void setPattern(const std::string &pattern)\r
351             {\r
352                 m_pattern = pattern;\r
353             }\r
354 \r
355 \r
356             /**\r
357                  * This method is for getting Pattern from Properties.\r
358                  *\r
359                  * @return Pattern as string\r
360                  */\r
361             std::string getPattern()\r
362             {\r
363                 return m_pattern;\r
364             }\r
365 \r
366             /**\r
367                  * This method is for setting Format to Properties\r
368                  *\r
369                  * @param format - Format as string.\r
370                  */\r
371             void setFormat(const std::string &format)\r
372             {\r
373                 m_format = format;\r
374             }\r
375 \r
376             /**\r
377                  * This method is for getting Format from Properties.\r
378                  *\r
379                  * @return Format as string\r
380                  */\r
381             std::string getFormat()\r
382             {\r
383                 return m_format;\r
384             }\r
385 \r
386             /**\r
387                  * This method is for setting Items to Properties\r
388                  *\r
389                  * @param item - pointer to Items\r
390                  */\r
391             void setItem(const ItemsPtr &item)\r
392             {\r
393                 m_items.push_back(item);\r
394             }\r
395 \r
396             /**\r
397                  * This method is for getting Items from Properties.\r
398                  *\r
399                  * @return list of pointer to Items\r
400                  */\r
401             std::vector<ItemsPtr> const &getItems() const\r
402             {\r
403                 return m_items;\r
404             }\r
405 \r
406             /**\r
407                  * This method is for setting Unique to Properties\r
408                  *\r
409                  * @param value - Unique as bool\r
410                  */\r
411             void setUnique( int value)\r
412             {\r
413                 if (value == cJSON_True) m_unique = true;\r
414                 else m_unique = false;\r
415             }\r
416 \r
417             /**\r
418                  * This method is for getting isUnique from Properties.\r
419                  *\r
420                  * @return isUnique as bool\r
421                  */\r
422             bool getUnique()\r
423             {\r
424                 return m_unique;\r
425             }\r
426 \r
427             /**\r
428                  * This method is for setting AdditionalItems to Properties\r
429                  *\r
430                  * @param value - AdditionalItems as bool\r
431                  */\r
432             void setAdditionalItems(int value)\r
433             {\r
434                 if (value == cJSON_True) m_additionalItems = true;\r
435                 else m_additionalItems = false;\r
436             }\r
437 \r
438             /**\r
439                  * This method is for getting AdditionalItems from Properties.\r
440                  *\r
441                  * @return AdditionalItems as bool\r
442                  */\r
443             bool getAdditionalItems()\r
444             {\r
445                 return m_additionalItems;\r
446             }\r
447         private:\r
448             std::string m_name;\r
449             ValueVariant m_value;\r
450             double m_min;\r
451             double m_max;\r
452             int m_multipleOf;\r
453             AllowedValues m_allowedValues;\r
454             std::string m_type;\r
455             std::string m_pattern;\r
456             std::string m_format;\r
457             std::string m_description;\r
458             bool m_unique;\r
459             bool m_additionalItems;\r
460             std::vector<ItemsPtr > m_items;\r
461     };\r
462 \r
463     /** PropertiesPtr - shared Ptr to Properties.*/\r
464     typedef std::shared_ptr<Properties> PropertiesPtr;\r
465 \r
466 }\r
467 #endif\r