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