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"
36 using namespace OIC::Service;
38 class ToStringVisitor: public boost::static_visitor<>
41 ToStringVisitor() = default;
42 ToStringVisitor(const ToStringVisitor&) = delete;
43 ToStringVisitor(ToStringVisitor&&) = delete;
45 ToStringVisitor& operator=(const ToStringVisitor&) = delete;
46 ToStringVisitor& operator=(ToStringVisitor&&) = delete;
48 template< typename T >
49 void operator()(const T& value)
51 m_stream << boost::lexical_cast< std::string >(value);
54 template< typename T >
55 void operator()(const std::vector< T >& v)
58 for (auto it = v.begin(); it != v.end(); ++it)
60 if (it != v.begin()) m_stream << ", ";
66 void operator()(std::nullptr_t)
71 void operator()(bool value)
73 m_stream << (value ? "true" : "false");
76 void operator()(const std::string& value)
78 m_stream << "\"" + value + "\"";
81 void operator()(const RCSResourceAttributes& attrs)
84 for (auto it = attrs.begin(); it != attrs.end(); ++it)
86 if (it != attrs.begin()) m_stream << ", ";
87 m_stream << "\"" << it->key() << "\" : " << it->value().toString();
92 std::string get() const {
93 return m_stream.str();
97 std::ostringstream m_stream;
100 class TypeVisitor: public boost::static_visitor< RCSResourceAttributes::Type >
103 TypeVisitor() = default;
104 TypeVisitor(const TypeVisitor&) = delete;
105 TypeVisitor(TypeVisitor&&) = delete;
107 TypeVisitor& operator=(const TypeVisitor&) = delete;
108 TypeVisitor& operator=(TypeVisitor&&) = delete;
110 template< typename T >
111 RCSResourceAttributes::Type operator()(const T& value) const
113 return RCSResourceAttributes::Type::typeOf(value);
121 template< typename T >
122 struct TypeInfoConverter;
125 struct TypeInfoConverter< int >
127 static constexpr RCSResourceAttributes::TypeId typeId = RCSResourceAttributes::TypeId::INT;
131 struct TypeInfoConverter< std::nullptr_t >
133 static constexpr RCSResourceAttributes::TypeId typeId =
134 RCSResourceAttributes::TypeId::NULL_T;
138 struct TypeInfoConverter< double >
140 static constexpr RCSResourceAttributes::TypeId typeId =
141 RCSResourceAttributes::TypeId::DOUBLE;
145 struct TypeInfoConverter< bool >
147 static constexpr RCSResourceAttributes::TypeId typeId = RCSResourceAttributes::TypeId::BOOL;
151 struct TypeInfoConverter< std::string >
153 static constexpr RCSResourceAttributes::TypeId typeId =
154 RCSResourceAttributes::TypeId::STRING;
158 struct TypeInfoConverter< RCSResourceAttributes >
160 static constexpr RCSResourceAttributes::TypeId typeId =
161 RCSResourceAttributes::TypeId::ATTRIBUTES;
164 template< typename T >
165 struct TypeInfoConverter< std::vector< T > >
167 static constexpr RCSResourceAttributes::TypeId typeId =
168 RCSResourceAttributes::TypeId::VECTOR;
171 template< typename T >
172 struct SequenceTraits
174 static constexpr size_t depth = 0;
178 template< typename T >
179 struct SequenceTraits< std::vector< T > >
181 static constexpr size_t depth = SequenceTraits< T >::depth + 1;
182 typedef typename SequenceTraits< T >::base_type base_type;
187 RCSResourceAttributes::TypeId m_typeId;
188 RCSResourceAttributes::Type m_baseType;
191 template< typename T, typename ST = SequenceTraits < T > >
192 constexpr static TypeInfo get()
194 return { TypeInfoConverter< T >::typeId ,
195 RCSResourceAttributes::Type::typeOf< typename ST::base_type >(), ST::depth };
198 template< typename VARIANT, int POS >
199 constexpr static TypeInfo get()
201 typedef typename boost::mpl::begin< typename VARIANT::types >::type mpl_begin;
202 typedef typename boost::mpl::advance< mpl_begin, boost::mpl::int_< POS > >::type iter;
204 return get< typename boost::mpl::deref< iter >::type >();
208 template< typename VARIANT, int POS >
209 constexpr inline std::vector< TypeInfo > getTypeInfo(Int2Type< POS >) noexcept
211 auto vec = getTypeInfo< VARIANT >(Int2Type< POS - 1 >{ });
212 vec.push_back(TypeInfo::get< VARIANT, POS >());
216 template< typename VARIANT >
217 constexpr inline std::vector< TypeInfo > getTypeInfo(Int2Type< 0 >) noexcept
219 return { TypeInfo::get< VARIANT, 0 >() };
222 template< typename VARIANT >
223 inline TypeInfo getTypeInfo(int which) noexcept
225 static constexpr size_t variantSize = boost::mpl::size< typename VARIANT::types >::value;
226 static constexpr size_t variantEnd = variantSize - 1;
227 static const std::vector< TypeInfo > typeInfos = getTypeInfo< VARIANT >(
228 Int2Type< variantEnd >{ });
230 static_assert(variantSize > 0, "Variant has no type!");
232 return typeInfos[which];
235 } // unnamed namespace
243 RCSResourceAttributes::Value::ComparisonHelper::ComparisonHelper(const Value& v) :
248 bool RCSResourceAttributes::Value::ComparisonHelper::operator==
249 (const Value::ComparisonHelper& rhs) const
251 return *m_valueRef.m_data == *rhs.m_valueRef.m_data;
254 bool operator==(const RCSResourceAttributes::Type& lhs,
255 const RCSResourceAttributes::Type& rhs) noexcept
257 return lhs.m_which == rhs.m_which;
260 bool operator!=(const RCSResourceAttributes::Type& lhs,
261 const RCSResourceAttributes::Type& rhs) noexcept
263 return !(lhs == rhs);
266 bool operator==(const RCSResourceAttributes::Value::ComparisonHelper& lhs,
267 const RCSResourceAttributes::Value::ComparisonHelper& rhs)
269 return lhs.operator==(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& lhs, const RCSResourceAttributes& rhs)
280 return lhs.m_values == rhs.m_values;
283 bool operator!=(const RCSResourceAttributes& lhs, const RCSResourceAttributes& rhs)
285 return !(lhs == rhs);
288 auto RCSResourceAttributes::Type::getId() const noexcept -> TypeId
290 return ::getTypeInfo< ValueVariant >(m_which).m_typeId;
293 auto RCSResourceAttributes::Type::getBaseTypeId(const Type& t) noexcept -> TypeId
295 return ::getTypeInfo< ValueVariant >(t.m_which).m_baseType.getId();
298 size_t RCSResourceAttributes::Type::getDepth(const Type& t) noexcept
300 return ::getTypeInfo< ValueVariant >(t.m_which).m_depth;
304 RCSResourceAttributes::Value::Value() :
305 m_data{ new ValueVariant{} }
309 RCSResourceAttributes::Value::Value(const Value& from) :
310 m_data{ new ValueVariant{ *from.m_data } }
314 RCSResourceAttributes::Value::Value(Value&& from) noexcept :
315 m_data{ new ValueVariant{} }
317 m_data.swap(from.m_data);
320 RCSResourceAttributes::Value::Value(const char* value) :
321 m_data{ new ValueVariant{ std::string{ value } } }
325 auto RCSResourceAttributes::Value::operator=(const Value& rhs) -> Value&
327 *m_data = *rhs.m_data;
331 auto RCSResourceAttributes::Value::operator=(Value&& rhs) -> Value&
333 *m_data = ValueVariant{};
334 m_data->swap(*rhs.m_data);
338 auto RCSResourceAttributes::Value::operator=(const char* rhs) -> Value&
340 *m_data = std::string{ rhs };
344 auto RCSResourceAttributes::Value::operator=(std::nullptr_t) -> Value&
350 auto RCSResourceAttributes::Value::getType() const -> Type
352 return boost::apply_visitor(TypeVisitor(), *m_data);
355 std::string RCSResourceAttributes::Value::toString() const
357 ToStringVisitor visitor;
358 boost::apply_visitor(visitor, *m_data);
359 return visitor.get();
362 void RCSResourceAttributes::Value::swap(Value& rhs) noexcept
364 m_data.swap(rhs.m_data);
367 auto RCSResourceAttributes::KeyValuePair::KeyVisitor::operator()(
368 iterator* iter) const noexcept -> result_type
370 return iter->m_cur->first;
373 auto RCSResourceAttributes::KeyValuePair::KeyVisitor::operator()(
374 const_iterator* iter) const noexcept -> result_type
376 return iter->m_cur->first;
379 auto RCSResourceAttributes::KeyValuePair::ValueVisitor::operator() (iterator* iter) noexcept
382 return iter->m_cur->second;
385 auto RCSResourceAttributes::KeyValuePair::ValueVisitor::operator() (const_iterator*)
388 // should not reach here.
389 throw RCSBadGetException("");
392 auto RCSResourceAttributes::KeyValuePair::ConstValueVisitor::operator()(
393 iterator*iter) const noexcept -> result_type
395 return iter->m_cur->second;
398 auto RCSResourceAttributes::KeyValuePair::ConstValueVisitor::operator()(
399 const_iterator* iter) const noexcept -> result_type
401 return iter->m_cur->second;
404 auto RCSResourceAttributes::KeyValuePair::key() const noexcept -> const std::string&
406 return boost::apply_visitor(m_keyVisitor, m_iterRef);
409 auto RCSResourceAttributes::KeyValuePair::value() const noexcept -> const Value&
411 return boost::apply_visitor(m_constValueVisitor, m_iterRef);
414 auto RCSResourceAttributes::KeyValuePair::value() -> Value&
416 return boost::apply_visitor(m_valueVisitor, m_iterRef);
419 RCSResourceAttributes::KeyValuePair::KeyValuePair(boost::variant<iterator*,
420 const_iterator*>&& ref) noexcept :
426 RCSResourceAttributes::iterator::iterator() :
427 m_cur{ base_iterator{ } },
428 m_keyValuePair{ this }
432 RCSResourceAttributes::iterator::iterator(const iterator& rhs) :
434 m_keyValuePair{ this }
438 auto RCSResourceAttributes::iterator::operator=(const iterator& rhs) -> iterator&
444 RCSResourceAttributes::iterator::iterator(base_iterator&& iter) :
445 m_cur{ std::move(iter) },
446 m_keyValuePair{ this }
450 auto RCSResourceAttributes::iterator::operator*() -> KeyValuePair&
452 return m_keyValuePair;
455 auto RCSResourceAttributes::iterator::iterator::operator->() -> KeyValuePair*
457 return &m_keyValuePair;
460 auto RCSResourceAttributes::iterator::operator++() -> iterator&
466 auto RCSResourceAttributes::iterator::operator++(int) -> iterator
468 iterator iter(*this);
473 bool RCSResourceAttributes::iterator::operator==(const iterator& rhs) const
475 return m_cur == rhs.m_cur;
478 bool RCSResourceAttributes::iterator::operator!=(const iterator& rhs) const
480 return !(*this == rhs);
484 RCSResourceAttributes::const_iterator::const_iterator() :
485 m_cur{ base_iterator{} }, m_keyValuePair{ this }
489 RCSResourceAttributes::const_iterator::const_iterator(base_iterator&& iter) :
490 m_cur{ iter }, m_keyValuePair{ this }
494 RCSResourceAttributes::const_iterator::const_iterator(const const_iterator& rhs) :
495 m_cur{ rhs.m_cur }, m_keyValuePair{ this }
499 RCSResourceAttributes::const_iterator::const_iterator(
500 const RCSResourceAttributes::iterator& iter) :
501 m_cur{ iter.m_cur }, m_keyValuePair{ this }
505 auto RCSResourceAttributes::const_iterator::operator=(
506 const const_iterator& rhs) -> const_iterator&
512 auto RCSResourceAttributes::const_iterator::operator=(
513 const RCSResourceAttributes::iterator& iter) -> const_iterator&
519 auto RCSResourceAttributes::const_iterator::operator*() const -> reference
521 return m_keyValuePair;
524 auto RCSResourceAttributes::const_iterator::operator->() const -> pointer
526 return &m_keyValuePair;
529 auto RCSResourceAttributes::const_iterator::operator++() -> const_iterator&
535 auto RCSResourceAttributes::const_iterator::operator++(int) -> const_iterator
537 const_iterator iter(*this);
542 bool RCSResourceAttributes::const_iterator::operator==(const const_iterator& rhs) const
544 return m_cur == rhs.m_cur;
547 bool RCSResourceAttributes::const_iterator::operator!=(const const_iterator& rhs) const
549 return !(*this == rhs);
553 auto RCSResourceAttributes::begin() noexcept -> iterator
555 return iterator{ m_values.begin() };
558 auto RCSResourceAttributes::end() noexcept -> iterator
560 return iterator{ m_values.end() };
563 auto RCSResourceAttributes::begin() const noexcept -> const_iterator
565 return const_iterator{ m_values.begin() };
568 auto RCSResourceAttributes::end() const noexcept -> const_iterator
570 return const_iterator{ m_values.end() };
573 auto RCSResourceAttributes::cbegin() const noexcept -> const_iterator
575 return const_iterator{ m_values.begin() };
578 auto RCSResourceAttributes::cend() const noexcept -> const_iterator
580 return const_iterator{ m_values.end() };
583 auto RCSResourceAttributes::operator[](const std::string& key) -> Value&
585 return m_values[key];
588 auto RCSResourceAttributes::operator[](std::string&& key) -> Value&
590 return m_values[std::move(key)];
593 auto RCSResourceAttributes::at(const std::string& key) -> Value&
597 return m_values.at(key);
599 catch (const std::out_of_range&)
601 throw RCSInvalidKeyException{ "No attribute named '" + key + "'" };
605 auto RCSResourceAttributes::at(const std::string& key) const -> const Value&
609 return m_values.at(key);
611 catch (const std::out_of_range&)
613 throw RCSInvalidKeyException{ "No attribute named '" + key + "'" };
617 void RCSResourceAttributes::clear() noexcept
619 return m_values.clear();
622 bool RCSResourceAttributes::erase(const std::string& key)
624 return m_values.erase(key) == 1U;
627 auto RCSResourceAttributes::erase(const_iterator pos) -> iterator
629 return iterator{ m_values.erase(pos.m_cur) };
632 bool RCSResourceAttributes::contains(const std::string& key) const
634 return m_values.find(key) != m_values.end();
637 bool RCSResourceAttributes::empty() const noexcept
639 return m_values.empty();
642 size_t RCSResourceAttributes::size() const noexcept
644 return m_values.size();
648 bool acceptableAttributeValue(const RCSResourceAttributes::Value& dest,
649 const RCSResourceAttributes::Value& value)
651 if (dest.getType() != value.getType())
656 static_assert(RCSResourceAttributes::is_supported_type< RCSResourceAttributes >::value,
657 "RCSResourceAttributes doesn't have RCSResourceAttributes recursively.");
658 if (dest.getType().getId() == RCSResourceAttributes::TypeId::ATTRIBUTES
659 && !acceptableAttributes(dest.get< RCSResourceAttributes >(),
660 value.get< RCSResourceAttributes >()))
668 bool acceptableAttributes(const RCSResourceAttributes& dest, const RCSResourceAttributes& attr)
670 for (const auto& kv : attr)
672 if (!dest.contains(kv.key()))
677 if (!acceptableAttributeValue(dest.at(kv.key()), kv.value()))
686 AttrKeyValuePairs replaceAttributes(RCSResourceAttributes& dest,
687 const RCSResourceAttributes& newAttrs)
689 AttrKeyValuePairs replacedList;
691 for (const auto& kv : newAttrs)
693 if (dest[kv.key()] != kv.value())
695 RCSResourceAttributes::Value replacedValue;
696 replacedValue.swap(dest[kv.key()]);
697 dest[kv.key()] = kv.value();
699 replacedList.push_back(AttrKeyValuePair{ kv.key(), std::move(replacedValue) });