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>
40 #include <OCException.h>
43 #include "android_cpp11_compat.h"
54 enum class InterfaceType
65 // The consumer requires resource info to be printed in 2 different ways, both with the "oc":[]
66 // and without. This enum is used to differentiate between the two situations. When the
67 // serialize is called with Include OC, we encode OC, otherwise we skip it and return just the
68 // contents of the array.
69 enum class OCInfoFormat
75 class MessageContainer
78 void setJSONRepresentation(const std::string& payload);
80 void setJSONRepresentation(const unsigned char* payload);
82 std::string getJSONRepresentation(OCInfoFormat f) const;
84 const std::vector<OCRepresentation>& representations() const;
86 void addRepresentation(const OCRepresentation& rep);
88 const OCRepresentation& operator[](int index) const
93 const OCRepresentation& back() const
98 std::vector<OCRepresentation> m_reps;
100 class OCRepresentation
103 OCRepresentation(): m_interfaceType(InterfaceType::None){}
105 OCRepresentation(OCRepresentation&&) = default;
107 OCRepresentation(const OCRepresentation&) = default;
109 OCRepresentation& operator=(const OCRepresentation&) = default;
111 OCRepresentation& operator=(OCRepresentation&&) = default;
113 virtual ~OCRepresentation(){}
115 std::string getJSONRepresentation() const;
117 void addChild(const OCRepresentation&);
119 void clearChildren();
121 const std::vector<OCRepresentation>& getChildren() const;
123 void setChildren(const std::vector<OCRepresentation>& children);
125 void setUri(const std::string& uri);
127 std::string getUri() const;
129 const std::vector<std::string>& getResourceTypes() const;
131 void setResourceTypes(const std::vector<std::string>& resourceTypes);
133 const std::vector<std::string>& getResourceInterfaces() const;
135 void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
137 bool emptyData() const;
139 int numberOfAttributes() const;
141 bool erase(const std::string& str);
143 template <typename T>
144 void setValue(const std::string& str, const T& val)
149 template <typename T>
150 bool getValue(const std::string& str, T& val) const
152 auto x = m_values.find(str);
154 if(x!= m_values.end())
156 val = boost::get<T>(x->second);
166 template <typename T>
167 T getValue(const std::string& str) const
170 auto x = m_values.find(str);
171 if(x != m_values.end())
173 val = boost::get<T>(x->second);
178 std::string getValueToString(const std::string& key) const;
179 bool hasAttribute(const std::string& str) const;
181 void setNULL(const std::string& str);
183 bool isNULL(const std::string& str) const;
185 // STL Container stuff
188 class const_iterator;
189 // Shim class to allow iterating and indexing of the OCRepresentation
193 friend class OCRepresentation;
194 friend class iterator;
195 friend class const_iterator;
197 const std::string& attrname() const;
198 AttributeType type() const;
199 AttributeType base_type() const;
200 size_t depth() const;
204 return boost::get<T>(m_values[m_attrName]);
207 std::string getValueToString() const;
210 AttributeItem& operator=(T&& rhs)
212 m_values[m_attrName] = std::forward<T>(rhs);
216 AttributeItem& operator=(std::nullptr_t rhs)
219 m_values[m_attrName] = t;
223 // Enable-if required to prevent conversions to alternate types. This prevents
224 // ambigious conversions in the case where conversions can include a number of
225 // types, such as the string constructor.
226 template<typename T, typename= typename std::enable_if<
227 std::is_same<T, int>::value ||
228 std::is_same<T, double>::value ||
229 std::is_same<T, bool>::value ||
230 std::is_same<T, std::string>::value ||
231 std::is_same<T, OCRepresentation>::value ||
232 std::is_same<T, std::vector<int>>::value ||
233 std::is_same<T, std::vector<std::vector<int>>>::value ||
234 std::is_same<T, std::vector<std::vector<std::vector<int>>>>::value ||
235 std::is_same<T, std::vector<double>>::value ||
236 std::is_same<T, std::vector<std::vector<double>>>::value ||
237 std::is_same<T, std::vector<std::vector<std::vector<double>>>>::value ||
238 std::is_same<T, std::vector<bool>>::value ||
239 std::is_same<T, std::vector<std::vector<bool>>>::value ||
240 std::is_same<T, std::vector<std::vector<std::vector<bool>>>>::value ||
241 std::is_same<T, std::vector<std::string>>::value ||
242 std::is_same<T, std::vector<std::vector<std::string>>>::value ||
243 std::is_same<T, std::vector<std::vector<std::vector<std::string>>>>::value ||
244 std::is_same<T, std::vector<OCRepresentation>>::value ||
245 std::is_same<T, std::vector<std::vector<OCRepresentation>>>::value ||
246 std::is_same<T, std::vector<std::vector<std::vector<OCRepresentation>>>>::value
251 return this->getValue<T>();
254 operator std::nullptr_t() const
256 this->getValue<NullType>();
261 AttributeItem(const std::string& name,
262 std::map<std::string, AttributeValue>& vals);
263 AttributeItem(const AttributeItem&) = default;
264 std::string m_attrName;
265 std::map<std::string, AttributeValue>& m_values;
268 // Iterator to allow iteration via STL containers/methods
271 friend class OCRepresentation;
273 typedef iterator self_type;
274 typedef AttributeItem value_type;
275 typedef value_type& reference;
276 typedef value_type* pointer;
277 typedef std::forward_iterator_tag iterator_category;
278 typedef int difference_type;
280 iterator(const iterator&) = default;
281 ~iterator() = default;
283 bool operator ==(const iterator&) const;
284 bool operator !=(const iterator&) const;
286 iterator& operator++();
287 iterator operator++(int);
289 reference operator*();
290 pointer operator->();
292 iterator(std::map<std::string, AttributeValue>::iterator&& itr,
293 std::map<std::string, AttributeValue>& vals)
294 : m_iterator(std::move(itr)),
295 m_item(m_iterator != vals.end() ? m_iterator->first:"", vals){}
296 std::map<std::string, AttributeValue>::iterator m_iterator;
297 AttributeItem m_item;
302 friend class OCRepresentation;
304 typedef iterator self_type;
305 typedef const AttributeItem value_type;
306 typedef value_type& const_reference;
307 typedef value_type* const_pointer;
308 typedef std::forward_iterator_tag iterator_category;
309 typedef int difference_type;
311 const_iterator(const iterator& rhs)
312 :m_iterator(rhs.m_iterator), m_item(rhs.m_item){}
313 const_iterator(const const_iterator&) = default;
314 ~const_iterator() = default;
316 bool operator ==(const const_iterator&) const;
317 bool operator !=(const const_iterator&) const;
319 const_iterator& operator++();
320 const_iterator operator++(int);
322 const_reference operator*() const;
323 const_pointer operator->() const;
325 const_iterator(std::map<std::string, AttributeValue>::const_iterator&& itr,
326 std::map<std::string, AttributeValue>& vals)
327 : m_iterator(std::move(itr)),
328 m_item(m_iterator != vals.end() ? m_iterator->first: "", vals){}
329 std::map<std::string, AttributeValue>::const_iterator m_iterator;
330 AttributeItem m_item;
334 const_iterator begin() const;
335 const_iterator cbegin() const;
337 const_iterator end() const;
338 const_iterator cend() const;
342 AttributeItem operator[](const std::string& key);
343 const AttributeItem operator[](const std::string& key) const;
345 friend class OCResourceResponse;
346 friend class cereal::access;
348 // the root node has a slightly different JSON version
349 // based on the interface type configured in ResourceResponse.
350 // This allows ResourceResponse to set it, so that the save function
351 // doesn't serialize things that it isn't supposed to serialize.
352 void setInterfaceType(InterfaceType ift)
354 m_interfaceType = ift;
357 // class used to wrap the 'prop' feature of the save/load
361 Prop(std::vector<std::string>& resourceTypes,
362 std::vector<std::string>& interfaces)
363 : m_types(resourceTypes), m_interfaces(interfaces)
366 /* Prop(const std::vector<std::string>& resourceTypes,
367 const std::vector<std::string>& interfaces)
368 :m_types(resourceTypes),
369 m_interfaces(interfaces)
372 friend class cereal::access;
373 template <class Archive>
374 void save(Archive& ar) const;
376 template<class Archive>
377 void load(Archive& ar);
379 std::vector<std::string>& m_types;
380 std::vector<std::string>& m_interfaces;
382 template<class Archive, class Val>
383 static void optional_load(Archive& ar, Val&& v);
385 template<class Archive>
386 void save(Archive& ar) const;
388 template<class Archive>
389 void load(Archive& ar);
393 std::vector<OCRepresentation> m_children;
394 mutable std::map<std::string, AttributeValue> m_values;
395 std::vector<std::string> m_resourceTypes;
396 std::vector<std::string> m_interfaces;
398 InterfaceType m_interfaceType;
401 std::ostream& operator <<(std::ostream& os, const OCRepresentation::AttributeItem& ai);
405 #endif //__OCREPRESENTATION_H