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
101 OCRepresentation(): m_interfaceType(InterfaceType::None){}
103 OCRepresentation(OCRepresentation&&) = default;
105 OCRepresentation(const OCRepresentation&) = default;
107 OCRepresentation& operator=(const OCRepresentation&) = default;
109 OCRepresentation& operator=(OCRepresentation&&) = default;
111 virtual ~OCRepresentation(){}
113 std::string getJSONRepresentation() const;
115 void addChild(const OCRepresentation&);
117 void clearChildren();
119 const std::vector<OCRepresentation>& getChildren() const;
121 void setChildren(const std::vector<OCRepresentation>& children);
123 void setUri(const std::string& uri);
125 std::string getUri() const;
127 const std::vector<std::string>& getResourceTypes() const;
129 void setResourceTypes(const std::vector<std::string>& resourceTypes);
131 const std::vector<std::string>& getResourceInterfaces() const;
133 void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
135 bool emptyData() const;
137 int numberOfAttributes() const;
139 bool erase(const std::string& str);
141 template <typename T>
142 void setValue(const std::string& str, const T& val)
147 template <typename T>
148 bool getValue(const std::string& str, T& val) const
150 auto x = m_values.find(str);
152 if(x!= m_values.end())
154 val = boost::get<T>(x->second);
164 template <typename T>
165 T getValue(const std::string& str) const
168 auto x = m_values.find(str);
169 if(x != m_values.end())
171 val = boost::get<T>(x->second);
176 std::string getValueToString(const std::string& key) const;
177 bool hasAttribute(const std::string& str) const;
179 void setNULL(const std::string& str);
181 bool isNULL(const std::string& str) const;
183 // STL Container stuff
186 class const_iterator;
187 // Shim class to allow iterating and indexing of the OCRepresentation
191 friend class OCRepresentation;
192 friend class iterator;
193 friend class const_iterator;
195 const std::string& attrname() const;
196 AttributeType type() const;
197 AttributeType base_type() const;
198 size_t depth() const;
202 return boost::get<T>(m_values[m_attrName]);
205 std::string getValueToString() const;
208 AttributeItem& operator=(T&& rhs)
210 m_values[m_attrName] = std::forward<T>(rhs);
214 AttributeItem& operator=(std::nullptr_t rhs)
217 m_values[m_attrName] = t;
221 // Enable-if required to prevent conversions to alternate types. This prevents
222 // ambigious conversions in the case where conversions can include a number of
223 // types, such as the string constructor.
224 template<typename T, typename= typename std::enable_if<
225 std::is_same<T, int>::value ||
226 std::is_same<T, double>::value ||
227 std::is_same<T, bool>::value ||
228 std::is_same<T, std::string>::value ||
229 std::is_same<T, OCRepresentation>::value ||
230 std::is_same<T, std::vector<int>>::value ||
231 std::is_same<T, std::vector<std::vector<int>>>::value ||
232 std::is_same<T, std::vector<std::vector<std::vector<int>>>>::value ||
233 std::is_same<T, std::vector<double>>::value ||
234 std::is_same<T, std::vector<std::vector<double>>>::value ||
235 std::is_same<T, std::vector<std::vector<std::vector<double>>>>::value ||
236 std::is_same<T, std::vector<bool>>::value ||
237 std::is_same<T, std::vector<std::vector<bool>>>::value ||
238 std::is_same<T, std::vector<std::vector<std::vector<bool>>>>::value ||
239 std::is_same<T, std::vector<std::string>>::value ||
240 std::is_same<T, std::vector<std::vector<std::string>>>::value ||
241 std::is_same<T, std::vector<std::vector<std::vector<std::string>>>>::value ||
242 std::is_same<T, std::vector<OCRepresentation>>::value ||
243 std::is_same<T, std::vector<std::vector<OCRepresentation>>>::value ||
244 std::is_same<T, std::vector<std::vector<std::vector<OCRepresentation>>>>::value
249 return this->getValue<T>();
252 operator std::nullptr_t() const
254 this->getValue<NullType>();
259 AttributeItem(const std::string& name,
260 std::map<std::string, AttributeValue>& vals);
261 AttributeItem(const AttributeItem&) = default;
262 std::string m_attrName;
263 std::map<std::string, AttributeValue>& m_values;
266 // Iterator to allow iteration via STL containers/methods
269 friend class OCRepresentation;
271 typedef iterator self_type;
272 typedef AttributeItem value_type;
273 typedef value_type& reference;
274 typedef value_type* pointer;
275 typedef std::forward_iterator_tag iterator_category;
276 typedef int difference_type;
278 iterator(const iterator&) = default;
279 ~iterator() = default;
281 bool operator ==(const iterator&) const;
282 bool operator !=(const iterator&) const;
284 iterator& operator++();
285 iterator operator++(int);
287 reference operator*();
288 pointer operator->();
290 iterator(std::map<std::string, AttributeValue>::iterator&& itr,
291 std::map<std::string, AttributeValue>& vals)
292 : m_iterator(std::move(itr)),
293 m_item(m_iterator != vals.end() ? m_iterator->first:"", vals){}
294 std::map<std::string, AttributeValue>::iterator m_iterator;
295 AttributeItem m_item;
300 friend class OCRepresentation;
302 typedef iterator self_type;
303 typedef const AttributeItem value_type;
304 typedef value_type& const_reference;
305 typedef value_type* const_pointer;
306 typedef std::forward_iterator_tag iterator_category;
307 typedef int difference_type;
309 const_iterator(const iterator& rhs)
310 :m_iterator(rhs.m_iterator), m_item(rhs.m_item){}
311 const_iterator(const const_iterator&) = default;
312 ~const_iterator() = default;
314 bool operator ==(const const_iterator&) const;
315 bool operator !=(const const_iterator&) const;
317 const_iterator& operator++();
318 const_iterator operator++(int);
320 const_reference operator*() const;
321 const_pointer operator->() const;
323 const_iterator(std::map<std::string, AttributeValue>::const_iterator&& itr,
324 std::map<std::string, AttributeValue>& vals)
325 : m_iterator(std::move(itr)),
326 m_item(m_iterator != vals.end() ? m_iterator->first: "", vals){}
327 std::map<std::string, AttributeValue>::const_iterator m_iterator;
328 AttributeItem m_item;
332 const_iterator begin() const;
333 const_iterator cbegin() const;
335 const_iterator end() const;
336 const_iterator cend() const;
340 AttributeItem operator[](const std::string& key);
341 const AttributeItem operator[](const std::string& key) const;
343 friend class OCResourceResponse;
344 friend class cereal::access;
346 // the root node has a slightly different JSON version
347 // based on the interface type configured in ResourceResponse.
348 // This allows ResourceResponse to set it, so that the save function
349 // doesn't serialize things that it isn't supposed to serialize.
350 void setInterfaceType(InterfaceType ift)
352 m_interfaceType = ift;
355 // class used to wrap the 'prop' feature of the save/load
359 Prop(std::vector<std::string>& resourceTypes,
360 std::vector<std::string>& interfaces)
361 : m_types(resourceTypes), m_interfaces(interfaces)
364 /* Prop(const std::vector<std::string>& resourceTypes,
365 const std::vector<std::string>& interfaces)
366 :m_types(resourceTypes),
367 m_interfaces(interfaces)
370 friend class cereal::access;
371 template <class Archive>
372 void save(Archive& ar) const;
374 template<class Archive>
375 void load(Archive& ar);
377 std::vector<std::string>& m_types;
378 std::vector<std::string>& m_interfaces;
380 template<class Archive, class Val>
381 static void optional_load(Archive& ar, Val&& v);
383 template<class Archive>
384 void save(Archive& ar) const;
386 template<class Archive>
387 void load(Archive& ar);
391 std::vector<OCRepresentation> m_children;
392 mutable std::map<std::string, AttributeValue> m_values;
393 std::vector<std::string> m_resourceTypes;
394 std::vector<std::string> m_interfaces;
396 InterfaceType m_interfaceType;
399 std::ostream& operator <<(std::ostream& os, const OCRepresentation::AttributeItem& ai);
403 #endif //__OCREPRESENTATION_H