Add a maximum presence TTL of 24 hours
[platform/upstream/iotivity.git] / resource / include / OCRepresentation.h
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 /**
22  * @file
23  *
24  * This file contains the declaration of classes and its members related 
25  * to OCRepresentation.
26  */
27
28 #ifndef __OCREPRESENTATION_H
29 #define __OCREPRESENTATION_H
30
31
32 #include <string>
33 #include <sstream>
34 #include <vector>
35 #include <map>
36
37 #include <AttributeValue.h>
38 #include <StringConstants.h>
39
40 #ifdef __ANDROID__
41 #include "OCAndroid.h"
42 #endif
43
44 #include <OCException.h>
45 #ifdef __ANDROID__
46 #include "android_cpp11_compat.h"
47 #endif
48
49 namespace cereal
50 {
51     class access;
52 }
53
54 namespace OC
55 {
56
57     enum class InterfaceType
58     {
59         None,
60         LinkParent,
61         BatchParent,
62         DefaultParent,
63         LinkChild,
64         BatchChild,
65         DefaultChild
66     };
67
68     // The consumer requires resource info to be printed in 2 different ways, both with the "oc":[]
69     // and without.  This enum is used to differentiate between the two situations.  When the
70     // serialize is called with Include OC, we encode OC, otherwise we skip it and return just the
71     // contents of the array.
72     enum class OCInfoFormat
73     {
74         IncludeOC,
75         ExcludeOC
76     };
77
78     class MessageContainer
79     {
80         public:
81             void setJSONRepresentation(const std::string& payload);
82
83             void setJSONRepresentation(const char* payload);
84
85             std::string getJSONRepresentation(OCInfoFormat f) const;
86
87             const std::vector<OCRepresentation>& representations() const;
88
89             void addRepresentation(const OCRepresentation& rep);
90
91             const OCRepresentation& operator[](int index) const
92             {
93                 return m_reps[index];
94             }
95
96             const OCRepresentation& back() const
97             {
98                 return m_reps.back();
99             }
100         private:
101             std::vector<OCRepresentation> m_reps;
102     };
103     class OCRepresentation
104     {
105         public:
106             // Note: Implementation of all constructors and destructors
107             // are all placed in the same location due to a crash that
108             // was observed in Android, where merely constructing/destructing
109             // an OCRepresentation object was enough to cause an invalid 'free'.
110             // It is believed that this is a result of incompatible compiler
111             // options between the gradle JNI and armeabi scons build, however
112             // this fix will work in the meantime.
113             OCRepresentation(): m_interfaceType(InterfaceType::None){}
114
115             OCRepresentation(OCRepresentation&&) = default;
116
117             OCRepresentation(const OCRepresentation&) = default;
118
119             OCRepresentation& operator=(const OCRepresentation&) = default;
120
121             OCRepresentation& operator=(OCRepresentation&&) = default;
122
123             virtual ~OCRepresentation(){}
124
125             std::string getJSONRepresentation() const;
126
127             void addChild(const OCRepresentation&);
128
129             void clearChildren();
130
131             const std::vector<OCRepresentation>& getChildren() const;
132
133             void setChildren(const std::vector<OCRepresentation>& children);
134
135             void setUri(const std::string& uri);
136
137             std::string getUri() const;
138
139             const std::vector<std::string>& getResourceTypes() const;
140
141             void setResourceTypes(const std::vector<std::string>& resourceTypes);
142
143             const std::vector<std::string>& getResourceInterfaces() const;
144
145             void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
146
147             bool emptyData() const;
148
149             int numberOfAttributes() const;
150
151             bool erase(const std::string& str);
152
153             template <typename T>
154             void setValue(const std::string& str, const T& val)
155             {
156                 m_values[str] = val;
157             }
158
159             /**
160              *  Retrieve the attribute value associated with the supplied name
161              *
162              *  @param str Name of the attribute
163              *  @param val Value of the attribute
164              *  @return The getValue method returns true if the attribute was
165              *        found in the representation.  Otherwise it returns false.
166              */
167             template <typename T>
168             bool getValue(const std::string& str, T& val) const
169             {
170                 auto x = m_values.find(str);
171
172                 if(x!= m_values.end())
173                 {
174                     val = boost::get<T>(x->second);
175                     return true;
176                 }
177                 else
178                 {
179                     val = T();
180                     return false;
181                 }
182             }
183
184             /**
185              *  Return the attribute value associated with the supplied name
186              *
187              *  @param str Name of the attribute
188              *  @return When the representation contains the attribute, the
189              *       the associated value is returned.  Otherwise, getValue
190              *       returns the default contructed value for the type.
191              */
192             template <typename T>
193             T getValue(const std::string& str) const
194             {
195                 T val = T();
196                 auto x = m_values.find(str);
197                 if(x != m_values.end())
198                 {
199                     val = boost::get<T>(x->second);
200                 }
201                 return val;
202             }
203
204             std::string getValueToString(const std::string& key) const;
205             bool hasAttribute(const std::string& str) const;
206
207             void setNULL(const std::string& str);
208
209             bool isNULL(const std::string& str) const;
210
211             // STL Container stuff
212         public:
213             class iterator;
214             class const_iterator;
215             // Shim class to allow iterating and indexing of the OCRepresentation
216             // object.
217             class AttributeItem
218             {
219                 friend class OCRepresentation;
220                 friend class iterator;
221                 friend class const_iterator;
222                 public:
223                     const std::string& attrname() const;
224                     AttributeType type() const;
225                     AttributeType base_type() const;
226                     size_t depth() const;
227                     template<typename T>
228                     T getValue() const
229                     {
230                         return boost::get<T>(m_values[m_attrName]);
231                     }
232
233                     std::string getValueToString() const;
234
235                     template<typename T>
236                     AttributeItem& operator=(T&& rhs)
237                     {
238                         m_values[m_attrName] = std::forward<T>(rhs);
239                         return *this;
240                     }
241
242                     AttributeItem& operator=(std::nullptr_t rhs)
243                     {
244                         NullType t;
245                         m_values[m_attrName] = t;
246                         return *this;
247                     }
248
249                     // Enable-if required to prevent conversions to alternate types.  This prevents
250                     // ambigious conversions in the case where conversions can include a number of
251                     // types, such as the string constructor.
252                     template<typename T, typename= typename std::enable_if<
253                      std::is_same<T, int>::value ||
254                      std::is_same<T, double>::value ||
255                      std::is_same<T, bool>::value ||
256                      std::is_same<T, std::string>::value ||
257                      std::is_same<T, OCRepresentation>::value ||
258                      std::is_same<T, std::vector<int>>::value ||
259                      std::is_same<T, std::vector<std::vector<int>>>::value ||
260                      std::is_same<T, std::vector<std::vector<std::vector<int>>>>::value ||
261                      std::is_same<T, std::vector<double>>::value ||
262                      std::is_same<T, std::vector<std::vector<double>>>::value ||
263                      std::is_same<T, std::vector<std::vector<std::vector<double>>>>::value ||
264                      std::is_same<T, std::vector<bool>>::value ||
265                      std::is_same<T, std::vector<std::vector<bool>>>::value ||
266                      std::is_same<T, std::vector<std::vector<std::vector<bool>>>>::value ||
267                      std::is_same<T, std::vector<std::string>>::value ||
268                      std::is_same<T, std::vector<std::vector<std::string>>>::value ||
269                      std::is_same<T, std::vector<std::vector<std::vector<std::string>>>>::value ||
270                      std::is_same<T, std::vector<OCRepresentation>>::value ||
271                      std::is_same<T, std::vector<std::vector<OCRepresentation>>>::value ||
272                      std::is_same<T, std::vector<std::vector<std::vector<OCRepresentation>>>>::value
273                      >::type // enable_if
274                     >
275                     operator T() const
276                     {
277                         return this->getValue<T>();
278                     }
279
280                     operator std::nullptr_t() const
281                     {
282                         this->getValue<NullType>();
283                         return nullptr;
284                     }
285
286                 private:
287                     AttributeItem(const std::string& name,
288                             std::map<std::string, AttributeValue>& vals);
289                     AttributeItem(const AttributeItem&) = default;
290                     std::string m_attrName;
291                     std::map<std::string, AttributeValue>& m_values;
292             };
293
294             // Iterator to allow iteration via STL containers/methods
295             class iterator
296             {
297                 friend class OCRepresentation;
298                 public:
299                     typedef iterator self_type;
300                     typedef AttributeItem value_type;
301                     typedef value_type& reference;
302                     typedef value_type* pointer;
303                     typedef std::forward_iterator_tag iterator_category;
304                     typedef int difference_type;
305
306                     iterator(const iterator&) = default;
307                     ~iterator() = default;
308
309                     bool operator ==(const iterator&) const;
310                     bool operator !=(const iterator&) const;
311
312                     iterator& operator++();
313                     iterator operator++(int);
314
315                     reference operator*();
316                     pointer operator->();
317                 private:
318                     iterator(std::map<std::string, AttributeValue>::iterator&& itr,
319                             std::map<std::string, AttributeValue>& vals)
320                         : m_iterator(std::move(itr)),
321                         m_item(m_iterator != vals.end() ? m_iterator->first:"", vals){}
322                     std::map<std::string, AttributeValue>::iterator m_iterator;
323                     AttributeItem m_item;
324             };
325
326             class const_iterator
327             {
328                 friend class OCRepresentation;
329                 public:
330                     typedef iterator self_type;
331                     typedef const AttributeItem value_type;
332                     typedef value_type& const_reference;
333                     typedef value_type* const_pointer;
334                     typedef std::forward_iterator_tag iterator_category;
335                     typedef int difference_type;
336
337                     const_iterator(const iterator& rhs)
338                         :m_iterator(rhs.m_iterator), m_item(rhs.m_item){}
339                     const_iterator(const const_iterator&) = default;
340                     ~const_iterator() = default;
341
342                     bool operator ==(const const_iterator&) const;
343                     bool operator !=(const const_iterator&) const;
344
345                     const_iterator& operator++();
346                     const_iterator operator++(int);
347
348                     const_reference operator*() const;
349                     const_pointer operator->() const;
350                 private:
351                     const_iterator(std::map<std::string, AttributeValue>::const_iterator&& itr,
352                             std::map<std::string, AttributeValue>& vals)
353                         : m_iterator(std::move(itr)),
354                         m_item(m_iterator != vals.end() ? m_iterator->first: "", vals){}
355                     std::map<std::string, AttributeValue>::const_iterator m_iterator;
356                     AttributeItem m_item;
357             };
358
359             iterator begin();
360             const_iterator begin() const;
361             const_iterator cbegin() const;
362             iterator end();
363             const_iterator end() const;
364             const_iterator cend() const;
365             size_t size() const;
366             bool empty() const;
367
368             AttributeItem operator[](const std::string& key);
369             const AttributeItem operator[](const std::string& key) const;
370         private:
371             friend class OCResourceResponse;
372             friend class cereal::access;
373
374             // the root node has a slightly different JSON version
375             // based on the interface type configured in ResourceResponse.
376             // This allows ResourceResponse to set it, so that the save function
377             // doesn't serialize things that it isn't supposed to serialize.
378             void setInterfaceType(InterfaceType ift)
379             {
380                 m_interfaceType = ift;
381             }
382
383             // class used to wrap the 'prop' feature of the save/load
384             class Prop
385             {
386                 public:
387                     Prop(std::vector<std::string>& resourceTypes,
388                             std::vector<std::string>& interfaces)
389                     : m_types(resourceTypes), m_interfaces(interfaces)
390                     {}
391
392                  /*   Prop(const std::vector<std::string>& resourceTypes,
393                             const std::vector<std::string>& interfaces)
394                     :m_types(resourceTypes),
395                     m_interfaces(interfaces)
396                     {}*/
397                 private:
398                     friend class cereal::access;
399                     template <class Archive>
400                     void save(Archive& ar) const;
401
402                     template<class Archive>
403                     void load(Archive& ar);
404
405                     std::vector<std::string>& m_types;
406                     std::vector<std::string>& m_interfaces;
407             };
408             template<class Archive, class Val>
409             static void optional_load(Archive& ar, Val&& v);
410
411             template<class Archive>
412             void save(Archive& ar) const;
413
414             template<class Archive>
415             void load(Archive& ar);
416
417         private:
418             std::string m_uri;
419             std::vector<OCRepresentation> m_children;
420             mutable std::map<std::string, AttributeValue> m_values;
421             std::vector<std::string> m_resourceTypes;
422             std::vector<std::string> m_interfaces;
423
424             InterfaceType m_interfaceType;
425     };
426
427     std::ostream& operator <<(std::ostream& os, const OCRepresentation::AttributeItem& ai);
428 } // namespace OC
429
430
431 #endif //__OCREPRESENTATION_H
432