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(base_iterator&& iter) :
433 m_cur{ std::move(iter) },
434 m_keyValuePair{ this }
438 auto RCSResourceAttributes::iterator::operator*() -> KeyValuePair&
440 return m_keyValuePair;
443 auto RCSResourceAttributes::iterator::iterator::operator->() -> KeyValuePair*
445 return &m_keyValuePair;
448 auto RCSResourceAttributes::iterator::operator++() -> iterator&
454 auto RCSResourceAttributes::iterator::operator++(int) -> iterator
456 iterator iter(*this);
461 bool RCSResourceAttributes::iterator::operator==(const iterator& rhs) const
463 return m_cur == rhs.m_cur;
466 bool RCSResourceAttributes::iterator::operator!=(const iterator& rhs) const
468 return !(*this == rhs);
472 RCSResourceAttributes::const_iterator::const_iterator() :
473 m_cur{ base_iterator{} }, m_keyValuePair{ this }
477 RCSResourceAttributes::const_iterator::const_iterator(base_iterator&& iter) :
478 m_cur{ iter }, m_keyValuePair{ this }
482 RCSResourceAttributes::const_iterator::const_iterator(
483 const RCSResourceAttributes::iterator& iter) :
484 m_cur{ iter.m_cur }, m_keyValuePair{ this }
488 auto RCSResourceAttributes::const_iterator::operator=(
489 const RCSResourceAttributes::iterator& iter) -> const_iterator&
495 auto RCSResourceAttributes::const_iterator::operator*() const -> reference
497 return m_keyValuePair;
500 auto RCSResourceAttributes::const_iterator::operator->() const -> pointer
502 return &m_keyValuePair;
505 auto RCSResourceAttributes::const_iterator::operator++() -> const_iterator&
511 auto RCSResourceAttributes::const_iterator::operator++(int) -> const_iterator
513 const_iterator iter(*this);
518 bool RCSResourceAttributes::const_iterator::operator==(const const_iterator& rhs) const
520 return m_cur == rhs.m_cur;
523 bool RCSResourceAttributes::const_iterator::operator!=(const const_iterator& rhs) const
525 return !(*this == rhs);
529 auto RCSResourceAttributes::begin() noexcept -> iterator
531 return iterator{ m_values.begin() };
534 auto RCSResourceAttributes::end() noexcept -> iterator
536 return iterator{ m_values.end() };
539 auto RCSResourceAttributes::begin() const noexcept -> const_iterator
541 return const_iterator{ m_values.begin() };
544 auto RCSResourceAttributes::end() const noexcept -> const_iterator
546 return const_iterator{ m_values.end() };
549 auto RCSResourceAttributes::cbegin() const noexcept -> const_iterator
551 return const_iterator{ m_values.begin() };
554 auto RCSResourceAttributes::cend() const noexcept -> const_iterator
556 return const_iterator{ m_values.end() };
559 auto RCSResourceAttributes::operator[](const std::string& key) -> Value&
561 return m_values[key];
564 auto RCSResourceAttributes::operator[](std::string&& key) -> Value&
566 return m_values[std::move(key)];
569 auto RCSResourceAttributes::at(const std::string& key) -> Value&
573 return m_values.at(key);
575 catch (const std::out_of_range&)
577 throw RCSInvalidKeyException{ "No attribute named '" + key + "'" };
581 auto RCSResourceAttributes::at(const std::string& key) const -> const Value&
585 return m_values.at(key);
587 catch (const std::out_of_range&)
589 throw RCSInvalidKeyException{ "No attribute named '" + key + "'" };
593 void RCSResourceAttributes::clear() noexcept
595 return m_values.clear();
598 bool RCSResourceAttributes::erase(const std::string& key)
600 return m_values.erase(key) == 1U;
603 bool RCSResourceAttributes::contains(const std::string& key) const
605 return m_values.find(key) != m_values.end();
608 bool RCSResourceAttributes::empty() const noexcept
610 return m_values.empty();
613 size_t RCSResourceAttributes::size() const noexcept
615 return m_values.size();
619 bool acceptableAttributeValue(const RCSResourceAttributes::Value& dest,
620 const RCSResourceAttributes::Value& value)
622 if (dest.getType() != value.getType())
627 static_assert(RCSResourceAttributes::is_supported_type< RCSResourceAttributes >::value,
628 "RCSResourceAttributes doesn't have RCSResourceAttributes recursively.");
629 if (dest.getType().getId() == RCSResourceAttributes::TypeId::ATTRIBUTES
630 && !acceptableAttributes(dest.get< RCSResourceAttributes >(),
631 value.get< RCSResourceAttributes >()))
639 bool acceptableAttributes(const RCSResourceAttributes& dest, const RCSResourceAttributes& attr)
641 for (const auto& kv : attr)
643 if (!dest.contains(kv.key()))
648 if (!acceptableAttributeValue(dest.at(kv.key()), kv.value()))
657 AttrKeyValuePairs replaceAttributes(RCSResourceAttributes& dest,
658 const RCSResourceAttributes& newAttrs)
660 AttrKeyValuePairs replacedList;
662 for (const auto& kv : newAttrs)
664 if (dest[kv.key()] != kv.value())
666 RCSResourceAttributes::Value replacedValue;
667 replacedValue.swap(dest[kv.key()]);
668 dest[kv.key()] = kv.value();
670 replacedList.push_back(AttrKeyValuePair{ kv.key(), std::move(replacedValue) });