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())
171 val = boost::get<T>(x->second);
182 * Return the attribute value associated with the supplied name
184 * @param str Name of the attribute
185 * @return When the representation contains the attribute, the
186 * the associated value is returned. Otherwise, getValue
187 * returns the default contructed value for the type.
189 template <typename T>
190 T getValue(const std::string& str) const
193 auto x = m_values.find(str);
194 if(x != m_values.end())
196 val = boost::get<T>(x->second);
202 * Retrieve the attributevalue structure associated with the supplied name
204 * @param str Name of the attribute
205 * @param attrValue Attribute Value structure
206 * @return The getAttributeValue method returns true if the attribute was
207 * found in the representation. Otherwise it returns false.
209 bool getAttributeValue(const std::string& str, AttributeValue& attrValue) const
211 auto x = m_values.find(str);
213 if (x != m_values.end())
215 attrValue = x->second;
224 std::string getValueToString(const std::string& key) const;
225 bool hasAttribute(const std::string& str) const;
227 void setNULL(const std::string& str);
229 bool isNULL(const std::string& str) const;
234 // STL Container stuff
237 class const_iterator;
238 // Shim class to allow iterating and indexing of the OCRepresentation
242 friend class OCRepresentation;
243 friend class iterator;
244 friend class const_iterator;
246 const std::string& attrname() const;
247 AttributeType type() const;
248 AttributeType base_type() const;
249 size_t depth() const;
253 return boost::get<T>(m_values[m_attrName]);
256 std::string getValueToString() const;
259 AttributeItem& operator=(T&& rhs)
261 m_values[m_attrName] = std::forward<T>(rhs);
265 AttributeItem& operator=(std::nullptr_t /*rhs*/)
268 m_values[m_attrName] = t;
272 // Enable-if required to prevent conversions to alternate types. This prevents
273 // ambigious conversions in the case where conversions can include a number of
274 // types, such as the string constructor.
275 template<typename T, typename std::enable_if<
277 remove_first<AttributeValue>::type
283 return this->getValue<T>();
286 template<typename T, typename std::enable_if<
287 std::is_same<T, std::nullptr_t>::value
292 this->getValue<NullType>();
297 AttributeItem(const std::string& name,
298 std::map<std::string, AttributeValue>& vals);
299 AttributeItem(const AttributeItem&) = default;
300 std::string m_attrName;
301 std::map<std::string, AttributeValue>& m_values;
304 // Iterator to allow iteration via STL containers/methods
307 friend class OCRepresentation;
309 typedef iterator self_type;
310 typedef AttributeItem value_type;
311 typedef value_type& reference;
312 typedef value_type* pointer;
313 typedef std::forward_iterator_tag iterator_category;
314 typedef int difference_type;
316 iterator(const iterator&) = default;
317 ~iterator() = default;
319 bool operator ==(const iterator&) const;
320 bool operator !=(const iterator&) const;
322 iterator& operator++();
323 iterator operator++(int);
325 reference operator*();
326 pointer operator->();
328 iterator(std::map<std::string, AttributeValue>::iterator&& itr,
329 std::map<std::string, AttributeValue>& vals)
330 : m_iterator(std::move(itr)),
331 m_item(m_iterator != vals.end() ? m_iterator->first:"", vals){}
332 std::map<std::string, AttributeValue>::iterator m_iterator;
333 AttributeItem m_item;
338 friend class OCRepresentation;
340 typedef iterator self_type;
341 typedef const AttributeItem value_type;
342 typedef value_type& const_reference;
343 typedef value_type* const_pointer;
344 typedef std::forward_iterator_tag iterator_category;
345 typedef int difference_type;
347 const_iterator(const iterator& rhs)
348 :m_iterator(rhs.m_iterator), m_item(rhs.m_item){}
349 const_iterator(const const_iterator&) = default;
350 ~const_iterator() = default;
352 bool operator ==(const const_iterator&) const;
353 bool operator !=(const const_iterator&) const;
355 const_iterator& operator++();
356 const_iterator operator++(int);
358 const_reference operator*() const;
359 const_pointer operator->() const;
361 const_iterator(std::map<std::string, AttributeValue>::const_iterator&& itr,
362 std::map<std::string, AttributeValue>& vals)
363 : m_iterator(std::move(itr)),
364 m_item(m_iterator != vals.end() ? m_iterator->first: "", vals){}
365 std::map<std::string, AttributeValue>::const_iterator m_iterator;
366 AttributeItem m_item;
370 const_iterator begin() const;
371 const_iterator cbegin() const;
373 const_iterator end() const;
374 const_iterator cend() const;
378 AttributeItem operator[](const std::string& key);
379 const AttributeItem operator[](const std::string& key) const;
381 friend class OCResourceResponse;
382 friend class MessageContainer;
385 void payload_array_helper(const OCRepPayloadValue* pl, size_t depth);
387 T payload_array_helper_copy(size_t index, const OCRepPayloadValue* pl);
388 void setPayload(const OCRepPayload* payload);
389 void setPayloadArray(const OCRepPayloadValue* pl);
390 void getPayloadArray(OCRepPayload* payload,
391 const OCRepresentation::AttributeItem& item) const;
392 // the root node has a slightly different JSON version
393 // based on the interface type configured in ResourceResponse.
394 // This allows ResourceResponse to set it, so that the save function
395 // doesn't serialize things that it isn't supposed to serialize.
396 void setInterfaceType(InterfaceType ift)
398 m_interfaceType = ift;
401 // class used to wrap the 'prop' feature of the save/load
405 Prop(std::vector<std::string>& resourceTypes,
406 std::vector<std::string>& interfaces)
407 : m_types(resourceTypes), m_interfaces(interfaces)
410 /* Prop(const std::vector<std::string>& resourceTypes,
411 const std::vector<std::string>& interfaces)
412 :m_types(resourceTypes),
413 m_interfaces(interfaces)
416 std::vector<std::string>& m_types;
417 std::vector<std::string>& m_interfaces;
421 std::vector<OCRepresentation> m_children;
422 mutable std::map<std::string, AttributeValue> m_values;
423 std::vector<std::string> m_resourceTypes;
424 std::vector<std::string> m_interfaces;
426 InterfaceType m_interfaceType;
429 std::ostream& operator <<(std::ostream& os, const OCRepresentation::AttributeItem& ai);
433 #endif // OC_REPRESENTATION_H_