Initial version
[platform/core/service/cloud/ua-client.git] / inc / iotivity / 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 addr);
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                         val = boost::get<T>(x->second);
199                         return true;
200                     }
201                     catch (boost::bad_get& e)
202                     {
203                         val = T();
204                         return false;
205                     }
206                 }
207                 else
208                 {
209                     val = T();
210                     return false;
211                 }
212             }
213
214             /**
215              *  Return the attribute value associated with the supplied name
216              *
217              *  @param str Name of the attribute
218              *  @return When the representation contains the attribute, the
219              *       the associated value is returned.  Otherwise, getValue
220              *       returns the default contructed value for the type.
221              */
222             template <typename T>
223             T getValue(const std::string& str) const
224             {
225                 T val = T();
226                 auto x = m_values.find(str);
227                 if(x != m_values.end())
228                 {
229                     try
230                     {
231                         val = boost::get<T>(x->second);
232                     }
233                     catch (boost::bad_get& e)
234                     {
235                         return val;
236                     }
237                 }
238                 return val;
239             }
240
241            /**
242             *  Retrieve the attributevalue structure associated with the supplied name
243             *
244             *  @param str Name of the attribute
245             *  @param attrValue Attribute Value structure
246             *  @return The getAttributeValue method returns true if the attribute was
247             *        found in the representation.  Otherwise it returns false.
248             */
249             bool getAttributeValue(const std::string& str, AttributeValue& attrValue) const
250             {
251                 auto x = m_values.find(str);
252
253                 if (x != m_values.end())
254                 {
255                     attrValue = x->second;
256                     return true;
257                 }
258                 else
259                 {
260                     return false;
261                 }
262             }
263
264             std::string getValueToString(const std::string& key) const;
265             bool hasAttribute(const std::string& str) const;
266
267             void setNULL(const std::string& str);
268
269             bool isNULL(const std::string& str) const;
270
271         private:
272             std::string m_host;
273
274             // STL Container stuff
275         public:
276             class iterator;
277             class const_iterator;
278             // Shim class to allow iterating and indexing of the OCRepresentation
279             // object.
280             class AttributeItem
281             {
282                 friend class OCRepresentation;
283                 friend class iterator;
284                 friend class const_iterator;
285                 public:
286                     const std::string& attrname() const;
287                     AttributeType type() const;
288                     AttributeType base_type() const;
289                     size_t depth() const;
290                     template<typename T>
291                     T getValue() const
292                     {
293                         try
294                         {
295                             return boost::get<T>(m_values[m_attrName]);
296                         }
297                         catch (boost::bad_get& e)
298                         {
299                             T val = T();
300                             return val;
301                         }
302                     }
303
304                     std::string getValueToString() const;
305
306                     template<typename T>
307                     AttributeItem& operator=(T&& rhs)
308                     {
309                         m_values[m_attrName] = std::forward<T>(rhs);
310                         return *this;
311                     }
312
313                     AttributeItem& operator=(std::nullptr_t /*rhs*/)
314                     {
315                         NullType t;
316                         m_values[m_attrName] = t;
317                         return *this;
318                     }
319
320                     // Enable-if required to prevent conversions to alternate types.  This prevents
321                     // ambigious conversions in the case where conversions can include a number of
322                     // types, such as the string constructor.
323 #if (defined(_MSC_VER) ) || (defined(__GNUC__) && (__GNUC__ <= 5))
324                     template<typename T, typename std::enable_if<
325                      std::is_same<T, int>::value ||
326                      std::is_same<T, double>::value ||
327                      std::is_same<T, bool>::value ||
328                      std::is_same<T, std::string>::value ||
329                      std::is_same<T, OCRepresentation>::value ||
330                      std::is_same<T, OCByteString>::value ||
331                      std::is_same<T, std::vector<int>>::value ||
332                      std::is_same<T, std::vector<std::vector<int>>>::value ||
333                      std::is_same<T, std::vector<std::vector<std::vector<int>>>>::value ||
334                      std::is_same<T, std::vector<double>>::value ||
335                      std::is_same<T, std::vector<std::vector<double>>>::value ||
336                      std::is_same<T, std::vector<std::vector<std::vector<double>>>>::value ||
337                      std::is_same<T, std::vector<bool>>::value ||
338                      std::is_same<T, std::vector<std::vector<bool>>>::value ||
339                      std::is_same<T, std::vector<std::vector<std::vector<bool>>>>::value ||
340                      std::is_same<T, std::vector<std::string>>::value ||
341                      std::is_same<T, std::vector<std::vector<std::string>>>::value ||
342                      std::is_same<T, std::vector<std::vector<std::vector<std::string>>>>::value ||
343                      std::is_same<T, std::vector<OCRepresentation>>::value ||
344                      std::is_same<T, std::vector<std::vector<OCRepresentation>>>::value ||
345                      std::is_same<T, std::vector<std::vector<std::vector<OCRepresentation>>>>::value ||
346                      std::is_same<T, std::vector<OCByteString>>::value ||
347                      std::is_same<T, std::vector<std::vector<OCByteString>>>::value ||
348                      std::is_same<T, std::vector<std::vector<std::vector<OCByteString>>>>::value
349                      , int>::type = 0// enable_if
350                     >
351 #else
352                     template<typename T, typename std::enable_if<
353                         is_component<T,
354                             remove_first<AttributeValue>::type
355                             >::value
356                         , int>::type = 0
357                     >
358 #endif
359                     operator T() const
360                     {
361                         return this->getValue<T>();
362                     }
363
364                     template<typename T, typename std::enable_if<
365                         std::is_same<T, std::nullptr_t>::value
366                         , int>::type = 0
367                     >
368                     operator T() const
369                     {
370                         this->getValue<NullType>();
371                         return nullptr;
372                     }
373
374                 private:
375                     AttributeItem(const std::string& name,
376                             std::map<std::string, AttributeValue>& vals);
377                     AttributeItem(const AttributeItem&) = default;
378                     std::string m_attrName;
379                     std::map<std::string, AttributeValue>& m_values;
380             };
381
382             // Iterator to allow iteration via STL containers/methods
383             class iterator
384             {
385                 friend class OCRepresentation;
386                 public:
387                     typedef iterator self_type;
388                     typedef AttributeItem value_type;
389                     typedef value_type& reference;
390                     typedef value_type* pointer;
391                     typedef std::forward_iterator_tag iterator_category;
392                     typedef int difference_type;
393
394                     iterator(const iterator&) = default;
395                     ~iterator() = default;
396
397                     bool operator ==(const iterator&) const;
398                     bool operator !=(const iterator&) const;
399
400                     iterator& operator++();
401                     iterator operator++(int);
402
403                     reference operator*();
404                     pointer operator->();
405                 private:
406                     iterator(std::map<std::string, AttributeValue>::iterator&& itr,
407                             std::map<std::string, AttributeValue>& vals)
408                         : m_iterator(std::move(itr)),
409                         m_item(m_iterator != vals.end() ? m_iterator->first:"", vals){}
410                     std::map<std::string, AttributeValue>::iterator m_iterator;
411                     AttributeItem m_item;
412             };
413
414             class const_iterator
415             {
416                 friend class OCRepresentation;
417                 public:
418                     typedef iterator self_type;
419                     typedef const AttributeItem value_type;
420                     typedef value_type& const_reference;
421                     typedef value_type* const_pointer;
422                     typedef std::forward_iterator_tag iterator_category;
423                     typedef int difference_type;
424
425                     const_iterator(const iterator& rhs)
426                         :m_iterator(rhs.m_iterator), m_item(rhs.m_item){}
427                     const_iterator(const const_iterator&) = default;
428                     ~const_iterator() = default;
429
430                     bool operator ==(const const_iterator&) const;
431                     bool operator !=(const const_iterator&) const;
432
433                     const_iterator& operator++();
434                     const_iterator operator++(int);
435
436                     const_reference operator*() const;
437                     const_pointer operator->() const;
438                 private:
439                     const_iterator(std::map<std::string, AttributeValue>::const_iterator&& itr,
440                             std::map<std::string, AttributeValue>& vals)
441                         : m_iterator(std::move(itr)),
442                         m_item(m_iterator != vals.end() ? m_iterator->first: "", vals){}
443                     std::map<std::string, AttributeValue>::const_iterator m_iterator;
444                     AttributeItem m_item;
445             };
446
447             iterator begin();
448             const_iterator begin() const;
449             const_iterator cbegin() const;
450             iterator end();
451             const_iterator end() const;
452             const_iterator cend() const;
453             size_t size() const;
454             bool empty() const;
455
456             AttributeItem operator[](const std::string& key);
457             const AttributeItem operator[](const std::string& key) const;
458         private:
459             friend class OCResourceResponse;
460             friend class MessageContainer;
461
462             template<typename T>
463             void payload_array_helper(const OCRepPayloadValue* pl, size_t depth);
464             template<typename T>
465             T payload_array_helper_copy(size_t index, const OCRepPayloadValue* pl);
466             void setPayload(const OCRepPayload* payload);
467             void setPayloadArray(const OCRepPayloadValue* pl);
468             void getPayloadArray(OCRepPayload* payload,
469                     const OCRepresentation::AttributeItem& item) const;
470             // the root node has a slightly different JSON version
471             // based on the interface type configured in ResourceResponse.
472             // This allows ResourceResponse to set it, so that the save function
473             // doesn't serialize things that it isn't supposed to serialize.
474             void setInterfaceType(InterfaceType ift)
475             {
476                 m_interfaceType = ift;
477             }
478
479             // class used to wrap the 'prop' feature of the save/load
480             class Prop
481             {
482                 public:
483                     Prop(std::vector<std::string>& resourceTypes,
484                             std::vector<std::string>& interfaces)
485                     : m_types(resourceTypes), m_interfaces(interfaces)
486                     {}
487
488                  /*   Prop(const std::vector<std::string>& resourceTypes,
489                             const std::vector<std::string>& interfaces)
490                     :m_types(resourceTypes),
491                     m_interfaces(interfaces)
492                     {}*/
493                 private:
494                     std::vector<std::string>& m_types;
495                     std::vector<std::string>& m_interfaces;
496             };
497         private:
498             std::string m_uri;
499             std::vector<OCRepresentation> m_children;
500             mutable std::map<std::string, AttributeValue> m_values;
501             std::vector<std::string> m_resourceTypes;
502             std::vector<std::string> m_interfaces;
503             std::vector<std::string> m_dataModelVersions;
504
505             InterfaceType m_interfaceType;
506     };
507
508     std::ostream& operator <<(std::ostream& os, const OCRepresentation::AttributeItem& ai);
509 } // namespace OC
510
511
512 #endif // OC_REPRESENTATION_H_