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>
46 #include "android_cpp11_compat.h"
57 enum class InterfaceType
68 // The consumer requires resource info to be printed in 2 different ways, both with the "oc":[]
69 // and without. This enum is used to differentiate between the two situations. When the
70 // serialize is called with Include OC, we encode OC, otherwise we skip it and return just the
71 // contents of the array.
72 enum class OCInfoFormat
78 class MessageContainer
81 void setJSONRepresentation(const std::string& payload);
83 void setJSONRepresentation(const char* payload);
85 std::string getJSONRepresentation(OCInfoFormat f) const;
87 const std::vector<OCRepresentation>& representations() const;
89 void addRepresentation(const OCRepresentation& rep);
91 const OCRepresentation& operator[](int index) const
96 const OCRepresentation& back() const
101 std::vector<OCRepresentation> m_reps;
103 class OCRepresentation
106 // Note: Implementation of all constructors and destructors
107 // are all placed in the same location due to a crash that
108 // was observed in Android, where merely constructing/destructing
109 // an OCRepresentation object was enough to cause an invalid 'free'.
110 // It is believed that this is a result of incompatible compiler
111 // options between the gradle JNI and armeabi scons build, however
112 // this fix will work in the meantime.
113 OCRepresentation(): m_interfaceType(InterfaceType::None){}
115 OCRepresentation(OCRepresentation&&) = default;
117 OCRepresentation(const OCRepresentation&) = default;
119 OCRepresentation& operator=(const OCRepresentation&) = default;
121 OCRepresentation& operator=(OCRepresentation&&) = default;
123 virtual ~OCRepresentation(){}
125 std::string getJSONRepresentation() const;
127 void addChild(const OCRepresentation&);
129 void clearChildren();
131 const std::vector<OCRepresentation>& getChildren() const;
133 void setChildren(const std::vector<OCRepresentation>& children);
135 void setUri(const std::string& uri);
137 std::string getUri() const;
139 const std::vector<std::string>& getResourceTypes() const;
141 void setResourceTypes(const std::vector<std::string>& resourceTypes);
143 const std::vector<std::string>& getResourceInterfaces() const;
145 void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
147 bool emptyData() const;
149 int numberOfAttributes() const;
151 bool erase(const std::string& str);
153 template <typename T>
154 void setValue(const std::string& str, const T& val)
160 * Retrieve the attribute value associated with the supplied name
162 * @param str Name of the attribute
163 * @param val Value of the attribute
164 * @return The getValue method returns true if the attribute was
165 * found in the representation. Otherwise it returns false.
167 template <typename T>
168 bool getValue(const std::string& str, T& val) const
170 auto x = m_values.find(str);
172 if(x!= m_values.end())
174 val = boost::get<T>(x->second);
185 * Return the attribute value associated with the supplied name
187 * @param str Name of the attribute
188 * @return When the representation contains the attribute, the
189 * the associated value is returned. Otherwise, getValue
190 * returns the default contructed value for the type.
192 template <typename T>
193 T getValue(const std::string& str) const
196 auto x = m_values.find(str);
197 if(x != m_values.end())
199 val = boost::get<T>(x->second);
204 std::string getValueToString(const std::string& key) const;
205 bool hasAttribute(const std::string& str) const;
207 void setNULL(const std::string& str);
209 bool isNULL(const std::string& str) const;
211 // STL Container stuff
214 class const_iterator;
215 // Shim class to allow iterating and indexing of the OCRepresentation
219 friend class OCRepresentation;
220 friend class iterator;
221 friend class const_iterator;
223 const std::string& attrname() const;
224 AttributeType type() const;
225 AttributeType base_type() const;
226 size_t depth() const;
230 return boost::get<T>(m_values[m_attrName]);
233 std::string getValueToString() const;
236 AttributeItem& operator=(T&& rhs)
238 m_values[m_attrName] = std::forward<T>(rhs);
242 AttributeItem& operator=(std::nullptr_t rhs)
245 m_values[m_attrName] = t;
249 // Enable-if required to prevent conversions to alternate types. This prevents
250 // ambigious conversions in the case where conversions can include a number of
251 // types, such as the string constructor.
252 template<typename T, typename= typename std::enable_if<
253 std::is_same<T, int>::value ||
254 std::is_same<T, double>::value ||
255 std::is_same<T, bool>::value ||
256 std::is_same<T, std::string>::value ||
257 std::is_same<T, OCRepresentation>::value ||
258 std::is_same<T, std::vector<int>>::value ||
259 std::is_same<T, std::vector<std::vector<int>>>::value ||
260 std::is_same<T, std::vector<std::vector<std::vector<int>>>>::value ||
261 std::is_same<T, std::vector<double>>::value ||
262 std::is_same<T, std::vector<std::vector<double>>>::value ||
263 std::is_same<T, std::vector<std::vector<std::vector<double>>>>::value ||
264 std::is_same<T, std::vector<bool>>::value ||
265 std::is_same<T, std::vector<std::vector<bool>>>::value ||
266 std::is_same<T, std::vector<std::vector<std::vector<bool>>>>::value ||
267 std::is_same<T, std::vector<std::string>>::value ||
268 std::is_same<T, std::vector<std::vector<std::string>>>::value ||
269 std::is_same<T, std::vector<std::vector<std::vector<std::string>>>>::value ||
270 std::is_same<T, std::vector<OCRepresentation>>::value ||
271 std::is_same<T, std::vector<std::vector<OCRepresentation>>>::value ||
272 std::is_same<T, std::vector<std::vector<std::vector<OCRepresentation>>>>::value
277 return this->getValue<T>();
280 operator std::nullptr_t() const
282 this->getValue<NullType>();
287 AttributeItem(const std::string& name,
288 std::map<std::string, AttributeValue>& vals);
289 AttributeItem(const AttributeItem&) = default;
290 std::string m_attrName;
291 std::map<std::string, AttributeValue>& m_values;
294 // Iterator to allow iteration via STL containers/methods
297 friend class OCRepresentation;
299 typedef iterator self_type;
300 typedef AttributeItem value_type;
301 typedef value_type& reference;
302 typedef value_type* pointer;
303 typedef std::forward_iterator_tag iterator_category;
304 typedef int difference_type;
306 iterator(const iterator&) = default;
307 ~iterator() = default;
309 bool operator ==(const iterator&) const;
310 bool operator !=(const iterator&) const;
312 iterator& operator++();
313 iterator operator++(int);
315 reference operator*();
316 pointer operator->();
318 iterator(std::map<std::string, AttributeValue>::iterator&& itr,
319 std::map<std::string, AttributeValue>& vals)
320 : m_iterator(std::move(itr)),
321 m_item(m_iterator != vals.end() ? m_iterator->first:"", vals){}
322 std::map<std::string, AttributeValue>::iterator m_iterator;
323 AttributeItem m_item;
328 friend class OCRepresentation;
330 typedef iterator self_type;
331 typedef const AttributeItem value_type;
332 typedef value_type& const_reference;
333 typedef value_type* const_pointer;
334 typedef std::forward_iterator_tag iterator_category;
335 typedef int difference_type;
337 const_iterator(const iterator& rhs)
338 :m_iterator(rhs.m_iterator), m_item(rhs.m_item){}
339 const_iterator(const const_iterator&) = default;
340 ~const_iterator() = default;
342 bool operator ==(const const_iterator&) const;
343 bool operator !=(const const_iterator&) const;
345 const_iterator& operator++();
346 const_iterator operator++(int);
348 const_reference operator*() const;
349 const_pointer operator->() const;
351 const_iterator(std::map<std::string, AttributeValue>::const_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>::const_iterator m_iterator;
356 AttributeItem m_item;
360 const_iterator begin() const;
361 const_iterator cbegin() const;
363 const_iterator end() const;
364 const_iterator cend() const;
368 AttributeItem operator[](const std::string& key);
369 const AttributeItem operator[](const std::string& key) const;
371 friend class OCResourceResponse;
372 friend class cereal::access;
374 // the root node has a slightly different JSON version
375 // based on the interface type configured in ResourceResponse.
376 // This allows ResourceResponse to set it, so that the save function
377 // doesn't serialize things that it isn't supposed to serialize.
378 void setInterfaceType(InterfaceType ift)
380 m_interfaceType = ift;
383 // class used to wrap the 'prop' feature of the save/load
387 Prop(std::vector<std::string>& resourceTypes,
388 std::vector<std::string>& interfaces)
389 : m_types(resourceTypes), m_interfaces(interfaces)
392 /* Prop(const std::vector<std::string>& resourceTypes,
393 const std::vector<std::string>& interfaces)
394 :m_types(resourceTypes),
395 m_interfaces(interfaces)
398 friend class cereal::access;
399 template <class Archive>
400 void save(Archive& ar) const;
402 template<class Archive>
403 void load(Archive& ar);
405 std::vector<std::string>& m_types;
406 std::vector<std::string>& m_interfaces;
408 template<class Archive, class Val>
409 static void optional_load(Archive& ar, Val&& v);
411 template<class Archive>
412 void save(Archive& ar) const;
414 template<class Archive>
415 void load(Archive& ar);
419 std::vector<OCRepresentation> m_children;
420 mutable std::map<std::string, AttributeValue> m_values;
421 std::vector<std::string> m_resourceTypes;
422 std::vector<std::string> m_interfaces;
424 InterfaceType m_interfaceType;
427 std::ostream& operator <<(std::ostream& os, const OCRepresentation::AttributeItem& ai);
431 #endif //__OCREPRESENTATION_H