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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 * This file contains the declaration of classes and its members related
25 * to OCRepresentation.
28 #ifndef OC_REPRESENTATION_H_
29 #define OC_REPRESENTATION_H_
31 #define GCC_VERSION (__GNUC__ * 10000 \
32 + __GNUC_MINOR__ * 100 \
33 + __GNUC_PATCHLEVEL__)
40 #include <AttributeValue.h>
41 #include <StringConstants.h>
44 #include "OCAndroid.h"
47 #include <OCException.h>
52 enum class InterfaceType
63 class MessageContainer
66 void setPayload(const OCPayload* rep);
68 void setPayload(const OCDevicePayload* rep);
70 void setPayload(const OCPlatformPayload* rep);
72 void setPayload(const OCRepPayload* rep);
74 OCRepPayload* getPayload() const;
76 const std::vector<OCRepresentation>& representations() const;
78 void addRepresentation(const OCRepresentation& rep);
80 const OCRepresentation& operator[](int index) const
85 const OCRepresentation& back() const
90 std::vector<OCRepresentation> m_reps;
92 class OCRepresentation
95 friend bool operator==(const OC::OCRepresentation&, const OC::OCRepresentation&);
96 // Note: Implementation of all constructors and destructors
97 // are all placed in the same location due to a crash that
98 // was observed in Android, where merely constructing/destructing
99 // an OCRepresentation object was enough to cause an invalid 'free'.
100 // It is believed that this is a result of incompatible compiler
101 // options between the gradle JNI and armeabi scons build, however
102 // this fix will work in the meantime.
103 OCRepresentation(): m_interfaceType(InterfaceType::None){}
105 #if defined(_MSC_VER) && (_MSC_VER < 1900)
106 OCRepresentation(OCRepresentation&& o)
108 std::memmove(this, &o, sizeof(o));
111 OCRepresentation(OCRepresentation&&) = default;
114 OCRepresentation(const OCRepresentation&) = default;
116 OCRepresentation& operator=(const OCRepresentation&) = default;
118 #if defined(_MSC_VER) && (_MSC_VER < 1900)
119 OCRepresentation& operator=(OCRepresentation&& o)
121 std::memmove(this, &o, sizeof(o));
125 OCRepresentation& operator=(OCRepresentation&&) = default;
128 virtual ~OCRepresentation(){}
130 void setDevAddr(const OCDevAddr addr);
132 const std::string getHost() const;
134 OCRepPayload* getPayload() const;
136 void addChild(const OCRepresentation&);
138 void clearChildren();
140 const std::vector<OCRepresentation>& getChildren() const;
142 void setChildren(const std::vector<OCRepresentation>& children);
144 void setUri(const char* uri);
146 void setUri(const std::string& uri);
148 std::string getUri() const;
150 const std::vector<std::string>& getResourceTypes() const;
152 void setResourceTypes(const std::vector<std::string>& resourceTypes);
154 void addResourceType(const std::string& str);
156 const std::vector<std::string>& getResourceInterfaces() const;
158 void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
160 void addResourceInterface(const std::string& str);
162 bool emptyData() const;
164 int numberOfAttributes() const;
166 bool erase(const std::string& str);
168 template <typename T>
169 void setValue(const std::string& str, const T& val)
174 // using R-value(or universal ref depending) to move string and vector<uint8_t>
175 template <typename T>
176 void setValue(const std::string& str, T&& val)
178 m_values[str] = std::forward<T>(val);
182 * Retrieve the attribute value associated with the supplied name
184 * @param str Name of the attribute
185 * @param val Value of the attribute
186 * @return The getValue method returns true if the attribute was
187 * found in the representation. Otherwise it returns false.
189 template <typename T>
190 bool getValue(const std::string& str, T& val) const
192 auto x = m_values.find(str);
194 if(x!= m_values.end())
198 val = boost::get<T>(x->second);
201 catch (boost::bad_get& e)
215 * Return the attribute value associated with the supplied name
217 * @param str Name of the attribute
218 * @return When the representation contains the attribute, the
219 * the associated value is returned. Otherwise, getValue
220 * returns the default contructed value for the type.
222 template <typename T>
223 T getValue(const std::string& str) const
226 auto x = m_values.find(str);
227 if(x != m_values.end())
231 val = boost::get<T>(x->second);
233 catch (boost::bad_get& e)
242 * Retrieve the attributevalue structure associated with the supplied name
244 * @param str Name of the attribute
245 * @param attrValue Attribute Value structure
246 * @return The getAttributeValue method returns true if the attribute was
247 * found in the representation. Otherwise it returns false.
249 bool getAttributeValue(const std::string& str, AttributeValue& attrValue) const
251 auto x = m_values.find(str);
253 if (x != m_values.end())
255 attrValue = x->second;
264 std::string getValueToString(const std::string& key) const;
265 bool hasAttribute(const std::string& str) const;
267 void setNULL(const std::string& str);
269 bool isNULL(const std::string& str) const;
274 // STL Container stuff
277 class const_iterator;
278 // Shim class to allow iterating and indexing of the OCRepresentation
282 friend class OCRepresentation;
283 friend class iterator;
284 friend class const_iterator;
286 const std::string& attrname() const;
287 AttributeType type() const;
288 AttributeType base_type() const;
289 size_t depth() const;
295 return boost::get<T>(m_values[m_attrName]);
297 catch (boost::bad_get& e)
304 std::string getValueToString() const;
307 AttributeItem& operator=(T&& rhs)
309 m_values[m_attrName] = std::forward<T>(rhs);
313 AttributeItem& operator=(std::nullptr_t /*rhs*/)
316 m_values[m_attrName] = t;
320 // Enable-if required to prevent conversions to alternate types. This prevents
321 // ambigious conversions in the case where conversions can include a number of
322 // types, such as the string constructor.
323 #if (defined(_MSC_VER) ) || (defined(__GNUC__) && (GCC_VERSION <= 50000))
324 template<typename T, typename std::enable_if<
325 std::is_same<T, int>::value ||
326 std::is_same<T, double>::value ||
327 std::is_same<T, bool>::value ||
328 std::is_same<T, std::string>::value ||
329 std::is_same<T, OCRepresentation>::value ||
330 std::is_same<T, std::vector<int>>::value ||
331 std::is_same<T, std::vector<std::vector<int>>>::value ||
332 std::is_same<T, std::vector<std::vector<std::vector<int>>>>::value ||
333 std::is_same<T, std::vector<double>>::value ||
334 std::is_same<T, std::vector<std::vector<double>>>::value ||
335 std::is_same<T, std::vector<std::vector<std::vector<double>>>>::value ||
336 std::is_same<T, std::vector<bool>>::value ||
337 std::is_same<T, std::vector<std::vector<bool>>>::value ||
338 std::is_same<T, std::vector<std::vector<std::vector<bool>>>>::value ||
339 std::is_same<T, std::vector<std::string>>::value ||
340 std::is_same<T, std::vector<std::vector<std::string>>>::value ||
341 std::is_same<T, std::vector<std::vector<std::vector<std::string>>>>::value ||
342 std::is_same<T, std::vector<OCRepresentation>>::value ||
343 std::is_same<T, std::vector<std::vector<OCRepresentation>>>::value ||
344 std::is_same<T, std::vector<std::vector<std::vector<OCRepresentation>>>>::value
345 , int>::type = 0// enable_if
348 template<typename T, typename std::enable_if<
350 remove_first<AttributeValue>::type
357 return this->getValue<T>();
360 template<typename T, typename std::enable_if<
361 std::is_same<T, std::nullptr_t>::value
366 this->getValue<NullType>();
371 AttributeItem(const std::string& name,
372 std::map<std::string, AttributeValue>& vals);
373 AttributeItem(const AttributeItem&) = default;
374 std::string m_attrName;
375 std::map<std::string, AttributeValue>& m_values;
378 // Iterator to allow iteration via STL containers/methods
381 friend class OCRepresentation;
383 typedef iterator self_type;
384 typedef AttributeItem value_type;
385 typedef value_type& reference;
386 typedef value_type* pointer;
387 typedef std::forward_iterator_tag iterator_category;
388 typedef int difference_type;
390 iterator(const iterator&) = default;
391 ~iterator() = default;
393 bool operator ==(const iterator&) const;
394 bool operator !=(const iterator&) const;
396 iterator& operator++();
397 iterator operator++(int);
399 reference operator*();
400 pointer operator->();
402 iterator(std::map<std::string, AttributeValue>::iterator&& itr,
403 std::map<std::string, AttributeValue>& vals)
404 : m_iterator(std::move(itr)),
405 m_item(m_iterator != vals.end() ? m_iterator->first:"", vals){}
406 std::map<std::string, AttributeValue>::iterator m_iterator;
407 AttributeItem m_item;
412 friend class OCRepresentation;
414 typedef iterator self_type;
415 typedef const AttributeItem value_type;
416 typedef value_type& const_reference;
417 typedef value_type* const_pointer;
418 typedef std::forward_iterator_tag iterator_category;
419 typedef int difference_type;
421 const_iterator(const iterator& rhs)
422 :m_iterator(rhs.m_iterator), m_item(rhs.m_item){}
423 const_iterator(const const_iterator&) = default;
424 ~const_iterator() = default;
426 bool operator ==(const const_iterator&) const;
427 bool operator !=(const const_iterator&) const;
429 const_iterator& operator++();
430 const_iterator operator++(int);
432 const_reference operator*() const;
433 const_pointer operator->() const;
435 const_iterator(std::map<std::string, AttributeValue>::const_iterator&& itr,
436 std::map<std::string, AttributeValue>& vals)
437 : m_iterator(std::move(itr)),
438 m_item(m_iterator != vals.end() ? m_iterator->first: "", vals){}
439 std::map<std::string, AttributeValue>::const_iterator m_iterator;
440 AttributeItem m_item;
444 const_iterator begin() const;
445 const_iterator cbegin() const;
447 const_iterator end() const;
448 const_iterator cend() const;
452 AttributeItem operator[](const std::string& key);
453 const AttributeItem operator[](const std::string& key) const;
455 friend class OCResourceResponse;
456 friend class MessageContainer;
459 void payload_array_helper(const OCRepPayloadValue* pl, size_t depth);
461 T payload_array_helper_copy(size_t index, const OCRepPayloadValue* pl);
462 void setPayload(const OCRepPayload* payload);
463 void setPayloadArray(const OCRepPayloadValue* pl);
464 void getPayloadArray(OCRepPayload* payload,
465 const OCRepresentation::AttributeItem& item) const;
466 // the root node has a slightly different JSON version
467 // based on the interface type configured in ResourceResponse.
468 // This allows ResourceResponse to set it, so that the save function
469 // doesn't serialize things that it isn't supposed to serialize.
470 void setInterfaceType(InterfaceType ift)
472 m_interfaceType = ift;
475 // class used to wrap the 'prop' feature of the save/load
479 Prop(std::vector<std::string>& resourceTypes,
480 std::vector<std::string>& interfaces)
481 : m_types(resourceTypes), m_interfaces(interfaces)
484 /* Prop(const std::vector<std::string>& resourceTypes,
485 const std::vector<std::string>& interfaces)
486 :m_types(resourceTypes),
487 m_interfaces(interfaces)
490 std::vector<std::string>& m_types;
491 std::vector<std::string>& m_interfaces;
495 std::vector<OCRepresentation> m_children;
496 mutable std::map<std::string, AttributeValue> m_values;
497 std::vector<std::string> m_resourceTypes;
498 std::vector<std::string> m_interfaces;
500 InterfaceType m_interfaceType;
503 std::ostream& operator <<(std::ostream& os, const OCRepresentation::AttributeItem& ai);
507 #endif // OC_REPRESENTATION_H_