04e9dc881cd42629a82171989bb43318bb786ed2
[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 "cJSON.h"\r
37 #include <memory>\r
38 \r
39 namespace RAML\r
40 {\r
41     class Properties;\r
42 \r
43     /** ValueVariant - Boost Variant to hold type of property*/\r
44     typedef boost::variant <\r
45     int,\r
46     double,\r
47     bool,\r
48     std::string,\r
49     Properties,\r
50     std::vector<Properties>\r
51 \r
52     > ValueVariant;\r
53 \r
54     /** VariantType - enumeration for variant types*/\r
55     enum class VariantType\r
56     {\r
57         UNKNOWN,\r
58         INTEGER,\r
59         DOUBLE,\r
60         BOOLEAN,\r
61         STRING,\r
62         PROPERTY,\r
63         ARRAY,\r
64         OBJECT\r
65     };\r
66     /**\r
67         * @class   ValueProperty\r
68         * @brief   This class provides data Model for Json Schema Value Property.\r
69         */\r
70     class ValueProperty\r
71     {\r
72         public:\r
73             /** Type - enumeration for ValueProperty types*/\r
74             enum class Type\r
75             {\r
76                 UNKNOWN,\r
77                 RANGE,\r
78                 VALUE_SET,\r
79                 PATTERN,\r
80                 FORMAT,\r
81                 ARRAY\r
82             };\r
83 \r
84             /**\r
85                              * Constructor of ValueProperty.\r
86                              */\r
87             ValueProperty();\r
88             /**\r
89                              * Copy Constructor of ValueProperty.\r
90                              *\r
91                              * @param ValueProperty.\r
92                              */\r
93             ValueProperty(const ValueProperty &) = default;\r
94             /**\r
95                              * Assignment operator for  ValueProperty.\r
96                              *\r
97                              * @param ValueProperty.\r
98                              */\r
99             ValueProperty &operator=(const ValueProperty &) = default;\r
100             /**\r
101                              * Copy Constructor of ValueProperty.\r
102                              *\r
103                              * @param ValueProperty.\r
104                              */\r
105             ValueProperty(ValueProperty &&) = default;\r
106             /**\r
107                              * Assignment operator for  ValueProperty.\r
108                              *\r
109                              * @param ValueProperty.\r
110                              */\r
111             ValueProperty &operator=(ValueProperty &&) = default;\r
112 \r
113             /**\r
114                              * explicit Constructor of ValueProperty for Type RANGE.\r
115                              *\r
116                              * @param min - minimum value of property.\r
117                              * @param max- maximum value of property.\r
118                              * @param multipleOf- multipleOf value of property.\r
119                              */\r
120             explicit ValueProperty(double min, double max, int multipleOf);\r
121             /**\r
122                              * explicit Constructor of ValueProperty for Type VALUE_SET.\r
123                              *\r
124                              * @param valueSet - allowed values in the Properties.\r
125                              */\r
126             explicit ValueProperty(const std::vector<int> &valueSet);\r
127             /**\r
128                              * explicit Constructor of ValueProperty for Type VALUE_SET.\r
129                              *\r
130                              * @param valueSet - allowed values in the Properties.\r
131                              */\r
132             explicit ValueProperty(const std::vector<double> &valueSet);\r
133             /**\r
134                              * explicit Constructor of ValueProperty for Type VALUE_SET.\r
135                              *\r
136                              * @param valueSet - allowed values in the Properties.\r
137                              */\r
138             explicit ValueProperty(const std::vector<bool> &valueSet);\r
139             /**\r
140                              * explicit Constructor of ValueProperty for Type VALUE_SET.\r
141                              *\r
142                              * @param valueSet - allowed values in the Properties.\r
143                              */\r
144             explicit ValueProperty(const std::vector<std::string> &valueSet);\r
145             /**\r
146                              * explicit Constructor of ValueProperty for Type VALUE_SET.\r
147                              *\r
148                              * @param valueSet - allowed values in the Properties.\r
149                              */\r
150             explicit ValueProperty(const std::vector<ValueVariant> &valueSet);\r
151             /**\r
152                              * explicit Constructor of ValueProperty for Type PATTERN or FORMAT.\r
153                              *\r
154                              * @param type - ValueProperty Type.\r
155                              * @param value - value for the pattern or format.\r
156                              */\r
157             explicit ValueProperty(Type type, std::string value);\r
158             /**\r
159                              * explicit Constructor of ValueProperty for Type ARRAY.\r
160                              *\r
161                              * @param type - ValueProperty Type.\r
162                              * @param minItems - minimum elements in the Array property.\r
163                              * @param maxItems - maximum elements in the Array property.\r
164                              * @param unique - unique elements in the Array property.\r
165                              * @param additionalItems - additional elements in the Array property.\r
166                              */\r
167             explicit ValueProperty(Type type, int minItems, int maxItems, bool unique, bool additionalItems);\r
168 \r
169             /**\r
170                              * This method is for getting type of ValueProperty.\r
171                              *\r
172                              * @return Type of ValueProperty\r
173                              */\r
174             Type type() const;\r
175             /**\r
176                              * This method is for getting minimum value of ValueProperty.\r
177                              *\r
178                              * @return min value of ValueProperty\r
179                              */\r
180             double min() const;\r
181             /**\r
182                              * This method is for getting maximum value of ValueProperty.\r
183                              *\r
184                              * @return max value of ValueProperty\r
185                              */\r
186             double max() const;\r
187             /**\r
188                              * This method is for getting multipleOf value of ValueProperty.\r
189                              *\r
190                              * @return multipleOf value of ValueProperty\r
191                              */\r
192             int multipleOf() const;\r
193             /**\r
194                              * This method is for getting pattern value of ValueProperty.\r
195                              *\r
196                              * @return pattern value of ValueProperty\r
197                              */\r
198             std::string pattern() const;\r
199             /**\r
200                              * This method is for getting format value of ValueProperty.\r
201                              *\r
202                              * @return format value of ValueProperty\r
203                              */\r
204             std::string format() const;\r
205             /**\r
206                              * This method is for getting valueSet of ValueProperty.\r
207                              *\r
208                              * @return valueSet of ValueProperty\r
209                              */\r
210             int valueSetSize() const;\r
211             /**\r
212                              * This method is for getting valueSet of ValueProperty.\r
213                              *\r
214                              * @return valueSet of ValueProperty\r
215                              */\r
216             std::vector<ValueVariant> valueSet() const;\r
217             /**\r
218                              * This method is for getting valueArray of ValueProperty.\r
219                              *\r
220                              * @param minItems - reference to get minimum elements in the Array property.\r
221                              * @param maxItems - reference to get maximum elements in the Array property.\r
222                              * @param unique - reference to get unique elements in the Array property.\r
223                              * @param additionalItems - reference to get additional elements in the Array property.\r
224                              */\r
225             void valueArray(int &minItems, int &maxItems, bool &unique, bool &additionalItems) const;\r
226 \r
227         private:\r
228             Type m_type;\r
229             double m_min;\r
230             double m_max;\r
231             int m_multipleOf;\r
232             std::vector<ValueVariant> m_valueSet;\r
233             std::string m_pattern;\r
234             std::string m_format;\r
235             int m_minItems;\r
236             int m_maxItems;\r
237             bool m_unique;\r
238             bool m_additionalItems;\r
239     };\r
240 \r
241     /**\r
242      * @class   Properties\r
243      * @brief   This class provides data Model for Json Schema Properties.\r
244      */\r
245     class Properties\r
246     {\r
247         public:\r
248             /**\r
249                  * @class   TypeInfo\r
250                  * @brief   This class provides type information of Json Properties.\r
251                  */\r
252             class TypeInfo\r
253             {\r
254                 public:\r
255                     /**\r
256                                      * Constructor of TypeInfo.\r
257                                      *\r
258                                      * @param VariantType - type of property.\r
259                                      * @param VariantType - type of parent property.\r
260                                      * @param int - depth of property.\r
261                                      */\r
262                     TypeInfo(VariantType, VariantType, int);\r
263                     /**\r
264                                      * Copy Constructor of TypeInfo.\r
265                                      *\r
266                                      * @param TypeInfo.\r
267                                      */\r
268                     TypeInfo(const TypeInfo &) = default;\r
269                     /**\r
270                                      * Assignment operator for  TypeInfo.\r
271                                      *\r
272                                      * @param TypeInfo.\r
273                                      */\r
274                     TypeInfo &operator=(const TypeInfo &) = default;\r
275                     /**\r
276                                      * Copy Constructor of TypeInfo.\r
277                                      *\r
278                                      * @param TypeInfo.\r
279                                      */\r
280                     TypeInfo(TypeInfo &&) = default;\r
281                     /**\r
282                                      * Assignment operator for  TypeInfo.\r
283                                      *\r
284                                      * @param TypeInfo.\r
285                                      */\r
286                     TypeInfo &operator=(TypeInfo &&) = default;\r
287                     /**\r
288                                      * Constructor of TypeInfo.\r
289                                      */\r
290                     TypeInfo() = default;\r
291 \r
292                     /**\r
293                                      * This method is for getting type of properties.\r
294                                      *\r
295                                      * @return VariantType of Property\r
296                                      */\r
297                     VariantType type() const;\r
298                     /**\r
299                                      * This method is for getting base or parent type of properties.\r
300                                      *\r
301                                      * @return VariantType of parent Property\r
302                                      */\r
303                     VariantType baseType() const;\r
304                     /**\r
305                                      * This method is for getting depth of properties.\r
306                                      *\r
307                                      * @return depth as int\r
308                                      */\r
309                     int depth() const;\r
310                     /**\r
311                                      *  operator for TypeInfo to check equality.\r
312                                      *\r
313                                      * @param TypeInfo.\r
314                                      */\r
315                     bool operator ==(const TypeInfo &) const;\r
316                     /**\r
317                                      *  operator for TypeInfo to check inequality.\r
318                                      *\r
319                                      * @param TypeInfo.\r
320                                      */\r
321                     bool operator !=(const TypeInfo &) const;\r
322 \r
323                 private:\r
324                     VariantType m_type;\r
325                     VariantType m_baseType;\r
326                     int m_depth;\r
327             };\r
328 \r
329             /**\r
330                   * Constructor of Properties.\r
331                   *\r
332                   * @param name - Properties name as string.\r
333                   */\r
334             Properties(const std::string &name) : m_name(name) {}\r
335             /**\r
336                   * Constructor of Properties.\r
337                   */\r
338             Properties() = default;\r
339             /**\r
340                      * Copy Constructor of Properties.\r
341                      *\r
342                      * @param Properties.\r
343                      */\r
344             Properties(const Properties &) = default;\r
345             /**\r
346                      * Assignment operator for  Properties.\r
347                      *\r
348                      * @param Properties.\r
349                      */\r
350             Properties &operator=(const Properties &) = default;\r
351             /**\r
352                      * Copy Constructor of Properties.\r
353                      *\r
354                      * @param Properties.\r
355                      */\r
356             Properties(Properties &&) = default;\r
357             /**\r
358                      * Assignment operator for  Properties.\r
359                      *\r
360                      * @param Properties.\r
361                      */\r
362             Properties &operator=(Properties &&) = default;\r
363 \r
364             /**\r
365                  * This method is for getting TypeInfo of Properties.\r
366                  *\r
367                  * @return Properties TypeInfo\r
368                  */\r
369             TypeInfo getType() const;\r
370 \r
371             /**\r
372                  * This method is for setting type of Properties.\r
373                  *\r
374                  * @param type -Propertie's Type\r
375                  */\r
376             void setTypeString(const std::string &type);\r
377 \r
378             /**\r
379                  * This method is for getting Name from Properties.\r
380                  *\r
381                  * @return Properties name as string\r
382                  */\r
383             std::string getName() const;\r
384             /**\r
385                  * This method is for setting name to Properties\r
386                  *\r
387                  * @param name - Properties name as string.\r
388                  */\r
389             void setName(const std::string &name);\r
390             /**\r
391                  * This method is for getting Description from Properties.\r
392                  *\r
393                  * @return Description as string\r
394                  */\r
395             std::string getDescription() const;\r
396 \r
397             /**\r
398                  * This method is for setting Description to Properties\r
399                  *\r
400                  * @param description - Description as string.\r
401                  */\r
402             void setDescription(const std::string &description);\r
403 \r
404             /**\r
405                  * This method is for setting Value to Properties.\r
406                  *\r
407                  * @param value -  Value of Properties\r
408                  */\r
409             template <typename T>\r
410             void setValue(const T &value)\r
411             {\r
412                 m_value = std::make_shared<ValueVariant>(value);\r
413             }\r
414 \r
415             /**\r
416                  * This method is for getting Value from Properties.\r
417                  *\r
418                  * @return Properties Value\r
419                  */\r
420             ValueVariant getValue() const;\r
421 \r
422             /**\r
423                  * This method is for getting Value from Properties.\r
424                  *\r
425                  * @return Properties Value\r
426                  */\r
427             template <typename T>\r
428             T getValue() const\r
429             {\r
430                 return boost::get<T>(*(m_value.get()));\r
431             }\r
432 \r
433             /**\r
434                  * This method is for checking if default Value exists in the Properties.\r
435                  *\r
436                  * @return true if present and false if not present\r
437                  */\r
438             bool isDefaultValue() const { return ((m_value != nullptr) ? true : false); }\r
439             /**\r
440                  * This method is for getting ValueProperty from Properties.\r
441                  *\r
442                  * @return vector of pointer to ValueProperty\r
443                  */\r
444             std::vector<std::shared_ptr<ValueProperty> > const &getValueProperties() const;\r
445 \r
446             /**\r
447                  * This method is for setting ValueProperty to Properties\r
448                  *\r
449                  * @param value - pointer to ValueProperty\r
450                  */\r
451             void setValueProperty(const std::shared_ptr<ValueProperty> &value);\r
452 \r
453 \r
454             /**\r
455                  * This method is for getting RequiredValue from Properties.\r
456                  *\r
457                  * @return list of RequiredValue as string\r
458                  */\r
459             std::vector<std::string> const &getRequiredValues() const;\r
460 \r
461             /**\r
462                  * This method is for setting RequiredValue to Properties\r
463                  *\r
464                  * @param reqValue - RequiredValue as string.\r
465                  */\r
466             void setRequiredValue(const std::string &reqValue);\r
467 \r
468         private:\r
469             TypeInfo m_type;\r
470             std::string m_typeString;\r
471             std::string m_name;\r
472             std::string m_description;\r
473             std::shared_ptr<ValueVariant> m_value;\r
474             std::vector<std::shared_ptr<ValueProperty> > m_valueProperty;\r
475             std::vector<std::string> m_required;\r
476     };\r
477 \r
478     /** PropertiesPtr - shared Ptr to Properties.*/\r
479     typedef std::shared_ptr<Properties> PropertiesPtr;\r
480 \r
481     /** ValuePropertyPtr - shared Ptr to ValueProperty.*/\r
482     typedef std::shared_ptr<ValueProperty> ValuePropertyPtr;\r
483 \r
484 }\r
485 #endif\r