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