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