IoTivity C++ SDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RCSResourceAttributes.h
Go to the documentation of this file.
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics 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 
26 #ifndef RES_ENCAPSULATION_RESOURCEATTRIBUTES_H
27 #define RES_ENCAPSULATION_RESOURCEATTRIBUTES_H
28 
29 // To avoid conflict using different boost::variant configuration with OC.
30 // It causes compile errors.
31 #define BOOST_MPL_CFG_NO_PREPROCESSED_HEADERS
32 #define BOOST_MPL_LIMIT_LIST_SIZE 30
33 #define BOOST_MPL_LIMIT_VECTOR_SIZE 30
34 
35 #include <functional>
36 #include <unordered_map>
37 #include <vector>
38 
39 #include "boost/variant.hpp"
40 #include "boost/mpl/contains.hpp"
41 #include "boost/mpl/find.hpp"
42 #include "boost/mpl/distance.hpp"
43 #include "boost/mpl/begin_end.hpp"
44 #include "boost/scoped_ptr.hpp"
45 
46 #include "RCSException.h"
47 
48 namespace OIC
49 {
50  namespace Service
51  {
65  {
66  public:
67  typedef std::vector<uint8_t> DataType;
68 
75  {
76  return {m_data};
77  }
78 
84  size_t size() const
85  {
86  return m_data.size();
87  }
88 
96  inline bool operator==(const RCSByteString& rhs) const
97  {
98  return this->m_data == rhs.getByteString();
99  }
100 
108  inline bool operator!=(const RCSByteString& rhs) const
109  {
110  return this->m_data != rhs.getByteString();
111  }
112 
120  inline uint8_t operator[](size_t it) const
121  {
122  return this->m_data[it];
123  }
124 
126  {
127  }
129  : m_data {std::move(rhs)}
130  {
131  }
132  RCSByteString(const DataType & rhs)
133  : m_data {rhs}
134  {
135  }
137  : m_data {DataType{rhs.getByteString()}}
138  {
139  }
141  : m_data {DataType{rhs.getByteString()}}
142  {
143  }
144 
146  : m_data {DataType{rhs.bytes, rhs.bytes + rhs.len}}
147  {
148  }
149  RCSByteString(const ::OCByteString & rhs)
150  : m_data {DataType{rhs.bytes, rhs.bytes + rhs.len}}
151  {
152  }
153 
154  RCSByteString(uint8_t* bytes, size_t size)
155  : m_data {DataType{bytes, bytes + size}}
156  {
157  }
159  {
160  return operator =(rhs);
161  }
163  {
164  if(&rhs != this)
165  {
166  if (!m_data.empty())
167  {
168  m_data.clear();
169  }
170  m_data = DataType{rhs.getByteString()};
171  }
172  return *this;
173  }
174  private:
175  DataType m_data;
176  };
177 
196  {
197  private:
198  template< typename T > struct IsSupportedTypeHelper;
199 
200  typedef boost::variant<
201  std::nullptr_t,
202  int,
203  double,
204  bool,
205  std::string,
208 
209  std::vector< int >,
210  std::vector< double >,
211  std::vector< bool >,
212  std::vector< std::string >,
213  std::vector< RCSByteString >,
214  std::vector< RCSResourceAttributes >,
215 
216  std::vector< std::vector< int > >,
217  std::vector< std::vector< std::vector< int > > >,
218 
219  std::vector< std::vector< double > >,
220  std::vector< std::vector< std::vector< double > > >,
221 
222  std::vector< std::vector< bool > >,
223  std::vector< std::vector< std::vector< bool > > >,
224 
225  std::vector< std::vector< std::string > >,
226  std::vector< std::vector< std::vector< std::string > > >,
227 
228  std::vector< std::vector< RCSByteString > >,
229  std::vector< std::vector< std::vector< RCSByteString > > >,
230 
231  std::vector< std::vector< RCSResourceAttributes > >,
232  std::vector< std::vector< std::vector< RCSResourceAttributes > > >
233  > ValueVariant;
234 
235  template< typename T, typename V = void,
236  typename = typename std::enable_if<
237  IsSupportedTypeHelper< T >::type::value, V >::type >
238  struct enable_if_supported
239  {
240  typedef V type;
241  };
242 
243  template< typename VISITOR, typename MOVE = std::false_type >
244  class KeyValueVisitorHelper: public boost::static_visitor< >
245  {
246  public:
247  KeyValueVisitorHelper(VISITOR& visitor) BOOST_NOEXCEPT :
248  m_visitor( visitor )
249  {
250  }
251 
252  template< typename T, typename M = MOVE >
253  typename std::enable_if< std::is_same< M, std::false_type >::value >::type
254  operator()(const std::string& key, const T& value) const
255  {
256  m_visitor(key, value);
257  }
258 
259  template< typename T, typename M = MOVE >
260  typename std::enable_if< std::is_same< M, std::true_type >::value >::type
261  operator()(const std::string& key, T& value)
262  {
263  m_visitor(key, std::move(value));
264  }
265 
266  private:
267  VISITOR& m_visitor;
268  };
269 
270  template <typename T> struct IndexOfType;
271 
272  public:
273 
277  template< typename T >
278  struct is_supported_type: public std::conditional<
279  IsSupportedTypeHelper< T >::type::value, std::true_type, std::false_type>::type { };
280 
286  enum class TypeId
287  {
288  NULL_T,
289  INT,
290  DOUBLE,
291  BOOL,
292  STRING,
293  BYTESTRING,
294  ATTRIBUTES,
295  VECTOR
296  };
297 
305  class Type
306  {
307  public:
308  Type(const Type&) = default;
309  Type(Type&&) = default;
310 
311  Type& operator=(const Type&) = default;
312  Type& operator=(Type&&) = default;
313 
321  TypeId getId() const BOOST_NOEXCEPT;
322 
333  static TypeId getBaseTypeId(const Type& t) BOOST_NOEXCEPT;
334 
342  static size_t getDepth(const Type& t) BOOST_NOEXCEPT;
343 
353  template < typename T >
354  constexpr static Type typeOf(const T&) BOOST_NOEXCEPT
355  {
356  return Type{ IndexOfType< T >::value };
357  }
358 
368  template < typename T >
369  constexpr static Type typeOf() BOOST_NOEXCEPT
370  {
371  return Type{ IndexOfType< T >::value };
372  }
373 
375  friend bool operator==(const Type&, const Type&) BOOST_NOEXCEPT;
377 
378  private:
379  constexpr explicit Type(int which) BOOST_NOEXCEPT :
380  m_which{ which }
381  {
382  }
383 
384  private:
385  int m_which;
386  };
387 
432  class Value
433  {
434  public:
435  class ComparisonHelper;
436 
437  Value();
438  Value(const Value&);
439  Value(Value&&) BOOST_NOEXCEPT;
440 
445  template< typename T, typename = typename enable_if_supported< T >::type >
446  Value(T&& value) :
447  m_data{ new ValueVariant{ std::forward< T >(value) } }
448  {
449  }
450 
451  Value(const char*);
452 
453  Value& operator=(const Value&);
454  Value& operator=(Value&&);
455 
456  template< typename T, typename = typename enable_if_supported< T >::type >
457  Value& operator=(T&& rhs)
458  {
459  *m_data = std::forward< T >(rhs);
460  return *this;
461  }
462 
463  Value& operator=(const char*);
464  Value& operator=(std::nullptr_t);
465 
473  template< typename T >
474  typename std::add_lvalue_reference< const T >::type get() const
475  {
476  return checkedGet< T >();
477  }
478 
486  template< typename T >
487  typename std::add_lvalue_reference< T >::type get()
488  {
489  return checkedGet< T >();
490  }
491 
497  Type getType() const;
498 
503  std::string toString() const;
504 
508  void swap(Value&) BOOST_NOEXCEPT;
509 
511  friend class RCSResourceAttributes;
513 
514  private:
515  template< typename T, typename = typename enable_if_supported< T >::type >
516  typename std::add_lvalue_reference< T >::type checkedGet() const
517  {
518  try
519  {
520  return boost::get< T >(*m_data);
521  }
522  catch (const boost::bad_get&)
523  {
524  throw RCSBadGetException{ "Wrong type" };
525  }
526  }
527 
528  template< typename T, typename U >
529  bool equals(const U& rhs) const
530  {
531  try
532  {
533  return get< T >() == rhs;
534  }
535  catch (const RCSBadGetException&)
536  {
537  return false;
538  }
539  }
540 
541  private:
542  boost::scoped_ptr< ValueVariant > m_data;
543  };
544 
545  class KeyValuePair;
546  class iterator;
547  class const_iterator;
548 
549  public:
550  RCSResourceAttributes() = default;
551  RCSResourceAttributes(const RCSResourceAttributes&) = default;
552  RCSResourceAttributes(RCSResourceAttributes&&) = default;
553 
554  RCSResourceAttributes& operator=(const RCSResourceAttributes&) = default;
555  RCSResourceAttributes& operator=(RCSResourceAttributes&&) = default;
556 
560  iterator begin() BOOST_NOEXCEPT;
561 
565  iterator end() BOOST_NOEXCEPT;
566 
570  const_iterator begin() const BOOST_NOEXCEPT;
571 
575  const_iterator end() const BOOST_NOEXCEPT;
576 
580  const_iterator cbegin() const BOOST_NOEXCEPT;
581 
585  const_iterator cend() const BOOST_NOEXCEPT;
586 
602  Value& operator[](const std::string& key);
603 
620  Value& operator[](std::string&& key);
621 
637  Value& at(const std::string& key);
638 
654  const Value& at(const std::string& key) const;
655 
659  void clear() BOOST_NOEXCEPT;
660 
668  bool erase(const std::string& key);
669 
677  iterator erase(const_iterator pos);
678 
686  bool contains(const std::string& key) const;
687 
693  bool empty() const BOOST_NOEXCEPT;
694 
700  size_t size() const BOOST_NOEXCEPT;
701 
702  private:
703  template< typename VISITOR >
704  void visit(VISITOR& visitor) const
705  {
706  KeyValueVisitorHelper< VISITOR > helper{ visitor };
707 
708  for (const auto& i : m_values)
709  {
710  boost::variant< const std::string& > key{ i.first };
711  boost::apply_visitor(helper, key, *i.second.m_data);
712  }
713  }
714 
715  template< typename VISITOR >
716  void visitToMove(VISITOR& visitor)
717  {
718  KeyValueVisitorHelper< VISITOR, std::true_type > helper{ visitor };
719 
720  for (auto& i : m_values)
721  {
722  boost::variant< const std::string& > key{ i.first };
723  boost::apply_visitor(helper, key, *i.second.m_data);
724  }
725  }
726 
727  private:
728  std::unordered_map< std::string, Value > m_values;
729 
731  friend class ResourceAttributesConverter;
732 
733  friend bool operator==(const RCSResourceAttributes&, const RCSResourceAttributes&);
735  };
736 
747  {
748  public:
749  ComparisonHelper(const Value&);
750 
751  ComparisonHelper(const ComparisonHelper&) = delete;
752  ComparisonHelper& operator=(const ComparisonHelper&) = delete;
753 
754  template< typename T >
755  typename std::enable_if< is_supported_type< T >::value, bool >::type equals(
756  const T& v) const
757  {
758  return m_valueRef.equals< T >(v);
759  }
760 
761  bool equals(const std::string& v) const
762  {
763  return m_valueRef.equals< std::string >(v);
764  }
765 
766  bool operator==(const ComparisonHelper&) const;
767 
768  private:
769  const Value& m_valueRef;
770  };
771 
773  template< typename T >
774  struct RCSResourceAttributes::IsSupportedTypeHelper
775  {
776  typedef boost::mpl::contains< ValueVariant::types, typename std::decay< T >::type > type;
777  };
778 
779  template < typename T >
780  struct RCSResourceAttributes::IndexOfType
781  {
783  "The type is not supported!");
784 
785  typedef typename boost::mpl::find< ValueVariant::types, T >::type iter;
786  typedef typename boost::mpl::begin< ValueVariant::types >::type mpl_begin;
787 
788  static constexpr int value = boost::mpl::distance< mpl_begin, iter >::value;
789  };
790 
791  template < typename T > constexpr int RCSResourceAttributes::IndexOfType< T >::value;
793 
801  bool operator==(const RCSResourceAttributes::Type&, const RCSResourceAttributes::Type&)
802  BOOST_NOEXCEPT;
803 
811  bool operator!=(const RCSResourceAttributes::Type&, const RCSResourceAttributes::Type&)
812  BOOST_NOEXCEPT;
813 
822  bool operator==(const RCSResourceAttributes::Value::ComparisonHelper&,
823  const RCSResourceAttributes::Value::ComparisonHelper&);
824 
833  bool operator!=(const RCSResourceAttributes::Value::ComparisonHelper&,
834  const RCSResourceAttributes::Value::ComparisonHelper&);
835 
837  template< typename T >
838  typename std::enable_if< RCSResourceAttributes::is_supported_type< T >::value ||
839  std::is_constructible< std::string, T >::value, bool >::type
840  operator==(const RCSResourceAttributes::Value::ComparisonHelper& lhs, const T& rhs)
841  {
842  return lhs.equals(rhs);
843  }
844 
845  template< typename T >
846  typename std::enable_if< RCSResourceAttributes::is_supported_type< T >::value ||
847  std::is_constructible< std::string, T >::value, bool >::type
848  operator==(const T& lhs, const RCSResourceAttributes::Value::ComparisonHelper& rhs)
849  {
850  return rhs == lhs;
851  }
852 
853  template< typename T >
854  typename std::enable_if< RCSResourceAttributes::is_supported_type< T >::value ||
855  std::is_constructible< std::string, T >::value, bool >::type
856  operator!=(const RCSResourceAttributes::Value::ComparisonHelper& lhs, const T& rhs)
857  {
858  return !(lhs == rhs);
859  }
860 
861  template< typename T >
862  typename std::enable_if< RCSResourceAttributes::is_supported_type< T >::value ||
863  std::is_constructible< std::string, T >::value, bool >::type
864  operator!=(const T& lhs, const RCSResourceAttributes::Value::ComparisonHelper& rhs)
865  {
866  return !(rhs == lhs);
867  }
869 
877  bool operator==(const RCSResourceAttributes& lhs, const RCSResourceAttributes& rhs);
878 
886  bool operator!=(const RCSResourceAttributes&, const RCSResourceAttributes&);
887 
898  {
899  private:
900  class KeyVisitor: public boost::static_visitor< const std::string& >
901  {
902  public:
903  result_type operator()(iterator*) const BOOST_NOEXCEPT;
904  result_type operator()(const_iterator*) const BOOST_NOEXCEPT;
905  };
906 
907  class ValueVisitor: public boost::static_visitor< Value& >
908  {
909  public:
910  result_type operator()(iterator*) BOOST_NOEXCEPT;
911  result_type operator()(const_iterator*);
912  };
913 
914  class ConstValueVisitor: public boost::static_visitor< const Value& >
915  {
916  public:
917  result_type operator()(iterator*) const BOOST_NOEXCEPT;
918  result_type operator()(const_iterator*) const BOOST_NOEXCEPT;
919  };
920 
921  public:
922  const std::string& key() const BOOST_NOEXCEPT;
923  const RCSResourceAttributes::Value& value() const BOOST_NOEXCEPT;
925 
926  private:
927  KeyValuePair(const KeyValuePair&) = default;
928  KeyValuePair(boost::variant< iterator*, const_iterator* >&&) BOOST_NOEXCEPT;
929 
930  KeyValuePair& operator=(const KeyValuePair&) = default;
931 
932  private:
933  boost::variant< iterator*, const_iterator* > m_iterRef;
934 
935  KeyVisitor m_keyVisitor;
936  ValueVisitor m_valueVisitor;
937  ConstValueVisitor m_constValueVisitor;
938 
940  friend class iterator;
941  friend class const_iterator;
943  };
944 
953  public std::iterator< std::forward_iterator_tag, RCSResourceAttributes::KeyValuePair >
954  {
955  private:
956  typedef std::unordered_map< std::string, Value >::iterator base_iterator;
957 
958  public:
959  iterator();
960  iterator(const iterator&);
961 
962  iterator& operator=(const iterator&);
963 
964  reference operator*();
965  pointer operator->();
966 
967  iterator& operator++();
968  iterator operator++(int);
969 
970  bool operator==(const iterator&) const;
971  bool operator!=(const iterator&) const;
972 
973  private:
974  explicit iterator(base_iterator&&);
975 
976  private:
977  base_iterator m_cur;
979 
981  friend class RCSResourceAttributes;
983  };
984 
985 
994  public std::iterator< std::forward_iterator_tag,
995  const RCSResourceAttributes::KeyValuePair >
996  {
997  private:
998  typedef std::unordered_map< std::string, Value >::const_iterator base_iterator;
999 
1000  public:
1001  const_iterator();
1004 
1007 
1008  reference operator*() const;
1009  pointer operator->() const;
1010 
1011  const_iterator& operator++();
1012  const_iterator operator++(int);
1013 
1014  bool operator==(const const_iterator&) const;
1015  bool operator!=(const const_iterator&) const;
1016 
1017  private:
1018  explicit const_iterator(base_iterator&&);
1019 
1020  private:
1021  base_iterator m_cur;
1022  RCSResourceAttributes::KeyValuePair m_keyValuePair;
1023 
1025  friend class RCSResourceAttributes;
1027  };
1028 
1029  }
1030 }
1031 
1032 #endif // RES_ENCAPSULATION_RESOURCEATTRIBUTES_H
static size_t getDepth(const Type &t) BOOST_NOEXCEPT
Returns the depth of a type.
RCSByteString & operator=(const RCSByteString &rhs)
Definition: RCSResourceAttributes.h:162
RCSByteString(const ::OCByteString &rhs)
Definition: RCSResourceAttributes.h:149
A forward iterator to KeyValuePair.
Definition: RCSResourceAttributes.h:952
bool empty() const BOOST_NOEXCEPT
Returns whether it is empty.
RCSByteString()
Definition: RCSResourceAttributes.h:125
TypeId getId() const BOOST_NOEXCEPT
Returns type identifier.
Trait class that identifies whether T is supported by the Value.
Definition: RCSResourceAttributes.h:278
Type getType() const
Returns Type information.
RCSByteString(const DataType &rhs)
Definition: RCSResourceAttributes.h:132
size_t size() const
Returns a size of stored vector<uint8_t>.
Definition: RCSResourceAttributes.h:84
bool operator!=(const RCSResourceAttributes::Value::ComparisonHelper &, const RCSResourceAttributes::Value::ComparisonHelper &)
bool equals(const std::string &v) const
Definition: RCSResourceAttributes.h:761
This RCSByteString the one of RCSResourceAttributes value for Byte String (Binary).
Definition: RCSResourceAttributes.h:64
KeyValuePair is a class to access attribute's key and value of an element pointed by iterators of RCS...
Definition: RCSResourceAttributes.h:897
static TypeId getBaseTypeId(const Type &t) BOOST_NOEXCEPT
Returns the type identifier of a base type of sequence.
RCSByteString(const RCSByteString &rhs)
Definition: RCSResourceAttributes.h:140
Thrown when getting value with wrong template parameter.
Definition: RCSException.h:139
TypeId
Identifiers for types of Value.
Definition: RCSResourceAttributes.h:286
iterator end() BOOST_NOEXCEPT
Returns an iterator referring to the past-the-end element.
bool operator==(const RCSResourceAttributes::Type &, const RCSResourceAttributes::Type &) BOOST_NOEXCEPT
Value(T &&value)
Constructs a Value if T is a supported type.
Definition: RCSResourceAttributes.h:446
uint8_t operator[](size_t it) const
Return a value of indexed byte string.
Definition: RCSResourceAttributes.h:120
static constexpr Type typeOf() BOOST_NOEXCEPT
Factory method to create Type instance from T.
Definition: RCSResourceAttributes.h:369
Value & operator=(T &&rhs)
Definition: RCSResourceAttributes.h:457
iterator begin() BOOST_NOEXCEPT
Returns an iterator referring to the first element.
Value holds a value among various types at a time.
Definition: RCSResourceAttributes.h:432
size_t size() const BOOST_NOEXCEPT
Returns the number of elements.
Type & operator=(const Type &)=default
A helper class to avoid obscure comparisons of values which are supported by RCSResourceAttributes::V...
Definition: RCSResourceAttributes.h:746
This file contains the declaration of exception classes for resource-encapsulation.
A forward iterator to const KeyValuePair.
Definition: RCSResourceAttributes.h:993
RCSByteString(::OCByteString &&rhs)
Definition: RCSResourceAttributes.h:145
RCSByteString(uint8_t *bytes, size_t size)
Definition: RCSResourceAttributes.h:154
This represents the attributes for a resource.
Definition: RCSResourceAttributes.h:195
RCSByteString(RCSByteString &&rhs)
Definition: RCSResourceAttributes.h:136
RCSByteString & operator=(RCSByteString &&rhs)
Definition: RCSResourceAttributes.h:158
bool contains(const std::string &key) const
Checks this contains an element for the specified key.
DataType getByteString() const
Returns a vector<uint8_t> type of byte string.
Definition: RCSResourceAttributes.h:74
std::vector< uint8_t > DataType
Definition: RCSResourceAttributes.h:67
void clear() BOOST_NOEXCEPT
Removes all elements.
bool erase(const std::string &key)
Removes a single element.
This structure will be used to represent a binary string for CBOR payloads.
Definition: octypes.h:1472
bool operator==(const RCSByteString &rhs) const
Definition: RCSResourceAttributes.h:96
RCSByteString(DataType &&rhs)
Definition: RCSResourceAttributes.h:128
bool operator!=(const RCSByteString &rhs) const
Definition: RCSResourceAttributes.h:108
std::string toString() const
Returns a string representation.
static constexpr Type typeOf(const T &) BOOST_NOEXCEPT
Factory method to create Type instance from T.
Definition: RCSResourceAttributes.h:354
std::enable_if< is_supported_type< T >::value, bool >::type equals(const T &v) const
Definition: RCSResourceAttributes.h:755
Value & at(const std::string &key)
Accesses a value.
const_iterator cbegin() const BOOST_NOEXCEPT
Returns a const_iterator referring to the first element.
const_iterator cend() const BOOST_NOEXCEPT
Returns a const_iterator referring to the past-the-end element.
void swap(Value &) BOOST_NOEXCEPT
Exchanges the content of the object by the content of the parameter.
A Helper class to identify types of Value.
Definition: RCSResourceAttributes.h:305
bool operator==(const RCSResourceAttributes::Value::ComparisonHelper &, const RCSResourceAttributes::Value::ComparisonHelper &)