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