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>
39 #include "OCAndroid.h"
42 #include <OCException.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 // Note: Implementation of all constructors and destructors
102 // are all placed in the same location due to a crash that
103 // was observed in Android, where merely constructing/destructing
104 // an OCRepresentation object was enough to cause an invalid 'free'.
105 // It is believed that this is a result of incompatible compiler
106 // options between the gradle JNI and armeabi scons build, however
107 // this fix will work in the meantime.
108 OCRepresentation(): m_interfaceType(InterfaceType::None){}
110 OCRepresentation(OCRepresentation&&) = default;
112 OCRepresentation(const OCRepresentation&) = default;
114 OCRepresentation& operator=(const OCRepresentation&) = default;
116 OCRepresentation& operator=(OCRepresentation&&) = default;
118 virtual ~OCRepresentation(){}
120 std::string getJSONRepresentation() const;
122 void addChild(const OCRepresentation&);
124 void clearChildren();
126 const std::vector<OCRepresentation>& getChildren() const;
128 void setChildren(const std::vector<OCRepresentation>& children);
130 void setUri(const std::string& uri);
132 std::string getUri() const;
134 const std::vector<std::string>& getResourceTypes() const;
136 void setResourceTypes(const std::vector<std::string>& resourceTypes);
138 const std::vector<std::string>& getResourceInterfaces() const;
140 void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
142 bool emptyData() const;
144 int numberOfAttributes() const;
146 bool erase(const std::string& str);
148 template <typename T>
149 void setValue(const std::string& str, const T& val)
154 template <typename T>
155 bool getValue(const std::string& str, T& val) const
157 auto x = m_values.find(str);
159 if(x!= m_values.end())
161 val = boost::get<T>(x->second);
171 template <typename T>
172 T getValue(const std::string& str) const
175 auto x = m_values.find(str);
176 if(x != m_values.end())
178 val = boost::get<T>(x->second);
183 std::string getValueToString(const std::string& key) const;
184 bool hasAttribute(const std::string& str) const;
186 void setNULL(const std::string& str);
188 bool isNULL(const std::string& str) const;
190 // STL Container stuff
193 class const_iterator;
194 // Shim class to allow iterating and indexing of the OCRepresentation
198 friend class OCRepresentation;
199 friend class iterator;
200 friend class const_iterator;
202 const std::string& attrname() const;
203 AttributeType type() const;
204 AttributeType base_type() const;
205 size_t depth() const;
209 return boost::get<T>(m_values[m_attrName]);
212 std::string getValueToString() const;
215 AttributeItem& operator=(T&& rhs)
217 m_values[m_attrName] = std::forward<T>(rhs);
221 AttributeItem& operator=(std::nullptr_t rhs)
224 m_values[m_attrName] = t;
228 // Enable-if required to prevent conversions to alternate types. This prevents
229 // ambigious conversions in the case where conversions can include a number of
230 // types, such as the string constructor.
231 template<typename T, typename= typename std::enable_if<
232 std::is_same<T, int>::value ||
233 std::is_same<T, double>::value ||
234 std::is_same<T, bool>::value ||
235 std::is_same<T, std::string>::value ||
236 std::is_same<T, OCRepresentation>::value ||
237 std::is_same<T, std::vector<int>>::value ||
238 std::is_same<T, std::vector<std::vector<int>>>::value ||
239 std::is_same<T, std::vector<std::vector<std::vector<int>>>>::value ||
240 std::is_same<T, std::vector<double>>::value ||
241 std::is_same<T, std::vector<std::vector<double>>>::value ||
242 std::is_same<T, std::vector<std::vector<std::vector<double>>>>::value ||
243 std::is_same<T, std::vector<bool>>::value ||
244 std::is_same<T, std::vector<std::vector<bool>>>::value ||
245 std::is_same<T, std::vector<std::vector<std::vector<bool>>>>::value ||
246 std::is_same<T, std::vector<std::string>>::value ||
247 std::is_same<T, std::vector<std::vector<std::string>>>::value ||
248 std::is_same<T, std::vector<std::vector<std::vector<std::string>>>>::value ||
249 std::is_same<T, std::vector<OCRepresentation>>::value ||
250 std::is_same<T, std::vector<std::vector<OCRepresentation>>>::value ||
251 std::is_same<T, std::vector<std::vector<std::vector<OCRepresentation>>>>::value
256 return this->getValue<T>();
259 operator std::nullptr_t() const
261 this->getValue<NullType>();
266 AttributeItem(const std::string& name,
267 std::map<std::string, AttributeValue>& vals);
268 AttributeItem(const AttributeItem&) = default;
269 std::string m_attrName;
270 std::map<std::string, AttributeValue>& m_values;
273 // Iterator to allow iteration via STL containers/methods
276 friend class OCRepresentation;
278 typedef iterator self_type;
279 typedef AttributeItem value_type;
280 typedef value_type& reference;
281 typedef value_type* pointer;
282 typedef std::forward_iterator_tag iterator_category;
283 typedef int difference_type;
285 iterator(const iterator&) = default;
286 ~iterator() = default;
288 bool operator ==(const iterator&) const;
289 bool operator !=(const iterator&) const;
291 iterator& operator++();
292 iterator operator++(int);
294 reference operator*();
295 pointer operator->();
297 iterator(std::map<std::string, AttributeValue>::iterator&& itr,
298 std::map<std::string, AttributeValue>& vals)
299 : m_iterator(std::move(itr)),
300 m_item(m_iterator != vals.end() ? m_iterator->first:"", vals){}
301 std::map<std::string, AttributeValue>::iterator m_iterator;
302 AttributeItem m_item;
307 friend class OCRepresentation;
309 typedef iterator self_type;
310 typedef const AttributeItem value_type;
311 typedef value_type& const_reference;
312 typedef value_type* const_pointer;
313 typedef std::forward_iterator_tag iterator_category;
314 typedef int difference_type;
316 const_iterator(const iterator& rhs)
317 :m_iterator(rhs.m_iterator), m_item(rhs.m_item){}
318 const_iterator(const const_iterator&) = default;
319 ~const_iterator() = default;
321 bool operator ==(const const_iterator&) const;
322 bool operator !=(const const_iterator&) const;
324 const_iterator& operator++();
325 const_iterator operator++(int);
327 const_reference operator*() const;
328 const_pointer operator->() const;
330 const_iterator(std::map<std::string, AttributeValue>::const_iterator&& itr,
331 std::map<std::string, AttributeValue>& vals)
332 : m_iterator(std::move(itr)),
333 m_item(m_iterator != vals.end() ? m_iterator->first: "", vals){}
334 std::map<std::string, AttributeValue>::const_iterator m_iterator;
335 AttributeItem m_item;
339 const_iterator begin() const;
340 const_iterator cbegin() const;
342 const_iterator end() const;
343 const_iterator cend() const;
347 AttributeItem operator[](const std::string& key);
348 const AttributeItem operator[](const std::string& key) const;
350 friend class OCResourceResponse;
351 friend class cereal::access;
353 // the root node has a slightly different JSON version
354 // based on the interface type configured in ResourceResponse.
355 // This allows ResourceResponse to set it, so that the save function
356 // doesn't serialize things that it isn't supposed to serialize.
357 void setInterfaceType(InterfaceType ift)
359 m_interfaceType = ift;
362 // class used to wrap the 'prop' feature of the save/load
366 Prop(std::vector<std::string>& resourceTypes,
367 std::vector<std::string>& interfaces)
368 : m_types(resourceTypes), m_interfaces(interfaces)
371 /* Prop(const std::vector<std::string>& resourceTypes,
372 const std::vector<std::string>& interfaces)
373 :m_types(resourceTypes),
374 m_interfaces(interfaces)
377 friend class cereal::access;
378 template <class Archive>
379 void save(Archive& ar) const;
381 template<class Archive>
382 void load(Archive& ar);
384 std::vector<std::string>& m_types;
385 std::vector<std::string>& m_interfaces;
387 template<class Archive, class Val>
388 static void optional_load(Archive& ar, Val&& v);
390 template<class Archive>
391 void save(Archive& ar) const;
393 template<class Archive>
394 void load(Archive& ar);
398 std::vector<OCRepresentation> m_children;
399 mutable std::map<std::string, AttributeValue> m_values;
400 std::vector<std::string> m_resourceTypes;
401 std::vector<std::string> m_interfaces;
403 InterfaceType m_interfaceType;
406 std::ostream& operator <<(std::ostream& os, const OCRepresentation::AttributeItem& ai);
410 #endif //__OCREPRESENTATION_H