From 9a66fb46498e3055ea09bd01a7f3ea3fb8441f7d Mon Sep 17 00:00:00 2001 From: coderhyme Date: Fri, 4 Mar 2016 00:45:47 -0800 Subject: [PATCH] Add new operations to RCSResourceAttributes A new public function, erase with iterator, is added. This is fundamental operation for using iterator. A new feature is added, which is a new visitor with rvalue. Change-Id: If2d5db51badfc3821f3b74a547c1f7251fb39126 Signed-off-by: coderhyme Reviewed-on: https://gerrit.iotivity.org/gerrit/5419 Tested-by: jenkins-iotivity Reviewed-by: Uze Choi --- .../include/RCSResourceAttributes.h | 45 +++- .../src/RCSResourceAttributes.cpp | 29 +++ .../src/serverBuilder/src/InterfaceHandler.cpp | 282 ++++++++++----------- 3 files changed, 207 insertions(+), 149 deletions(-) diff --git a/service/resource-encapsulation/include/RCSResourceAttributes.h b/service/resource-encapsulation/include/RCSResourceAttributes.h index 645c228..2901d78 100644 --- a/service/resource-encapsulation/include/RCSResourceAttributes.h +++ b/service/resource-encapsulation/include/RCSResourceAttributes.h @@ -109,7 +109,7 @@ namespace OIC typedef V type; }; - template< typename VISITOR > + template< typename VISITOR, typename MOVE = std::false_type > class KeyValueVisitorHelper: public boost::static_visitor< > { public: @@ -118,12 +118,20 @@ namespace OIC { } - template< typename T > - void operator()(const std::string& key, const T& value) const + template< typename T, typename M = MOVE > + typename std::enable_if< std::is_same< M, std::false_type >::value >::type + operator()(const std::string& key, const T& value) const { m_visitor(key, value); } + template< typename T, typename M = MOVE > + typename std::enable_if< std::is_same< M, std::true_type >::value >::type + operator()(const std::string& key, T& value) + { + m_visitor(key, std::move(value)); + } + private: VISITOR& m_visitor; }; @@ -523,6 +531,15 @@ namespace OIC bool erase(const std::string& key); /** + * Removes a single element. + * + * @param pos Iterator to the element to remove. + * + * @return Iterator following the last removed element. + */ + iterator erase(const_iterator pos); + + /** * Checks this contains an element for the specified key. * * @param key Key to check. @@ -558,6 +575,18 @@ namespace OIC } } + template< typename VISITOR > + void visitToMove(VISITOR& visitor) + { + KeyValueVisitorHelper< VISITOR, std::true_type > helper{ visitor }; + + for (auto& i : m_values) + { + boost::variant< const std::string& > key{ i.first }; + boost::apply_visitor(helper, key, *i.second.m_data); + } + } + private: std::unordered_map< std::string, Value > m_values; @@ -791,9 +820,9 @@ namespace OIC public: iterator(); - iterator(const iterator&) = default; + iterator(const iterator&); - iterator& operator=(const iterator&) = default; + iterator& operator=(const iterator&); reference operator*(); pointer operator->(); @@ -825,7 +854,7 @@ namespace OIC * @see iterator */ class RCSResourceAttributes::const_iterator: - public std::iterator < std::forward_iterator_tag, + public std::iterator< std::forward_iterator_tag, const RCSResourceAttributes::KeyValuePair > { private: @@ -833,10 +862,10 @@ namespace OIC public: const_iterator(); - const_iterator(const const_iterator&) = default; + const_iterator(const const_iterator&); const_iterator(const RCSResourceAttributes::iterator&); - const_iterator& operator=(const const_iterator&) = default; + const_iterator& operator=(const const_iterator&); const_iterator& operator=(const RCSResourceAttributes::iterator&); reference operator*() const; diff --git a/service/resource-encapsulation/src/common/primitiveResource/src/RCSResourceAttributes.cpp b/service/resource-encapsulation/src/common/primitiveResource/src/RCSResourceAttributes.cpp index a9d4cae..e96e7cc 100644 --- a/service/resource-encapsulation/src/common/primitiveResource/src/RCSResourceAttributes.cpp +++ b/service/resource-encapsulation/src/common/primitiveResource/src/RCSResourceAttributes.cpp @@ -429,6 +429,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 } @@ -479,6 +491,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 } @@ -486,6 +503,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; @@ -600,6 +624,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(); diff --git a/service/resource-encapsulation/src/serverBuilder/src/InterfaceHandler.cpp b/service/resource-encapsulation/src/serverBuilder/src/InterfaceHandler.cpp index 1825246..14287c4 100644 --- a/service/resource-encapsulation/src/serverBuilder/src/InterfaceHandler.cpp +++ b/service/resource-encapsulation/src/serverBuilder/src/InterfaceHandler.cpp @@ -1,141 +1,141 @@ -#include "InterfaceHandler.h" - -#include "OCResourceRequest.h" - -#include "RCSRepresentation.h" -#include "RCSRequest.h" -#include "RCSResourceObject.h" -#include "RCSResourceAttributes.h" -#include "ResourceAttributesConverter.h" - -#include - -namespace -{ - using namespace OIC::Service; - - RCSRepresentation toRepresentation(const RCSResourceObject& resource) - { - RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER }; - - return RCSRepresentation{ resource.getUri(), resource.getInterfaces(), resource.getTypes(), - resource.getAttributes()}; - } - - RCSRepresentation buildGetBaselineResponse(const RCSRequest&, const RCSResourceObject& resource) - { - return toRepresentation(resource); - } - - RCSRepresentation buildSetBaselineResponse(const RCSRequest& rcsRequest, - const RCSResourceObject& resource) - { - return buildGetBaselineResponse(rcsRequest, resource); - } - - RCSRepresentation buildGetRequestResponse(const RCSRequest&, const RCSResourceObject& resource) - { - RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER }; - - return RCSRepresentation{ resource.getAttributes() }; - } - - RCSRepresentation buildSetRequestResponse(const RCSRequest& rcsRequest, - const RCSResourceObject& resource) - { - auto requestAttr = ResourceAttributesConverter::fromOCRepresentation( - (rcsRequest.getOCRequest())->getResourceRepresentation()); - - RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER }; - - const RCSResourceAttributes& updatedAttr = resource.getAttributes(); - - for (auto& kvPair : requestAttr) - { - if(updatedAttr.contains(kvPair.key())) - { - kvPair.value() = updatedAttr.at(kvPair.key()); - } - else - { - //FIXME erase item with iterator. - requestAttr.erase(kvPair.key()); - } - } - - return RCSRepresentation{ requestAttr }; - } - - RCSRepresentation buildGetBatchResponse(RCSRequest, const RCSResourceObject& resource) - { - RCSRepresentation rcsRep; - - for (const auto& bound : resource.getBoundResources()) - { - rcsRep.addChild(toRepresentation(*bound)); - } - - return rcsRep; - } - - const std::unordered_map< std::string, InterfaceHandler > g_defaultHandlers { - - { BASELINE_INTERFACE, - InterfaceHandler(buildGetBaselineResponse, buildSetBaselineResponse) }, - - { ACTUATOR_INTERFACE, - InterfaceHandler(buildGetRequestResponse, buildSetRequestResponse) }, - - { SENSOR_INTERFACE, - InterfaceHandler(buildGetRequestResponse, nullptr) }, - - { BATCH_INTERFACE, - InterfaceHandler(buildGetBatchResponse, buildSetBaselineResponse) } - }; -} - -namespace OIC -{ - namespace Service - { - - InterfaceHandler::InterfaceHandler(GetResponseBuilder getBuilder, - SetResponseBuilder setBuilder) : - m_getBuilder{ std::move(getBuilder) }, - m_setBuilder{ std::move(setBuilder) } - { - } - - bool InterfaceHandler::isGetSupported() const - { - return m_getBuilder != nullptr; - } - - bool InterfaceHandler::isSetSupported() const - { - return m_setBuilder != nullptr; - } - - InterfaceHandler::GetResponseBuilder InterfaceHandler::getGetResponseBuilder() const - { - return m_getBuilder; - } - - InterfaceHandler::SetResponseBuilder InterfaceHandler::getSetResponseBuilder() const - { - return m_setBuilder; - } - - InterfaceHandler getDefaultInterfaceHandler(const std::string& interfaceName, - const std::string& defaultInterfaceName) - { - auto it = g_defaultHandlers.find(interfaceName); - if (it != g_defaultHandlers.end()) return it->second; - - it = g_defaultHandlers.find(defaultInterfaceName); - if (it != g_defaultHandlers.end()) return it->second; - - return g_defaultHandlers.find(OIC::Service::BASELINE_INTERFACE)->second; - } - } -} +#include "InterfaceHandler.h" + +#include "OCResourceRequest.h" + +#include "RCSRepresentation.h" +#include "RCSRequest.h" +#include "RCSResourceObject.h" +#include "RCSResourceAttributes.h" +#include "ResourceAttributesConverter.h" + +#include + +namespace +{ + using namespace OIC::Service; + + RCSRepresentation toRepresentation(const RCSResourceObject& resource) + { + RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER }; + + return RCSRepresentation{ resource.getUri(), resource.getInterfaces(), resource.getTypes(), + resource.getAttributes() }; + } + + RCSRepresentation buildGetBaselineResponse(const RCSRequest&, const RCSResourceObject& resource) + { + return toRepresentation(resource); + } + + RCSRepresentation buildSetBaselineResponse(const RCSRequest& rcsRequest, + const RCSResourceObject& resource) + { + return buildGetBaselineResponse(rcsRequest, resource); + } + + RCSRepresentation buildGetRequestResponse(const RCSRequest&, const RCSResourceObject& resource) + { + RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER }; + + return RCSRepresentation{ resource.getAttributes() }; + } + + RCSRepresentation buildSetRequestResponse(const RCSRequest& rcsRequest, + const RCSResourceObject& resource) + { + auto requestAttr = ResourceAttributesConverter::fromOCRepresentation( + rcsRequest.getOCRequest()->getResourceRepresentation()); + + RCSResourceObject::LockGuard lock{ resource, RCSResourceObject::AutoNotifyPolicy::NEVER }; + + const RCSResourceAttributes& updatedAttr = resource.getAttributes(); + + for (auto it = requestAttr.begin(); it != requestAttr.end();) + { + if(updatedAttr.contains(it->key())) + { + it->value() = updatedAttr.at(it->key()); + ++it; + } + else + { + it = requestAttr.erase(it); + } + } + + return RCSRepresentation{ requestAttr }; + } + + RCSRepresentation buildGetBatchResponse(RCSRequest, const RCSResourceObject& resource) + { + RCSRepresentation rcsRep; + + for (const auto& bound : resource.getBoundResources()) + { + rcsRep.addChild(toRepresentation(*bound)); + } + + return rcsRep; + } + + const std::unordered_map< std::string, InterfaceHandler > g_defaultHandlers { + + { BASELINE_INTERFACE, + InterfaceHandler(buildGetBaselineResponse, buildSetBaselineResponse) }, + + { ACTUATOR_INTERFACE, + InterfaceHandler(buildGetRequestResponse, buildSetRequestResponse) }, + + { SENSOR_INTERFACE, + InterfaceHandler(buildGetRequestResponse, nullptr) }, + + { BATCH_INTERFACE, + InterfaceHandler(buildGetBatchResponse, buildSetBaselineResponse) } + }; +} + +namespace OIC +{ + namespace Service + { + + InterfaceHandler::InterfaceHandler(GetResponseBuilder getBuilder, + SetResponseBuilder setBuilder) : + m_getBuilder{ std::move(getBuilder) }, + m_setBuilder{ std::move(setBuilder) } + { + } + + bool InterfaceHandler::isGetSupported() const + { + return m_getBuilder != nullptr; + } + + bool InterfaceHandler::isSetSupported() const + { + return m_setBuilder != nullptr; + } + + InterfaceHandler::GetResponseBuilder InterfaceHandler::getGetResponseBuilder() const + { + return m_getBuilder; + } + + InterfaceHandler::SetResponseBuilder InterfaceHandler::getSetResponseBuilder() const + { + return m_setBuilder; + } + + InterfaceHandler getDefaultInterfaceHandler(const std::string& interfaceName, + const std::string& defaultInterfaceName) + { + auto it = g_defaultHandlers.find(interfaceName); + if (it != g_defaultHandlers.end()) return it->second; + + it = g_defaultHandlers.find(defaultInterfaceName); + if (it != g_defaultHandlers.end()) return it->second; + + return g_defaultHandlers.find(OIC::Service::BASELINE_INTERFACE)->second; + } + } +} -- 2.7.4