1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 /// @file OCRepresentation.h
23 /// @brief This file contains the declaration of classes and its members
24 /// related to OCRepresentation
26 #ifndef __OCREPRESENTATION_H
27 #define __OCREPRESENTATION_H
35 #include <AttributeValue.h>
36 #include <StringConstants.h>
39 #include "OCAndroid.h"
42 #include <OCException.h>
52 enum class InterfaceType
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
73 class MessageContainer
76 void setJSONRepresentation(const std::string& payload);
78 void setJSONRepresentation(const unsigned char* payload);
80 std::string getJSONRepresentation(OCInfoFormat f) const;
82 const std::vector<OCRepresentation>& representations() const;
84 void addRepresentation(const OCRepresentation& rep);
86 const OCRepresentation& operator[](int index) const
91 const OCRepresentation& back() const
96 std::vector<OCRepresentation> m_reps;
98 class OCRepresentation
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){}
110 OCRepresentation(OCRepresentation&&) = default;
112 OCRepresentation(const OCRepresentation&) = default;
114 OCRepresentation& operator=(const OCRepresentation&) = default;
116 OCRepresentation& operator=(OCRepresentation&&) = default;
118 virtual ~OCRepresentation(){}
120 std::string getJSONRepresentation() const;
122 void addChild(const OCRepresentation&);
124 void clearChildren();
126 const std::vector<OCRepresentation>& getChildren() const;
128 void setChildren(const std::vector<OCRepresentation>& children);
130 void setUri(const std::string& uri);
132 std::string getUri() const;
134 const std::vector<std::string>& getResourceTypes() const;
136 void setResourceTypes(const std::vector<std::string>& resourceTypes);
138 const std::vector<std::string>& getResourceInterfaces() const;
140 void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
142 bool emptyData() const;
144 int numberOfAttributes() const;
146 bool erase(const std::string& str);
148 template <typename T>
149 void setValue(const std::string& str, const T& val)
155 * Retrieve the attribute value associated with the supplied name
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.
162 template <typename T>
163 bool getValue(const std::string& str, T& val) const
165 auto x = m_values.find(str);
167 if(x!= m_values.end())
169 val = boost::get<T>(x->second);
180 * Return the attribute value associated with the supplied name
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.
187 template <typename T>
188 T getValue(const std::string& str) const
191 auto x = m_values.find(str);
192 if(x != m_values.end())
194 val = boost::get<T>(x->second);
199 std::string getValueToString(const std::string& key) const;
200 bool hasAttribute(const std::string& str) const;
202 void setNULL(const std::string& str);
204 bool isNULL(const std::string& str) const;
206 // STL Container stuff
209 class const_iterator;
210 // Shim class to allow iterating and indexing of the OCRepresentation
214 friend class OCRepresentation;
215 friend class iterator;
216 friend class const_iterator;
218 const std::string& attrname() const;
219 AttributeType type() const;
220 AttributeType base_type() const;
221 size_t depth() const;
225 return boost::get<T>(m_values[m_attrName]);
228 std::string getValueToString() const;
231 AttributeItem& operator=(T&& rhs)
233 m_values[m_attrName] = std::forward<T>(rhs);
237 AttributeItem& operator=(std::nullptr_t rhs)
240 m_values[m_attrName] = t;
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
272 return this->getValue<T>();
275 operator std::nullptr_t() const
277 this->getValue<NullType>();
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;
289 // Iterator to allow iteration via STL containers/methods
292 friend class OCRepresentation;
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;
301 iterator(const iterator&) = default;
302 ~iterator() = default;
304 bool operator ==(const iterator&) const;
305 bool operator !=(const iterator&) const;
307 iterator& operator++();
308 iterator operator++(int);
310 reference operator*();
311 pointer operator->();
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;
323 friend class OCRepresentation;
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;
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;
337 bool operator ==(const const_iterator&) const;
338 bool operator !=(const const_iterator&) const;
340 const_iterator& operator++();
341 const_iterator operator++(int);
343 const_reference operator*() const;
344 const_pointer operator->() const;
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;
355 const_iterator begin() const;
356 const_iterator cbegin() const;
358 const_iterator end() const;
359 const_iterator cend() const;
363 AttributeItem operator[](const std::string& key);
364 const AttributeItem operator[](const std::string& key) const;
366 friend class OCResourceResponse;
367 friend class cereal::access;
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)
375 m_interfaceType = ift;
378 // class used to wrap the 'prop' feature of the save/load
382 Prop(std::vector<std::string>& resourceTypes,
383 std::vector<std::string>& interfaces)
384 : m_types(resourceTypes), m_interfaces(interfaces)
387 /* Prop(const std::vector<std::string>& resourceTypes,
388 const std::vector<std::string>& interfaces)
389 :m_types(resourceTypes),
390 m_interfaces(interfaces)
393 friend class cereal::access;
394 template <class Archive>
395 void save(Archive& ar) const;
397 template<class Archive>
398 void load(Archive& ar);
400 std::vector<std::string>& m_types;
401 std::vector<std::string>& m_interfaces;
403 template<class Archive, class Val>
404 static void optional_load(Archive& ar, Val&& v);
406 template<class Archive>
407 void save(Archive& ar) const;
409 template<class Archive>
410 void load(Archive& ar);
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;
419 InterfaceType m_interfaceType;
422 std::ostream& operator <<(std::ostream& os, const OCRepresentation::AttributeItem& ai);
426 #endif //__OCREPRESENTATION_H