Add util methods for ResourceAttributes
authorcoderhyme <jhyo.kim@samsung.com>
Mon, 15 Jun 2015 11:00:24 +0000 (20:00 +0900)
committerUze Choi <uzchoi@samsung.com>
Wed, 17 Jun 2015 05:29:14 +0000 (05:29 +0000)
Change-Id: Id8747dc53e439086c123eb7e34d53c1130cd8fcb
Signed-off-by: coderhyme <jhyo.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1274
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Hun-je Yeon <hunje.yeon@samsung.com>
Reviewed-by: JungYong KIM <jyong2.kim@samsung.com>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/basis/common/primitiveResource/include/ResourceAttributes.h
service/basis/common/primitiveResource/include/internal/ResourceAttributesUtils.h [new file with mode: 0644]
service/basis/common/primitiveResource/src/ResourceAttributes.cpp

index 6a522e0..6ebb392 100755 (executable)
@@ -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 (file)
index 0000000..cd0618b
--- /dev/null
@@ -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
index 31c5952..1d6c565 100755 (executable)
 
 #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;
@@ -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());
+            }
+        }
+    }
+}