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