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_
37 #include <AttributeValue.h>
38 #include <StringConstants.h>
41 #include "OCAndroid.h"
44 #include <OCException.h>
49 enum class InterfaceType
60 class MessageContainer
63 void setPayload(const OCPayload* rep);
65 void setPayload(const OCDevicePayload* rep);
67 void setPayload(const OCPlatformPayload* rep);
69 void setPayload(const OCRepPayload* rep);
71 OCRepPayload* getPayload() const;
73 const std::vector<OCRepresentation>& representations() const;
75 void addRepresentation(const OCRepresentation& rep);
77 const OCRepresentation& operator[](int index) const
82 const OCRepresentation& back() const
87 std::vector<OCRepresentation> m_reps;
89 class OCRepresentation
92 friend bool operator==(const OC::OCRepresentation&, const OC::OCRepresentation&);
93 // Note: Implementation of all constructors and destructors
94 // are all placed in the same location due to a crash that
95 // was observed in Android, where merely constructing/destructing
96 // an OCRepresentation object was enough to cause an invalid 'free'.
97 // It is believed that this is a result of incompatible compiler
98 // options between the gradle JNI and armeabi scons build, however
99 // this fix will work in the meantime.
100 OCRepresentation(): m_interfaceType(InterfaceType::None){}
102 OCRepresentation(OCRepresentation&&) = default;
104 OCRepresentation(const OCRepresentation&) = default;
106 OCRepresentation& operator=(const OCRepresentation&) = default;
108 OCRepresentation& operator=(OCRepresentation&&) = default;
110 virtual ~OCRepresentation(){}
112 void setDevAddr(const OCDevAddr addr);
114 const std::string getHost() const;
116 OCRepPayload* getPayload() const;
118 void addChild(const OCRepresentation&);
120 void clearChildren();
122 const std::vector<OCRepresentation>& getChildren() const;
124 void setChildren(const std::vector<OCRepresentation>& children);
126 void setUri(const char* uri);
128 void setUri(const std::string& uri);
130 std::string getUri() const;
132 const std::vector<std::string>& getResourceTypes() const;
134 const std::vector<std::string>& getDataModelVersions() const;
136 void setResourceTypes(const std::vector<std::string>& resourceTypes);
138 void addResourceType(const std::string& str);
140 const std::vector<std::string>& getResourceInterfaces() const;
142 void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
144 void addResourceInterface(const std::string& str);
146 void addDataModelVersion(const std::string& str);
148 bool emptyData() const;
150 int numberOfAttributes() const;
152 bool erase(const std::string& str);
154 template <typename T>
155 void setValue(const std::string& str, const T& val)
160 // using R-value(or universal ref depending) to move string and vector<uint8_t>
161 template <typename T>
162 void setValue(const std::string& str, T&& val)
164 m_values[str] = std::forward<T>(val);
167 const std::map<std::string, AttributeValue>& getValues() const {
172 * Retrieve the attribute value associated with the supplied name
174 * @param str Name of the attribute
175 * @param val Value of the attribute
176 * @return The getValue method returns true if the attribute was
177 * found in the representation. Otherwise it returns false.
179 template <typename T>
180 bool getValue(const std::string& str, T& val) const
182 auto x = m_values.find(str);
184 if(x!= m_values.end())
188 val = boost::get<T>(x->second);
191 catch (boost::bad_get& e)
205 * Return the attribute value associated with the supplied name
207 * @param str Name of the attribute
208 * @return When the representation contains the attribute, the
209 * the associated value is returned. Otherwise, getValue
210 * returns the default contructed value for the type.
212 template <typename T>
213 T getValue(const std::string& str) const
216 auto x = m_values.find(str);
217 if(x != m_values.end())
221 val = boost::get<T>(x->second);
223 catch (boost::bad_get& e)
232 * Retrieve the attributevalue structure associated with the supplied name
234 * @param str Name of the attribute
235 * @param attrValue Attribute Value structure
236 * @return The getAttributeValue method returns true if the attribute was
237 * found in the representation. Otherwise it returns false.
239 bool getAttributeValue(const std::string& str, AttributeValue& attrValue) const
241 auto x = m_values.find(str);
243 if (x != m_values.end())
245 attrValue = x->second;
254 std::string getValueToString(const std::string& key) const;
255 bool hasAttribute(const std::string& str) const;
257 void setNULL(const std::string& str);
259 bool isNULL(const std::string& str) const;
264 // STL Container stuff
267 class const_iterator;
268 // Shim class to allow iterating and indexing of the OCRepresentation
272 friend class OCRepresentation;
273 friend class iterator;
274 friend class const_iterator;
276 const std::string& attrname() const;
277 AttributeType type() const;
278 AttributeType base_type() const;
279 size_t depth() const;
285 return boost::get<T>(m_values[m_attrName]);
287 catch (boost::bad_get& e)
294 std::string getValueToString() const;
297 AttributeItem& operator=(T&& rhs)
299 m_values[m_attrName] = std::forward<T>(rhs);
303 AttributeItem& operator=(std::nullptr_t /*rhs*/)
306 m_values[m_attrName] = t;
310 // Enable-if required to prevent conversions to alternate types. This prevents
311 // ambigious conversions in the case where conversions can include a number of
312 // types, such as the string constructor.
313 template<typename T, typename std::enable_if<
315 remove_first<AttributeValue>::type
321 return this->getValue<T>();
324 template<typename T, typename std::enable_if<
325 std::is_same<T, std::nullptr_t>::value
330 this->getValue<NullType>();
335 AttributeItem(const std::string& name,
336 std::map<std::string, AttributeValue>& vals);
337 AttributeItem(const AttributeItem&) = default;
338 std::string m_attrName;
339 std::map<std::string, AttributeValue>& m_values;
342 // Iterator to allow iteration via STL containers/methods
345 friend class OCRepresentation;
347 typedef iterator self_type;
348 typedef AttributeItem value_type;
349 typedef value_type& reference;
350 typedef value_type* pointer;
351 typedef std::forward_iterator_tag iterator_category;
352 typedef int difference_type;
354 iterator(const iterator&) = default;
355 ~iterator() = default;
357 bool operator ==(const iterator&) const;
358 bool operator !=(const iterator&) const;
360 iterator& operator++();
361 iterator operator++(int);
363 reference operator*();
364 pointer operator->();
366 iterator(std::map<std::string, AttributeValue>::iterator&& itr,
367 std::map<std::string, AttributeValue>& vals)
368 : m_iterator(std::move(itr)),
369 m_item(m_iterator != vals.end() ? m_iterator->first:"", vals){}
370 std::map<std::string, AttributeValue>::iterator m_iterator;
371 AttributeItem m_item;
376 friend class OCRepresentation;
378 typedef iterator self_type;
379 typedef const AttributeItem value_type;
380 typedef value_type& const_reference;
381 typedef value_type* const_pointer;
382 typedef std::forward_iterator_tag iterator_category;
383 typedef int difference_type;
385 const_iterator(const iterator& rhs)
386 :m_iterator(rhs.m_iterator), m_item(rhs.m_item){}
387 const_iterator(const const_iterator&) = default;
388 ~const_iterator() = default;
390 bool operator ==(const const_iterator&) const;
391 bool operator !=(const const_iterator&) const;
393 const_iterator& operator++();
394 const_iterator operator++(int);
396 const_reference operator*() const;
397 const_pointer operator->() const;
399 const_iterator(std::map<std::string, AttributeValue>::const_iterator&& itr,
400 std::map<std::string, AttributeValue>& vals)
401 : m_iterator(std::move(itr)),
402 m_item(m_iterator != vals.end() ? m_iterator->first: "", vals){}
403 std::map<std::string, AttributeValue>::const_iterator m_iterator;
404 AttributeItem m_item;
408 const_iterator begin() const;
409 const_iterator cbegin() const;
411 const_iterator end() const;
412 const_iterator cend() const;
416 AttributeItem operator[](const std::string& key);
417 const AttributeItem operator[](const std::string& key) const;
419 friend class OCResourceResponse;
420 friend class MessageContainer;
423 void payload_array_helper(const OCRepPayloadValue* pl, size_t depth);
425 T payload_array_helper_copy(size_t index, const OCRepPayloadValue* pl);
426 void setPayload(const OCRepPayload* payload);
427 void setPayloadArray(const OCRepPayloadValue* pl);
428 void getPayloadArray(OCRepPayload* payload,
429 const OCRepresentation::AttributeItem& item) const;
430 // the root node has a slightly different JSON version
431 // based on the interface type configured in ResourceResponse.
432 // This allows ResourceResponse to set it, so that the save function
433 // doesn't serialize things that it isn't supposed to serialize.
434 void setInterfaceType(InterfaceType ift)
436 m_interfaceType = ift;
439 // class used to wrap the 'prop' feature of the save/load
443 Prop(std::vector<std::string>& resourceTypes,
444 std::vector<std::string>& interfaces)
445 : m_types(resourceTypes), m_interfaces(interfaces)
448 /* Prop(const std::vector<std::string>& resourceTypes,
449 const std::vector<std::string>& interfaces)
450 :m_types(resourceTypes),
451 m_interfaces(interfaces)
454 std::vector<std::string>& m_types;
455 std::vector<std::string>& m_interfaces;
459 std::vector<OCRepresentation> m_children;
460 mutable std::map<std::string, AttributeValue> m_values;
461 std::vector<std::string> m_resourceTypes;
462 std::vector<std::string> m_interfaces;
463 std::vector<std::string> m_dataModelVersions;
465 InterfaceType m_interfaceType;
468 std::ostream& operator <<(std::ostream& os, const OCRepresentation::AttributeItem& ai);
472 #endif // OC_REPRESENTATION_H_