Imported Upstream version 1.1.0
[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     /**\r
68      * @class   ValueProperty\r
69      * @brief   This class provides data Model for Json Schema Value Property.\r
70      */\r
71     class ValueProperty\r
72     {\r
73         public:\r
74             /** Type - enumeration for ValueProperty types*/\r
75             enum class Type\r
76             {\r
77                 UNKNOWN,\r
78                 RANGE,\r
79                 VALUE_SET,\r
80                 PATTERN,\r
81                 FORMAT,\r
82                 ARRAY\r
83             };\r
84 \r
85             /**\r
86              * Constructor of ValueProperty.\r
87              */\r
88             ValueProperty();\r
89 \r
90             ValueProperty(const ValueProperty &) = default;\r
91 \r
92             ValueProperty &operator=(const ValueProperty &) = default;\r
93 \r
94             ValueProperty(ValueProperty &&) = default;\r
95 \r
96             ValueProperty &operator=(ValueProperty &&) = default;\r
97 \r
98             /**\r
99              * explicit Constructor of ValueProperty for Type RANGE.\r
100              *\r
101              * @param min - minimum value of property.\r
102              * @param max- maximum value of property.\r
103              * @param multipleOf- multipleOf value of property.\r
104              */\r
105             explicit ValueProperty(double min, double max, int multipleOf);\r
106 \r
107             /**\r
108              * explicit Constructor of ValueProperty for Type VALUE_SET.\r
109              *\r
110              * @param valueSet - allowed values in the Properties.\r
111              */\r
112             explicit ValueProperty(const std::vector<int> &valueSet);\r
113 \r
114             /**\r
115              * explicit Constructor of ValueProperty for Type VALUE_SET.\r
116              *\r
117              * @param valueSet - allowed values in the Properties.\r
118              */\r
119             explicit ValueProperty(const std::vector<double> &valueSet);\r
120 \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<bool> &valueSet);\r
127 \r
128             /**\r
129              * explicit Constructor of ValueProperty for Type VALUE_SET.\r
130              *\r
131              * @param valueSet - allowed values in the Properties.\r
132              */\r
133             explicit ValueProperty(const std::vector<std::string> &valueSet);\r
134 \r
135             /**\r
136              * explicit Constructor of ValueProperty for Type VALUE_SET.\r
137              *\r
138              * @param valueSet - allowed values in the Properties.\r
139              */\r
140             explicit ValueProperty(const std::vector<ValueVariant> &valueSet);\r
141 \r
142             /**\r
143              * explicit Constructor of ValueProperty for Type PATTERN or FORMAT.\r
144              *\r
145              * @param type - ValueProperty Type.\r
146              * @param value - value for the pattern or format.\r
147              */\r
148             explicit ValueProperty(Type type, std::string value);\r
149 \r
150             /**\r
151              * explicit Constructor of ValueProperty for Type ARRAY.\r
152              *\r
153              * @param type - ValueProperty Type.\r
154              * @param minItems - minimum elements in the Array property.\r
155              * @param maxItems - maximum elements in the Array property.\r
156              * @param unique - unique elements in the Array property.\r
157              * @param additionalItems - additional elements in the Array property.\r
158              */\r
159             explicit ValueProperty(Type type, int minItems, int maxItems, bool unique, bool additionalItems);\r
160 \r
161             /**\r
162              * This method is for getting type of ValueProperty.\r
163              *\r
164              * @return Type of ValueProperty\r
165              */\r
166             Type type() const;\r
167 \r
168             /**\r
169              * This method is for getting minimum value of ValueProperty.\r
170              *\r
171              * @return min value of ValueProperty\r
172              */\r
173             double min() const;\r
174 \r
175             /**\r
176              * This method is for getting maximum value of ValueProperty.\r
177              *\r
178              * @return max value of ValueProperty\r
179              */\r
180             double max() const;\r
181 \r
182             /**\r
183              * This method is for getting multipleOf value of ValueProperty.\r
184              *\r
185              * @return multipleOf value of ValueProperty\r
186              */\r
187             int multipleOf() const;\r
188 \r
189             /**\r
190              * This method is for getting pattern value of ValueProperty.\r
191              *\r
192              * @return pattern value of ValueProperty\r
193              */\r
194             std::string pattern() const;\r
195 \r
196             /**\r
197              * This method is for getting format value of ValueProperty.\r
198              *\r
199              * @return format value of ValueProperty\r
200              */\r
201             std::string format() const;\r
202 \r
203             /**\r
204              * This method is for getting valueSet of ValueProperty.\r
205              *\r
206              * @return valueSet of ValueProperty\r
207              */\r
208             int valueSetSize() const;\r
209 \r
210             /**\r
211              * This method is for getting valueSet of ValueProperty.\r
212              *\r
213              * @return valueSet of ValueProperty\r
214              */\r
215             std::vector<ValueVariant> valueSet() const;\r
216 \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 type = VariantType::UNKNOWN,\r
263                              VariantType baseType = VariantType::UNKNOWN,\r
264                              int depth = 0);\r
265 \r
266                     TypeInfo(const TypeInfo &) = default;\r
267 \r
268                     TypeInfo &operator=(const TypeInfo &) = default;\r
269 \r
270                     TypeInfo(TypeInfo &&) = default;\r
271 \r
272                     TypeInfo &operator=(TypeInfo &&) = default;\r
273 \r
274                     /**\r
275                      * This method is for getting type of properties.\r
276                      *\r
277                      * @return VariantType of Property\r
278                      */\r
279                     VariantType type() const;\r
280 \r
281                     /**\r
282                      * This method is for getting base or parent type of properties.\r
283                      *\r
284                      * @return VariantType of parent Property\r
285                      */\r
286                     VariantType baseType() const;\r
287 \r
288                     /**\r
289                      * This method is for getting depth of properties.\r
290                      *\r
291                      * @return depth as int\r
292                      */\r
293                     int depth() const;\r
294 \r
295                     /**\r
296                      *  operator for TypeInfo to check equality.\r
297                      *\r
298                      * @param TypeInfo.\r
299                      */\r
300                     bool operator ==(const TypeInfo &) const;\r
301 \r
302                     /**\r
303                      *  operator for TypeInfo to check inequality.\r
304                      *\r
305                      * @param TypeInfo.\r
306                      */\r
307                     bool operator !=(const TypeInfo &) const;\r
308 \r
309                 private:\r
310                     VariantType m_type;\r
311                     VariantType m_baseType;\r
312                     int m_depth;\r
313             };\r
314 \r
315             /**\r
316               * Constructor of Properties.\r
317               *\r
318               * @param name - Properties name as string.\r
319               */\r
320             Properties(const std::string &name) : m_name(name) {}\r
321 \r
322             Properties() = default;\r
323 \r
324             Properties(const Properties &) = default;\r
325 \r
326             Properties &operator=(const Properties &) = default;\r
327 \r
328             Properties(Properties &&) = default;\r
329 \r
330             Properties &operator=(Properties &&) = default;\r
331 \r
332             /**\r
333              * This method is for getting TypeInfo of Properties.\r
334              *\r
335              * @return Properties TypeInfo\r
336              */\r
337             TypeInfo getType() const;\r
338 \r
339             /**\r
340              * This method is for setting type of Properties.\r
341              *\r
342              * @param type -Propertie's Type\r
343              */\r
344             void setTypeString(const std::string &type);\r
345 \r
346             /**\r
347              * This method is for getting Name from Properties.\r
348              *\r
349              * @return Properties name as string\r
350              */\r
351             std::string getName() const;\r
352 \r
353             /**\r
354              * This method is for setting name to Properties\r
355              *\r
356              * @param name - Properties name as string.\r
357              */\r
358             void setName(const std::string &name);\r
359 \r
360             /**\r
361              * This method is for getting Description from Properties.\r
362              *\r
363              * @return Description as string\r
364              */\r
365             std::string getDescription() const;\r
366 \r
367             /**\r
368              * This method is for setting Description to Properties\r
369              *\r
370              * @param description - Description as string.\r
371              */\r
372             void setDescription(const std::string &description);\r
373 \r
374             /**\r
375              * This method is for setting Value to Properties.\r
376              *\r
377              * @param value -  Value of Properties\r
378              */\r
379             template <typename T>\r
380             void setValue(const T &value)\r
381             {\r
382                 m_value = std::make_shared<ValueVariant>(value);\r
383             }\r
384 \r
385             /**\r
386              * This method is for getting Value from Properties.\r
387              *\r
388              * @return Properties Value\r
389              */\r
390             ValueVariant getValue() const;\r
391 \r
392             /**\r
393              * This method is for getting Value from Properties.\r
394              *\r
395              * @return Properties Value\r
396              */\r
397             template <typename T>\r
398             T getValue() const\r
399             {\r
400                 return boost::get<T>(*(m_value.get()));\r
401             }\r
402 \r
403             /**\r
404              * This method is for checking if default Value exists in the Properties.\r
405              *\r
406              * @return true if present and false if not present\r
407              */\r
408             bool isDefaultValue() const { return ((m_value != nullptr) ? true : false); }\r
409 \r
410             /**\r
411              * This method is for getting ValueProperty from Properties.\r
412              *\r
413              * @return vector of pointer to ValueProperty\r
414              */\r
415             std::vector<std::shared_ptr<ValueProperty> > const &getValueProperties() const;\r
416 \r
417             /**\r
418              * This method is for setting ValueProperty to Properties\r
419              *\r
420              * @param value - pointer to ValueProperty\r
421              */\r
422             void setValueProperty(const std::shared_ptr<ValueProperty> &value);\r
423 \r
424 \r
425             /**\r
426              * This method is for getting RequiredValue from Properties.\r
427              *\r
428              * @return list of RequiredValue as string\r
429              */\r
430             std::vector<std::string> const &getRequiredValues() const;\r
431 \r
432             /**\r
433              * This method is for setting RequiredValue to Properties\r
434              *\r
435              * @param reqValue - RequiredValue as string.\r
436              */\r
437             void setRequiredValue(const std::string &reqValue);\r
438 \r
439         private:\r
440             TypeInfo m_type;\r
441             std::string m_typeString;\r
442             std::string m_name;\r
443             std::string m_description;\r
444             std::shared_ptr<ValueVariant> m_value;\r
445             std::vector<std::shared_ptr<ValueProperty> > m_valueProperty;\r
446             std::vector<std::string> m_required;\r
447     };\r
448 \r
449     /** PropertiesPtr - shared Ptr to Properties.*/\r
450     typedef std::shared_ptr<Properties> PropertiesPtr;\r
451 \r
452     /** ValuePropertyPtr - shared Ptr to ValueProperty.*/\r
453     typedef std::shared_ptr<ValueProperty> ValuePropertyPtr;\r
454 \r
455 }\r
456 #endif\r