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