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