1 //******************************************************************
3 // Copyright 2015 Samsung Electronics 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
24 * This file contains the declaration of classes and its members related to RCSResourceAttributes
26 #ifndef RES_ENCAPSULATION_RESOURCEATTRIBUTES_H
27 #define RES_ENCAPSULATION_RESOURCEATTRIBUTES_H
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
36 #include <unordered_map>
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"
46 #include "RCSException.h"
53 * This RCSByteString the one of RCSResourceAttributes value for Byte String (Binary).
55 * It provides similar usage to c++ standard vector.<br/>
56 * An RCSByteString can be one of various attribute value type.
60 * @see RCSRemoteResourceObject
61 * @see RCSResourceObject
62 * @see RCSResourceAttributes
67 typedef std::vector<uint8_t> DataType;
70 * Returns a vector<uint8_t> type of byte string.
72 * @return A stored byte string with std::vector<uint8_t>
74 DataType getByteString() const
80 * Returns a size of stored vector<uint8_t>.
82 * @return A size of stored byte string.
90 * @relates RCSByteString
92 * Checks if the byte string is same contents, or not.
94 * @return true if the byte string are equal, false otherwise.
96 inline bool operator==(const RCSByteString& rhs) const
98 return this->m_data == rhs.getByteString();
102 * @relates RCSByteString
104 * Checks if the byte string is not same contents, or is same.
106 * @return true if the byte string are not equal, false otherwise.
108 inline bool operator!=(const RCSByteString& rhs) const
110 return this->m_data != rhs.getByteString();
114 * Return a value of indexed byte string.
116 * @param it location of the element.
118 * @return A copied value of indexed byte string.
120 inline uint8_t operator[](size_t it) const
122 return this->m_data[it];
128 RCSByteString(DataType && rhs)
129 : m_data {std::move(rhs)}
132 RCSByteString(const DataType & rhs)
136 RCSByteString(RCSByteString && rhs)
137 : m_data {DataType{rhs.getByteString()}}
140 RCSByteString(const RCSByteString & rhs)
141 : m_data {DataType{rhs.getByteString()}}
145 RCSByteString(::OCByteString && rhs)
146 : m_data {DataType{rhs.bytes, rhs.bytes + rhs.len}}
149 RCSByteString(const ::OCByteString & rhs)
150 : m_data {DataType{rhs.bytes, rhs.bytes + rhs.len}}
154 RCSByteString(uint8_t* bytes, size_t size)
155 : m_data {DataType{bytes, bytes + size}}
158 inline RCSByteString& operator=(RCSByteString&& rhs)
160 return operator =(rhs);
162 inline RCSByteString& operator=(const RCSByteString& rhs)
168 m_data = DataType{rhs.getByteString()};
176 class RCSResourceAttributes;
177 typedef boost::variant<
184 RCSResourceAttributes,
187 std::vector< double >,
189 std::vector< std::string >,
190 std::vector< RCSByteString >,
191 std::vector< RCSResourceAttributes >,
193 std::vector< std::vector< int > >,
194 std::vector< std::vector< std::vector< int > > >,
196 std::vector< std::vector< double > >,
197 std::vector< std::vector< std::vector< double > > >,
199 std::vector< std::vector< bool > >,
200 std::vector< std::vector< std::vector< bool > > >,
202 std::vector< std::vector< std::string > >,
203 std::vector< std::vector< std::vector< std::string > > >,
205 std::vector< std::vector< RCSByteString > >,
206 std::vector< std::vector< std::vector< RCSByteString > > >,
208 std::vector< std::vector< RCSResourceAttributes > >,
209 std::vector< std::vector< std::vector< RCSResourceAttributes > > >
214 * This represents the attributes for a resource.
216 * It provides similar usage to c++ standard containers. (iterator,
217 * operators and accessors)<br/>
218 * An attribute value can be one of various types. <br/>
224 * @see const_iterator
225 * @see RCSDiscoveryManager
226 * @see RCSRemoteResourceObject
227 * @see RCSResourceObject
230 class RCSResourceAttributes
233 template< typename T > struct IsSupportedTypeHelper;
235 typedef boost::variant<
242 RCSResourceAttributes,
245 std::vector< double >,
247 std::vector< std::string >,
248 std::vector< RCSByteString >,
249 std::vector< RCSResourceAttributes >,
251 std::vector< std::vector< int > >,
252 std::vector< std::vector< std::vector< int > > >,
254 std::vector< std::vector< double > >,
255 std::vector< std::vector< std::vector< double > > >,
257 std::vector< std::vector< bool > >,
258 std::vector< std::vector< std::vector< bool > > >,
260 std::vector< std::vector< std::string > >,
261 std::vector< std::vector< std::vector< std::string > > >,
263 std::vector< std::vector< RCSByteString > >,
264 std::vector< std::vector< std::vector< RCSByteString > > >,
266 std::vector< std::vector< RCSResourceAttributes > >,
267 std::vector< std::vector< std::vector< RCSResourceAttributes > > >
270 template< typename T, typename V = void,
271 typename = typename std::enable_if<
272 IsSupportedTypeHelper< T >::type::value, V >::type >
273 struct enable_if_supported
278 template< typename VISITOR, typename MOVE = std::false_type >
279 class KeyValueVisitorHelper: public boost::static_visitor< >
282 KeyValueVisitorHelper(VISITOR& visitor) BOOST_NOEXCEPT :
287 template< typename T, typename M = MOVE >
288 typename std::enable_if< std::is_same< M, std::false_type >::value >::type
289 operator()(const std::string& key, const T& value) const
291 m_visitor(key, value);
294 template< typename T, typename M = MOVE >
295 typename std::enable_if< std::is_same< M, std::true_type >::value >::type
296 operator()(const std::string& key, T& value)
298 m_visitor(key, std::move(value));
305 template <typename T> struct IndexOfType;
310 * Trait class that identifies whether T is supported by the Value.
312 template< typename T >
313 struct is_supported_type: public std::conditional<
314 IsSupportedTypeHelper< T >::type::value, std::true_type, std::false_type>::type { };
317 * Identifiers for types of Value.
323 NULL_T, /**< nullptr_t */
325 DOUBLE, /**< double */
327 STRING, /**< std::string */
328 BYTESTRING, /**< RCSByteString */
329 ATTRIBUTES, /**< RCSResourceAttributes */
330 VECTOR /**< std::vector */
334 * A Helper class to identify types of Value.
336 * @see RCSResourceAttributes
343 Type(const Type&) = default;
344 Type(Type&&) = default;
346 Type& operator=(const Type&) = default;
347 Type& operator=(Type&&) = default;
350 * Returns type identifier.
352 * @return Identifier of type.
356 TypeId getId() const BOOST_NOEXCEPT;
359 * Returns the type identifier of a base type of sequence.
361 * For non sequence types, it is equivalent to calling getId.
363 * @return Identifier of type.
368 static TypeId getBaseTypeId(const Type& t) BOOST_NOEXCEPT;
371 * Returns the depth of a type.
373 * The return will be zero for non sequence types.
377 static size_t getDepth(const Type& t) BOOST_NOEXCEPT;
380 * Factory method to create Type instance from T.
382 * @return An instance that has TypeId for T.
384 * @note T must be supported by Value. Otherwise, it won't compile.
386 * @see is_supported_type
388 template < typename T >
389 constexpr static Type typeOf(const T&) BOOST_NOEXCEPT
391 return Type{ IndexOfType< T >::value };
395 * Factory method to create Type instance from T.
397 * @return An instance that has TypeId for T.
399 * @note T must be supported by Value. Otherwise, it won't compile.
401 * @see is_supported_type
403 template < typename T >
404 constexpr static Type typeOf() BOOST_NOEXCEPT
406 return Type{ IndexOfType< T >::value };
410 friend bool operator==(const Type&, const Type&) BOOST_NOEXCEPT;
414 constexpr explicit Type(int which) BOOST_NOEXCEPT :
424 * Value holds a value among various types at a time.
426 * Type helps identify type information of Value.
428 * Supported types are below
435 RCSResourceAttributes
438 std::vector< double >
440 std::vector< std::string >
441 std::vector< RCSByteString >
442 std::vector< RCSResourceAttributes >
444 std::vector< std::vector< int > >
445 std::vector< std::vector< std::vector< int > > >
447 std::vector< std::vector< double > >
448 std::vector< std::vector< std::vector< double > > >
450 std::vector< std::vector< bool > >
451 std::vector< std::vector< std::vector< bool > > >
453 std::vector< std::vector< std::string > >
454 std::vector< std::vector< std::vector< std::string > > >
456 std::vector< std::vector< RCSByteString > >
457 std::vector< std::vector< std::vector< RCSByteString > > >
459 std::vector< std::vector< RCSResourceAttributes > >
460 std::vector< std::vector< std::vector< RCSResourceAttributes > > >
463 * @see RCSResourceAttributes
465 * @see is_supported_type
470 class ComparisonHelper;
474 Value(Value&&) BOOST_NOEXCEPT;
477 * Constructs a Value if T is a supported type.<br/>
478 * Otherwise it won't compile.
480 template< typename T, typename = typename enable_if_supported< T >::type >
482 m_data{ new ValueVariant{ std::forward< T >(value) } }
488 Value& operator=(const Value&);
489 Value& operator=(Value&&);
491 template< typename T, typename = typename enable_if_supported< T >::type >
492 Value& operator=(T&& rhs)
494 *m_data = std::forward< T >(rhs);
498 Value& operator=(const char*);
499 Value& operator=(std::nullptr_t);
502 * Returns the underlying value as T.
504 * @return const reference to the underlying value.
506 * @throws BadGetException If type of the underlying value is not T.
508 template< typename T >
509 typename std::add_lvalue_reference< const T >::type get() const
511 return checkedGet< T >();
515 * Returns the underlying value as T.
517 * @return reference to the underlying value.
519 * @throws BadGetException If type of the underlying value is not T.
521 template< typename T >
522 typename std::add_lvalue_reference< T >::type get()
524 return checkedGet< T >();
528 * Returns Type information.
532 Type getType() const;
535 * Returns a string representation.
538 std::string toString() const;
541 * Exchanges the content of the object by the content of the parameter.
543 void swap(Value&) BOOST_NOEXCEPT;
546 friend class RCSResourceAttributes;
550 template< typename T, typename = typename enable_if_supported< T >::type >
551 typename std::add_lvalue_reference< T >::type checkedGet() const
555 if ((*m_data).type() == typeid(T))
557 return boost::get< T >(*m_data);
561 throw RCSBadGetException{ "Wrong type" };
564 catch (const boost::bad_get&)
566 throw RCSBadGetException{ "Wrong type" };
570 template< typename T, typename U >
571 bool equals(const U& rhs) const
575 return get< T >() == rhs;
577 catch (const RCSBadGetException&)
588 boost::scoped_ptr< ValueVariant > m_data;
593 class const_iterator;
596 RCSResourceAttributes() = default;
597 RCSResourceAttributes(const RCSResourceAttributes&) = default;
598 RCSResourceAttributes(RCSResourceAttributes&&) = default;
600 RCSResourceAttributes& operator=(const RCSResourceAttributes&) = default;
601 RCSResourceAttributes& operator=(RCSResourceAttributes&&) = default;
604 * Returns an {@link iterator} referring to the first element.
606 iterator begin() BOOST_NOEXCEPT;
609 * Returns an {@link iterator} referring to the <i>past-the-end element</i>.
611 iterator end() BOOST_NOEXCEPT;
616 const_iterator begin() const BOOST_NOEXCEPT;
621 const_iterator end() const BOOST_NOEXCEPT;
624 * Returns a const_iterator referring to the first element.
626 const_iterator cbegin() const BOOST_NOEXCEPT;
629 * Returns a const_iterator referring to the <i>past-the-end element</i>.
631 const_iterator cend() const BOOST_NOEXCEPT;
636 * If @a key matches the key of a value,
637 * returns a reference to its mapped value. <br/>
638 * If @a key doesn't match the key of any value,
639 * inserts a new value with that key and returns a reference to it.
640 * The element is a Value that has null.
642 * @param key Key of the element whose mapped value is accessed.
644 * @return A reference to the mapped value with @a key.
648 Value& operator[](const std::string& key);
653 * If @a key matches the key of a value,
654 * returns a reference to its mapped value. <br/>
655 * If @a key doesn't match the key of any value,
656 * inserts a new value with that key and returns a reference to it.
657 * The value has null.
659 * @param key Key of the element whose mapped value is accessed.
660 * This is moved instead of copied when a new value is inserted.
662 * @return A reference to the mapped value with @a key.
666 Value& operator[](std::string&& key);
671 * If @a key matches the key of a value,
672 * returns a reference to its mapped value. <br/>
673 * If @a key doesn't match the key of any value, throws InvalidKeyException.
675 * @param key Key of the element whose mapped value is accessed.
677 * @throws InvalidKeyException If @a key doesn't match the key of any value.
679 * @return A reference to the mapped value with @a key.
683 Value& at(const std::string& key);
688 * If @a key matches the key of a value,
689 * returns a reference to its mapped value. <br/>
690 * If @a key doesn't match the key of any value, throws InvalidKeyException.
692 * @param key Key of the element whose mapped value is accessed.
694 * @throws InvalidKeyException If @a key doesn't match the key of any value.
696 * @return A const reference to the mapped value with @a key.
700 const Value& at(const std::string& key) const;
703 * Removes all elements.
705 void clear() BOOST_NOEXCEPT;
708 * Removes a single element.
710 * @param key Key of the element to be removed.
712 * @return true if an element is erased, false otherwise.
714 bool erase(const std::string& key);
717 * Removes a single element.
719 * @param pos Iterator to the element to remove.
721 * @return Iterator following the last removed element.
723 iterator erase(const_iterator pos);
726 * Checks this contains an element for the specified key.
728 * @param key Key to check.
730 * @return true if an element exists, false otherwise.
732 bool contains(const std::string& key) const;
735 * Returns whether it is empty.
739 bool empty() const BOOST_NOEXCEPT;
742 * Returns the number of elements.
746 size_t size() const BOOST_NOEXCEPT;
749 template< typename VISITOR >
750 void visit(VISITOR& visitor) const
752 KeyValueVisitorHelper< VISITOR > helper{ visitor };
754 for (const auto& i : m_values)
756 boost::variant< const std::string& > key{ i.first };
757 boost::apply_visitor(helper, key, *i.second.m_data);
761 template< typename VISITOR >
762 void visitToMove(VISITOR& visitor)
764 KeyValueVisitorHelper< VISITOR, std::true_type > helper{ visitor };
766 for (auto& i : m_values)
768 boost::variant< const std::string& > key{ i.first };
769 boost::apply_visitor(helper, key, *i.second.m_data);
777 std::unordered_map< std::string, Value > m_values;
780 friend class ResourceAttributesConverter;
782 friend bool operator==(const RCSResourceAttributes&, const RCSResourceAttributes&);
787 * A helper class to avoid obscure comparisons of values which are supported
788 * by RCSResourceAttributes::Value caused by implicitly converting a value
789 * to a RCSResourceAttributes::Value.
792 * @see RCSResourceAttributes
793 * @see is_supported_type
795 class RCSResourceAttributes::Value::ComparisonHelper
798 ComparisonHelper(const Value&);
800 ComparisonHelper(const ComparisonHelper&) = delete;
801 ComparisonHelper& operator=(const ComparisonHelper&) = delete;
803 template< typename T >
804 typename std::enable_if< is_supported_type< T >::value, bool >::type equals(
807 return m_valueRef.equals< T >(v);
810 bool equals(const std::string& v) const
812 return m_valueRef.equals< std::string >(v);
815 bool operator==(const ComparisonHelper&) const;
818 const Value& m_valueRef;
822 template< typename T >
823 struct RCSResourceAttributes::IsSupportedTypeHelper
825 typedef boost::mpl::contains< ValueVariant::types, typename std::decay< T >::type > type;
828 template < typename T >
829 struct RCSResourceAttributes::IndexOfType
831 static_assert(RCSResourceAttributes::is_supported_type< T >::value,
832 "The type is not supported!");
834 typedef typename boost::mpl::find< ValueVariant::types, T >::type iter;
835 typedef typename boost::mpl::begin< ValueVariant::types >::type mpl_begin;
837 static constexpr int value = boost::mpl::distance< mpl_begin, iter >::value;
840 template < typename T > constexpr int RCSResourceAttributes::IndexOfType< T >::value;
844 * @relates RCSResourceAttributes::Type
846 * Checks if the objects are equal, that is, whether types are exactly same.
848 * @return true if the objects are equal, false otherwise.
850 bool operator==(const RCSResourceAttributes::Type&, const RCSResourceAttributes::Type&)
854 * @relates RCSResourceAttributes::Type
856 * Checks if the objects are not equal, that is, whether types are not exactly same.
858 * @return true if the objects are not equal, false otherwise.
860 bool operator!=(const RCSResourceAttributes::Type&, const RCSResourceAttributes::Type&)
864 * @relates RCSResourceAttributes::Value
866 * Checks if the contents are equal, that is,
867 * whether types are matched and underlying values are equal.
869 * @return true if the contents are equal, false otherwise.
871 bool operator==(const RCSResourceAttributes::Value::ComparisonHelper&,
872 const RCSResourceAttributes::Value::ComparisonHelper&);
875 * @relates RCSResourceAttributes::Value
877 * Checks if the contents are not equal, that is,
878 * whether types are not matched or underlying values are not equal.
880 * @return true if the contents are not equal, false otherwise.
882 bool operator!=(const RCSResourceAttributes::Value::ComparisonHelper&,
883 const RCSResourceAttributes::Value::ComparisonHelper&);
886 template< typename T >
887 typename std::enable_if< RCSResourceAttributes::is_supported_type< T >::value ||
888 std::is_constructible< std::string, T >::value, bool >::type
889 operator==(const RCSResourceAttributes::Value::ComparisonHelper& lhs, const T& rhs)
891 return lhs.equals(rhs);
894 template< typename T >
895 typename std::enable_if< RCSResourceAttributes::is_supported_type< T >::value ||
896 std::is_constructible< std::string, T >::value, bool >::type
897 operator==(const T& lhs, const RCSResourceAttributes::Value::ComparisonHelper& rhs)
902 template< typename T >
903 typename std::enable_if< RCSResourceAttributes::is_supported_type< T >::value ||
904 std::is_constructible< std::string, T >::value, bool >::type
905 operator!=(const RCSResourceAttributes::Value::ComparisonHelper& lhs, const T& rhs)
907 return !(lhs == rhs);
910 template< typename T >
911 typename std::enable_if< RCSResourceAttributes::is_supported_type< T >::value ||
912 std::is_constructible< std::string, T >::value, bool >::type
913 operator!=(const T& lhs, const RCSResourceAttributes::Value::ComparisonHelper& rhs)
915 return !(rhs == lhs);
920 * @relates RCSResourceAttributes
922 * Checks if the attributes are equal, that is, whether contents are equal.
924 * @return true if the attributes are equal, false otherwise.
926 bool operator==(const RCSResourceAttributes& lhs, const RCSResourceAttributes& rhs);
929 * @relates RCSResourceAttributes
931 * Checks if the attributes are not equal, that is, whether contents are not equal.
933 * @return true if the attributes are not equal, false otherwise.
935 bool operator!=(const RCSResourceAttributes&, const RCSResourceAttributes&);
938 * KeyValuePair is a class to access attribute's key and value of an element pointed by
939 * iterators of RCSResourceAttributes.
942 * @see RCSResourceAttributes
944 * @see const_iterator
946 class RCSResourceAttributes::KeyValuePair
949 class KeyVisitor: public boost::static_visitor< const std::string& >
952 result_type operator()(iterator*) const BOOST_NOEXCEPT;
953 result_type operator()(const_iterator*) const BOOST_NOEXCEPT;
956 class ValueVisitor: public boost::static_visitor< Value& >
959 result_type operator()(iterator*) BOOST_NOEXCEPT;
960 result_type operator()(const_iterator*);
963 class ConstValueVisitor: public boost::static_visitor< const Value& >
966 result_type operator()(iterator*) const BOOST_NOEXCEPT;
967 result_type operator()(const_iterator*) const BOOST_NOEXCEPT;
971 const std::string& key() const BOOST_NOEXCEPT;
972 const RCSResourceAttributes::Value& value() const BOOST_NOEXCEPT;
973 RCSResourceAttributes::Value& value();
976 KeyValuePair(const KeyValuePair&) = default;
977 KeyValuePair(boost::variant< iterator*, const_iterator* >&&) BOOST_NOEXCEPT;
979 KeyValuePair& operator=(const KeyValuePair&) = default;
982 boost::variant< iterator*, const_iterator* > m_iterRef;
984 KeyVisitor m_keyVisitor;
985 ValueVisitor m_valueVisitor;
986 ConstValueVisitor m_constValueVisitor;
989 friend class iterator;
990 friend class const_iterator;
995 * A forward iterator to KeyValuePair.
997 * @see RCSResourceAttributes
999 * @see const_iterator
1001 class RCSResourceAttributes::iterator:
1002 public std::iterator< std::forward_iterator_tag, RCSResourceAttributes::KeyValuePair >
1005 typedef std::unordered_map< std::string, Value >::iterator base_iterator;
1009 iterator(const iterator&);
1011 iterator& operator=(const iterator&);
1013 reference operator*();
1014 pointer operator->();
1016 iterator& operator++();
1017 iterator operator++(int);
1019 bool operator==(const iterator&) const;
1020 bool operator!=(const iterator&) const;
1023 explicit iterator(base_iterator&&);
1026 base_iterator m_cur;
1027 RCSResourceAttributes::KeyValuePair m_keyValuePair;
1030 friend class RCSResourceAttributes;
1036 * A forward iterator to const KeyValuePair.
1038 * @see RCSResourceAttributes
1042 class RCSResourceAttributes::const_iterator:
1043 public std::iterator< std::forward_iterator_tag,
1044 const RCSResourceAttributes::KeyValuePair >
1047 typedef std::unordered_map< std::string, Value >::const_iterator base_iterator;
1051 const_iterator(const const_iterator&);
1052 const_iterator(const RCSResourceAttributes::iterator&);
1054 const_iterator& operator=(const const_iterator&);
1055 const_iterator& operator=(const RCSResourceAttributes::iterator&);
1057 reference operator*() const;
1058 pointer operator->() const;
1060 const_iterator& operator++();
1061 const_iterator operator++(int);
1063 bool operator==(const const_iterator&) const;
1064 bool operator!=(const const_iterator&) const;
1067 explicit const_iterator(base_iterator&&);
1070 base_iterator m_cur;
1071 RCSResourceAttributes::KeyValuePair m_keyValuePair;
1074 friend class RCSResourceAttributes;
1081 #endif // RES_ENCAPSULATION_RESOURCEATTRIBUTES_H