IoTivity C++ SDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OCRepresentation.h
Go to the documentation of this file.
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 
28 #ifndef OC_REPRESENTATION_H_
29 #define OC_REPRESENTATION_H_
30 
31 #include <string>
32 #include <sstream>
33 #include <vector>
34 #include <map>
35 
36 #include <AttributeValue.h>
37 #include <StringConstants.h>
38 
39 #ifdef __ANDROID__
40 #include "OCAndroid.h"
41 #endif
42 
43 #include <OCException.h>
44 
45 namespace OC
46 {
47 
48  enum class InterfaceType
49  {
50  None,
51  LinkParent,
54  LinkChild,
55  BatchChild,
57  };
58 
60  {
61  public:
62  void setPayload(const OCPayload* rep);
63 
64  void setPayload(const OCRepPayload* rep);
65 
66  OCRepPayload* getPayload() const;
67 
68  const std::vector<OCRepresentation>& representations() const;
69 
70  void addRepresentation(const OCRepresentation& rep);
71 
72  const OCRepresentation& operator[](int index) const
73  {
74  return m_reps[index];
75  }
76 
77  const OCRepresentation& back() const
78  {
79  return m_reps.back();
80  }
81  private:
82  std::vector<OCRepresentation> m_reps;
83  };
85  {
86  public:
87  friend bool operator==(const OC::OCRepresentation&, const OC::OCRepresentation&);
88  // Note: Implementation of all constructors and destructors
89  // are all placed in the same location due to a crash that
90  // was observed in Android, where merely constructing/destructing
91  // an OCRepresentation object was enough to cause an invalid 'free'.
92  // It is believed that this is a result of incompatible compiler
93  // options between the gradle JNI and armeabi scons build, however
94  // this fix will work in the meantime.
95  OCRepresentation(): m_interfaceType(InterfaceType::None){}
96 
97  virtual ~OCRepresentation(){}
98 
99  void setDevAddr(const OCDevAddr&);
100 
101  const std::string getHost() const;
102 
103  OCRepPayload* getPayload() const;
104 
105  void addChild(const OCRepresentation&);
106 
107  void clearChildren();
108 
109  const std::vector<OCRepresentation>& getChildren() const;
110 
111  void setChildren(const std::vector<OCRepresentation>& children);
112 
113  void setUri(const char* uri);
114 
115  void setUri(const std::string& uri);
116 
117  std::string getUri() const;
118 
119  const std::vector<std::string>& getResourceTypes() const;
120 
121  const std::vector<std::string>& getDataModelVersions() const;
122 
123  void setResourceTypes(const std::vector<std::string>& resourceTypes);
124 
125  void addResourceType(const std::string& str);
126 
127  const std::vector<std::string>& getResourceInterfaces() const;
128 
129  void setResourceInterfaces(const std::vector<std::string>& resourceInterfaces);
130 
131  void addResourceInterface(const std::string& str);
132 
133  void addDataModelVersion(const std::string& str);
134 
135  bool emptyData() const;
136 
137  size_t numberOfAttributes() const;
138 
139  bool erase(const std::string& str);
140 
141  template <typename T>
142  void setValue(const std::string& str, const T& val)
143  {
144  m_values[str] = val;
145  }
146 
147  // using R-value(or universal ref depending) to move string and vector<uint8_t>
148  template <typename T>
149  void setValue(const std::string& str, T&& val)
150  {
151  m_values[str] = std::forward<T>(val);
152  }
153 
154  const std::map<std::string, AttributeValue>& getValues() const {
155  return m_values;
156  }
157 
166  template <typename T>
167  bool getValue(const std::string& str, T& val) const
168  {
169  auto x = m_values.find(str);
170 
171  if(x!= m_values.end())
172  {
173  try
174  {
175  val = boost::get<T>(x->second);
176  return true;
177  }
178  catch (boost::bad_get&)
179  {
180  val = T();
181  return false;
182  }
183  }
184  else
185  {
186  val = T();
187  return false;
188  }
189  }
190 
199  template <typename T>
200  T getValue(const std::string& str) const
201  {
202  T val = T();
203  auto x = m_values.find(str);
204  if(x != m_values.end())
205  {
206  try
207  {
208  val = boost::get<T>(x->second);
209  }
210  catch (boost::bad_get&)
211  {
212  return val;
213  }
214  }
215  return val;
216  }
217 
226  bool getAttributeValue(const std::string& str, AttributeValue& attrValue) const
227  {
228  auto x = m_values.find(str);
229 
230  if (x != m_values.end())
231  {
232  attrValue = x->second;
233  return true;
234  }
235  else
236  {
237  return false;
238  }
239  }
240 
241  std::string getValueToString(const std::string& key) const;
242  bool hasAttribute(const std::string& str) const;
243 
244  void setNULL(const std::string& str);
245 
246  bool isNULL(const std::string& str) const;
247 
248  private:
249  std::string m_host;
250 
251  // STL Container stuff
252  public:
253  class iterator;
254  class const_iterator;
255  // Shim class to allow iterating and indexing of the OCRepresentation
256  // object.
258  {
259  friend class OCRepresentation;
260  friend class iterator;
261  friend class const_iterator;
262  public:
263  const std::string& attrname() const;
264  AttributeType type() const;
265  AttributeType base_type() const;
266  size_t depth() const;
267  template<typename T>
268  T getValue() const
269  {
270  try
271  {
272  return boost::get<T>(m_values[m_attrName]);
273  }
274  catch (boost::bad_get&)
275  {
276  T val = T();
277  return val;
278  }
279  }
280 
281  std::string getValueToString() const;
282 
283  template<typename T>
285  {
286  m_values[m_attrName] = std::forward<T>(rhs);
287  return *this;
288  }
289 
290  AttributeItem& operator=(std::nullptr_t /*rhs*/)
291  {
292  NullType t;
293  m_values[m_attrName] = t;
294  return *this;
295  }
296 
297  // Enable-if required to prevent conversions to alternate types. This prevents
298  // ambigious conversions in the case where conversions can include a number of
299  // types, such as the string constructor.
300 #if (defined(_MSC_VER) ) || (defined(__GNUC__) && (__GNUC__ <= 5))
301  template<typename T, typename std::enable_if<
302  std::is_same<T, int>::value ||
303  std::is_same<T, double>::value ||
304  std::is_same<T, bool>::value ||
305  std::is_same<T, std::string>::value ||
306  std::is_same<T, OCRepresentation>::value ||
307  std::is_same<T, OCByteString>::value ||
308  std::is_same<T, std::vector<int>>::value ||
309  std::is_same<T, std::vector<std::vector<int>>>::value ||
310  std::is_same<T, std::vector<std::vector<std::vector<int>>>>::value ||
311  std::is_same<T, std::vector<double>>::value ||
312  std::is_same<T, std::vector<std::vector<double>>>::value ||
313  std::is_same<T, std::vector<std::vector<std::vector<double>>>>::value ||
314  std::is_same<T, std::vector<bool>>::value ||
315  std::is_same<T, std::vector<std::vector<bool>>>::value ||
316  std::is_same<T, std::vector<std::vector<std::vector<bool>>>>::value ||
317  std::is_same<T, std::vector<std::string>>::value ||
318  std::is_same<T, std::vector<std::vector<std::string>>>::value ||
319  std::is_same<T, std::vector<std::vector<std::vector<std::string>>>>::value ||
320  std::is_same<T, std::vector<OCRepresentation>>::value ||
321  std::is_same<T, std::vector<std::vector<OCRepresentation>>>::value ||
322  std::is_same<T, std::vector<std::vector<std::vector<OCRepresentation>>>>::value ||
323  std::is_same<T, std::vector<OCByteString>>::value ||
324  std::is_same<T, std::vector<std::vector<OCByteString>>>::value ||
325  std::is_same<T, std::vector<std::vector<std::vector<OCByteString>>>>::value
326  , int>::type = 0// enable_if
327  >
328 #else
329  template<typename T, typename std::enable_if<
330  is_component<T,
331  remove_first<AttributeValue>::type
332  >::value
333  , int>::type = 0
334  >
335 #endif
336  operator T() const
337  {
338  return this->getValue<T>();
339  }
340 
341  template<typename T, typename std::enable_if<
342  std::is_same<T, std::nullptr_t>::value
343  , int>::type = 0
344  >
345  operator T() const
346  {
347  this->getValue<NullType>();
348  return nullptr;
349  }
350 
351  private:
352  AttributeItem(const std::string& name,
353  std::map<std::string, AttributeValue>& vals);
354  AttributeItem(const AttributeItem&) = default;
355  std::string m_attrName;
356  std::map<std::string, AttributeValue>& m_values;
357  };
358 
359  // Iterator to allow iteration via STL containers/methods
360  class iterator
361  {
362  friend class OCRepresentation;
363  public:
367  typedef value_type* pointer;
368  typedef std::forward_iterator_tag iterator_category;
369  typedef int difference_type;
370 
371  iterator(const iterator&) = default;
372  ~iterator() = default;
373 
374  bool operator ==(const iterator&) const;
375  bool operator !=(const iterator&) const;
376 
377  iterator& operator++();
378  iterator operator++(int);
379 
382  private:
383  iterator(std::map<std::string, AttributeValue>::iterator&& itr,
384  std::map<std::string, AttributeValue>& vals)
385  : m_iterator(std::move(itr)),
386  m_item(m_iterator != vals.end() ? m_iterator->first:"", vals){}
387  std::map<std::string, AttributeValue>::iterator m_iterator;
388  AttributeItem m_item;
389  };
390 
392  {
393  friend class OCRepresentation;
394  public:
396  typedef const AttributeItem value_type;
399  typedef std::forward_iterator_tag iterator_category;
400  typedef int difference_type;
401 
403  :m_iterator(rhs.m_iterator), m_item(rhs.m_item){}
404  const_iterator(const const_iterator&) = default;
405  ~const_iterator() = default;
406 
407  bool operator ==(const const_iterator&) const;
408  bool operator !=(const const_iterator&) const;
409 
412 
413  const_reference operator*() const;
414  const_pointer operator->() const;
415  private:
416  const_iterator(std::map<std::string, AttributeValue>::const_iterator&& itr,
417  std::map<std::string, AttributeValue>& vals)
418  : m_iterator(std::move(itr)),
419  m_item(m_iterator != vals.end() ? m_iterator->first: "", vals){}
420  std::map<std::string, AttributeValue>::const_iterator m_iterator;
421  AttributeItem m_item;
422  };
423 
424  iterator begin();
425  const_iterator begin() const;
426  const_iterator cbegin() const;
427  iterator end();
428  const_iterator end() const;
429  const_iterator cend() const;
430  size_t size() const;
431  bool empty() const;
432 
433  AttributeItem operator[](const std::string& key);
434  const AttributeItem operator[](const std::string& key) const;
435  private:
436  friend class OCResourceResponse;
437  friend class MessageContainer;
438 
439  template<typename T>
440  void payload_array_helper(const OCRepPayloadValue* pl, size_t depth);
441  template<typename T>
442  T payload_array_helper_copy(size_t index, const OCRepPayloadValue* pl);
443  void setPayload(const OCRepPayload* payload);
444  void setPayloadArray(const OCRepPayloadValue* pl);
445  void getPayloadArray(OCRepPayload* payload,
446  const OCRepresentation::AttributeItem& item) const;
447  // the root node has a slightly different JSON version
448  // based on the interface type configured in ResourceResponse.
449  // This allows ResourceResponse to set it, so that the save function
450  // doesn't serialize things that it isn't supposed to serialize.
451  void setInterfaceType(InterfaceType ift)
452  {
453  m_interfaceType = ift;
454  }
455 
456  // class used to wrap the 'prop' feature of the save/load
457  class Prop
458  {
459  public:
460  Prop(std::vector<std::string>& resourceTypes,
461  std::vector<std::string>& interfaces)
462  : m_types(resourceTypes), m_interfaces(interfaces)
463  {}
464 
465  /* Prop(const std::vector<std::string>& resourceTypes,
466  const std::vector<std::string>& interfaces)
467  :m_types(resourceTypes),
468  m_interfaces(interfaces)
469  {}*/
470  private:
471  std::vector<std::string>& m_types;
472  std::vector<std::string>& m_interfaces;
473  };
474  private:
475  std::string m_uri;
476  std::vector<OCRepresentation> m_children;
477  mutable std::map<std::string, AttributeValue> m_values;
478  std::vector<std::string> m_resourceTypes;
479  std::vector<std::string> m_interfaces;
480  std::vector<std::string> m_dataModelVersions;
481 
482  InterfaceType m_interfaceType;
483  };
484 
485  std::ostream& operator <<(std::ostream& os, const OCRepresentation::AttributeItem& ai);
486 } // namespace OC
487 
488 
489 #endif // OC_REPRESENTATION_H_
T getValue(const std::string &str) const
Return the attribute value associated with the supplied name.
Definition: OCRepresentation.h:200
AttributeType base_type() const
void setValue(const std::string &str, T &&val)
Definition: OCRepresentation.h:149
void addResourceType(const std::string &str)
int difference_type
Definition: OCRepresentation.h:400
void addResourceInterface(const std::string &str)
const_iterator cbegin() const
value_type * const_pointer
Definition: OCRepresentation.h:398
void setResourceTypes(const std::vector< std::string > &resourceTypes)
const std::vector< OCRepresentation > & getChildren() const
void setUri(const char *uri)
std::string getUri() const
T getValue() const
Definition: OCRepresentation.h:268
void setChildren(const std::vector< OCRepresentation > &children)
AttributeItem operator[](const std::string &key)
OCRepPayload * getPayload() const
InterfaceType
Definition: OCRepresentation.h:48
iterator self_type
Definition: OCRepresentation.h:364
AttributeItem value_type
Definition: OCRepresentation.h:365
Definition: OCRepresentation.h:59
Definition: octypes.h:1501
const_iterator(const iterator &rhs)
Definition: OCRepresentation.h:402
Definition: octypes.h:1523
void setValue(const std::string &str, const T &val)
Definition: OCRepresentation.h:142
AttributeItem & operator=(std::nullptr_t)
Definition: OCRepresentation.h:290
bool operator!=(const iterator &) const
void setNULL(const std::string &str)
size_t numberOfAttributes() const
int difference_type
Definition: OCRepresentation.h:369
const std::string getHost() const
friend bool operator==(const OC::OCRepresentation &, const OC::OCRepresentation &)
const_iterator cend() const
void addDataModelVersion(const std::string &str)
bool isNULL(const std::string &str) const
void setDevAddr(const OCDevAddr &)
Definition: OCRepresentation.h:391
std::string getValueToString(const std::string &key) const
const std::map< std::string, AttributeValue > & getValues() const
Definition: OCRepresentation.h:154
OCRepPayload * getPayload() const
const std::vector< std::string > & getResourceInterfaces() const
const_pointer operator->() const
AttributeItem & operator=(T &&rhs)
Definition: OCRepresentation.h:284
void addChild(const OCRepresentation &)
A generic struct representing a payload returned from a resource operation.
Definition: octypes.h:1453
bool getValue(const std::string &str, T &val) const
Retrieve the attribute value associated with the supplied name.
Definition: OCRepresentation.h:167
bool empty() const
std::ostream & operator<<(std::ostream &os, const OCRepresentation::AttributeItem &ai)
const std::vector< std::string > & getResourceTypes() const
Definition: OCRepresentation.h:257
OCRepresentation()
Definition: OCRepresentation.h:95
value_type * pointer
Definition: OCRepresentation.h:367
void setResourceInterfaces(const std::vector< std::string > &resourceInterfaces)
std::forward_iterator_tag iterator_category
Definition: OCRepresentation.h:368
bool operator==(const iterator &) const
std::forward_iterator_tag iterator_category
Definition: OCRepresentation.h:399
bool erase(const std::string &str)
value_type & const_reference
Definition: OCRepresentation.h:397
const_reference operator*() const
const std::vector< OCRepresentation > & representations() const
bool emptyData() const
Definition: OCRepresentation.h:360
void addRepresentation(const OCRepresentation &rep)
const OCRepresentation & back() const
Definition: OCRepresentation.h:77
std::string getValueToString() const
Data structure to encapsulate IPv4/IPv6/Contiki/lwIP device addresses.
Definition: octypes.h:796
void setPayload(const OCPayload *rep)
virtual ~OCRepresentation()
Definition: OCRepresentation.h:97
iterator(const iterator &)=default
bool hasAttribute(const std::string &str) const
bool operator!=(const const_iterator &) const
const OCRepresentation & operator[](int index) const
Definition: OCRepresentation.h:72
value_type & reference
Definition: OCRepresentation.h:366
iterator self_type
Definition: OCRepresentation.h:395
const std::string & attrname() const
size_t size() const
Definition: OCRepresentation.h:84
const AttributeItem value_type
Definition: OCRepresentation.h:396
bool operator==(const const_iterator &) const
const std::vector< std::string > & getDataModelVersions() const
OCResourceResponse provides APIs to set the response details.
Definition: OCResourceResponse.h:43
bool getAttributeValue(const std::string &str, AttributeValue &attrValue) const
Retrieve the attributevalue structure associated with the supplied name.
Definition: OCRepresentation.h:226