Added documentation for OCRepresentation::getValue
[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             /**
155              *  Retrieve the attribute value associated with the supplied name
156              *
157              *  @param str Name of the attribute
158              *  @param val Value of the attribute
159              *  @return The getValue method returns true if the attribute was
160              *        found in the representation.  Otherwise it returns false.
161              */
162             template <typename T>
163             bool getValue(const std::string& str, T& val) const
164             {
165                 auto x = m_values.find(str);
166
167                 if(x!= m_values.end())
168                 {
169                     val = boost::get<T>(x->second);
170                     return true;
171                 }
172                 else
173                 {
174                     val = T();
175                     return false;
176                 }
177             }
178
179             /**
180              *  Return the attribute value associated with the supplied name
181              *
182              *  @param str Name of the attribute
183              *  @return When the representation contains the attribute, the
184              *       the associated value is returned.  Otherwise, getValue
185              *       returns the default contructed value for the type.
186              */
187             template <typename T>
188             T getValue(const std::string& str) const
189             {
190                 T val = T();
191                 auto x = m_values.find(str);
192                 if(x != m_values.end())
193                 {
194                     val = boost::get<T>(x->second);
195                 }
196                 return val;
197             }
198
199             std::string getValueToString(const std::string& key) const;
200             bool hasAttribute(const std::string& str) const;
201
202             void setNULL(const std::string& str);
203
204             bool isNULL(const std::string& str) const;
205
206             // STL Container stuff
207         public:
208             class iterator;
209             class const_iterator;
210             // Shim class to allow iterating and indexing of the OCRepresentation
211             // object.
212             class AttributeItem
213             {
214                 friend class OCRepresentation;
215                 friend class iterator;
216                 friend class const_iterator;
217                 public:
218                     const std::string& attrname() const;
219                     AttributeType type() const;
220                     AttributeType base_type() const;
221                     size_t depth() const;
222                     template<typename T>
223                     T getValue() const
224                     {
225                         return boost::get<T>(m_values[m_attrName]);
226                     }
227
228                     std::string getValueToString() const;
229
230                     template<typename T>
231                     AttributeItem& operator=(T&& rhs)
232                     {
233                         m_values[m_attrName] = std::forward<T>(rhs);
234                         return *this;
235                     }
236
237                     AttributeItem& operator=(std::nullptr_t rhs)
238                     {
239                         NullType t;
240                         m_values[m_attrName] = t;
241                         return *this;
242                     }
243
244                     // Enable-if required to prevent conversions to alternate types.  This prevents
245                     // ambigious conversions in the case where conversions can include a number of
246                     // types, such as the string constructor.
247                     template<typename T, typename= typename std::enable_if<
248                      std::is_same<T, int>::value ||
249                      std::is_same<T, double>::value ||
250                      std::is_same<T, bool>::value ||
251                      std::is_same<T, std::string>::value ||
252                      std::is_same<T, OCRepresentation>::value ||
253                      std::is_same<T, std::vector<int>>::value ||
254                      std::is_same<T, std::vector<std::vector<int>>>::value ||
255                      std::is_same<T, std::vector<std::vector<std::vector<int>>>>::value ||
256                      std::is_same<T, std::vector<double>>::value ||
257                      std::is_same<T, std::vector<std::vector<double>>>::value ||
258                      std::is_same<T, std::vector<std::vector<std::vector<double>>>>::value ||
259                      std::is_same<T, std::vector<bool>>::value ||
260                      std::is_same<T, std::vector<std::vector<bool>>>::value ||
261                      std::is_same<T, std::vector<std::vector<std::vector<bool>>>>::value ||
262                      std::is_same<T, std::vector<std::string>>::value ||
263                      std::is_same<T, std::vector<std::vector<std::string>>>::value ||
264                      std::is_same<T, std::vector<std::vector<std::vector<std::string>>>>::value ||
265                      std::is_same<T, std::vector<OCRepresentation>>::value ||
266                      std::is_same<T, std::vector<std::vector<OCRepresentation>>>::value ||
267                      std::is_same<T, std::vector<std::vector<std::vector<OCRepresentation>>>>::value
268                      >::type // enable_if
269                     >
270                     operator T() const
271                     {
272                         return this->getValue<T>();
273                     }
274
275                     operator std::nullptr_t() const
276                     {
277                         this->getValue<NullType>();
278                         return nullptr;
279                     }
280
281                 private:
282                     AttributeItem(const std::string& name,
283                             std::map<std::string, AttributeValue>& vals);
284                     AttributeItem(const AttributeItem&) = default;
285                     std::string m_attrName;
286                     std::map<std::string, AttributeValue>& m_values;
287             };
288
289             // Iterator to allow iteration via STL containers/methods
290             class iterator
291             {
292                 friend class OCRepresentation;
293                 public:
294                     typedef iterator self_type;
295                     typedef AttributeItem value_type;
296                     typedef value_type& reference;
297                     typedef value_type* pointer;
298                     typedef std::forward_iterator_tag iterator_category;
299                     typedef int difference_type;
300
301                     iterator(const iterator&) = default;
302                     ~iterator() = default;
303
304                     bool operator ==(const iterator&) const;
305                     bool operator !=(const iterator&) const;
306
307                     iterator& operator++();
308                     iterator operator++(int);
309
310                     reference operator*();
311                     pointer operator->();
312                 private:
313                     iterator(std::map<std::string, AttributeValue>::iterator&& itr,
314                             std::map<std::string, AttributeValue>& vals)
315                         : m_iterator(std::move(itr)),
316                         m_item(m_iterator != vals.end() ? m_iterator->first:"", vals){}
317                     std::map<std::string, AttributeValue>::iterator m_iterator;
318                     AttributeItem m_item;
319             };
320
321             class const_iterator
322             {
323                 friend class OCRepresentation;
324                 public:
325                     typedef iterator self_type;
326                     typedef const AttributeItem value_type;
327                     typedef value_type& const_reference;
328                     typedef value_type* const_pointer;
329                     typedef std::forward_iterator_tag iterator_category;
330                     typedef int difference_type;
331
332                     const_iterator(const iterator& rhs)
333                         :m_iterator(rhs.m_iterator), m_item(rhs.m_item){}
334                     const_iterator(const const_iterator&) = default;
335                     ~const_iterator() = default;
336
337                     bool operator ==(const const_iterator&) const;
338                     bool operator !=(const const_iterator&) const;
339
340                     const_iterator& operator++();
341                     const_iterator operator++(int);
342
343                     const_reference operator*() const;
344                     const_pointer operator->() const;
345                 private:
346                     const_iterator(std::map<std::string, AttributeValue>::const_iterator&& itr,
347                             std::map<std::string, AttributeValue>& vals)
348                         : m_iterator(std::move(itr)),
349                         m_item(m_iterator != vals.end() ? m_iterator->first: "", vals){}
350                     std::map<std::string, AttributeValue>::const_iterator m_iterator;
351                     AttributeItem m_item;
352             };
353
354             iterator begin();
355             const_iterator begin() const;
356             const_iterator cbegin() const;
357             iterator end();
358             const_iterator end() const;
359             const_iterator cend() const;
360             size_t size() const;
361             bool empty() const;
362
363             AttributeItem operator[](const std::string& key);
364             const AttributeItem operator[](const std::string& key) const;
365         private:
366             friend class OCResourceResponse;
367             friend class cereal::access;
368
369             // the root node has a slightly different JSON version
370             // based on the interface type configured in ResourceResponse.
371             // This allows ResourceResponse to set it, so that the save function
372             // doesn't serialize things that it isn't supposed to serialize.
373             void setInterfaceType(InterfaceType ift)
374             {
375                 m_interfaceType = ift;
376             }
377
378             // class used to wrap the 'prop' feature of the save/load
379             class Prop
380             {
381                 public:
382                     Prop(std::vector<std::string>& resourceTypes,
383                             std::vector<std::string>& interfaces)
384                     : m_types(resourceTypes), m_interfaces(interfaces)
385                     {}
386
387                  /*   Prop(const std::vector<std::string>& resourceTypes,
388                             const std::vector<std::string>& interfaces)
389                     :m_types(resourceTypes),
390                     m_interfaces(interfaces)
391                     {}*/
392                 private:
393                     friend class cereal::access;
394                     template <class Archive>
395                     void save(Archive& ar) const;
396
397                     template<class Archive>
398                     void load(Archive& ar);
399
400                     std::vector<std::string>& m_types;
401                     std::vector<std::string>& m_interfaces;
402             };
403             template<class Archive, class Val>
404             static void optional_load(Archive& ar, Val&& v);
405
406             template<class Archive>
407             void save(Archive& ar) const;
408
409             template<class Archive>
410             void load(Archive& ar);
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