From: coderhyme Date: Mon, 15 Jun 2015 11:00:24 +0000 (+0900) Subject: Add util methods for ResourceAttributes X-Git-Tag: 1.2.0+RC1~1430^2~153 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cd42cac968ef60267b6b940ada48e3523b64a9d2;p=platform%2Fupstream%2Fiotivity.git Add util methods for ResourceAttributes Change-Id: Id8747dc53e439086c123eb7e34d53c1130cd8fcb Signed-off-by: coderhyme Reviewed-on: https://gerrit.iotivity.org/gerrit/1274 Tested-by: jenkins-iotivity Reviewed-by: Hun-je Yeon Reviewed-by: JungYong KIM Reviewed-by: Uze Choi --- diff --git a/service/basis/common/primitiveResource/include/ResourceAttributes.h b/service/basis/common/primitiveResource/include/ResourceAttributes.h index 6a522e0..6ebb392 100755 --- a/service/basis/common/primitiveResource/include/ResourceAttributes.h +++ b/service/basis/common/primitiveResource/include/ResourceAttributes.h @@ -143,6 +143,8 @@ public: return false; } + std::string toString() const; + friend bool operator==(const Value&, const Value&); template< typename T > diff --git a/service/basis/common/primitiveResource/include/internal/ResourceAttributesUtils.h b/service/basis/common/primitiveResource/include/internal/ResourceAttributesUtils.h new file mode 100644 index 0000000..cd0618b --- /dev/null +++ b/service/basis/common/primitiveResource/include/internal/ResourceAttributesUtils.h @@ -0,0 +1,47 @@ +//****************************************************************** +// +// Copyright 2015 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +/* + * ResourceAttributesUtils.h + * + * Created on: 2015. 6. 11. + * Author: jhyo.kim + */ + +#ifndef __RESOURCEATTRIBUTESUTILS_H +#define __RESOURCEATTRIBUTESUTILS_H + +namespace OIC +{ + namespace Service + { + bool acceptableAttributes(const ResourceAttributes& dest, const ResourceAttributes& attr); + + bool acceptableAttributeValue(const ResourceAttributes::Value& dest, + const ResourceAttributes::Value& value); + + void replaceAttributesRecursively(ResourceAttributes& dest, const ResourceAttributes& attr); + + void replaceAttributeValueRecursively(ResourceAttributes::Value& dest, + const ResourceAttributes::Value& value); + } +} + +#endif // __RESOURCEATTRIBUTESUTILS_H diff --git a/service/basis/common/primitiveResource/src/ResourceAttributes.cpp b/service/basis/common/primitiveResource/src/ResourceAttributes.cpp index 31c5952..1d6c565 100755 --- a/service/basis/common/primitiveResource/src/ResourceAttributes.cpp +++ b/service/basis/common/primitiveResource/src/ResourceAttributes.cpp @@ -20,8 +20,48 @@ #include +#include #include +#include + +class ToStringVisitor : public boost::static_visitor +{ +public: + ToStringVisitor() = default; + ToStringVisitor(const ToStringVisitor&) = delete; + ToStringVisitor(ToStringVisitor&&) = delete; + + ToStringVisitor& operator=(const ToStringVisitor&) = delete; + ToStringVisitor& operator=(ToStringVisitor&&) = delete; + + template < typename T > + std::string operator()(const T& value) const + { + return boost::lexical_cast(value); + } + + std::string operator()(std::nullptr_t) const + { + return ""; + } + + std::string operator()(bool value) const + { + return value ? "true" : "false"; + } + + std::string operator()(const std::string& value) const + { + return value; + } + + std::string operator()(const ResourceAttributes&) const + { + return "Attributes"; + } +}; + bool operator==(const char* lhs, const ResourceAttributes::Value& rhs) { return rhs == lhs; @@ -56,7 +96,7 @@ ResourceAttributes::Value::Value(Value&& from) : auto ResourceAttributes::Value::operator=(const Value& rhs) -> Value& { - *m_data = new ValueVariant{ *rhs.m_data }; + *m_data = *rhs.m_data; return *this; } @@ -77,10 +117,10 @@ bool ResourceAttributes::Value::operator==(const char* rhs) const return equals< std::string >(rhs); } -//bool ResourceAttributes::Value::operator==(std::nullptr_t) const -//{ -// return isTypeOf< std::nullptr_t >(); -//} +std::string ResourceAttributes::Value::toString() const +{ + return boost::apply_visitor(ToStringVisitor(), *m_data); +} auto ResourceAttributes::KeyValuePair::KeyVisitor::operator() (iterator* iter) const -> result_type { @@ -252,12 +292,16 @@ auto ResourceAttributes::end() -> iterator return iterator{ m_keyValues.end() }; } - auto ResourceAttributes::begin() const -> const_iterator { return const_iterator{ m_keyValues.begin() }; } +auto ResourceAttributes::end() const -> const_iterator +{ + return const_iterator{ m_keyValues.end() }; +} + auto ResourceAttributes::cbegin() const -> const_iterator { return const_iterator{ m_keyValues.begin() }; @@ -285,6 +329,18 @@ auto ResourceAttributes::at(const std::string& key) -> Value& } } +auto ResourceAttributes::at(const std::string& key) const -> const Value& +{ + try + { + return m_keyValues.at(key); + } + catch (const std::out_of_range&) + { + throw InvalidKeyException{ "" }; + } +} + bool ResourceAttributes::erase(const std::string& key) { return m_keyValues.erase(key) == 1U; @@ -305,3 +361,72 @@ size_t ResourceAttributes::size() const return m_keyValues.size(); } +namespace OIC +{ + namespace Service + { + bool acceptableAttributeValue(const ResourceAttributes::Value& dest, + const ResourceAttributes::Value& value) + { + if (!dest.isTypeEqualWith(value)) + { + return false; + } + + static_assert(ResourceAttributes::is_supported_type< ResourceAttributes >::value, + "ResourceAttributes doesn't have ResourceAttributes recursively."); + + if (dest.isTypeOf< ResourceAttributes >() + && !acceptableAttributes(dest.get< ResourceAttributes >(), + value.get< ResourceAttributes >())) + { + return false; + } + + return true; + } + + bool acceptableAttributes(const ResourceAttributes& dest, const ResourceAttributes& attr) + { + for (const auto& kv : attr) + { + if (!dest.contains(kv.key())) + { + return false; + } + + if (!acceptableAttributeValue(dest.at(kv.key()), kv.value())) + { + return false; + } + } + + return true; + } + + void replaceAttributeValueRecursively(ResourceAttributes::Value& dest, + const ResourceAttributes::Value& value) + { + static_assert(ResourceAttributes::is_supported_type< ResourceAttributes >::value, + "ResourceAttributes doesn't have ResourceAttributes recursively."); + + if (dest.isTypeOf< ResourceAttributes >()) + { + replaceAttributesRecursively(dest.get< ResourceAttributes >(), + value.get< ResourceAttributes >()); + } + else + { + dest = value; + } + } + + void replaceAttributesRecursively(ResourceAttributes& dest, const ResourceAttributes& attr) + { + for (const auto& kv : attr) + { + replaceAttributeValueRecursively(dest[kv.key()], kv.value()); + } + } + } +}