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>
23 #include <ResourceAttributesUtils.h>
24 #include <ResourceAttributesConverter.h>
26 #include <boost/lexical_cast.hpp>
27 #include <boost/mpl/advance.hpp>
28 #include <boost/mpl/size.hpp>
29 #include <boost/mpl/deref.hpp>
34 using namespace OIC::Service;
36 class ToStringVisitor: public boost::static_visitor< std::string >
39 ToStringVisitor() = default;
40 ToStringVisitor(const ToStringVisitor&) = delete;
41 ToStringVisitor(ToStringVisitor&&) = delete;
43 ToStringVisitor& operator=(const ToStringVisitor&) = delete;
44 ToStringVisitor& operator=(ToStringVisitor&&) = delete;
46 template < typename T >
47 std::string operator()(const T& value) const
49 return boost::lexical_cast<std::string>(value);
52 std::string operator()(std::nullptr_t) const
57 std::string operator()(bool value) const
59 return value ? "true" : "false";
62 std::string operator()(const std::string& value) const
67 std::string operator()(const OIC::Service::RCSResourceAttributes&) const
73 class TypeVisitor: public boost::static_visitor< RCSResourceAttributes::Type >
76 TypeVisitor() = default;
77 TypeVisitor(const TypeVisitor&) = delete;
78 TypeVisitor(TypeVisitor&&) = delete;
80 TypeVisitor& operator=(const TypeVisitor&) = delete;
81 TypeVisitor& operator=(TypeVisitor&&) = delete;
83 template< typename T >
84 RCSResourceAttributes::Type operator()(const T& value) const
86 return RCSResourceAttributes::Type::typeOf(value);
94 template< typename T >
95 struct TypeInfoConverter;
98 struct TypeInfoConverter< int >
100 static constexpr RCSResourceAttributes::TypeId typeId = RCSResourceAttributes::TypeId::INT;
104 struct TypeInfoConverter< std::nullptr_t >
106 static constexpr RCSResourceAttributes::TypeId typeId = RCSResourceAttributes::TypeId::NULL_T;
110 struct TypeInfoConverter< double >
112 static constexpr RCSResourceAttributes::TypeId typeId = RCSResourceAttributes::TypeId::DOUBLE;
116 struct TypeInfoConverter< bool >
118 static constexpr RCSResourceAttributes::TypeId typeId = RCSResourceAttributes::TypeId::BOOL;
122 struct TypeInfoConverter< std::string >
124 static constexpr RCSResourceAttributes::TypeId typeId = RCSResourceAttributes::TypeId::STRING;
128 struct TypeInfoConverter< RCSResourceAttributes >
130 static constexpr RCSResourceAttributes::TypeId typeId = RCSResourceAttributes::TypeId::ATTRIBUTES;
135 RCSResourceAttributes::TypeId typeId;
137 template< typename TRAIT >
138 constexpr TypeInfo(TRAIT) :
139 typeId{ TRAIT::typeId }
143 template< typename VARIANT, int POS >
144 static constexpr TypeInfo get()
146 return TypeInfo(TypeInfoConverter<
147 typename boost::mpl::deref<
148 typename boost::mpl::advance<
149 typename boost::mpl::begin< typename VARIANT::types>::type,
150 boost::mpl::int_< POS >
156 template< typename VARIANT, int POS >
157 constexpr inline std::vector< TypeInfo > getTypeInfo(Int2Type< POS >)
159 auto&& vec = getTypeInfo< VARIANT >(Int2Type< POS - 1 >{ });
160 vec.push_back(TypeInfo::get< VARIANT, POS >());
164 template< typename VARIANT >
165 constexpr inline std::vector< TypeInfo > getTypeInfo(Int2Type< 0 >)
167 return { TypeInfo::get< VARIANT, 0 >() };
170 template< typename VARIANT >
171 inline TypeInfo getTypeInfo(int which)
173 static constexpr int variantEnd = boost::mpl::size< typename VARIANT::types >::value - 1;
174 static const std::vector< TypeInfo > typeInfos = getTypeInfo< VARIANT >(
175 Int2Type< variantEnd >{ });
177 return typeInfos[which];
180 } // unnamed namespace
188 RCSResourceAttributes::Value::ComparisonHelper::ComparisonHelper(const Value& v) :
193 bool RCSResourceAttributes::Value::ComparisonHelper::operator==
194 (const Value::ComparisonHelper& rhs) const
196 return *m_valueRef.m_data == *rhs.m_valueRef.m_data;
199 bool operator==(const RCSResourceAttributes::Type& lhs, const RCSResourceAttributes::Type& rhs)
201 return lhs.m_which == rhs.m_which;
204 bool operator!=(const RCSResourceAttributes::Type& lhs, const RCSResourceAttributes::Type& rhs)
206 return !(lhs == rhs);
209 bool operator==(const RCSResourceAttributes::Value::ComparisonHelper& lhs,
210 const RCSResourceAttributes::Value::ComparisonHelper& rhs)
212 return lhs.operator==(rhs);
215 bool operator!=(const RCSResourceAttributes::Value::ComparisonHelper& lhs,
216 const RCSResourceAttributes::Value::ComparisonHelper& rhs)
218 return !lhs.operator==(rhs);
221 bool operator==(const RCSResourceAttributes& lhs, const RCSResourceAttributes& rhs)
223 return lhs.m_values == rhs.m_values;
226 bool operator!=(const RCSResourceAttributes& lhs, const RCSResourceAttributes& rhs)
228 return !(lhs == rhs);
231 auto RCSResourceAttributes::Type::getId() const -> TypeId
233 return ::getTypeInfo< ValueVariant >(m_which).typeId;
236 RCSResourceAttributes::Value::Value() :
237 m_data{ new ValueVariant{} }
241 RCSResourceAttributes::Value::Value(const Value& from) :
242 m_data{ new ValueVariant{ *from.m_data } }
246 RCSResourceAttributes::Value::Value(Value&& from) :
247 m_data{ new ValueVariant{} }
249 m_data->swap(*from.m_data);
252 RCSResourceAttributes::Value::Value(const char* value) :
253 m_data{ new ValueVariant{ std::string{ value } } }
257 auto RCSResourceAttributes::Value::operator=(const Value& rhs) -> Value&
259 *m_data = *rhs.m_data;
263 auto RCSResourceAttributes::Value::operator=(Value&& rhs) -> Value&
265 *m_data = ValueVariant{};
266 m_data->swap(*rhs.m_data);
270 auto RCSResourceAttributes::Value::operator=(const char* rhs) -> Value&
272 *m_data = std::string{ rhs };
276 auto RCSResourceAttributes::Value::operator=(std::nullptr_t) -> Value&
282 auto RCSResourceAttributes::Value::getType() const -> Type
284 return boost::apply_visitor(TypeVisitor(), *m_data);
287 std::string RCSResourceAttributes::Value::toString() const
289 return boost::apply_visitor(ToStringVisitor(), *m_data);
292 void RCSResourceAttributes::Value::swap(Value& rhs)
294 m_data.swap(rhs.m_data);
297 auto RCSResourceAttributes::KeyValuePair::KeyVisitor::operator()(
298 iterator* iter) const -> result_type
300 return iter->m_cur->first;
303 auto RCSResourceAttributes::KeyValuePair::KeyVisitor::operator()(
304 const_iterator* iter) const -> result_type
306 return iter->m_cur->first;
309 auto RCSResourceAttributes::KeyValuePair::ValueVisitor::operator() (iterator* iter)
312 return iter->m_cur->second;
315 auto RCSResourceAttributes::KeyValuePair::ValueVisitor::operator() (const_iterator*)
318 // should not reach here.
319 throw BadGetException("");
322 auto RCSResourceAttributes::KeyValuePair::ConstValueVisitor::operator()(
323 iterator*iter) const -> result_type
325 return iter->m_cur->second;
328 auto RCSResourceAttributes::KeyValuePair::ConstValueVisitor::operator()(
329 const_iterator* iter) const -> result_type
331 return iter->m_cur->second;
334 auto RCSResourceAttributes::KeyValuePair::key() const -> const std::string&
336 return boost::apply_visitor(m_keyVisitor, m_iterRef);
339 auto RCSResourceAttributes::KeyValuePair::value() const -> const Value&
341 return boost::apply_visitor(m_constValueVisitor, m_iterRef);
344 auto RCSResourceAttributes::KeyValuePair::value() -> Value&
346 return boost::apply_visitor(m_valueVisitor, m_iterRef);
349 RCSResourceAttributes::KeyValuePair::KeyValuePair(boost::variant<iterator*,
350 const_iterator*>&& ref) :
356 RCSResourceAttributes::iterator::iterator() :
357 m_cur{ base_iterator{ } },
358 m_keyValuePair{ this }
362 RCSResourceAttributes::iterator::iterator(base_iterator&& iter) :
363 m_cur{ std::move(iter) },
364 m_keyValuePair{ this }
368 auto RCSResourceAttributes::iterator::operator*() -> KeyValuePair&
370 return m_keyValuePair;
373 auto RCSResourceAttributes::iterator::iterator::operator->() -> KeyValuePair*
375 return &m_keyValuePair;
378 auto RCSResourceAttributes::iterator::operator++() -> iterator&
384 auto RCSResourceAttributes::iterator::operator++(int) -> iterator
386 iterator iter(*this);
391 bool RCSResourceAttributes::iterator::operator==(const iterator& rhs) const
393 return m_cur == rhs.m_cur;
396 bool RCSResourceAttributes::iterator::operator!=(const iterator& rhs) const
398 return !(*this == rhs);
402 RCSResourceAttributes::const_iterator::const_iterator() :
403 m_cur{ base_iterator{} }, m_keyValuePair{ this }
407 RCSResourceAttributes::const_iterator::const_iterator(base_iterator&& iter) :
408 m_cur{ iter }, m_keyValuePair{ this }
412 RCSResourceAttributes::const_iterator::const_iterator(
413 const RCSResourceAttributes::iterator& iter) :
414 m_cur{ iter.m_cur }, m_keyValuePair{ this }
418 auto RCSResourceAttributes::const_iterator::operator=(const RCSResourceAttributes::iterator& iter)
424 auto RCSResourceAttributes::const_iterator::operator*() const -> reference
426 return m_keyValuePair;
428 auto RCSResourceAttributes::const_iterator::operator->() const -> pointer
430 return &m_keyValuePair;
433 auto RCSResourceAttributes::const_iterator::operator++() -> const_iterator&
439 auto RCSResourceAttributes::const_iterator::operator++(int) -> const_iterator
441 const_iterator iter(*this);
446 bool RCSResourceAttributes::const_iterator::operator==(const const_iterator& rhs) const
448 return m_cur == rhs.m_cur;
451 bool RCSResourceAttributes::const_iterator::operator!=(const const_iterator& rhs) const
453 return !(*this == rhs);
456 auto RCSResourceAttributes::begin() -> iterator
458 return iterator{ m_values.begin() };
461 auto RCSResourceAttributes::end() -> iterator
463 return iterator{ m_values.end() };
466 auto RCSResourceAttributes::begin() const -> const_iterator
468 return const_iterator{ m_values.begin() };
471 auto RCSResourceAttributes::end() const -> const_iterator
473 return const_iterator{ m_values.end() };
476 auto RCSResourceAttributes::cbegin() const -> const_iterator
478 return const_iterator{ m_values.begin() };
481 auto RCSResourceAttributes::cend() const -> const_iterator
483 return const_iterator{ m_values.end() };
486 auto RCSResourceAttributes::operator[](const std::string& key) -> Value&
488 return m_values[key];
491 auto RCSResourceAttributes::operator[](std::string&& key) -> Value&
493 return m_values[std::move(key)];
496 auto RCSResourceAttributes::at(const std::string& key) -> Value&
500 return m_values.at(key);
502 catch (const std::out_of_range&)
504 throw InvalidKeyException{ "No attribute named '" + key + "'" };
508 auto RCSResourceAttributes::at(const std::string& key) const -> const Value&
512 return m_values.at(key);
514 catch (const std::out_of_range&)
516 throw InvalidKeyException{ "No attribute named '" + key + "'" };
520 void RCSResourceAttributes::clear()
522 return m_values.clear();
525 bool RCSResourceAttributes::erase(const std::string& key)
527 return m_values.erase(key) == 1U;
530 bool RCSResourceAttributes::contains(const std::string& key) const
532 return m_values.find(key) != m_values.end();
535 bool RCSResourceAttributes::empty() const
537 return m_values.empty();
540 size_t RCSResourceAttributes::size() const
542 return m_values.size();
546 bool acceptableAttributeValue(const RCSResourceAttributes::Value& dest,
547 const RCSResourceAttributes::Value& value)
549 if (dest.getType() != value.getType())
554 static_assert(RCSResourceAttributes::is_supported_type< RCSResourceAttributes >::value,
555 "RCSResourceAttributes doesn't have RCSResourceAttributes recursively.");
556 if (dest.getType().getId() == RCSResourceAttributes::TypeId::ATTRIBUTES
557 && !acceptableAttributes(dest.get< RCSResourceAttributes >(),
558 value.get< RCSResourceAttributes >()))
566 bool acceptableAttributes(const RCSResourceAttributes& dest, const RCSResourceAttributes& attr)
568 for (const auto& kv : attr)
570 if (!dest.contains(kv.key()))
575 if (!acceptableAttributeValue(dest.at(kv.key()), kv.value()))
584 AttrKeyValuePairs replaceAttributes(RCSResourceAttributes& dest,
585 const RCSResourceAttributes& newAttrs)
587 AttrKeyValuePairs replacedList;
589 for (const auto& kv : newAttrs)
591 if (dest[kv.key()] != kv.value())
593 RCSResourceAttributes::Value replacedValue;
594 replacedValue.swap(dest[kv.key()]);
595 dest[kv.key()] = kv.value();
597 replacedList.push_back(AttrKeyValuePair{ kv.key(), std::move(replacedValue) });