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