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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 /// @file OCRepresentation.h
23 /// @brief This file contains the declaration of classes and its members
24 /// related to OCRepresentation
26 #ifndef __OCREPRESENTATION_H
27 #define __OCREPRESENTATION_H
35 #include <AttributeValue.h>
36 #include <StringConstants.h>
38 #include <OCException.h>
41 #include "android_cpp11_compat.h"
52 enum class InterfaceType
63 // The consumer requires resource info to be printed in 2 different ways, both with the "oc":[]
64 // and without. This enum is used to differentiate between the two situations. When the
65 // serialize is called with Include OC, we encode OC, otherwise we skip it and return just the
66 // contents of the array.
67 enum class OCInfoFormat
73 class MessageContainer
76 void setJSONRepresentation(const std::string& payload);
78 void setJSONRepresentation(const unsigned char* payload);
80 std::string getJSONRepresentation(OCInfoFormat f) const;
82 const std::vector<OCRepresentation>& representations() const;
84 void addRepresentation(const OCRepresentation& rep);
86 const OCRepresentation& operator[](int index) const
91 const OCRepresentation& back() const
96 std::vector<OCRepresentation> m_reps;
98 class OCRepresentation
102 std::string getJSONRepresentation() const;
104 void addChild(const OCRepresentation&);
106 void clearChildren();
108 const std::vector<OCRepresentation>& getChildren() const;
110 void setChildren(const std::vector<OCRepresentation>& children);
112 void setUri(const std::string& uri);
114 std::string getUri() const;
116 const std::vector<std::string>& getResourceTypes() const;
118 void setResourceTypes(const std::vector<std::string>& resourceTypes);
120 const std::vector<std::string>& getResourceInterfaces() const;
122 void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
124 bool emptyData() const;
126 int numberOfAttributes() const;
128 bool erase(const std::string& str);
130 template <typename T>
131 void setValue(const std::string& str, const T& val)
136 template <typename T>
137 bool getValue(const std::string& str, T& val) const
139 auto x = m_values.find(str);
141 if(x!= m_values.end())
143 val = boost::get<T>(x->second);
153 template <typename T>
154 T getValue(const std::string& str) const
157 auto x = m_values.find(str);
158 if(x != m_values.end())
160 val = boost::get<T>(x->second);
165 std::string getValueToString(const std::string& key) const;
166 bool hasAttribute(const std::string& str) const;
168 void setNULL(const std::string& str);
170 bool isNULL(const std::string& str) const;
172 // STL Container stuff
175 class const_iterator;
176 // Shim class to allow iterating and indexing of the OCRepresentation
180 friend class OCRepresentation;
181 friend class iterator;
182 friend class const_iterator;
184 const std::string& attrname() const;
185 AttributeType type() const;
186 AttributeType base_type() const;
187 size_t depth() const;
191 return boost::get<T>(m_values[m_attrName]);
194 std::string getValueToString() const;
197 AttributeItem& operator=(T&& rhs)
199 m_values[m_attrName] = std::forward<T>(rhs);
203 AttributeItem& operator=(std::nullptr_t rhs)
206 m_values[m_attrName] = t;
210 // Enable-if required to prevent conversions to alternate types. This prevents
211 // ambigious conversions in the case where conversions can include a number of
212 // types, such as the string constructor.
213 template<typename T, typename= typename std::enable_if<
214 std::is_same<T, int>::value ||
215 std::is_same<T, double>::value ||
216 std::is_same<T, bool>::value ||
217 std::is_same<T, std::string>::value ||
218 std::is_same<T, OCRepresentation>::value ||
219 std::is_same<T, std::vector<int>>::value ||
220 std::is_same<T, std::vector<std::vector<int>>>::value ||
221 std::is_same<T, std::vector<std::vector<std::vector<int>>>>::value ||
222 std::is_same<T, std::vector<double>>::value ||
223 std::is_same<T, std::vector<std::vector<double>>>::value ||
224 std::is_same<T, std::vector<std::vector<std::vector<double>>>>::value ||
225 std::is_same<T, std::vector<bool>>::value ||
226 std::is_same<T, std::vector<std::vector<bool>>>::value ||
227 std::is_same<T, std::vector<std::vector<std::vector<bool>>>>::value ||
228 std::is_same<T, std::vector<std::string>>::value ||
229 std::is_same<T, std::vector<std::vector<std::string>>>::value ||
230 std::is_same<T, std::vector<std::vector<std::vector<std::string>>>>::value ||
231 std::is_same<T, std::vector<OCRepresentation>>::value ||
232 std::is_same<T, std::vector<std::vector<OCRepresentation>>>::value ||
233 std::is_same<T, std::vector<std::vector<std::vector<OCRepresentation>>>>::value
238 return this->getValue<T>();
241 operator std::nullptr_t() const
243 this->getValue<NullType>();
248 AttributeItem(const std::string& name,
249 std::map<std::string, AttributeValue>& vals);
250 AttributeItem(const AttributeItem&) = default;
251 std::string m_attrName;
252 std::map<std::string, AttributeValue>& m_values;
255 // Iterator to allow iteration via STL containers/methods
258 friend class OCRepresentation;
260 typedef iterator self_type;
261 typedef AttributeItem value_type;
262 typedef value_type& reference;
263 typedef value_type* pointer;
264 typedef std::forward_iterator_tag iterator_category;
265 typedef int difference_type;
267 iterator(const iterator&) = default;
268 ~iterator() = default;
270 bool operator ==(const iterator&) const;
271 bool operator !=(const iterator&) const;
273 iterator& operator++();
274 iterator operator++(int);
276 reference operator*();
277 pointer operator->();
279 iterator(std::map<std::string, AttributeValue>::iterator&& itr,
280 std::map<std::string, AttributeValue>& vals)
281 : m_iterator(std::move(itr)),
282 m_item(m_iterator != vals.end() ? m_iterator->first:"", vals){}
283 std::map<std::string, AttributeValue>::iterator m_iterator;
284 AttributeItem m_item;
289 friend class OCRepresentation;
291 typedef iterator self_type;
292 typedef const AttributeItem value_type;
293 typedef value_type& const_reference;
294 typedef value_type* const_pointer;
295 typedef std::forward_iterator_tag iterator_category;
296 typedef int difference_type;
298 const_iterator(const iterator& rhs)
299 :m_iterator(rhs.m_iterator), m_item(rhs.m_item){}
300 const_iterator(const const_iterator&) = default;
301 ~const_iterator() = default;
303 bool operator ==(const const_iterator&) const;
304 bool operator !=(const const_iterator&) const;
306 const_iterator& operator++();
307 const_iterator operator++(int);
309 const_reference operator*() const;
310 const_pointer operator->() const;
312 const_iterator(std::map<std::string, AttributeValue>::const_iterator&& itr,
313 std::map<std::string, AttributeValue>& vals)
314 : m_iterator(std::move(itr)),
315 m_item(m_iterator != vals.end() ? m_iterator->first: "", vals){}
316 std::map<std::string, AttributeValue>::const_iterator m_iterator;
317 AttributeItem m_item;
321 const_iterator begin() const;
322 const_iterator cbegin() const;
324 const_iterator end() const;
325 const_iterator cend() const;
329 AttributeItem operator[](const std::string& key);
330 const AttributeItem operator[](const std::string& key) const;
332 friend class OCResourceResponse;
333 friend class cereal::access;
335 // the root node has a slightly different JSON version
336 // based on the interface type configured in ResourceResponse.
337 // This allows ResourceResponse to set it, so that the save function
338 // doesn't serialize things that it isn't supposed to serialize.
339 void setInterfaceType(InterfaceType ift)
341 m_interfaceType = ift;
344 // class used to wrap the 'prop' feature of the save/load
348 Prop(std::vector<std::string>& resourceTypes,
349 std::vector<std::string>& interfaces)
350 : m_types(resourceTypes), m_interfaces(interfaces)
353 /* Prop(const std::vector<std::string>& resourceTypes,
354 const std::vector<std::string>& interfaces)
355 :m_types(resourceTypes),
356 m_interfaces(interfaces)
359 friend class cereal::access;
360 template <class Archive>
361 void save(Archive& ar) const;
363 template<class Archive>
364 void load(Archive& ar);
366 std::vector<std::string>& m_types;
367 std::vector<std::string>& m_interfaces;
369 template<class Archive, class Val>
370 static void optional_load(Archive& ar, Val&& v);
372 template<class Archive>
373 void save(Archive& ar) const;
375 template<class Archive>
376 void load(Archive& ar);
380 std::vector<OCRepresentation> m_children;
381 mutable std::map<std::string, AttributeValue> m_values;
382 std::vector<std::string> m_resourceTypes;
383 std::vector<std::string> m_interfaces;
385 InterfaceType m_interfaceType;
388 std::ostream& operator <<(std::ostream& os, const OCRepresentation::AttributeItem& ai);
392 #endif //__OCREPRESENTATION_H