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