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 __OCREPRESENTATION_H
29 #define __OCREPRESENTATION_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 OCRepPayload* getPayload() const;
114 void addChild(const OCRepresentation&);
116 void clearChildren();
118 const std::vector<OCRepresentation>& getChildren() const;
120 void setChildren(const std::vector<OCRepresentation>& children);
122 void setUri(const char* uri);
124 void setUri(const std::string& uri);
126 std::string getUri() const;
128 const std::vector<std::string>& getResourceTypes() const;
130 void setResourceTypes(const std::vector<std::string>& resourceTypes);
132 void addResourceType(const std::string& str);
134 const std::vector<std::string>& getResourceInterfaces() const;
136 void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
138 void addResourceInterface(const std::string& str);
140 bool emptyData() const;
142 int numberOfAttributes() const;
144 bool erase(const std::string& str);
146 template <typename T>
147 void setValue(const std::string& str, const T& val)
153 * Retrieve the attribute value associated with the supplied name
155 * @param str Name of the attribute
156 * @param val Value of the attribute
157 * @return The getValue method returns true if the attribute was
158 * found in the representation. Otherwise it returns false.
160 template <typename T>
161 bool getValue(const std::string& str, T& val) const
163 auto x = m_values.find(str);
165 if(x!= m_values.end())
167 val = boost::get<T>(x->second);
178 * Return the attribute value associated with the supplied name
180 * @param str Name of the attribute
181 * @return When the representation contains the attribute, the
182 * the associated value is returned. Otherwise, getValue
183 * returns the default contructed value for the type.
185 template <typename T>
186 T getValue(const std::string& str) const
189 auto x = m_values.find(str);
190 if(x != m_values.end())
192 val = boost::get<T>(x->second);
198 * Retrieve the attributevalue structure associated with the supplied name
200 * @param str Name of the attribute
201 * @param attrValue Attribute Value structure
202 * @return The getAttributeValue method returns true if the attribute was
203 * found in the representation. Otherwise it returns false.
205 bool getAttributeValue(const std::string& str, AttributeValue& attrValue) const
207 auto x = m_values.find(str);
209 if (x != m_values.end())
211 attrValue = x->second;
220 std::string getValueToString(const std::string& key) const;
221 bool hasAttribute(const std::string& str) const;
223 void setNULL(const std::string& str);
225 bool isNULL(const std::string& str) const;
227 // STL Container stuff
230 class const_iterator;
231 // Shim class to allow iterating and indexing of the OCRepresentation
235 friend class OCRepresentation;
236 friend class iterator;
237 friend class const_iterator;
239 const std::string& attrname() const;
240 AttributeType type() const;
241 AttributeType base_type() const;
242 size_t depth() const;
246 return boost::get<T>(m_values[m_attrName]);
249 std::string getValueToString() const;
252 AttributeItem& operator=(T&& rhs)
254 m_values[m_attrName] = std::forward<T>(rhs);
258 AttributeItem& operator=(std::nullptr_t /*rhs*/)
261 m_values[m_attrName] = t;
265 // Enable-if required to prevent conversions to alternate types. This prevents
266 // ambigious conversions in the case where conversions can include a number of
267 // types, such as the string constructor.
268 template<typename T, typename std::enable_if<
269 std::is_same<T, int>::value ||
270 std::is_same<T, double>::value ||
271 std::is_same<T, bool>::value ||
272 std::is_same<T, std::string>::value ||
273 std::is_same<T, OCRepresentation>::value ||
274 std::is_same<T, std::vector<int>>::value ||
275 std::is_same<T, std::vector<std::vector<int>>>::value ||
276 std::is_same<T, std::vector<std::vector<std::vector<int>>>>::value ||
277 std::is_same<T, std::vector<double>>::value ||
278 std::is_same<T, std::vector<std::vector<double>>>::value ||
279 std::is_same<T, std::vector<std::vector<std::vector<double>>>>::value ||
280 std::is_same<T, std::vector<bool>>::value ||
281 std::is_same<T, std::vector<std::vector<bool>>>::value ||
282 std::is_same<T, std::vector<std::vector<std::vector<bool>>>>::value ||
283 std::is_same<T, std::vector<std::string>>::value ||
284 std::is_same<T, std::vector<std::vector<std::string>>>::value ||
285 std::is_same<T, std::vector<std::vector<std::vector<std::string>>>>::value ||
286 std::is_same<T, std::vector<OCRepresentation>>::value ||
287 std::is_same<T, std::vector<std::vector<OCRepresentation>>>::value ||
288 std::is_same<T, std::vector<std::vector<std::vector<OCRepresentation>>>>::value
289 , int>::type = 0// enable_if
293 return this->getValue<T>();
296 template<typename T, typename std::enable_if<
297 std::is_same<T, std::nullptr_t>::value
302 this->getValue<NullType>();
307 AttributeItem(const std::string& name,
308 std::map<std::string, AttributeValue>& vals);
309 AttributeItem(const AttributeItem&) = default;
310 std::string m_attrName;
311 std::map<std::string, AttributeValue>& m_values;
314 // Iterator to allow iteration via STL containers/methods
317 friend class OCRepresentation;
319 typedef iterator self_type;
320 typedef AttributeItem value_type;
321 typedef value_type& reference;
322 typedef value_type* pointer;
323 typedef std::forward_iterator_tag iterator_category;
324 typedef int difference_type;
326 iterator(const iterator&) = default;
327 ~iterator() = default;
329 bool operator ==(const iterator&) const;
330 bool operator !=(const iterator&) const;
332 iterator& operator++();
333 iterator operator++(int);
335 reference operator*();
336 pointer operator->();
338 iterator(std::map<std::string, AttributeValue>::iterator&& itr,
339 std::map<std::string, AttributeValue>& vals)
340 : m_iterator(std::move(itr)),
341 m_item(m_iterator != vals.end() ? m_iterator->first:"", vals){}
342 std::map<std::string, AttributeValue>::iterator m_iterator;
343 AttributeItem m_item;
348 friend class OCRepresentation;
350 typedef iterator self_type;
351 typedef const AttributeItem value_type;
352 typedef value_type& const_reference;
353 typedef value_type* const_pointer;
354 typedef std::forward_iterator_tag iterator_category;
355 typedef int difference_type;
357 const_iterator(const iterator& rhs)
358 :m_iterator(rhs.m_iterator), m_item(rhs.m_item){}
359 const_iterator(const const_iterator&) = default;
360 ~const_iterator() = default;
362 bool operator ==(const const_iterator&) const;
363 bool operator !=(const const_iterator&) const;
365 const_iterator& operator++();
366 const_iterator operator++(int);
368 const_reference operator*() const;
369 const_pointer operator->() const;
371 const_iterator(std::map<std::string, AttributeValue>::const_iterator&& itr,
372 std::map<std::string, AttributeValue>& vals)
373 : m_iterator(std::move(itr)),
374 m_item(m_iterator != vals.end() ? m_iterator->first: "", vals){}
375 std::map<std::string, AttributeValue>::const_iterator m_iterator;
376 AttributeItem m_item;
380 const_iterator begin() const;
381 const_iterator cbegin() const;
383 const_iterator end() const;
384 const_iterator cend() const;
388 AttributeItem operator[](const std::string& key);
389 const AttributeItem operator[](const std::string& key) const;
391 friend class OCResourceResponse;
392 friend class MessageContainer;
395 void payload_array_helper(const OCRepPayloadValue* pl, size_t depth);
397 T payload_array_helper_copy(size_t index, const OCRepPayloadValue* pl);
398 void setPayload(const OCRepPayload* payload);
399 void setPayloadArray(const OCRepPayloadValue* pl);
400 void getPayloadArray(OCRepPayload* payload,
401 const OCRepresentation::AttributeItem& item) const;
402 // the root node has a slightly different JSON version
403 // based on the interface type configured in ResourceResponse.
404 // This allows ResourceResponse to set it, so that the save function
405 // doesn't serialize things that it isn't supposed to serialize.
406 void setInterfaceType(InterfaceType ift)
408 m_interfaceType = ift;
411 // class used to wrap the 'prop' feature of the save/load
415 Prop(std::vector<std::string>& resourceTypes,
416 std::vector<std::string>& interfaces)
417 : m_types(resourceTypes), m_interfaces(interfaces)
420 /* Prop(const std::vector<std::string>& resourceTypes,
421 const std::vector<std::string>& interfaces)
422 :m_types(resourceTypes),
423 m_interfaces(interfaces)
426 std::vector<std::string>& m_types;
427 std::vector<std::string>& m_interfaces;
431 std::vector<OCRepresentation> m_children;
432 mutable std::map<std::string, AttributeValue> m_values;
433 std::vector<std::string> m_resourceTypes;
434 std::vector<std::string> m_interfaces;
436 InterfaceType m_interfaceType;
439 std::ostream& operator <<(std::ostream& os, const OCRepresentation::AttributeItem& ai);
443 #endif //__OCREPRESENTATION_H