Refine toString method of RCSResourceAttributes
authorcoderhyme <jhyo.kim@samsung.com>
Mon, 19 Oct 2015 01:55:48 +0000 (18:55 -0700)
committerMadan Lanka <lanka.madan@samsung.com>
Mon, 19 Oct 2015 08:33:35 +0000 (08:33 +0000)
Nested values were not corretly encoded, they were just described with their type names.
Now it encodes every attribute value including nested olnes.

Change-Id: Ia228a34e6df05ac05c509012afd7f026c0f253e0
Signed-off-by: coderhyme <jhyo.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/3907
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
service/resource-encapsulation/include/RCSResourceAttributes.h
service/resource-encapsulation/src/common/primitiveResource/src/RCSResourceAttributes.cpp

index 063a71f..645c228 100644 (file)
 #include <unordered_map>
 #include <vector>
 
-#include <boost/variant.hpp>
-#include <boost/mpl/contains.hpp>
-#include <boost/mpl/find.hpp>
-#include <boost/mpl/distance.hpp>
-#include <boost/mpl/begin_end.hpp>
-#include <boost/scoped_ptr.hpp>
+#include "boost/variant.hpp"
+#include "boost/mpl/contains.hpp"
+#include "boost/mpl/find.hpp"
+#include "boost/mpl/distance.hpp"
+#include "boost/mpl/begin_end.hpp"
+#include "boost/scoped_ptr.hpp"
 
-#include <RCSException.h>
+#include "RCSException.h"
 
 namespace OIC
 {
@@ -582,6 +582,9 @@ namespace OIC
         public:
             ComparisonHelper(const Value&);
 
+            ComparisonHelper(const ComparisonHelper&) = delete;
+            ComparisonHelper& operator=(const ComparisonHelper&) = delete;
+
             template< typename T >
             typename std::enable_if< is_supported_type< T >::value, bool >::type equals(
                     const T& v) const
index cb339ec..a9d4cae 100644 (file)
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
-#include <RCSResourceAttributes.h>
+#include "RCSResourceAttributes.h"
 
-#include <ResourceAttributesUtils.h>
-#include <ResourceAttributesConverter.h>
+#include <sstream>
 
-#include <boost/lexical_cast.hpp>
-#include <boost/mpl/advance.hpp>
-#include <boost/mpl/size.hpp>
-#include <boost/mpl/deref.hpp>
+#include "ResourceAttributesUtils.h"
+#include "ResourceAttributesConverter.h"
+
+#include "boost/lexical_cast.hpp"
+#include "boost/mpl/advance.hpp"
+#include "boost/mpl/size.hpp"
+#include "boost/mpl/deref.hpp"
 
 namespace
 {
 
     using namespace OIC::Service;
 
-    class ToStringVisitor: public boost::static_visitor< std::string >
+    class ToStringVisitor: public boost::static_visitor<>
     {
     public:
         ToStringVisitor() = default;
@@ -43,37 +45,56 @@ namespace
         ToStringVisitor& operator=(const ToStringVisitor&) = delete;
         ToStringVisitor& operator=(ToStringVisitor&&) = delete;
 
-        template < typename T >
-        std::string operator()(const T& value) const
+        template< typename T >
+        void operator()(const T& value)
         {
-            return boost::lexical_cast<std::string>(value);
+            m_stream << boost::lexical_cast< std::string >(value);
         }
 
         template< typename T >
-        std::string operator()(const std::vector< T >&) const
+        void operator()(const std::vector< T >& v)
         {
-            return "Vector";
+            m_stream << "[";
+            for (auto it = v.begin(); it != v.end(); ++it)
+            {
+                if (it != v.begin()) m_stream << ", ";
+                (*this)(*it);
+            }
+            m_stream << "]";
         }
 
-        std::string operator()(std::nullptr_t) const
+        void operator()(std::nullptr_t)
         {
-            return "";
+            m_stream << "";
         }
 
-        std::string operator()(bool value) const
+        void operator()(bool value)
         {
-            return value ? "true" : "false";
+            m_stream << (value ? "true" : "false");
         }
 
-        std::string operator()(const std::string& value) const
+        void operator()(const std::string& value)
         {
-            return value;
+            m_stream << "\"" + value + "\"";
         }
 
-        std::string operator()(const OIC::Service::RCSResourceAttributes&) const
+        void operator()(const RCSResourceAttributes& attrs)
         {
-            return "Attributes";
+            m_stream << "{";
+            for (auto it = attrs.begin(); it != attrs.end(); ++it)
+            {
+                if (it != attrs.begin()) m_stream << ", ";
+                m_stream << "\"" << it->key() << "\" : " << it->value().toString();
+            }
+            m_stream << "}";
+        }
+
+        std::string get() const {
+            return m_stream.str();
         }
+
+    private:
+        std::ostringstream m_stream;
     };
 
     class TypeVisitor: public boost::static_visitor< RCSResourceAttributes::Type >
@@ -333,7 +354,9 @@ namespace OIC
 
         std::string RCSResourceAttributes::Value::toString() const
         {
-            return boost::apply_visitor(ToStringVisitor(), *m_data);
+            ToStringVisitor visitor;
+            boost::apply_visitor(visitor, *m_data);
+            return visitor.get();
         }
 
         void RCSResourceAttributes::Value::swap(Value& rhs) noexcept