Replace setAttribute with non-template methods.
authorcoderhyme <jhyo.kim@samsung.com>
Sat, 4 Jul 2015 07:51:07 +0000 (16:51 +0900)
committerUze Choi <uzchoi@samsung.com>
Tue, 7 Jul 2015 09:08:11 +0000 (09:08 +0000)
To extract a common interface that manipulates attributes.
And add invokeOCFunc that takes multiple OCStackResult values considered as success.

Change-Id: I39fa49525e95739b7fdbbc06384782453e156f65
Signed-off-by: coderhyme <jhyo.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1548
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/resource-manipulation/modules/common/primitiveResource/include/internal/AssertUtils.h
service/resource-manipulation/modules/serverBuilder/include/ResourceObject.h
service/resource-manipulation/modules/serverBuilder/src/ResourceObject.cpp

index eccc51e..f1df6bd 100644 (file)
@@ -76,6 +76,17 @@ namespace OIC
              };
         }
 
+        inline void expectOCStackResult(OCStackResult actual,
+                std::initializer_list<OCStackResult> allowed)
+        {
+            for (auto r : allowed)
+            {
+                if (actual == r) return;
+            }
+
+            throw PlatformException(actual);
+        }
+
         inline void expectOCStackResult(OCStackResult actual, OCStackResult expected)
         {
             if (actual != expected)
@@ -92,13 +103,30 @@ namespace OIC
         template< typename FUNC, typename ...PARAMS >
         typename Detail::EnableIfTypeIs< typename Detail::ResultType< FUNC, PARAMS... >::type,
                 OCStackResult >::type
+        invokeOCFuncWithResultExpect(std::initializer_list<OCStackResult> allowed,
+                FUNC&& fn, PARAMS&& ...params)
+        {
+            try
+            {
+                expectOCStackResult(fn(std::forward< PARAMS >(params)...), std::move(allowed));
+            }
+            catch (const OC::OCException& e)
+            {
+                throw PlatformException(e.code());
+            }
+        }
+
+
+        template< typename FUNC, typename ...PARAMS >
+        typename Detail::EnableIfTypeIs< typename Detail::ResultType< FUNC, PARAMS... >::type,
+                OCStackResult >::type
         invokeOCFunc(FUNC&& fn, PARAMS&& ...params)
         {
             try
             {
                 expectOCStackResultOK(fn(std::forward< PARAMS >(params)...));
             }
-            catch (OC::OCException& e)
+            catch (const OC::OCException& e)
             {
                 throw PlatformException(e.code());
             }
@@ -107,13 +135,13 @@ namespace OIC
         template< typename FUNC, typename ...PARAMS >
         typename Detail::EnableIfTypeIs< typename Detail::ResultType< FUNC, PARAMS... >::type,
                         Detail::NotOCStackResult >::type
-        invokeOC(FUNC* fn, PARAMS&& ...params)
+        invokeOCFunc(FUNC* fn, PARAMS&& ...params)
         {
             try
             {
                 return fn(std::forward< PARAMS >(params)...);
             }
-            catch (OC::OCException& e)
+            catch (const OC::OCException& e)
             {
                 throw PlatformException(e.code());
             }
@@ -130,7 +158,7 @@ namespace OIC
             {
                 expectOCStackResultOK(obj->*fn(std::forward< PARAMS >(params)...));
             }
-            catch (OC::OCException& e)
+            catch (const OC::OCException& e)
             {
                 throw PlatformException(e.code());
             }
@@ -148,7 +176,7 @@ namespace OIC
             {
                 obj->*fn(std::forward< PARAMS >(params)...);
             }
-            catch (OC::OCException& e)
+            catch (const OC::OCException& e)
             {
                 throw PlatformException(e.code());
             }
@@ -165,7 +193,7 @@ namespace OIC
             {
                 expectOCStackResultOK((obj.get()->*fn)(std::forward< PARAMS >(params)...));
             }
-            catch (OC::OCException& e)
+            catch (const OC::OCException& e)
             {
                 throw PlatformException(e.code());
             }
@@ -183,7 +211,7 @@ namespace OIC
             {
                 return (obj.get()->*fn)(std::forward< PARAMS >(params)...);
             }
-            catch (OC::OCException& e)
+            catch (const OC::OCException& e)
             {
                 throw PlatformException(e.code());
             }
index 61382a3..fbb0414 100644 (file)
@@ -110,19 +110,13 @@ namespace OIC
 
             virtual ~ResourceObject();
 
-            template< typename T >
-            void setAttribute(const std::string& key, T&& value)
-            {
-                WeakGuard lock(*this);
-                m_resourceAttributes[key] = std::forward<T>(value);
-            }
+            void setAttribute(const std::string& key, const ResourceAttributes::Value&);
+            void setAttribute(const std::string& key, ResourceAttributes::Value&&);
 
-            template< typename T >
-            void setAttribute(std::string&& key, T&& value)
-            {
-                WeakGuard lock(*this);
-                m_resourceAttributes[std::move(key)] = std::forward<T>(value);
-            }
+            void setAttribute(std::string&& key, const ResourceAttributes::Value&);
+            void setAttribute(std::string&& key, ResourceAttributes::Value&&);
+
+            ResourceAttributes::Value getAttributeValue(const std::string& key) const;
 
             template< typename T >
             T getAttribute(const std::string& key) const
@@ -133,7 +127,7 @@ namespace OIC
 
             bool removeAttribute(const std::string& key);
 
-            bool hasAttribute(const std::string& key) const;
+            bool containsAttribute(const std::string& key) const;
 
             ResourceAttributes& getAttributes();
             const ResourceAttributes& getAttributes() const;
index 342c751..30c3ba1 100644 (file)
@@ -196,13 +196,44 @@ namespace OIC
             }
         }
 
+        void ResourceObject::setAttribute(const std::string& key,
+                const ResourceAttributes::Value& value)
+        {
+            WeakGuard lock(*this);
+            m_resourceAttributes[key] = value;
+        }
+
+        void ResourceObject::setAttribute(const std::string& key, ResourceAttributes::Value&& value)
+        {
+            WeakGuard lock(*this);
+            m_resourceAttributes[key] = std::move(value);
+        }
+
+        void ResourceObject::setAttribute(std::string&& key, const ResourceAttributes::Value& value)
+        {
+            WeakGuard lock(*this);
+            m_resourceAttributes[std::move(key)] = value;
+        }
+
+        void ResourceObject::setAttribute(std::string&& key, ResourceAttributes::Value&& value)
+        {
+            WeakGuard lock(*this);
+            m_resourceAttributes[std::move(key)] = std::move(value);
+        }
+
+        ResourceAttributes::Value ResourceObject::getAttributeValue(const std::string& key) const
+        {
+            WeakGuard lock(*this);
+            return m_resourceAttributes.at(key);
+        }
+
         bool ResourceObject::removeAttribute(const std::string& key)
         {
             WeakGuard lock(*this);
             return m_resourceAttributes.erase(key);
         }
 
-        bool ResourceObject::hasAttribute(const std::string& key) const
+        bool ResourceObject::containsAttribute(const std::string& key) const
         {
             WeakGuard lock(*this);
             return m_resourceAttributes.contains(key);
@@ -252,7 +283,9 @@ namespace OIC
         {
             using NotifyAllObservers = OCStackResult (*)(OCResourceHandle);
 
-            invokeOCFunc(static_cast<NotifyAllObservers>(OC::OCPlatform::notifyAllObservers),
+            invokeOCFuncWithResultExpect(
+                    { OC_STACK_OK, OC_STACK_NO_OBSERVERS },
+                    static_cast< NotifyAllObservers >(OC::OCPlatform::notifyAllObservers),
                     m_resourceHandle);
         }