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 void setResourceTypes(const std::vector<std::string>& resourceTypes);
136 void addResourceType(const std::string& str);
138 const std::vector<std::string>& getResourceInterfaces() const;
140 void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
142 void addResourceInterface(const std::string& str);
144 bool emptyData() const;
146 int numberOfAttributes() const;
148 bool erase(const std::string& str);
150 template <typename T>
151 void setValue(const std::string& str, const T& val)
156 // using R-value(or universal ref depending) to move string and vector<uint8_t>
157 template <typename T>
158 void setValue(const std::string& str, T&& val)
160 m_values[str] = std::forward<T>(val);
163 const std::map<std::string, AttributeValue>& getValues() const {
168 * Retrieve the attribute value associated with the supplied name
170 * @param str Name of the attribute
171 * @param val Value of the attribute
172 * @return The getValue method returns true if the attribute was
173 * found in the representation. Otherwise it returns false.
175 template <typename T>
176 bool getValue(const std::string& str, T& val) const
178 auto x = m_values.find(str);
180 if(x!= m_values.end())
184 val = boost::get<T>(x->second);
187 catch (boost::bad_get& e)
201 * Return the attribute value associated with the supplied name
203 * @param str Name of the attribute
204 * @return When the representation contains the attribute, the
205 * the associated value is returned. Otherwise, getValue
206 * returns the default contructed value for the type.
208 template <typename T>
209 T getValue(const std::string& str) const
212 auto x = m_values.find(str);
213 if(x != m_values.end())
217 val = boost::get<T>(x->second);
219 catch (boost::bad_get& e)
228 * Retrieve the attributevalue structure associated with the supplied name
230 * @param str Name of the attribute
231 * @param attrValue Attribute Value structure
232 * @return The getAttributeValue method returns true if the attribute was
233 * found in the representation. Otherwise it returns false.
235 bool getAttributeValue(const std::string& str, AttributeValue& attrValue) const
237 auto x = m_values.find(str);
239 if (x != m_values.end())
241 attrValue = x->second;
250 std::string getValueToString(const std::string& key) const;
251 bool hasAttribute(const std::string& str) const;
253 void setNULL(const std::string& str);
255 bool isNULL(const std::string& str) const;
260 // STL Container stuff
263 class const_iterator;
264 // Shim class to allow iterating and indexing of the OCRepresentation
268 friend class OCRepresentation;
269 friend class iterator;
270 friend class const_iterator;
272 const std::string& attrname() const;
273 AttributeType type() const;
274 AttributeType base_type() const;
275 size_t depth() const;
281 return boost::get<T>(m_values[m_attrName]);
283 catch (boost::bad_get& e)
290 std::string getValueToString() const;
293 AttributeItem& operator=(T&& rhs)
295 m_values[m_attrName] = std::forward<T>(rhs);
299 AttributeItem& operator=(std::nullptr_t /*rhs*/)
302 m_values[m_attrName] = t;
306 // Enable-if required to prevent conversions to alternate types. This prevents
307 // ambigious conversions in the case where conversions can include a number of
308 // types, such as the string constructor.
309 template<typename T, typename std::enable_if<
311 remove_first<AttributeValue>::type
317 return this->getValue<T>();
320 template<typename T, typename std::enable_if<
321 std::is_same<T, std::nullptr_t>::value
326 this->getValue<NullType>();
331 AttributeItem(const std::string& name,
332 std::map<std::string, AttributeValue>& vals);
333 AttributeItem(const AttributeItem&) = default;
334 std::string m_attrName;
335 std::map<std::string, AttributeValue>& m_values;
338 // Iterator to allow iteration via STL containers/methods
341 friend class OCRepresentation;
343 typedef iterator self_type;
344 typedef AttributeItem value_type;
345 typedef value_type& reference;
346 typedef value_type* pointer;
347 typedef std::forward_iterator_tag iterator_category;
348 typedef int difference_type;
350 iterator(const iterator&) = default;
351 ~iterator() = default;
353 bool operator ==(const iterator&) const;
354 bool operator !=(const iterator&) const;
356 iterator& operator++();
357 iterator operator++(int);
359 reference operator*();
360 pointer operator->();
362 iterator(std::map<std::string, AttributeValue>::iterator&& itr,
363 std::map<std::string, AttributeValue>& vals)
364 : m_iterator(std::move(itr)),
365 m_item(m_iterator != vals.end() ? m_iterator->first:"", vals){}
366 std::map<std::string, AttributeValue>::iterator m_iterator;
367 AttributeItem m_item;
372 friend class OCRepresentation;
374 typedef iterator self_type;
375 typedef const AttributeItem value_type;
376 typedef value_type& const_reference;
377 typedef value_type* const_pointer;
378 typedef std::forward_iterator_tag iterator_category;
379 typedef int difference_type;
381 const_iterator(const iterator& rhs)
382 :m_iterator(rhs.m_iterator), m_item(rhs.m_item){}
383 const_iterator(const const_iterator&) = default;
384 ~const_iterator() = default;
386 bool operator ==(const const_iterator&) const;
387 bool operator !=(const const_iterator&) const;
389 const_iterator& operator++();
390 const_iterator operator++(int);
392 const_reference operator*() const;
393 const_pointer operator->() const;
395 const_iterator(std::map<std::string, AttributeValue>::const_iterator&& itr,
396 std::map<std::string, AttributeValue>& vals)
397 : m_iterator(std::move(itr)),
398 m_item(m_iterator != vals.end() ? m_iterator->first: "", vals){}
399 std::map<std::string, AttributeValue>::const_iterator m_iterator;
400 AttributeItem m_item;
404 const_iterator begin() const;
405 const_iterator cbegin() const;
407 const_iterator end() const;
408 const_iterator cend() const;
412 AttributeItem operator[](const std::string& key);
413 const AttributeItem operator[](const std::string& key) const;
415 friend class OCResourceResponse;
416 friend class MessageContainer;
419 void payload_array_helper(const OCRepPayloadValue* pl, size_t depth);
421 T payload_array_helper_copy(size_t index, const OCRepPayloadValue* pl);
422 void setPayload(const OCRepPayload* payload);
423 void setPayloadArray(const OCRepPayloadValue* pl);
424 void getPayloadArray(OCRepPayload* payload,
425 const OCRepresentation::AttributeItem& item) const;
426 // the root node has a slightly different JSON version
427 // based on the interface type configured in ResourceResponse.
428 // This allows ResourceResponse to set it, so that the save function
429 // doesn't serialize things that it isn't supposed to serialize.
430 void setInterfaceType(InterfaceType ift)
432 m_interfaceType = ift;
435 // class used to wrap the 'prop' feature of the save/load
439 Prop(std::vector<std::string>& resourceTypes,
440 std::vector<std::string>& interfaces)
441 : m_types(resourceTypes), m_interfaces(interfaces)
444 /* Prop(const std::vector<std::string>& resourceTypes,
445 const std::vector<std::string>& interfaces)
446 :m_types(resourceTypes),
447 m_interfaces(interfaces)
450 std::vector<std::string>& m_types;
451 std::vector<std::string>& m_interfaces;
455 std::vector<OCRepresentation> m_children;
456 mutable std::map<std::string, AttributeValue> m_values;
457 std::vector<std::string> m_resourceTypes;
458 std::vector<std::string> m_interfaces;
460 InterfaceType m_interfaceType;
463 std::ostream& operator <<(std::ostream& os, const OCRepresentation::AttributeItem& ai);
467 #endif // OC_REPRESENTATION_H_