replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / common / primitiveResource / src / RCSResourceAttributes.cpp
index cb339ec..75e9d56 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,64 @@ 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)
+        {
+            m_stream << "[";
+            for (auto it = v.begin(); it != v.end(); ++it)
+            {
+                if (it != v.begin()) m_stream << ", ";
+                (*this)(*it);
+            }
+            m_stream << "]";
+        }
+
+        void operator()(std::nullptr_t)
         {
-            return "Vector";
+            m_stream << "";
         }
 
-        std::string operator()(std::nullptr_t) const
+        void operator()(bool value)
         {
-            return "";
+            m_stream << (value ? "true" : "false");
         }
 
-        std::string operator()(bool value) const
+        void operator()(const std::string& value)
         {
-            return value ? "true" : "false";
+            m_stream << "\"" + value + "\"";
         }
 
-        std::string operator()(const std::string& value) const
+        void operator()(const RCSByteString& value)
         {
-            return value;
+            for (size_t i = 0; i < value.size(); ++i)
+            {
+                m_stream << "\\x" << std::hex << (int)value[i];
+            }
         }
 
-        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 >
@@ -134,6 +163,13 @@ namespace
     };
 
     template< >
+    struct TypeInfoConverter< RCSByteString >
+    {
+        static constexpr RCSResourceAttributes::TypeId typeId =
+                RCSResourceAttributes::TypeId::BYTESTRING;
+    };
+
+    template< >
     struct TypeInfoConverter< RCSResourceAttributes >
     {
         static constexpr RCSResourceAttributes::TypeId typeId =
@@ -185,7 +221,7 @@ namespace
     };
 
     template< typename VARIANT, int POS >
-    constexpr inline std::vector< TypeInfo > getTypeInfo(Int2Type< POS >) noexcept
+    inline std::vector< TypeInfo > getTypeInfo(Int2Type< POS >) noexcept
     {
         auto vec = getTypeInfo< VARIANT >(Int2Type< POS - 1 >{ });
         vec.push_back(TypeInfo::get< VARIANT, POS >());
@@ -193,7 +229,7 @@ namespace
     }
 
     template< typename VARIANT >
-    constexpr inline std::vector< TypeInfo > getTypeInfo(Int2Type< 0 >) noexcept
+    inline std::vector< TypeInfo > getTypeInfo(Int2Type< 0 >) noexcept
     {
         return { TypeInfo::get< VARIANT, 0 >() };
     }
@@ -210,7 +246,6 @@ namespace
 
         return typeInfos[which];
     }
-
 } // unnamed namespace
 
 
@@ -218,7 +253,6 @@ namespace OIC
 {
     namespace Service
     {
-
         RCSResourceAttributes::Value::ComparisonHelper::ComparisonHelper(const Value& v) :
                 m_valueRef(v)
         {
@@ -333,7 +367,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
@@ -406,6 +442,18 @@ namespace OIC
         {
         }
 
+        RCSResourceAttributes::iterator::iterator(const iterator& rhs) :
+                m_cur{ rhs.m_cur },
+                m_keyValuePair{ this }
+        {
+        }
+
+        auto RCSResourceAttributes::iterator::operator=(const iterator& rhs) -> iterator&
+        {
+            m_cur = rhs.m_cur;
+            return *this;
+        }
+
         RCSResourceAttributes::iterator::iterator(base_iterator&& iter) :
                 m_cur{ std::move(iter) },
                 m_keyValuePair{ this }
@@ -456,6 +504,11 @@ namespace OIC
         {
         }
 
+        RCSResourceAttributes::const_iterator::const_iterator(const const_iterator& rhs) :
+                m_cur{ rhs.m_cur }, m_keyValuePair{ this }
+        {
+        }
+
         RCSResourceAttributes::const_iterator::const_iterator(
                 const RCSResourceAttributes::iterator& iter) :
                 m_cur{ iter.m_cur }, m_keyValuePair{ this }
@@ -463,6 +516,13 @@ namespace OIC
         }
 
         auto RCSResourceAttributes::const_iterator::operator=(
+                const const_iterator& rhs) -> const_iterator&
+        {
+            m_cur = rhs.m_cur;
+            return *this;
+        }
+
+        auto RCSResourceAttributes::const_iterator::operator=(
                 const RCSResourceAttributes::iterator& iter) -> const_iterator&
         {
             m_cur = iter.m_cur;
@@ -577,6 +637,11 @@ namespace OIC
             return m_values.erase(key) == 1U;
         }
 
+        auto RCSResourceAttributes::erase(const_iterator pos) -> iterator
+        {
+            return iterator{ m_values.erase(pos.m_cur) };
+        }
+
         bool RCSResourceAttributes::contains(const std::string& key) const
         {
             return m_values.find(key) != m_values.end();