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