--- /dev/null
+//******************************************************************
+//
+// 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
#include <ResourceAttributes.h>
+#include <internal/ResourceAttributesUtils.h>
#include <internal/ResourceAtrributesConverter.h>
+#include <boost/lexical_cast.hpp>
+
+class ToStringVisitor : public boost::static_visitor<std::string>
+{
+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<std::string>(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;
auto ResourceAttributes::Value::operator=(const Value& rhs) -> Value&
{
- *m_data = new ValueVariant{ *rhs.m_data };
+ *m_data = *rhs.m_data;
return *this;
}
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 {
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() };
}
}
+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;
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());
+ }
+ }
+ }
+}