Imported Upstream version 0.9.1
[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
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            /**
202             *  Retrieve the attributevalue structure associated with the supplied name
203             *
204             *  @param str Name of the attribute
205             *  @param attrValue Attribute Value structure
206             *  @return The getAttributeValue method returns true if the attribute was
207             *        found in the representation.  Otherwise it returns false.
208             */
209             bool getAttributeValue(const std::string& str, AttributeValue& attrValue) const
210             {
211                 auto x = m_values.find(str);
212
213                 if (x != m_values.end())
214                 {
215                     attrValue = x->second;
216                     return true;
217                 }
218                 else
219                 {
220                     return false;
221                 }
222             }
223
224             std::string getValueToString(const std::string& key) const;
225             bool hasAttribute(const std::string& str) const;
226
227             void setNULL(const std::string& str);
228
229             bool isNULL(const std::string& str) const;
230
231             // STL Container stuff
232         public:
233             class iterator;
234             class const_iterator;
235             // Shim class to allow iterating and indexing of the OCRepresentation
236             // object.
237             class AttributeItem
238             {
239                 friend class OCRepresentation;
240                 friend class iterator;
241                 friend class const_iterator;
242                 public:
243                     const std::string& attrname() const;
244                     AttributeType type() const;
245                     AttributeType base_type() const;
246                     size_t depth() const;
247                     template<typename T>
248                     T getValue() const
249                     {
250                         return boost::get<T>(m_values[m_attrName]);
251                     }
252
253                     std::string getValueToString() const;
254
255                     template<typename T>
256                     AttributeItem& operator=(T&& rhs)
257                     {
258                         m_values[m_attrName] = std::forward<T>(rhs);
259                         return *this;
260                     }
261
262                     AttributeItem& operator=(std::nullptr_t rhs)
263                     {
264                         NullType t;
265                         m_values[m_attrName] = t;
266                         return *this;
267                     }
268
269                     // Enable-if required to prevent conversions to alternate types.  This prevents
270                     // ambigious conversions in the case where conversions can include a number of
271                     // types, such as the string constructor.
272                     template<typename T, typename= typename std::enable_if<
273                      std::is_same<T, int>::value ||
274                      std::is_same<T, double>::value ||
275                      std::is_same<T, bool>::value ||
276                      std::is_same<T, std::string>::value ||
277                      std::is_same<T, OCRepresentation>::value ||
278                      std::is_same<T, std::vector<int>>::value ||
279                      std::is_same<T, std::vector<std::vector<int>>>::value ||
280                      std::is_same<T, std::vector<std::vector<std::vector<int>>>>::value ||
281                      std::is_same<T, std::vector<double>>::value ||
282                      std::is_same<T, std::vector<std::vector<double>>>::value ||
283                      std::is_same<T, std::vector<std::vector<std::vector<double>>>>::value ||
284                      std::is_same<T, std::vector<bool>>::value ||
285                      std::is_same<T, std::vector<std::vector<bool>>>::value ||
286                      std::is_same<T, std::vector<std::vector<std::vector<bool>>>>::value ||
287                      std::is_same<T, std::vector<std::string>>::value ||
288                      std::is_same<T, std::vector<std::vector<std::string>>>::value ||
289                      std::is_same<T, std::vector<std::vector<std::vector<std::string>>>>::value ||
290                      std::is_same<T, std::vector<OCRepresentation>>::value ||
291                      std::is_same<T, std::vector<std::vector<OCRepresentation>>>::value ||
292                      std::is_same<T, std::vector<std::vector<std::vector<OCRepresentation>>>>::value
293                      >::type // enable_if
294                     >
295                     operator T() const
296                     {
297                         return this->getValue<T>();
298                     }
299
300                     operator std::nullptr_t() const
301                     {
302                         this->getValue<NullType>();
303                         return nullptr;
304                     }
305
306                 private:
307                     AttributeItem(const std::string& name,
308                             std::map<std::string, AttributeValue>& vals);
309                     AttributeItem(const AttributeItem&) = default;
310                     std::string m_attrName;
311                     std::map<std::string, AttributeValue>& m_values;
312             };
313
314             // Iterator to allow iteration via STL containers/methods
315             class iterator
316             {
317                 friend class OCRepresentation;
318                 public:
319                     typedef iterator self_type;
320                     typedef AttributeItem value_type;
321                     typedef value_type& reference;
322                     typedef value_type* pointer;
323                     typedef std::forward_iterator_tag iterator_category;
324                     typedef int difference_type;
325
326                     iterator(const iterator&) = default;
327                     ~iterator() = default;
328
329                     bool operator ==(const iterator&) const;
330                     bool operator !=(const iterator&) const;
331
332                     iterator& operator++();
333                     iterator operator++(int);
334
335                     reference operator*();
336                     pointer operator->();
337                 private:
338                     iterator(std::map<std::string, AttributeValue>::iterator&& itr,
339                             std::map<std::string, AttributeValue>& vals)
340                         : m_iterator(std::move(itr)),
341                         m_item(m_iterator != vals.end() ? m_iterator->first:"", vals){}
342                     std::map<std::string, AttributeValue>::iterator m_iterator;
343                     AttributeItem m_item;
344             };
345
346             class const_iterator
347             {
348                 friend class OCRepresentation;
349                 public:
350                     typedef iterator self_type;
351                     typedef const AttributeItem value_type;
352                     typedef value_type& const_reference;
353                     typedef value_type* const_pointer;
354                     typedef std::forward_iterator_tag iterator_category;
355                     typedef int difference_type;
356
357                     const_iterator(const iterator& rhs)
358                         :m_iterator(rhs.m_iterator), m_item(rhs.m_item){}
359                     const_iterator(const const_iterator&) = default;
360                     ~const_iterator() = default;
361
362                     bool operator ==(const const_iterator&) const;
363                     bool operator !=(const const_iterator&) const;
364
365                     const_iterator& operator++();
366                     const_iterator operator++(int);
367
368                     const_reference operator*() const;
369                     const_pointer operator->() const;
370                 private:
371                     const_iterator(std::map<std::string, AttributeValue>::const_iterator&& itr,
372                             std::map<std::string, AttributeValue>& vals)
373                         : m_iterator(std::move(itr)),
374                         m_item(m_iterator != vals.end() ? m_iterator->first: "", vals){}
375                     std::map<std::string, AttributeValue>::const_iterator m_iterator;
376                     AttributeItem m_item;
377             };
378
379             iterator begin();
380             const_iterator begin() const;
381             const_iterator cbegin() const;
382             iterator end();
383             const_iterator end() const;
384             const_iterator cend() const;
385             size_t size() const;
386             bool empty() const;
387
388             AttributeItem operator[](const std::string& key);
389             const AttributeItem operator[](const std::string& key) const;
390         private:
391             friend class OCResourceResponse;
392             friend class cereal::access;
393
394             // the root node has a slightly different JSON version
395             // based on the interface type configured in ResourceResponse.
396             // This allows ResourceResponse to set it, so that the save function
397             // doesn't serialize things that it isn't supposed to serialize.
398             void setInterfaceType(InterfaceType ift)
399             {
400                 m_interfaceType = ift;
401             }
402
403             // class used to wrap the 'prop' feature of the save/load
404             class Prop
405             {
406                 public:
407                     Prop(std::vector<std::string>& resourceTypes,
408                             std::vector<std::string>& interfaces)
409                     : m_types(resourceTypes), m_interfaces(interfaces)
410                     {}
411
412                  /*   Prop(const std::vector<std::string>& resourceTypes,
413                             const std::vector<std::string>& interfaces)
414                     :m_types(resourceTypes),
415                     m_interfaces(interfaces)
416                     {}*/
417                 private:
418                     friend class cereal::access;
419                     template <class Archive>
420                     void save(Archive& ar) const;
421
422                     template<class Archive>
423                     void load(Archive& ar);
424
425                     std::vector<std::string>& m_types;
426                     std::vector<std::string>& m_interfaces;
427             };
428             template<class Archive, class Val>
429             static void optional_load(Archive& ar, Val&& v);
430
431             template<class Archive>
432             void save(Archive& ar) const;
433
434             template<class Archive>
435             void load(Archive& ar);
436
437         private:
438             std::string m_uri;
439             std::vector<OCRepresentation> m_children;
440             mutable std::map<std::string, AttributeValue> m_values;
441             std::vector<std::string> m_resourceTypes;
442             std::vector<std::string> m_interfaces;
443
444             InterfaceType m_interfaceType;
445     };
446
447     std::ostream& operator <<(std::ostream& os, const OCRepresentation::AttributeItem& ai);
448 } // namespace OC
449
450
451 #endif //__OCREPRESENTATION_H
452