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