Prepare for merge. Improved consistency and style.
[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
70             OCRepPayload* getPayload() const;
71
72             const std::vector<OCRepresentation>& representations() const;
73
74             void addRepresentation(const OCRepresentation& rep);
75
76             const OCRepresentation& operator[](int index) const
77             {
78                 return m_reps[index];
79             }
80
81             const OCRepresentation& back() const
82             {
83                 return m_reps.back();
84             }
85         private:
86             std::vector<OCRepresentation> m_reps;
87     };
88     class OCRepresentation
89     {
90         public:
91             friend bool operator==(const OC::OCRepresentation&, const OC::OCRepresentation&);
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 #if defined(_MSC_VER) && (_MSC_VER < 1900)
102             OCRepresentation(OCRepresentation&& o)
103             {
104                 std::memmove(this, &o, sizeof(o));
105             }
106 #else
107             OCRepresentation(OCRepresentation&&) = default;
108 #endif
109
110             OCRepresentation(const OCRepresentation&) = default;
111
112             OCRepresentation& operator=(const OCRepresentation&) = default;
113
114 #if defined(_MSC_VER) && (_MSC_VER < 1900)
115             OCRepresentation& operator=(OCRepresentation&& o)
116             {
117                 std::memmove(this, &o, sizeof(o));
118                 return *this;
119             }
120 #else
121             OCRepresentation& operator=(OCRepresentation&&) = default;
122 #endif
123
124             virtual ~OCRepresentation(){}
125
126             void setDevAddr(const OCDevAddr addr);
127
128             const std::string getHost() const;
129
130             OCRepPayload* getPayload() const;
131
132             void addChild(const OCRepresentation&);
133
134             void clearChildren();
135
136             const std::vector<OCRepresentation>& getChildren() const;
137
138             void setChildren(const std::vector<OCRepresentation>& children);
139
140             void setUri(const char* uri);
141
142             void setUri(const std::string& uri);
143
144             std::string getUri() const;
145
146             const std::vector<std::string>& getResourceTypes() const;
147
148             void setResourceTypes(const std::vector<std::string>& resourceTypes);
149
150             void addResourceType(const std::string& str);
151
152             const std::vector<std::string>& getResourceInterfaces() const;
153
154             void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
155
156             void addResourceInterface(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, std::vector<int>>::value ||
331                      std::is_same<T, std::vector<std::vector<int>>>::value ||
332                      std::is_same<T, std::vector<std::vector<std::vector<int>>>>::value ||
333                      std::is_same<T, std::vector<double>>::value ||
334                      std::is_same<T, std::vector<std::vector<double>>>::value ||
335                      std::is_same<T, std::vector<std::vector<std::vector<double>>>>::value ||
336                      std::is_same<T, std::vector<bool>>::value ||
337                      std::is_same<T, std::vector<std::vector<bool>>>::value ||
338                      std::is_same<T, std::vector<std::vector<std::vector<bool>>>>::value ||
339                      std::is_same<T, std::vector<std::string>>::value ||
340                      std::is_same<T, std::vector<std::vector<std::string>>>::value ||
341                      std::is_same<T, std::vector<std::vector<std::vector<std::string>>>>::value ||
342                      std::is_same<T, std::vector<OCRepresentation>>::value ||
343                      std::is_same<T, std::vector<std::vector<OCRepresentation>>>::value ||
344                      std::is_same<T, std::vector<std::vector<std::vector<OCRepresentation>>>>::value
345                      , int>::type = 0// enable_if
346                     >
347 #else
348                     template<typename T, typename std::enable_if<
349                         is_component<T,
350                             remove_first<AttributeValue>::type
351                             >::value
352                         , int>::type = 0
353                     >
354 #endif
355                     operator T() const
356                     {
357                         return this->getValue<T>();
358                     }
359
360                     template<typename T, typename std::enable_if<
361                         std::is_same<T, std::nullptr_t>::value
362                         , int>::type = 0
363                     >
364                     operator T() const
365                     {
366                         this->getValue<NullType>();
367                         return nullptr;
368                     }
369
370                 private:
371                     AttributeItem(const std::string& name,
372                             std::map<std::string, AttributeValue>& vals);
373                     AttributeItem(const AttributeItem&) = default;
374                     std::string m_attrName;
375                     std::map<std::string, AttributeValue>& m_values;
376             };
377
378             // Iterator to allow iteration via STL containers/methods
379             class iterator
380             {
381                 friend class OCRepresentation;
382                 public:
383                     typedef iterator self_type;
384                     typedef AttributeItem value_type;
385                     typedef value_type& reference;
386                     typedef value_type* pointer;
387                     typedef std::forward_iterator_tag iterator_category;
388                     typedef int difference_type;
389
390                     iterator(const iterator&) = default;
391                     ~iterator() = default;
392
393                     bool operator ==(const iterator&) const;
394                     bool operator !=(const iterator&) const;
395
396                     iterator& operator++();
397                     iterator operator++(int);
398
399                     reference operator*();
400                     pointer operator->();
401                 private:
402                     iterator(std::map<std::string, AttributeValue>::iterator&& itr,
403                             std::map<std::string, AttributeValue>& vals)
404                         : m_iterator(std::move(itr)),
405                         m_item(m_iterator != vals.end() ? m_iterator->first:"", vals){}
406                     std::map<std::string, AttributeValue>::iterator m_iterator;
407                     AttributeItem m_item;
408             };
409
410             class const_iterator
411             {
412                 friend class OCRepresentation;
413                 public:
414                     typedef iterator self_type;
415                     typedef const AttributeItem value_type;
416                     typedef value_type& const_reference;
417                     typedef value_type* const_pointer;
418                     typedef std::forward_iterator_tag iterator_category;
419                     typedef int difference_type;
420
421                     const_iterator(const iterator& rhs)
422                         :m_iterator(rhs.m_iterator), m_item(rhs.m_item){}
423                     const_iterator(const const_iterator&) = default;
424                     ~const_iterator() = default;
425
426                     bool operator ==(const const_iterator&) const;
427                     bool operator !=(const const_iterator&) const;
428
429                     const_iterator& operator++();
430                     const_iterator operator++(int);
431
432                     const_reference operator*() const;
433                     const_pointer operator->() const;
434                 private:
435                     const_iterator(std::map<std::string, AttributeValue>::const_iterator&& itr,
436                             std::map<std::string, AttributeValue>& vals)
437                         : m_iterator(std::move(itr)),
438                         m_item(m_iterator != vals.end() ? m_iterator->first: "", vals){}
439                     std::map<std::string, AttributeValue>::const_iterator m_iterator;
440                     AttributeItem m_item;
441             };
442
443             iterator begin();
444             const_iterator begin() const;
445             const_iterator cbegin() const;
446             iterator end();
447             const_iterator end() const;
448             const_iterator cend() const;
449             size_t size() const;
450             bool empty() const;
451
452             AttributeItem operator[](const std::string& key);
453             const AttributeItem operator[](const std::string& key) const;
454         private:
455             friend class OCResourceResponse;
456             friend class MessageContainer;
457
458             template<typename T>
459             void payload_array_helper(const OCRepPayloadValue* pl, size_t depth);
460             template<typename T>
461             T payload_array_helper_copy(size_t index, const OCRepPayloadValue* pl);
462             void setPayload(const OCRepPayload* payload);
463             void setPayloadArray(const OCRepPayloadValue* pl);
464             void getPayloadArray(OCRepPayload* payload,
465                     const OCRepresentation::AttributeItem& item) const;
466             // the root node has a slightly different JSON version
467             // based on the interface type configured in ResourceResponse.
468             // This allows ResourceResponse to set it, so that the save function
469             // doesn't serialize things that it isn't supposed to serialize.
470             void setInterfaceType(InterfaceType ift)
471             {
472                 m_interfaceType = ift;
473             }
474
475             // class used to wrap the 'prop' feature of the save/load
476             class Prop
477             {
478                 public:
479                     Prop(std::vector<std::string>& resourceTypes,
480                             std::vector<std::string>& interfaces)
481                     : m_types(resourceTypes), m_interfaces(interfaces)
482                     {}
483
484                  /*   Prop(const std::vector<std::string>& resourceTypes,
485                             const std::vector<std::string>& interfaces)
486                     :m_types(resourceTypes),
487                     m_interfaces(interfaces)
488                     {}*/
489                 private:
490                     std::vector<std::string>& m_types;
491                     std::vector<std::string>& m_interfaces;
492             };
493         private:
494             std::string m_uri;
495             std::vector<OCRepresentation> m_children;
496             mutable std::map<std::string, AttributeValue> m_values;
497             std::vector<std::string> m_resourceTypes;
498             std::vector<std::string> m_interfaces;
499
500             InterfaceType m_interfaceType;
501     };
502
503     std::ostream& operator <<(std::ostream& os, const OCRepresentation::AttributeItem& ai);
504 } // namespace OC
505
506
507 #endif // OC_REPRESENTATION_H_
508