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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "RCSResourceAttributes.h"
25 #include "ResourceAttributesUtils.h"
26 #include "ResourceAttributesConverter.h"
28 #include "boost/lexical_cast.hpp"
29 #include "boost/mpl/advance.hpp"
30 #include "boost/mpl/size.hpp"
31 #include "boost/mpl/deref.hpp"
34 #define OC_CONSTEXPR_INLINE inline
36 #define OC_CONSTEXPR_INLINE constexpr inline
42 using namespace OIC::Service;
44 class ToStringVisitor: public boost::static_visitor<>
47 ToStringVisitor() = default;
48 ToStringVisitor(const ToStringVisitor&) = delete;
49 ToStringVisitor(ToStringVisitor&&) = delete;
51 ToStringVisitor& operator=(const ToStringVisitor&) = delete;
52 ToStringVisitor& operator=(ToStringVisitor&&) = delete;
54 template< typename T >
55 void operator()(const T& value)
57 m_stream << boost::lexical_cast< std::string >(value);
60 template< typename T >
61 void operator()(const std::vector< T >& v)
64 for (auto it = v.begin(); it != v.end(); ++it)
66 if (it != v.begin()) m_stream << ", ";
72 void operator()(std::nullptr_t)
77 void operator()(bool value)
79 m_stream << (value ? "true" : "false");
82 void operator()(const std::string& value)
84 m_stream << "\"" + value + "\"";
87 void operator()(const RCSResourceAttributes& attrs)
90 for (auto it = attrs.begin(); it != attrs.end(); ++it)
92 if (it != attrs.begin()) m_stream << ", ";
93 m_stream << "\"" << it->key() << "\" : " << it->value().toString();
98 std::string get() const {
99 return m_stream.str();
103 std::ostringstream m_stream;
106 class TypeVisitor: public boost::static_visitor< RCSResourceAttributes::Type >
109 TypeVisitor() = default;
110 TypeVisitor(const TypeVisitor&) = delete;
111 TypeVisitor(TypeVisitor&&) = delete;
113 TypeVisitor& operator=(const TypeVisitor&) = delete;
114 TypeVisitor& operator=(TypeVisitor&&) = delete;
116 template< typename T >
117 RCSResourceAttributes::Type operator()(const T& value) const
119 return RCSResourceAttributes::Type::typeOf(value);
127 template< typename T >
128 struct TypeInfoConverter;
131 struct TypeInfoConverter< int >
133 static constexpr RCSResourceAttributes::TypeId typeId = RCSResourceAttributes::TypeId::INT;
137 struct TypeInfoConverter< std::nullptr_t >
139 static constexpr RCSResourceAttributes::TypeId typeId =
140 RCSResourceAttributes::TypeId::NULL_T;
144 struct TypeInfoConverter< double >
146 static constexpr RCSResourceAttributes::TypeId typeId =
147 RCSResourceAttributes::TypeId::DOUBLE;
151 struct TypeInfoConverter< bool >
153 static constexpr RCSResourceAttributes::TypeId typeId = RCSResourceAttributes::TypeId::BOOL;
157 struct TypeInfoConverter< std::string >
159 static constexpr RCSResourceAttributes::TypeId typeId =
160 RCSResourceAttributes::TypeId::STRING;
164 struct TypeInfoConverter< RCSResourceAttributes >
166 static constexpr RCSResourceAttributes::TypeId typeId =
167 RCSResourceAttributes::TypeId::ATTRIBUTES;
170 template< typename T >
171 struct TypeInfoConverter< std::vector< T > >
173 static constexpr RCSResourceAttributes::TypeId typeId =
174 RCSResourceAttributes::TypeId::VECTOR;
177 template< typename T >
178 struct SequenceTraits
180 static constexpr size_t depth = 0;
184 template< typename T >
185 struct SequenceTraits< std::vector< T > >
187 static constexpr size_t depth = SequenceTraits< T >::depth + 1;
188 typedef typename SequenceTraits< T >::base_type base_type;
193 RCSResourceAttributes::TypeId m_typeId;
194 RCSResourceAttributes::Type m_baseType;
197 template< typename T, typename ST = SequenceTraits < T > >
198 constexpr static TypeInfo get()
200 return { TypeInfoConverter< T >::typeId ,
201 RCSResourceAttributes::Type::typeOf< typename ST::base_type >(), ST::depth };
204 template< typename VARIANT, int POS >
205 constexpr static TypeInfo get()
207 typedef typename boost::mpl::begin< typename VARIANT::types >::type mpl_begin;
208 typedef typename boost::mpl::advance< mpl_begin, boost::mpl::int_< POS > >::type iter;
210 return get< typename boost::mpl::deref< iter >::type >();
214 template< typename VARIANT, int POS >
215 OC_CONSTEXPR_INLINE std::vector< TypeInfo > getTypeInfo(Int2Type< POS >) noexcept
217 auto vec = getTypeInfo< VARIANT >(Int2Type< POS - 1 >{ });
218 vec.push_back(TypeInfo::get< VARIANT, POS >());
222 template< typename VARIANT >
223 OC_CONSTEXPR_INLINE std::vector< TypeInfo > getTypeInfo(Int2Type< 0 >) noexcept
225 return { TypeInfo::get< VARIANT, 0 >() };
228 template< typename VARIANT >
229 inline TypeInfo getTypeInfo(int which) noexcept
231 static constexpr size_t variantSize = boost::mpl::size< typename VARIANT::types >::value;
232 static constexpr size_t variantEnd = variantSize - 1;
233 static const std::vector< TypeInfo > typeInfos = getTypeInfo< VARIANT >(
234 Int2Type< variantEnd >{ });
236 static_assert(variantSize > 0, "Variant has no type!");
238 return typeInfos[which];
241 } // unnamed namespace
249 RCSResourceAttributes::Value::ComparisonHelper::ComparisonHelper(const Value& v) :
254 bool RCSResourceAttributes::Value::ComparisonHelper::operator==
255 (const Value::ComparisonHelper& rhs) const
257 return *m_valueRef.m_data == *rhs.m_valueRef.m_data;
260 bool operator==(const RCSResourceAttributes::Type& lhs,
261 const RCSResourceAttributes::Type& rhs) noexcept
263 return lhs.m_which == rhs.m_which;
266 bool operator!=(const RCSResourceAttributes::Type& lhs,
267 const RCSResourceAttributes::Type& rhs) noexcept
269 return !(lhs == rhs);
272 bool operator==(const RCSResourceAttributes::Value::ComparisonHelper& lhs,
273 const RCSResourceAttributes::Value::ComparisonHelper& rhs)
275 return lhs.operator==(rhs);
278 bool operator!=(const RCSResourceAttributes::Value::ComparisonHelper& lhs,
279 const RCSResourceAttributes::Value::ComparisonHelper& rhs)
281 return !lhs.operator==(rhs);
284 bool operator==(const RCSResourceAttributes& lhs, const RCSResourceAttributes& rhs)
286 return lhs.m_values == rhs.m_values;
289 bool operator!=(const RCSResourceAttributes& lhs, const RCSResourceAttributes& rhs)
291 return !(lhs == rhs);
294 auto RCSResourceAttributes::Type::getId() const noexcept -> TypeId
296 return ::getTypeInfo< ValueVariant >(m_which).m_typeId;
299 auto RCSResourceAttributes::Type::getBaseTypeId(const Type& t) noexcept -> TypeId
301 return ::getTypeInfo< ValueVariant >(t.m_which).m_baseType.getId();
304 size_t RCSResourceAttributes::Type::getDepth(const Type& t) noexcept
306 return ::getTypeInfo< ValueVariant >(t.m_which).m_depth;
310 RCSResourceAttributes::Value::Value() :
311 m_data{ new ValueVariant{} }
315 RCSResourceAttributes::Value::Value(const Value& from) :
316 m_data{ new ValueVariant{ *from.m_data } }
320 RCSResourceAttributes::Value::Value(Value&& from) noexcept :
321 m_data{ new ValueVariant{} }
323 m_data.swap(from.m_data);
326 RCSResourceAttributes::Value::Value(const char* value) :
327 m_data{ new ValueVariant{ std::string{ value } } }
331 auto RCSResourceAttributes::Value::operator=(const Value& rhs) -> Value&
333 *m_data = *rhs.m_data;
337 auto RCSResourceAttributes::Value::operator=(Value&& rhs) -> Value&
339 *m_data = ValueVariant{};
340 m_data->swap(*rhs.m_data);
344 auto RCSResourceAttributes::Value::operator=(const char* rhs) -> Value&
346 *m_data = std::string{ rhs };
350 auto RCSResourceAttributes::Value::operator=(std::nullptr_t) -> Value&
356 auto RCSResourceAttributes::Value::getType() const -> Type
358 return boost::apply_visitor(TypeVisitor(), *m_data);
361 std::string RCSResourceAttributes::Value::toString() const
363 ToStringVisitor visitor;
364 boost::apply_visitor(visitor, *m_data);
365 return visitor.get();
368 void RCSResourceAttributes::Value::swap(Value& rhs) noexcept
370 m_data.swap(rhs.m_data);
373 auto RCSResourceAttributes::KeyValuePair::KeyVisitor::operator()(
374 iterator* iter) const noexcept -> result_type
376 return iter->m_cur->first;
379 auto RCSResourceAttributes::KeyValuePair::KeyVisitor::operator()(
380 const_iterator* iter) const noexcept -> result_type
382 return iter->m_cur->first;
385 auto RCSResourceAttributes::KeyValuePair::ValueVisitor::operator() (iterator* iter) noexcept
388 return iter->m_cur->second;
391 auto RCSResourceAttributes::KeyValuePair::ValueVisitor::operator() (const_iterator*)
394 // should not reach here.
395 throw RCSBadGetException("");
398 auto RCSResourceAttributes::KeyValuePair::ConstValueVisitor::operator()(
399 iterator*iter) const noexcept -> result_type
401 return iter->m_cur->second;
404 auto RCSResourceAttributes::KeyValuePair::ConstValueVisitor::operator()(
405 const_iterator* iter) const noexcept -> result_type
407 return iter->m_cur->second;
410 auto RCSResourceAttributes::KeyValuePair::key() const noexcept -> const std::string&
412 return boost::apply_visitor(m_keyVisitor, m_iterRef);
415 auto RCSResourceAttributes::KeyValuePair::value() const noexcept -> const Value&
417 return boost::apply_visitor(m_constValueVisitor, m_iterRef);
420 auto RCSResourceAttributes::KeyValuePair::value() -> Value&
422 return boost::apply_visitor(m_valueVisitor, m_iterRef);
425 RCSResourceAttributes::KeyValuePair::KeyValuePair(boost::variant<iterator*,
426 const_iterator*>&& ref) noexcept :
432 RCSResourceAttributes::iterator::iterator() :
433 m_cur{ base_iterator{ } },
434 m_keyValuePair{ this }
438 RCSResourceAttributes::iterator::iterator(const iterator& rhs) :
440 m_keyValuePair{ this }
444 auto RCSResourceAttributes::iterator::operator=(const iterator& rhs) -> iterator&
450 RCSResourceAttributes::iterator::iterator(base_iterator&& iter) :
451 m_cur{ std::move(iter) },
452 m_keyValuePair{ this }
456 auto RCSResourceAttributes::iterator::operator*() -> KeyValuePair&
458 return m_keyValuePair;
461 auto RCSResourceAttributes::iterator::iterator::operator->() -> KeyValuePair*
463 return &m_keyValuePair;
466 auto RCSResourceAttributes::iterator::operator++() -> iterator&
472 auto RCSResourceAttributes::iterator::operator++(int) -> iterator
474 iterator iter(*this);
479 bool RCSResourceAttributes::iterator::operator==(const iterator& rhs) const
481 return m_cur == rhs.m_cur;
484 bool RCSResourceAttributes::iterator::operator!=(const iterator& rhs) const
486 return !(*this == rhs);
490 RCSResourceAttributes::const_iterator::const_iterator() :
491 m_cur{ base_iterator{} }, m_keyValuePair{ this }
495 RCSResourceAttributes::const_iterator::const_iterator(base_iterator&& iter) :
496 m_cur{ iter }, m_keyValuePair{ this }
500 RCSResourceAttributes::const_iterator::const_iterator(const const_iterator& rhs) :
501 m_cur{ rhs.m_cur }, m_keyValuePair{ this }
505 RCSResourceAttributes::const_iterator::const_iterator(
506 const RCSResourceAttributes::iterator& iter) :
507 m_cur{ iter.m_cur }, m_keyValuePair{ this }
511 auto RCSResourceAttributes::const_iterator::operator=(
512 const const_iterator& rhs) -> const_iterator&
518 auto RCSResourceAttributes::const_iterator::operator=(
519 const RCSResourceAttributes::iterator& iter) -> const_iterator&
525 auto RCSResourceAttributes::const_iterator::operator*() const -> reference
527 return m_keyValuePair;
530 auto RCSResourceAttributes::const_iterator::operator->() const -> pointer
532 return &m_keyValuePair;
535 auto RCSResourceAttributes::const_iterator::operator++() -> const_iterator&
541 auto RCSResourceAttributes::const_iterator::operator++(int) -> const_iterator
543 const_iterator iter(*this);
548 bool RCSResourceAttributes::const_iterator::operator==(const const_iterator& rhs) const
550 return m_cur == rhs.m_cur;
553 bool RCSResourceAttributes::const_iterator::operator!=(const const_iterator& rhs) const
555 return !(*this == rhs);
559 auto RCSResourceAttributes::begin() noexcept -> iterator
561 return iterator{ m_values.begin() };
564 auto RCSResourceAttributes::end() noexcept -> iterator
566 return iterator{ m_values.end() };
569 auto RCSResourceAttributes::begin() const noexcept -> const_iterator
571 return const_iterator{ m_values.begin() };
574 auto RCSResourceAttributes::end() const noexcept -> const_iterator
576 return const_iterator{ m_values.end() };
579 auto RCSResourceAttributes::cbegin() const noexcept -> const_iterator
581 return const_iterator{ m_values.begin() };
584 auto RCSResourceAttributes::cend() const noexcept -> const_iterator
586 return const_iterator{ m_values.end() };
589 auto RCSResourceAttributes::operator[](const std::string& key) -> Value&
591 return m_values[key];
594 auto RCSResourceAttributes::operator[](std::string&& key) -> Value&
596 return m_values[std::move(key)];
599 auto RCSResourceAttributes::at(const std::string& key) -> Value&
603 return m_values.at(key);
605 catch (const std::out_of_range&)
607 throw RCSInvalidKeyException{ "No attribute named '" + key + "'" };
611 auto RCSResourceAttributes::at(const std::string& key) const -> const Value&
615 return m_values.at(key);
617 catch (const std::out_of_range&)
619 throw RCSInvalidKeyException{ "No attribute named '" + key + "'" };
623 void RCSResourceAttributes::clear() noexcept
625 return m_values.clear();
628 bool RCSResourceAttributes::erase(const std::string& key)
630 return m_values.erase(key) == 1U;
633 auto RCSResourceAttributes::erase(const_iterator pos) -> iterator
635 return iterator{ m_values.erase(pos.m_cur) };
638 bool RCSResourceAttributes::contains(const std::string& key) const
640 return m_values.find(key) != m_values.end();
643 bool RCSResourceAttributes::empty() const noexcept
645 return m_values.empty();
648 size_t RCSResourceAttributes::size() const noexcept
650 return m_values.size();
654 bool acceptableAttributeValue(const RCSResourceAttributes::Value& dest,
655 const RCSResourceAttributes::Value& value)
657 if (dest.getType() != value.getType())
662 static_assert(RCSResourceAttributes::is_supported_type< RCSResourceAttributes >::value,
663 "RCSResourceAttributes doesn't have RCSResourceAttributes recursively.");
664 if (dest.getType().getId() == RCSResourceAttributes::TypeId::ATTRIBUTES
665 && !acceptableAttributes(dest.get< RCSResourceAttributes >(),
666 value.get< RCSResourceAttributes >()))
674 bool acceptableAttributes(const RCSResourceAttributes& dest, const RCSResourceAttributes& attr)
676 for (const auto& kv : attr)
678 if (!dest.contains(kv.key()))
683 if (!acceptableAttributeValue(dest.at(kv.key()), kv.value()))
692 AttrKeyValuePairs replaceAttributes(RCSResourceAttributes& dest,
693 const RCSResourceAttributes& newAttrs)
695 AttrKeyValuePairs replacedList;
697 for (const auto& kv : newAttrs)
699 if (dest[kv.key()] != kv.value())
701 RCSResourceAttributes::Value replacedValue;
702 replacedValue.swap(dest[kv.key()]);
703 dest[kv.key()] = kv.value();
705 replacedList.push_back(AttrKeyValuePair{ kv.key(), std::move(replacedValue) });