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