Removed Dead Code from CBOR merge
[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 std::string& uri);
122
123             std::string getUri() const;
124
125             const std::vector<std::string>& getResourceTypes() const;
126
127             void setResourceTypes(const std::vector<std::string>& resourceTypes);
128
129             void addResourceType(const std::string& str);
130
131             const std::vector<std::string>& getResourceInterfaces() const;
132
133             void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
134
135             void addResourceInterface(const std::string& str);
136
137             bool emptyData() const;
138
139             int numberOfAttributes() const;
140
141             bool erase(const std::string& str);
142
143             template <typename T>
144             void setValue(const std::string& str, const T& val)
145             {
146                 m_values[str] = val;
147             }
148
149             /**
150              *  Retrieve the attribute value associated with the supplied name
151              *
152              *  @param str Name of the attribute
153              *  @param val Value of the attribute
154              *  @return The getValue method returns true if the attribute was
155              *        found in the representation.  Otherwise it returns false.
156              */
157             template <typename T>
158             bool getValue(const std::string& str, T& val) const
159             {
160                 auto x = m_values.find(str);
161
162                 if(x!= m_values.end())
163                 {
164                     val = boost::get<T>(x->second);
165                     return true;
166                 }
167                 else
168                 {
169                     val = T();
170                     return false;
171                 }
172             }
173
174             /**
175              *  Return the attribute value associated with the supplied name
176              *
177              *  @param str Name of the attribute
178              *  @return When the representation contains the attribute, the
179              *       the associated value is returned.  Otherwise, getValue
180              *       returns the default contructed value for the type.
181              */
182             template <typename T>
183             T getValue(const std::string& str) const
184             {
185                 T val = T();
186                 auto x = m_values.find(str);
187                 if(x != m_values.end())
188                 {
189                     val = boost::get<T>(x->second);
190                 }
191                 return val;
192             }
193
194            /**
195             *  Retrieve the attributevalue structure associated with the supplied name
196             *
197             *  @param str Name of the attribute
198             *  @param attrValue Attribute Value structure
199             *  @return The getAttributeValue method returns true if the attribute was
200             *        found in the representation.  Otherwise it returns false.
201             */
202             bool getAttributeValue(const std::string& str, AttributeValue& attrValue) const
203             {
204                 auto x = m_values.find(str);
205
206                 if (x != m_values.end())
207                 {
208                     attrValue = x->second;
209                     return true;
210                 }
211                 else
212                 {
213                     return false;
214                 }
215             }
216
217             std::string getValueToString(const std::string& key) const;
218             bool hasAttribute(const std::string& str) const;
219
220             void setNULL(const std::string& str);
221
222             bool isNULL(const std::string& str) const;
223
224             // STL Container stuff
225         public:
226             class iterator;
227             class const_iterator;
228             // Shim class to allow iterating and indexing of the OCRepresentation
229             // object.
230             class AttributeItem
231             {
232                 friend class OCRepresentation;
233                 friend class iterator;
234                 friend class const_iterator;
235                 public:
236                     const std::string& attrname() const;
237                     AttributeType type() const;
238                     AttributeType base_type() const;
239                     size_t depth() const;
240                     template<typename T>
241                     T getValue() const
242                     {
243                         return boost::get<T>(m_values[m_attrName]);
244                     }
245
246                     std::string getValueToString() const;
247
248                     template<typename T>
249                     AttributeItem& operator=(T&& rhs)
250                     {
251                         m_values[m_attrName] = std::forward<T>(rhs);
252                         return *this;
253                     }
254
255                     AttributeItem& operator=(std::nullptr_t rhs)
256                     {
257                         NullType t;
258                         m_values[m_attrName] = t;
259                         return *this;
260                     }
261
262                     // Enable-if required to prevent conversions to alternate types.  This prevents
263                     // ambigious conversions in the case where conversions can include a number of
264                     // types, such as the string constructor.
265                     template<typename T, typename= typename std::enable_if<
266                      std::is_same<T, int>::value ||
267                      std::is_same<T, double>::value ||
268                      std::is_same<T, bool>::value ||
269                      std::is_same<T, std::string>::value ||
270                      std::is_same<T, OCRepresentation>::value ||
271                      std::is_same<T, std::vector<int>>::value ||
272                      std::is_same<T, std::vector<std::vector<int>>>::value ||
273                      std::is_same<T, std::vector<std::vector<std::vector<int>>>>::value ||
274                      std::is_same<T, std::vector<double>>::value ||
275                      std::is_same<T, std::vector<std::vector<double>>>::value ||
276                      std::is_same<T, std::vector<std::vector<std::vector<double>>>>::value ||
277                      std::is_same<T, std::vector<bool>>::value ||
278                      std::is_same<T, std::vector<std::vector<bool>>>::value ||
279                      std::is_same<T, std::vector<std::vector<std::vector<bool>>>>::value ||
280                      std::is_same<T, std::vector<std::string>>::value ||
281                      std::is_same<T, std::vector<std::vector<std::string>>>::value ||
282                      std::is_same<T, std::vector<std::vector<std::vector<std::string>>>>::value ||
283                      std::is_same<T, std::vector<OCRepresentation>>::value ||
284                      std::is_same<T, std::vector<std::vector<OCRepresentation>>>::value ||
285                      std::is_same<T, std::vector<std::vector<std::vector<OCRepresentation>>>>::value
286                      >::type // enable_if
287                     >
288                     operator T() const
289                     {
290                         return this->getValue<T>();
291                     }
292
293                     operator std::nullptr_t() const
294                     {
295                         this->getValue<NullType>();
296                         return nullptr;
297                     }
298
299                 private:
300                     AttributeItem(const std::string& name,
301                             std::map<std::string, AttributeValue>& vals);
302                     AttributeItem(const AttributeItem&) = default;
303                     std::string m_attrName;
304                     std::map<std::string, AttributeValue>& m_values;
305             };
306
307             // Iterator to allow iteration via STL containers/methods
308             class iterator
309             {
310                 friend class OCRepresentation;
311                 public:
312                     typedef iterator self_type;
313                     typedef AttributeItem value_type;
314                     typedef value_type& reference;
315                     typedef value_type* pointer;
316                     typedef std::forward_iterator_tag iterator_category;
317                     typedef int difference_type;
318
319                     iterator(const iterator&) = default;
320                     ~iterator() = default;
321
322                     bool operator ==(const iterator&) const;
323                     bool operator !=(const iterator&) const;
324
325                     iterator& operator++();
326                     iterator operator++(int);
327
328                     reference operator*();
329                     pointer operator->();
330                 private:
331                     iterator(std::map<std::string, AttributeValue>::iterator&& itr,
332                             std::map<std::string, AttributeValue>& vals)
333                         : m_iterator(std::move(itr)),
334                         m_item(m_iterator != vals.end() ? m_iterator->first:"", vals){}
335                     std::map<std::string, AttributeValue>::iterator m_iterator;
336                     AttributeItem m_item;
337             };
338
339             class const_iterator
340             {
341                 friend class OCRepresentation;
342                 public:
343                     typedef iterator self_type;
344                     typedef const AttributeItem value_type;
345                     typedef value_type& const_reference;
346                     typedef value_type* const_pointer;
347                     typedef std::forward_iterator_tag iterator_category;
348                     typedef int difference_type;
349
350                     const_iterator(const iterator& rhs)
351                         :m_iterator(rhs.m_iterator), m_item(rhs.m_item){}
352                     const_iterator(const const_iterator&) = default;
353                     ~const_iterator() = default;
354
355                     bool operator ==(const const_iterator&) const;
356                     bool operator !=(const const_iterator&) const;
357
358                     const_iterator& operator++();
359                     const_iterator operator++(int);
360
361                     const_reference operator*() const;
362                     const_pointer operator->() const;
363                 private:
364                     const_iterator(std::map<std::string, AttributeValue>::const_iterator&& itr,
365                             std::map<std::string, AttributeValue>& vals)
366                         : m_iterator(std::move(itr)),
367                         m_item(m_iterator != vals.end() ? m_iterator->first: "", vals){}
368                     std::map<std::string, AttributeValue>::const_iterator m_iterator;
369                     AttributeItem m_item;
370             };
371
372             iterator begin();
373             const_iterator begin() const;
374             const_iterator cbegin() const;
375             iterator end();
376             const_iterator end() const;
377             const_iterator cend() const;
378             size_t size() const;
379             bool empty() const;
380
381             AttributeItem operator[](const std::string& key);
382             const AttributeItem operator[](const std::string& key) const;
383         private:
384             friend class OCResourceResponse;
385             friend class MessageContainer;
386
387             template<typename T>
388             void payload_array_helper(const OCRepPayloadValue* pl, size_t depth);
389             template<typename T>
390             T payload_array_helper_copy(size_t index, const OCRepPayloadValue* pl);
391             void setPayload(const OCRepPayload* payload);
392             void setPayloadArray(const OCRepPayloadValue* pl);
393             void getPayloadArray(OCRepPayload* payload,
394                     const OCRepresentation::AttributeItem& item) const;
395             // the root node has a slightly different JSON version
396             // based on the interface type configured in ResourceResponse.
397             // This allows ResourceResponse to set it, so that the save function
398             // doesn't serialize things that it isn't supposed to serialize.
399             void setInterfaceType(InterfaceType ift)
400             {
401                 m_interfaceType = ift;
402             }
403
404             // class used to wrap the 'prop' feature of the save/load
405             class Prop
406             {
407                 public:
408                     Prop(std::vector<std::string>& resourceTypes,
409                             std::vector<std::string>& interfaces)
410                     : m_types(resourceTypes), m_interfaces(interfaces)
411                     {}
412
413                  /*   Prop(const std::vector<std::string>& resourceTypes,
414                             const std::vector<std::string>& interfaces)
415                     :m_types(resourceTypes),
416                     m_interfaces(interfaces)
417                     {}*/
418                 private:
419                     std::vector<std::string>& m_types;
420                     std::vector<std::string>& m_interfaces;
421             };
422         private:
423             std::string m_uri;
424             std::vector<OCRepresentation> m_children;
425             mutable std::map<std::string, AttributeValue> m_values;
426             std::vector<std::string> m_resourceTypes;
427             std::vector<std::string> m_interfaces;
428
429             InterfaceType m_interfaceType;
430     };
431
432     std::ostream& operator <<(std::ostream& os, const OCRepresentation::AttributeItem& ai);
433 } // namespace OC
434
435
436 #endif //__OCREPRESENTATION_H
437