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)
157 * Retrieve the attribute value associated with the supplied name
159 * @param str Name of the attribute
160 * @param val Value of the attribute
161 * @return The getValue method returns true if the attribute was
162 * found in the representation. Otherwise it returns false.
164 template <typename T>
165 bool getValue(const std::string& str, T& val) const
167 auto x = m_values.find(str);
169 if(x!= m_values.end())
173 val = boost::get<T>(x->second);
176 catch (boost::bad_get& e)
190 * Return the attribute value associated with the supplied name
192 * @param str Name of the attribute
193 * @return When the representation contains the attribute, the
194 * the associated value is returned. Otherwise, getValue
195 * returns the default contructed value for the type.
197 template <typename T>
198 T getValue(const std::string& str) const
201 auto x = m_values.find(str);
202 if(x != m_values.end())
206 val = boost::get<T>(x->second);
208 catch (boost::bad_get& e)
217 * Retrieve the attributevalue structure associated with the supplied name
219 * @param str Name of the attribute
220 * @param attrValue Attribute Value structure
221 * @return The getAttributeValue method returns true if the attribute was
222 * found in the representation. Otherwise it returns false.
224 bool getAttributeValue(const std::string& str, AttributeValue& attrValue) const
226 auto x = m_values.find(str);
228 if (x != m_values.end())
230 attrValue = x->second;
239 std::string getValueToString(const std::string& key) const;
240 bool hasAttribute(const std::string& str) const;
242 void setNULL(const std::string& str);
244 bool isNULL(const std::string& str) const;
249 // STL Container stuff
252 class const_iterator;
253 // Shim class to allow iterating and indexing of the OCRepresentation
257 friend class OCRepresentation;
258 friend class iterator;
259 friend class const_iterator;
261 const std::string& attrname() const;
262 AttributeType type() const;
263 AttributeType base_type() const;
264 size_t depth() const;
270 return boost::get<T>(m_values[m_attrName]);
272 catch (boost::bad_get& e)
279 std::string getValueToString() const;
282 AttributeItem& operator=(T&& rhs)
284 m_values[m_attrName] = std::forward<T>(rhs);
288 AttributeItem& operator=(std::nullptr_t /*rhs*/)
291 m_values[m_attrName] = t;
295 // Enable-if required to prevent conversions to alternate types. This prevents
296 // ambigious conversions in the case where conversions can include a number of
297 // types, such as the string constructor.
298 template<typename T, typename std::enable_if<
300 remove_first<AttributeValue>::type
306 return this->getValue<T>();
309 template<typename T, typename std::enable_if<
310 std::is_same<T, std::nullptr_t>::value
315 this->getValue<NullType>();
320 AttributeItem(const std::string& name,
321 std::map<std::string, AttributeValue>& vals);
322 AttributeItem(const AttributeItem&) = default;
323 std::string m_attrName;
324 std::map<std::string, AttributeValue>& m_values;
327 // Iterator to allow iteration via STL containers/methods
330 friend class OCRepresentation;
332 typedef iterator self_type;
333 typedef AttributeItem value_type;
334 typedef value_type& reference;
335 typedef value_type* pointer;
336 typedef std::forward_iterator_tag iterator_category;
337 typedef int difference_type;
339 iterator(const iterator&) = default;
340 ~iterator() = default;
342 bool operator ==(const iterator&) const;
343 bool operator !=(const iterator&) const;
345 iterator& operator++();
346 iterator operator++(int);
348 reference operator*();
349 pointer operator->();
351 iterator(std::map<std::string, AttributeValue>::iterator&& itr,
352 std::map<std::string, AttributeValue>& vals)
353 : m_iterator(std::move(itr)),
354 m_item(m_iterator != vals.end() ? m_iterator->first:"", vals){}
355 std::map<std::string, AttributeValue>::iterator m_iterator;
356 AttributeItem m_item;
361 friend class OCRepresentation;
363 typedef iterator self_type;
364 typedef const AttributeItem value_type;
365 typedef value_type& const_reference;
366 typedef value_type* const_pointer;
367 typedef std::forward_iterator_tag iterator_category;
368 typedef int difference_type;
370 const_iterator(const iterator& rhs)
371 :m_iterator(rhs.m_iterator), m_item(rhs.m_item){}
372 const_iterator(const const_iterator&) = default;
373 ~const_iterator() = default;
375 bool operator ==(const const_iterator&) const;
376 bool operator !=(const const_iterator&) const;
378 const_iterator& operator++();
379 const_iterator operator++(int);
381 const_reference operator*() const;
382 const_pointer operator->() const;
384 const_iterator(std::map<std::string, AttributeValue>::const_iterator&& itr,
385 std::map<std::string, AttributeValue>& vals)
386 : m_iterator(std::move(itr)),
387 m_item(m_iterator != vals.end() ? m_iterator->first: "", vals){}
388 std::map<std::string, AttributeValue>::const_iterator m_iterator;
389 AttributeItem m_item;
393 const_iterator begin() const;
394 const_iterator cbegin() const;
396 const_iterator end() const;
397 const_iterator cend() const;
401 AttributeItem operator[](const std::string& key);
402 const AttributeItem operator[](const std::string& key) const;
404 friend class OCResourceResponse;
405 friend class MessageContainer;
408 void payload_array_helper(const OCRepPayloadValue* pl, size_t depth);
410 T payload_array_helper_copy(size_t index, const OCRepPayloadValue* pl);
411 void setPayload(const OCRepPayload* payload);
412 void setPayloadArray(const OCRepPayloadValue* pl);
413 void getPayloadArray(OCRepPayload* payload,
414 const OCRepresentation::AttributeItem& item) const;
415 // the root node has a slightly different JSON version
416 // based on the interface type configured in ResourceResponse.
417 // This allows ResourceResponse to set it, so that the save function
418 // doesn't serialize things that it isn't supposed to serialize.
419 void setInterfaceType(InterfaceType ift)
421 m_interfaceType = ift;
424 // class used to wrap the 'prop' feature of the save/load
428 Prop(std::vector<std::string>& resourceTypes,
429 std::vector<std::string>& interfaces)
430 : m_types(resourceTypes), m_interfaces(interfaces)
433 /* Prop(const std::vector<std::string>& resourceTypes,
434 const std::vector<std::string>& interfaces)
435 :m_types(resourceTypes),
436 m_interfaces(interfaces)
439 std::vector<std::string>& m_types;
440 std::vector<std::string>& m_interfaces;
444 std::vector<OCRepresentation> m_children;
445 mutable std::map<std::string, AttributeValue> m_values;
446 std::vector<std::string> m_resourceTypes;
447 std::vector<std::string> m_interfaces;
449 InterfaceType m_interfaceType;
452 std::ostream& operator <<(std::ostream& os, const OCRepresentation::AttributeItem& ai);
456 #endif // OC_REPRESENTATION_H_