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 OCDevicePayload* rep);
66 void setPayload(const OCPlatformPayload* rep);
68 void setPayload(const OCRepPayload* rep);
70 OCRepPayload* getPayload() const;
72 const std::vector<OCRepresentation>& representations() const;
74 void addRepresentation(const OCRepresentation& rep);
76 const OCRepresentation& operator[](int index) const
81 const OCRepresentation& back() const
86 std::vector<OCRepresentation> m_reps;
88 class OCRepresentation
91 friend bool operator==(const OC::OCRepresentation&, const OC::OCRepresentation&);
92 // Note: Implementation of all constructors and destructors
93 // are all placed in the same location due to a crash that
94 // was observed in Android, where merely constructing/destructing
95 // an OCRepresentation object was enough to cause an invalid 'free'.
96 // It is believed that this is a result of incompatible compiler
97 // options between the gradle JNI and armeabi scons build, however
98 // this fix will work in the meantime.
99 OCRepresentation(): m_interfaceType(InterfaceType::None){}
101 #if defined(_MSC_VER) && (_MSC_VER < 1900)
102 OCRepresentation(OCRepresentation&& o)
104 std::memmove(this, &o, sizeof(o));
107 OCRepresentation(OCRepresentation&&) = default;
110 OCRepresentation(const OCRepresentation&) = default;
112 OCRepresentation& operator=(const OCRepresentation&) = default;
114 #if defined(_MSC_VER) && (_MSC_VER < 1900)
115 OCRepresentation& operator=(OCRepresentation&& o)
117 std::memmove(this, &o, sizeof(o));
121 OCRepresentation& operator=(OCRepresentation&&) = default;
124 virtual ~OCRepresentation(){}
126 void setDevAddr(const OCDevAddr addr);
128 const std::string getHost() const;
130 OCRepPayload* getPayload() const;
132 void addChild(const OCRepresentation&);
134 void clearChildren();
136 const std::vector<OCRepresentation>& getChildren() const;
138 void setChildren(const std::vector<OCRepresentation>& children);
140 void setUri(const char* uri);
142 void setUri(const std::string& uri);
144 std::string getUri() const;
146 const std::vector<std::string>& getResourceTypes() const;
148 const std::vector<std::string>& getDataModelVersions() const;
150 void setResourceTypes(const std::vector<std::string>& resourceTypes);
152 void addResourceType(const std::string& str);
154 const std::vector<std::string>& getResourceInterfaces() const;
156 void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
158 void addResourceInterface(const std::string& str);
160 void addDataModelVersion(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);
181 const std::map<std::string, AttributeValue>& getValues() const {
186 * Retrieve the attribute value associated with the supplied name
188 * @param str Name of the attribute
189 * @param val Value of the attribute
190 * @return The getValue method returns true if the attribute was
191 * found in the representation. Otherwise it returns false.
193 template <typename T>
194 bool getValue(const std::string& str, T& val) const
196 auto x = m_values.find(str);
198 if(x!= m_values.end())
202 val = boost::get<T>(x->second);
205 catch (boost::bad_get& e)
219 * Return the attribute value associated with the supplied name
221 * @param str Name of the attribute
222 * @return When the representation contains the attribute, the
223 * the associated value is returned. Otherwise, getValue
224 * returns the default contructed value for the type.
226 template <typename T>
227 T getValue(const std::string& str) const
230 auto x = m_values.find(str);
231 if(x != m_values.end())
235 val = boost::get<T>(x->second);
237 catch (boost::bad_get& e)
246 * Retrieve the attributevalue structure associated with the supplied name
248 * @param str Name of the attribute
249 * @param attrValue Attribute Value structure
250 * @return The getAttributeValue method returns true if the attribute was
251 * found in the representation. Otherwise it returns false.
253 bool getAttributeValue(const std::string& str, AttributeValue& attrValue) const
255 auto x = m_values.find(str);
257 if (x != m_values.end())
259 attrValue = x->second;
268 std::string getValueToString(const std::string& key) const;
269 bool hasAttribute(const std::string& str) const;
271 void setNULL(const std::string& str);
273 bool isNULL(const std::string& str) const;
278 // STL Container stuff
281 class const_iterator;
282 // Shim class to allow iterating and indexing of the OCRepresentation
286 friend class OCRepresentation;
287 friend class iterator;
288 friend class const_iterator;
290 const std::string& attrname() const;
291 AttributeType type() const;
292 AttributeType base_type() const;
293 size_t depth() const;
299 return boost::get<T>(m_values[m_attrName]);
301 catch (boost::bad_get& e)
308 std::string getValueToString() const;
311 AttributeItem& operator=(T&& rhs)
313 m_values[m_attrName] = std::forward<T>(rhs);
317 AttributeItem& operator=(std::nullptr_t /*rhs*/)
320 m_values[m_attrName] = t;
324 // Enable-if required to prevent conversions to alternate types. This prevents
325 // ambigious conversions in the case where conversions can include a number of
326 // types, such as the string constructor.
327 #if (defined(_MSC_VER) ) || (defined(__GNUC__) && (__GNUC__ <= 5))
328 template<typename T, typename std::enable_if<
329 std::is_same<T, int>::value ||
330 std::is_same<T, double>::value ||
331 std::is_same<T, bool>::value ||
332 std::is_same<T, std::string>::value ||
333 std::is_same<T, OCRepresentation>::value ||
334 std::is_same<T, std::vector<int>>::value ||
335 std::is_same<T, std::vector<std::vector<int>>>::value ||
336 std::is_same<T, std::vector<std::vector<std::vector<int>>>>::value ||
337 std::is_same<T, std::vector<double>>::value ||
338 std::is_same<T, std::vector<std::vector<double>>>::value ||
339 std::is_same<T, std::vector<std::vector<std::vector<double>>>>::value ||
340 std::is_same<T, std::vector<bool>>::value ||
341 std::is_same<T, std::vector<std::vector<bool>>>::value ||
342 std::is_same<T, std::vector<std::vector<std::vector<bool>>>>::value ||
343 std::is_same<T, std::vector<std::string>>::value ||
344 std::is_same<T, std::vector<std::vector<std::string>>>::value ||
345 std::is_same<T, std::vector<std::vector<std::vector<std::string>>>>::value ||
346 std::is_same<T, std::vector<OCRepresentation>>::value ||
347 std::is_same<T, std::vector<std::vector<OCRepresentation>>>::value ||
348 std::is_same<T, std::vector<std::vector<std::vector<OCRepresentation>>>>::value
349 , int>::type = 0// enable_if
352 template<typename T, typename std::enable_if<
354 remove_first<AttributeValue>::type
361 return this->getValue<T>();
364 template<typename T, typename std::enable_if<
365 std::is_same<T, std::nullptr_t>::value
370 this->getValue<NullType>();
375 AttributeItem(const std::string& name,
376 std::map<std::string, AttributeValue>& vals);
377 AttributeItem(const AttributeItem&) = default;
378 std::string m_attrName;
379 std::map<std::string, AttributeValue>& m_values;
382 // Iterator to allow iteration via STL containers/methods
385 friend class OCRepresentation;
387 typedef iterator self_type;
388 typedef AttributeItem value_type;
389 typedef value_type& reference;
390 typedef value_type* pointer;
391 typedef std::forward_iterator_tag iterator_category;
392 typedef int difference_type;
394 iterator(const iterator&) = default;
395 ~iterator() = default;
397 bool operator ==(const iterator&) const;
398 bool operator !=(const iterator&) const;
400 iterator& operator++();
401 iterator operator++(int);
403 reference operator*();
404 pointer operator->();
406 iterator(std::map<std::string, AttributeValue>::iterator&& itr,
407 std::map<std::string, AttributeValue>& vals)
408 : m_iterator(std::move(itr)),
409 m_item(m_iterator != vals.end() ? m_iterator->first:"", vals){}
410 std::map<std::string, AttributeValue>::iterator m_iterator;
411 AttributeItem m_item;
416 friend class OCRepresentation;
418 typedef iterator self_type;
419 typedef const AttributeItem value_type;
420 typedef value_type& const_reference;
421 typedef value_type* const_pointer;
422 typedef std::forward_iterator_tag iterator_category;
423 typedef int difference_type;
425 const_iterator(const iterator& rhs)
426 :m_iterator(rhs.m_iterator), m_item(rhs.m_item){}
427 const_iterator(const const_iterator&) = default;
428 ~const_iterator() = default;
430 bool operator ==(const const_iterator&) const;
431 bool operator !=(const const_iterator&) const;
433 const_iterator& operator++();
434 const_iterator operator++(int);
436 const_reference operator*() const;
437 const_pointer operator->() const;
439 const_iterator(std::map<std::string, AttributeValue>::const_iterator&& itr,
440 std::map<std::string, AttributeValue>& vals)
441 : m_iterator(std::move(itr)),
442 m_item(m_iterator != vals.end() ? m_iterator->first: "", vals){}
443 std::map<std::string, AttributeValue>::const_iterator m_iterator;
444 AttributeItem m_item;
448 const_iterator begin() const;
449 const_iterator cbegin() const;
451 const_iterator end() const;
452 const_iterator cend() const;
456 AttributeItem operator[](const std::string& key);
457 const AttributeItem operator[](const std::string& key) const;
459 friend class OCResourceResponse;
460 friend class MessageContainer;
463 void payload_array_helper(const OCRepPayloadValue* pl, size_t depth);
465 T payload_array_helper_copy(size_t index, const OCRepPayloadValue* pl);
466 void setPayload(const OCRepPayload* payload);
467 void setPayloadArray(const OCRepPayloadValue* pl);
468 void getPayloadArray(OCRepPayload* payload,
469 const OCRepresentation::AttributeItem& item) const;
470 // the root node has a slightly different JSON version
471 // based on the interface type configured in ResourceResponse.
472 // This allows ResourceResponse to set it, so that the save function
473 // doesn't serialize things that it isn't supposed to serialize.
474 void setInterfaceType(InterfaceType ift)
476 m_interfaceType = ift;
479 // class used to wrap the 'prop' feature of the save/load
483 Prop(std::vector<std::string>& resourceTypes,
484 std::vector<std::string>& interfaces)
485 : m_types(resourceTypes), m_interfaces(interfaces)
488 /* Prop(const std::vector<std::string>& resourceTypes,
489 const std::vector<std::string>& interfaces)
490 :m_types(resourceTypes),
491 m_interfaces(interfaces)
494 std::vector<std::string>& m_types;
495 std::vector<std::string>& m_interfaces;
499 std::vector<OCRepresentation> m_children;
500 mutable std::map<std::string, AttributeValue> m_values;
501 std::vector<std::string> m_resourceTypes;
502 std::vector<std::string> m_interfaces;
503 std::vector<std::string> m_dataModelVersions;
505 InterfaceType m_interfaceType;
508 std::ostream& operator <<(std::ostream& os, const OCRepresentation::AttributeItem& ai);
512 #endif // OC_REPRESENTATION_H_