Updated Doxygen @param for OCResourceResponse::setResourceRepresentation
[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 /// @file OCRepresentation.h
22
23 /// @brief  This file contains the declaration of classes and its members
24 ///         related to OCRepresentation
25
26 #ifndef __OCREPRESENTATION_H
27 #define __OCREPRESENTATION_H
28
29
30 #include <string>
31 #include <sstream>
32 #include <vector>
33 #include <map>
34
35 #include <AttributeValue.h>
36 #include <StringConstants.h>
37
38 #include <OCException.h>
39
40 #ifdef __ANDROID__
41 #include "android_cpp11_compat.h"
42 #endif
43
44 namespace cereal
45 {
46     class access;
47 }
48
49 namespace OC
50 {
51
52     enum class InterfaceType
53     {
54         None,
55         LinkParent,
56         BatchParent,
57         DefaultParent,
58         LinkChild,
59         BatchChild,
60         DefaultChild
61     };
62
63     // The consumer requires resource info to be printed in 2 different ways, both with the "oc":[]
64     // and without.  This enum is used to differentiate between the two situations.  When the
65     // serialize is called with Include OC, we encode OC, otherwise we skip it and return just the
66     // contents of the array.
67     enum class OCInfoFormat
68     {
69         IncludeOC,
70         ExcludeOC
71     };
72
73     class MessageContainer
74     {
75         public:
76             void setJSONRepresentation(const std::string& payload);
77
78             void setJSONRepresentation(const unsigned char* payload);
79
80             std::string getJSONRepresentation(OCInfoFormat f) const;
81
82             const std::vector<OCRepresentation>& representations() const;
83
84             void addRepresentation(const OCRepresentation& rep);
85
86             const OCRepresentation& operator[](int index) const
87             {
88                 return m_reps[index];
89             }
90
91             const OCRepresentation& back() const
92             {
93                 return m_reps.back();
94             }
95         private:
96             std::vector<OCRepresentation> m_reps;
97     };
98     class OCRepresentation
99     {
100         public:
101             OCRepresentation(): m_interfaceType(InterfaceType::None){}
102
103             OCRepresentation(OCRepresentation&&) = default;
104
105             OCRepresentation(const OCRepresentation&) = default;
106
107             OCRepresentation& operator=(const OCRepresentation&) = default;
108
109             OCRepresentation& operator=(OCRepresentation&&) = default;
110
111             virtual ~OCRepresentation(){}
112
113             std::string getJSONRepresentation() const;
114
115             void addChild(const OCRepresentation&);
116
117             void clearChildren();
118
119             const std::vector<OCRepresentation>& getChildren() const;
120
121             void setChildren(const std::vector<OCRepresentation>& children);
122
123             void setUri(const std::string& uri);
124
125             std::string getUri() const;
126
127             const std::vector<std::string>& getResourceTypes() const;
128
129             void setResourceTypes(const std::vector<std::string>& resourceTypes);
130
131             const std::vector<std::string>& getResourceInterfaces() const;
132
133             void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
134
135             bool emptyData() const;
136
137             int numberOfAttributes() const;
138
139             bool erase(const std::string& str);
140
141             template <typename T>
142             void setValue(const std::string& str, const T& val)
143             {
144                 m_values[str] = val;
145             }
146
147             template <typename T>
148             bool getValue(const std::string& str, T& val) const
149             {
150                 auto x = m_values.find(str);
151
152                 if(x!= m_values.end())
153                 {
154                     val = boost::get<T>(x->second);
155                     return true;
156                 }
157                 else
158                 {
159                     val = T();
160                     return false;
161                 }
162             }
163
164             template <typename T>
165             T getValue(const std::string& str) const
166             {
167                 T val = T();
168                 auto x = m_values.find(str);
169                 if(x != m_values.end())
170                 {
171                     val = boost::get<T>(x->second);
172                 }
173                 return val;
174             }
175
176             std::string getValueToString(const std::string& key) const;
177             bool hasAttribute(const std::string& str) const;
178
179             void setNULL(const std::string& str);
180
181             bool isNULL(const std::string& str) const;
182
183             // STL Container stuff
184         public:
185             class iterator;
186             class const_iterator;
187             // Shim class to allow iterating and indexing of the OCRepresentation
188             // object.
189             class AttributeItem
190             {
191                 friend class OCRepresentation;
192                 friend class iterator;
193                 friend class const_iterator;
194                 public:
195                     const std::string& attrname() const;
196                     AttributeType type() const;
197                     AttributeType base_type() const;
198                     size_t depth() const;
199                     template<typename T>
200                     T getValue() const
201                     {
202                         return boost::get<T>(m_values[m_attrName]);
203                     }
204
205                     std::string getValueToString() const;
206
207                     template<typename T>
208                     AttributeItem& operator=(T&& rhs)
209                     {
210                         m_values[m_attrName] = std::forward<T>(rhs);
211                         return *this;
212                     }
213
214                     AttributeItem& operator=(std::nullptr_t rhs)
215                     {
216                         NullType t;
217                         m_values[m_attrName] = t;
218                         return *this;
219                     }
220
221                     // Enable-if required to prevent conversions to alternate types.  This prevents
222                     // ambigious conversions in the case where conversions can include a number of
223                     // types, such as the string constructor.
224                     template<typename T, typename= typename std::enable_if<
225                      std::is_same<T, int>::value ||
226                      std::is_same<T, double>::value ||
227                      std::is_same<T, bool>::value ||
228                      std::is_same<T, std::string>::value ||
229                      std::is_same<T, OCRepresentation>::value ||
230                      std::is_same<T, std::vector<int>>::value ||
231                      std::is_same<T, std::vector<std::vector<int>>>::value ||
232                      std::is_same<T, std::vector<std::vector<std::vector<int>>>>::value ||
233                      std::is_same<T, std::vector<double>>::value ||
234                      std::is_same<T, std::vector<std::vector<double>>>::value ||
235                      std::is_same<T, std::vector<std::vector<std::vector<double>>>>::value ||
236                      std::is_same<T, std::vector<bool>>::value ||
237                      std::is_same<T, std::vector<std::vector<bool>>>::value ||
238                      std::is_same<T, std::vector<std::vector<std::vector<bool>>>>::value ||
239                      std::is_same<T, std::vector<std::string>>::value ||
240                      std::is_same<T, std::vector<std::vector<std::string>>>::value ||
241                      std::is_same<T, std::vector<std::vector<std::vector<std::string>>>>::value ||
242                      std::is_same<T, std::vector<OCRepresentation>>::value ||
243                      std::is_same<T, std::vector<std::vector<OCRepresentation>>>::value ||
244                      std::is_same<T, std::vector<std::vector<std::vector<OCRepresentation>>>>::value
245                      >::type // enable_if
246                     >
247                     operator T() const
248                     {
249                         return this->getValue<T>();
250                     }
251
252                     operator std::nullptr_t() const
253                     {
254                         this->getValue<NullType>();
255                         return nullptr;
256                     }
257
258                 private:
259                     AttributeItem(const std::string& name,
260                             std::map<std::string, AttributeValue>& vals);
261                     AttributeItem(const AttributeItem&) = default;
262                     std::string m_attrName;
263                     std::map<std::string, AttributeValue>& m_values;
264             };
265
266             // Iterator to allow iteration via STL containers/methods
267             class iterator
268             {
269                 friend class OCRepresentation;
270                 public:
271                     typedef iterator self_type;
272                     typedef AttributeItem value_type;
273                     typedef value_type& reference;
274                     typedef value_type* pointer;
275                     typedef std::forward_iterator_tag iterator_category;
276                     typedef int difference_type;
277
278                     iterator(const iterator&) = default;
279                     ~iterator() = default;
280
281                     bool operator ==(const iterator&) const;
282                     bool operator !=(const iterator&) const;
283
284                     iterator& operator++();
285                     iterator operator++(int);
286
287                     reference operator*();
288                     pointer operator->();
289                 private:
290                     iterator(std::map<std::string, AttributeValue>::iterator&& itr,
291                             std::map<std::string, AttributeValue>& vals)
292                         : m_iterator(std::move(itr)),
293                         m_item(m_iterator != vals.end() ? m_iterator->first:"", vals){}
294                     std::map<std::string, AttributeValue>::iterator m_iterator;
295                     AttributeItem m_item;
296             };
297
298             class const_iterator
299             {
300                 friend class OCRepresentation;
301                 public:
302                     typedef iterator self_type;
303                     typedef const AttributeItem value_type;
304                     typedef value_type& const_reference;
305                     typedef value_type* const_pointer;
306                     typedef std::forward_iterator_tag iterator_category;
307                     typedef int difference_type;
308
309                     const_iterator(const iterator& rhs)
310                         :m_iterator(rhs.m_iterator), m_item(rhs.m_item){}
311                     const_iterator(const const_iterator&) = default;
312                     ~const_iterator() = default;
313
314                     bool operator ==(const const_iterator&) const;
315                     bool operator !=(const const_iterator&) const;
316
317                     const_iterator& operator++();
318                     const_iterator operator++(int);
319
320                     const_reference operator*() const;
321                     const_pointer operator->() const;
322                 private:
323                     const_iterator(std::map<std::string, AttributeValue>::const_iterator&& itr,
324                             std::map<std::string, AttributeValue>& vals)
325                         : m_iterator(std::move(itr)),
326                         m_item(m_iterator != vals.end() ? m_iterator->first: "", vals){}
327                     std::map<std::string, AttributeValue>::const_iterator m_iterator;
328                     AttributeItem m_item;
329             };
330
331             iterator begin();
332             const_iterator begin() const;
333             const_iterator cbegin() const;
334             iterator end();
335             const_iterator end() const;
336             const_iterator cend() const;
337             size_t size() const;
338             bool empty() const;
339
340             AttributeItem operator[](const std::string& key);
341             const AttributeItem operator[](const std::string& key) const;
342         private:
343             friend class OCResourceResponse;
344             friend class cereal::access;
345
346             // the root node has a slightly different JSON version
347             // based on the interface type configured in ResourceResponse.
348             // This allows ResourceResponse to set it, so that the save function
349             // doesn't serialize things that it isn't supposed to serialize.
350             void setInterfaceType(InterfaceType ift)
351             {
352                 m_interfaceType = ift;
353             }
354
355             // class used to wrap the 'prop' feature of the save/load
356             class Prop
357             {
358                 public:
359                     Prop(std::vector<std::string>& resourceTypes,
360                             std::vector<std::string>& interfaces)
361                     : m_types(resourceTypes), m_interfaces(interfaces)
362                     {}
363
364                  /*   Prop(const std::vector<std::string>& resourceTypes,
365                             const std::vector<std::string>& interfaces)
366                     :m_types(resourceTypes),
367                     m_interfaces(interfaces)
368                     {}*/
369                 private:
370                     friend class cereal::access;
371                     template <class Archive>
372                     void save(Archive& ar) const;
373
374                     template<class Archive>
375                     void load(Archive& ar);
376
377                     std::vector<std::string>& m_types;
378                     std::vector<std::string>& m_interfaces;
379             };
380             template<class Archive, class Val>
381             static void optional_load(Archive& ar, Val&& v);
382
383             template<class Archive>
384             void save(Archive& ar) const;
385
386             template<class Archive>
387             void load(Archive& ar);
388
389         private:
390             std::string m_uri;
391             std::vector<OCRepresentation> m_children;
392             mutable std::map<std::string, AttributeValue> m_values;
393             std::vector<std::string> m_resourceTypes;
394             std::vector<std::string> m_interfaces;
395
396             InterfaceType m_interfaceType;
397     };
398
399     std::ostream& operator <<(std::ostream& os, const OCRepresentation::AttributeItem& ai);
400 } // namespace OC
401
402
403 #endif //__OCREPRESENTATION_H