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_
36 #include <AttributeValue.h>
37 #include <StringConstants.h>
40 #include "OCAndroid.h"
43 #include <OCException.h>
48 enum class InterfaceType
59 class MessageContainer
62 void setPayload(const OCPayload* rep);
64 void setPayload(const OCRepPayload* rep);
66 OCRepPayload* getPayload() const;
68 const std::vector<OCRepresentation>& representations() const;
70 void addRepresentation(const OCRepresentation& rep);
72 const OCRepresentation& operator[](int index) const
77 const OCRepresentation& back() const
82 std::vector<OCRepresentation> m_reps;
84 class OCRepresentation
87 friend bool operator==(const OC::OCRepresentation&, const OC::OCRepresentation&);
88 // Note: Implementation of all constructors and destructors
89 // are all placed in the same location due to a crash that
90 // was observed in Android, where merely constructing/destructing
91 // an OCRepresentation object was enough to cause an invalid 'free'.
92 // It is believed that this is a result of incompatible compiler
93 // options between the gradle JNI and armeabi scons build, however
94 // this fix will work in the meantime.
95 OCRepresentation(): m_interfaceType(InterfaceType::None){}
97 #if defined(_MSC_VER) && (_MSC_VER < 1900)
98 OCRepresentation(OCRepresentation&& o)
100 std::memmove(this, &o, sizeof(o));
103 OCRepresentation(OCRepresentation&&) = default;
106 OCRepresentation(const OCRepresentation&) = default;
108 OCRepresentation& operator=(const OCRepresentation&) = default;
110 #if defined(_MSC_VER) && (_MSC_VER < 1900)
111 OCRepresentation& operator=(OCRepresentation&& o)
113 std::memmove(this, &o, sizeof(o));
117 OCRepresentation& operator=(OCRepresentation&&) = default;
120 virtual ~OCRepresentation(){}
122 void setDevAddr(const OCDevAddr&);
124 const std::string getHost() const;
126 OCRepPayload* getPayload() const;
128 void addChild(const OCRepresentation&);
130 void clearChildren();
132 const std::vector<OCRepresentation>& getChildren() const;
134 void setChildren(const std::vector<OCRepresentation>& children);
136 void setUri(const char* uri);
138 void setUri(const std::string& uri);
140 std::string getUri() const;
142 const std::vector<std::string>& getResourceTypes() const;
144 const std::vector<std::string>& getDataModelVersions() const;
146 void setResourceTypes(const std::vector<std::string>& resourceTypes);
148 void addResourceType(const std::string& str);
150 const std::vector<std::string>& getResourceInterfaces() const;
152 void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
154 void addResourceInterface(const std::string& str);
156 void addDataModelVersion(const std::string& str);
158 bool emptyData() const;
160 int numberOfAttributes() const;
162 bool erase(const std::string& str);
164 template <typename T>
165 void setValue(const std::string& str, const T& val)
170 // using R-value(or universal ref depending) to move string and vector<uint8_t>
171 template <typename T>
172 void setValue(const std::string& str, T&& val)
174 m_values[str] = std::forward<T>(val);
177 const std::map<std::string, AttributeValue>& getValues() const {
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 if (x->second.type() == typeid(T))
200 val = boost::get<T>(x->second);
209 catch (boost::bad_get& e)
223 * Return the attribute value associated with the supplied name
225 * @param str Name of the attribute
226 * @return When the representation contains the attribute, the
227 * the associated value is returned. Otherwise, getValue
228 * returns the default contructed value for the type.
230 template <typename T>
231 T getValue(const std::string& str) const
234 auto x = m_values.find(str);
235 if(x != m_values.end())
239 if (x->second.type() == typeid(T))
241 val = boost::get<T>(x->second);
248 catch (boost::bad_get& e)
257 * Retrieve the attributevalue structure associated with the supplied name
259 * @param str Name of the attribute
260 * @param attrValue Attribute Value structure
261 * @return The getAttributeValue method returns true if the attribute was
262 * found in the representation. Otherwise it returns false.
264 bool getAttributeValue(const std::string& str, AttributeValue& attrValue) const
266 auto x = m_values.find(str);
268 if (x != m_values.end())
270 attrValue = x->second;
279 std::string getValueToString(const std::string& key) const;
280 bool hasAttribute(const std::string& str) const;
282 void setNULL(const std::string& str);
284 bool isNULL(const std::string& str) const;
289 // STL Container stuff
292 class const_iterator;
293 // Shim class to allow iterating and indexing of the OCRepresentation
297 friend class OCRepresentation;
298 friend class iterator;
299 friend class const_iterator;
301 const std::string& attrname() const;
302 AttributeType type() const;
303 AttributeType base_type() const;
304 size_t depth() const;
311 if (m_values[m_attrName].type() == typeid(T))
313 val = boost::get<T>(m_values[m_attrName]);
320 catch (boost::bad_get& e)
327 std::string getValueToString() const;
330 AttributeItem& operator=(T&& rhs)
332 m_values[m_attrName] = std::forward<T>(rhs);
336 AttributeItem& operator=(std::nullptr_t /*rhs*/)
339 m_values[m_attrName] = t;
343 // Enable-if required to prevent conversions to alternate types. This prevents
344 // ambigious conversions in the case where conversions can include a number of
345 // types, such as the string constructor.
346 #if (defined(_MSC_VER) ) || (defined(__GNUC__) && (__GNUC__ <= 5))
347 template<typename T, typename std::enable_if<
348 std::is_same<T, int>::value ||
349 std::is_same<T, double>::value ||
350 std::is_same<T, bool>::value ||
351 std::is_same<T, std::string>::value ||
352 std::is_same<T, OCRepresentation>::value ||
353 std::is_same<T, OCByteString>::value ||
354 std::is_same<T, std::vector<int>>::value ||
355 std::is_same<T, std::vector<std::vector<int>>>::value ||
356 std::is_same<T, std::vector<std::vector<std::vector<int>>>>::value ||
357 std::is_same<T, std::vector<double>>::value ||
358 std::is_same<T, std::vector<std::vector<double>>>::value ||
359 std::is_same<T, std::vector<std::vector<std::vector<double>>>>::value ||
360 std::is_same<T, std::vector<bool>>::value ||
361 std::is_same<T, std::vector<std::vector<bool>>>::value ||
362 std::is_same<T, std::vector<std::vector<std::vector<bool>>>>::value ||
363 std::is_same<T, std::vector<std::string>>::value ||
364 std::is_same<T, std::vector<std::vector<std::string>>>::value ||
365 std::is_same<T, std::vector<std::vector<std::vector<std::string>>>>::value ||
366 std::is_same<T, std::vector<OCRepresentation>>::value ||
367 std::is_same<T, std::vector<std::vector<OCRepresentation>>>::value ||
368 std::is_same<T, std::vector<std::vector<std::vector<OCRepresentation>>>>::value ||
369 std::is_same<T, std::vector<OCByteString>>::value ||
370 std::is_same<T, std::vector<std::vector<OCByteString>>>::value ||
371 std::is_same<T, std::vector<std::vector<std::vector<OCByteString>>>>::value
372 , int>::type = 0// enable_if
375 template<typename T, typename std::enable_if<
377 remove_first<AttributeValue>::type
384 return this->getValue<T>();
387 template<typename T, typename std::enable_if<
388 std::is_same<T, std::nullptr_t>::value
393 this->getValue<NullType>();
398 AttributeItem(const std::string& name,
399 std::map<std::string, AttributeValue>& vals);
400 AttributeItem(const AttributeItem&) = default;
401 std::string m_attrName;
402 std::map<std::string, AttributeValue>& m_values;
405 // Iterator to allow iteration via STL containers/methods
408 friend class OCRepresentation;
410 typedef iterator self_type;
411 typedef AttributeItem value_type;
412 typedef value_type& reference;
413 typedef value_type* pointer;
414 typedef std::forward_iterator_tag iterator_category;
415 typedef int difference_type;
417 iterator(const iterator&) = default;
418 ~iterator() = default;
420 bool operator ==(const iterator&) const;
421 bool operator !=(const iterator&) const;
423 iterator& operator++();
424 iterator operator++(int);
426 reference operator*();
427 pointer operator->();
429 iterator(std::map<std::string, AttributeValue>::iterator&& itr,
430 std::map<std::string, AttributeValue>& vals)
431 : m_iterator(std::move(itr)),
432 m_item(m_iterator != vals.end() ? m_iterator->first:"", vals){}
433 std::map<std::string, AttributeValue>::iterator m_iterator;
434 AttributeItem m_item;
439 friend class OCRepresentation;
441 typedef iterator self_type;
442 typedef const AttributeItem value_type;
443 typedef value_type& const_reference;
444 typedef value_type* const_pointer;
445 typedef std::forward_iterator_tag iterator_category;
446 typedef int difference_type;
448 const_iterator(const iterator& rhs)
449 :m_iterator(rhs.m_iterator), m_item(rhs.m_item){}
450 const_iterator(const const_iterator&) = default;
451 ~const_iterator() = default;
453 bool operator ==(const const_iterator&) const;
454 bool operator !=(const const_iterator&) const;
456 const_iterator& operator++();
457 const_iterator operator++(int);
459 const_reference operator*() const;
460 const_pointer operator->() const;
462 const_iterator(std::map<std::string, AttributeValue>::const_iterator&& itr,
463 std::map<std::string, AttributeValue>& vals)
464 : m_iterator(std::move(itr)),
465 m_item(m_iterator != vals.end() ? m_iterator->first: "", vals){}
466 std::map<std::string, AttributeValue>::const_iterator m_iterator;
467 AttributeItem m_item;
471 const_iterator begin() const;
472 const_iterator cbegin() const;
474 const_iterator end() const;
475 const_iterator cend() const;
479 AttributeItem operator[](const std::string& key);
480 const AttributeItem operator[](const std::string& key) const;
482 friend class OCResourceResponse;
483 friend class MessageContainer;
486 void payload_array_helper(const OCRepPayloadValue* pl, size_t depth);
488 T payload_array_helper_copy(size_t index, const OCRepPayloadValue* pl);
489 void setPayload(const OCRepPayload* payload);
490 void setPayloadArray(const OCRepPayloadValue* pl);
491 void getPayloadArray(OCRepPayload* payload,
492 const OCRepresentation::AttributeItem& item) const;
493 // the root node has a slightly different JSON version
494 // based on the interface type configured in ResourceResponse.
495 // This allows ResourceResponse to set it, so that the save function
496 // doesn't serialize things that it isn't supposed to serialize.
497 void setInterfaceType(InterfaceType ift)
499 m_interfaceType = ift;
502 // class used to wrap the 'prop' feature of the save/load
506 Prop(std::vector<std::string>& resourceTypes,
507 std::vector<std::string>& interfaces)
508 : m_types(resourceTypes), m_interfaces(interfaces)
511 /* Prop(const std::vector<std::string>& resourceTypes,
512 const std::vector<std::string>& interfaces)
513 :m_types(resourceTypes),
514 m_interfaces(interfaces)
517 std::vector<std::string>& m_types;
518 std::vector<std::string>& m_interfaces;
522 std::vector<OCRepresentation> m_children;
523 mutable std::map<std::string, AttributeValue> m_values;
524 std::vector<std::string> m_resourceTypes;
525 std::vector<std::string> m_interfaces;
526 std::vector<std::string> m_dataModelVersions;
528 InterfaceType m_interfaceType;
531 std::ostream& operator <<(std::ostream& os, const OCRepresentation::AttributeItem& ai);
535 #endif // OC_REPRESENTATION_H_