From 10105af33b4f51d235f295642bb6f274e6d083db Mon Sep 17 00:00:00 2001 From: Erich Keane Date: Fri, 5 Sep 2014 16:43:55 -0700 Subject: [PATCH] Gave typedefs to every use of std::function in the C++ stack. Only existing use of std::function< without a typedef is a comment in OCClientTestApp.cpp Patch 2: Sudarshan's review fixes Change-Id: I8c62347c1091a2225d5db1b0bda5221a3346586e --- examples/simpleclientserver.cpp | 8 +- include/IClientWrapper.h | 10 +- include/IServerWrapper.h | 2 +- include/InProcClientWrapper.h | 11 +- include/InProcServerWrapper.h | 2 +- include/OCApi.h | 215 ++++++++++++++++++++------------------- include/OCPlatform.h | 6 +- include/OCResource.h | 2 +- include/OutOfProcClientWrapper.h | 10 +- include/OutOfProcServerWrapper.h | 2 +- src/InProcClientWrapper.cpp | 29 ++---- src/InProcServerWrapper.cpp | 4 +- src/OCPlatform.cpp | 26 ++--- src/OCResource.cpp | 2 +- 14 files changed, 159 insertions(+), 170 deletions(-) diff --git a/examples/simpleclientserver.cpp b/examples/simpleclientserver.cpp index 25e6d9a..a28fe6d 100644 --- a/examples/simpleclientserver.cpp +++ b/examples/simpleclientserver.cpp @@ -109,7 +109,7 @@ private: rep2.setAttributeMap(attrMap2); - m_resource->put(rep2, QueryParamsMap(), std::function(std::bind(&ClientWorker::putResourceInfo, this, rep2, std::placeholders::_1, std::placeholders::_2))); + m_resource->put(rep2, QueryParamsMap(), PutCallback(std::bind(&ClientWorker::putResourceInfo, this, rep2, std::placeholders::_1, std::placeholders::_2))); } } @@ -148,7 +148,7 @@ private: std::cout<<"Doing a get on q/foo."<get(test, std::function(std::bind(&ClientWorker::getResourceInfo, this, std::placeholders::_1, std::placeholders::_2))); + resource->get(test, GetCallback(std::bind(&ClientWorker::getResourceInfo, this, std::placeholders::_1, std::placeholders::_2))); } } @@ -156,7 +156,7 @@ public: void start(OCPlatform& platform) { std::cout<<"Starting Client find:"<)> f (std::bind(&ClientWorker::foundResource, this, std::placeholders::_1)); + FindCallback f (std::bind(&ClientWorker::foundResource, this, std::placeholders::_1)); std::cout<<"result:" << platform.findResource("", "coap://224.0.1.187/oc/core?rt=core.foo", f)<< std::endl; std::cout<<"Finding Resource..."<, std::shared_ptr)> eh(std::bind(&FooResource::entityHandler, this, std::placeholders::_1, std::placeholders::_2)); + RegisterCallback eh(std::bind(&FooResource::entityHandler, this, std::placeholders::_1, std::placeholders::_2)); OCStackResult result = platform.registerResource(m_resourceHandle, resourceURI, resourceTypeName, resourceInterface, eh, resourceProperty); diff --git a/include/IClientWrapper.h b/include/IClientWrapper.h index a486d8a..1adee61 100644 --- a/include/IClientWrapper.h +++ b/include/IClientWrapper.h @@ -34,7 +34,7 @@ namespace OC typedef std::shared_ptr Ptr; virtual OCStackResult ListenForResource(const std::string& serviceUrl, const std::string& resourceType, - std::function)>& callback) = 0; + FindCallback& callback) = 0; virtual OCStackResult GetResourceAttributes(const std::string& host, const std::string& uri, const QueryParamsMap& queryParams, GetCallback& callback)=0; @@ -44,14 +44,14 @@ namespace OC virtual OCStackResult ObserveResource(ObserveType observeType, OCDoHandle* handle, const std::string& host, const std::string& uri, const QueryParamsMap& queryParams, - std::function& callback)=0; + ObserveCallback& callback)=0; virtual OCStackResult CancelObserveResource(OCDoHandle handle, const std::string& host, const std::string& uri)=0; - virtual OCStackResult subscribePresence(OCDoHandle* handle, const std::string& host, - std::function presenceHandler)=0; + virtual OCStackResult SubscribePresence(OCDoHandle* handle, const std::string& host, + SubscribeCallback& presenceHandler)=0; - virtual OCStackResult unsubscribePresence(OCDoHandle handle) =0; + virtual OCStackResult UnsubscribePresence(OCDoHandle handle) =0; virtual ~IClientWrapper(){} diff --git a/include/IServerWrapper.h b/include/IServerWrapper.h index 4126a17..fb6d710 100644 --- a/include/IServerWrapper.h +++ b/include/IServerWrapper.h @@ -45,7 +45,7 @@ namespace OC std::string& resourceURI, const std::string& resourceTypeName, const std::string& resourceInterface, - std::function entityHandler, + RegisterCallback& entityHandler, uint8_t resourceProperty) = 0; virtual OCStackResult unregisterResource( const OCResourceHandle& resourceHandle) = 0; diff --git a/include/InProcClientWrapper.h b/include/InProcClientWrapper.h index 4ecc4a0..060ea6b 100644 --- a/include/InProcClientWrapper.h +++ b/include/InProcClientWrapper.h @@ -44,7 +44,7 @@ namespace OC virtual ~InProcClientWrapper(); virtual OCStackResult ListenForResource(const std::string& serviceUrl, const std::string& resourceType, - std::function)>& callback); + FindCallback& callback); virtual OCStackResult GetResourceAttributes(const std::string& host, const std::string& uri, const QueryParamsMap& queryParams, GetCallback& callback); @@ -54,14 +54,14 @@ namespace OC virtual OCStackResult ObserveResource(ObserveType observeType, OCDoHandle* handle, const std::string& host, const std::string& uri, const QueryParamsMap& queryParams, - std::function& callback); + ObserveCallback& callback); virtual OCStackResult CancelObserveResource(OCDoHandle handle, const std::string& host, const std::string& uri); - virtual OCStackResult subscribePresence(OCDoHandle* handle, const std::string& host, - std::function presenceHandler); + virtual OCStackResult SubscribePresence(OCDoHandle* handle, const std::string& host, + SubscribeCallback& presenceHandler); - virtual OCStackResult unsubscribePresence(OCDoHandle handle); + virtual OCStackResult UnsubscribePresence(OCDoHandle handle); // Note: this should never be called by anyone but the handler for the listen command. It is public becuase that needs to be a non-instance callback virtual std::shared_ptr parseOCResource(IClientWrapper::Ptr clientWrapper, const std::string& host, const boost::property_tree::ptree resourceNode); private: @@ -71,7 +71,6 @@ namespace OC std::thread m_listeningThread; bool m_threadRun; std::weak_ptr m_csdkLock; - std::vector> callbackList; private: PlatformConfig m_cfg; diff --git a/include/InProcServerWrapper.h b/include/InProcServerWrapper.h index f06c3db..450825f 100644 --- a/include/InProcServerWrapper.h +++ b/include/InProcServerWrapper.h @@ -42,7 +42,7 @@ namespace OC std::string& resourceURI, const std::string& resourceTypeName, const std::string& resourceInterface, - std::function entityHandler, + RegisterCallback& entityHandler, uint8_t resourceProperty); virtual OCStackResult unregisterResource( diff --git a/include/OCApi.h b/include/OCApi.h index 5f61c0e..3f6ae3a 100644 --- a/include/OCApi.h +++ b/include/OCApi.h @@ -19,7 +19,7 @@ //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #ifndef __INTEL_OCAPI_H_2014_07_10 - #define __INTEL_OCAPI_H_2014_07_10 +#define __INTEL_OCAPI_H_2014_07_10 #include #include @@ -28,114 +28,113 @@ #include #include "ocstack.h" -namespace OC { +namespace OC +{ + class OCResource; + class OCResourceRequest; + class OCResourceResponse; +} // namespace OC +namespace OC { namespace OCReflect { + struct entity; +}} // namespace OC::OCReflect -class OCResource; +namespace OC +{ + enum class OCPlatformStatus + { + PlatformUp, + PlatformDown + }; -} // namespace OC + enum class OCAdvertisementStatus + { + None + }; -namespace OC { namespace OCReflect { + typedef std::string URI; -struct entity; + enum class ServiceType + { + InProc, + OutOfProc + }; -}} // namespace OC::OCReflect + enum class ModeType + { + Server, + Client, + Both + }; + + enum class QualityOfService : uint8_t + { + Confirmable = OC_CONFIRMABLE, + NonConfirmable = OC_NON_CONFIRMABLE + }; -namespace OC { - - enum class OCPlatformStatus { - PlatformUp, - PlatformDown - }; - - enum class OCAdvertisementStatus{ - None - }; - - typedef std::string URI; - - enum class ServiceType - { - InProc, - OutOfProc - }; - - enum class ModeType - { - Server, - Client, - Both - }; - - enum class QualityOfService : uint8_t - { - Confirmable = OC_CONFIRMABLE, - NonConfirmable = OC_NON_CONFIRMABLE - }; - - struct PlatformConfig - { - ServiceType serviceType; // This will indicate whether it is InProc or OutOfProc - ModeType mode; // This will indicate whether we want to do server, client or both - std::string ipAddress; // This is the ipAddress of the server to connect to - uint16_t port; // Port of the server - - QualityOfService QoS; - - public: - PlatformConfig(const ServiceType serviceType_, - const ModeType mode_, - const std::string& ipAddress_, - const uint16_t port_, - const QualityOfService QoS_) - : serviceType(serviceType_), - mode(mode_), - ipAddress(ipAddress_), - port(port_), - QoS(QoS_) - {} - }; - - enum class RequestHandlerFlag - { - InitFlag, - RequestFlag, - ObserverFlag - }; - - enum class ObserveType - { - Observe, - ObserveAll - }; - - // TODO: To find the complete JSon data structure and modify map value type - // Typedef for attribute values and attribute map. - typedef std::vector AttributeValues; - typedef std::map AttributeMap; - - // Typedef for query parameter map - typedef std::map QueryParamsMap; - - // const strings for different interfaces - - // Default interface - const std::string DEFAULT_INTERFACE = "oc.mi.def"; - - // Used in discovering (GET) links to other resources of a collection. - const std::string LINK_INTERFACE = "oc.mi.ll"; - - // Used in GET, PUT, POST, DELETE methods on links to other resources of a collection. - const std::string BATCH_INTERFACE = "oc.mi.b"; - - // Helper function to escape character in a string. - std::string escapeString(const std::string& value); - - class OCRepresentation + struct PlatformConfig { + ServiceType serviceType; // This will indicate whether it is InProc or OutOfProc + ModeType mode; // This will indicate whether we want to do server, client or both + std::string ipAddress; // This is the ipAddress of the server to connect to + uint16_t port; // Port of the server + + QualityOfService QoS; + + public: + PlatformConfig(const ServiceType serviceType_, + const ModeType mode_, + const std::string& ipAddress_, + const uint16_t port_, + const QualityOfService QoS_) + : serviceType(serviceType_), + mode(mode_), + ipAddress(ipAddress_), + port(port_), + QoS(QoS_) + {} + }; + + enum class RequestHandlerFlag + { + InitFlag, + RequestFlag, + ObserverFlag + }; + + enum class ObserveType + { + Observe, + ObserveAll + }; + + // TODO: To find the complete JSon data structure and modify map value type + // Typedef for attribute values and attribute map. + typedef std::vector AttributeValues; + typedef std::map AttributeMap; + + // Typedef for query parameter map + typedef std::map QueryParamsMap; - private: + // const strings for different interfaces + // Default interface + const std::string DEFAULT_INTERFACE = "oc.mi.def"; + + // Used in discovering (GET) links to other resources of a collection. + const std::string LINK_INTERFACE = "oc.mi.ll"; + + // Used in GET, PUT, POST, DELETE methods on links to other resources of a collection. + const std::string BATCH_INTERFACE = "oc.mi.b"; + + // Helper function to escape character in a string. + std::string escapeString(const std::string& value); + + class OCRepresentation + { + + private: std::string m_uri; AttributeMap m_attributeMap; std::vector m_resourceTypes; @@ -145,8 +144,7 @@ namespace OC { std::vector m_children; - public: - + public: OCRepresentation() {} std::string getUri(void) const @@ -166,7 +164,7 @@ namespace OC { void setChildren(std::vector children) { - m_children = children; + m_children = children; } OCResource* getResource() const @@ -177,7 +175,7 @@ namespace OC { return res; } - AttributeMap getAttributeMap() const + AttributeMap getAttributeMap() const { return m_attributeMap; } @@ -207,8 +205,13 @@ namespace OC { m_resourceInterfaces = resourceInterfaces; } }; - typedef std::function GetCallback; - typedef std::function PutCallback; + + typedef std::function)> FindCallback; + typedef std::function, const std::shared_ptr)> RegisterCallback; + typedef std::function SubscribeCallback; + typedef std::function GetCallback; + typedef std::function PutCallback; + typedef std::function ObserveCallback; } // namespace OC -#endif +#endif diff --git a/include/OCPlatform.h b/include/OCPlatform.h index 26ce6a6..b9209b0 100644 --- a/include/OCPlatform.h +++ b/include/OCPlatform.h @@ -98,7 +98,7 @@ namespace OC * NOTE: OCStackResult is defined in ocstack.h. */ OCStackResult findResource(const std::string& host, const std::string& resourceURI, - std::function resourceHandler); + FindCallback resourceHandler); /** * This API registers a resource with the server @@ -128,7 +128,7 @@ namespace OC std::string& resourceURI, const std::string& resourceTypeName, const std::string& resourceInterface, - std::function entityHandler, + RegisterCallback entityHandler, uint8_t resourceProperty); /** @@ -268,7 +268,7 @@ namespace OC * @return OCStackResult - return value of the API. Returns OCSTACK_OK if success
*/ OCStackResult subscribePresence(OCPresenceHandle& presenceHandle, const std::string& host, - std::function presenceHandler); + SubscribeCallback presenceHandler); /** * unsubscribes from a previously subscribed server's presence events. Note that diff --git a/include/OCResource.h b/include/OCResource.h index 810dc4c..c224fd1 100644 --- a/include/OCResource.h +++ b/include/OCResource.h @@ -145,7 +145,7 @@ namespace OC * NOTE: OCStackResult is defined in ocstack.h. */ OCStackResult observe(ObserveType observeType, const QueryParamsMap& queryParametersMap, - std::function observeHandler); + ObserveCallback observeHandler); /** * Function to cancel the observation on the resource diff --git a/include/OutOfProcClientWrapper.h b/include/OutOfProcClientWrapper.h index a769f4b..2ff41fe 100644 --- a/include/OutOfProcClientWrapper.h +++ b/include/OutOfProcClientWrapper.h @@ -31,7 +31,7 @@ namespace OC OutOfProcClientWrapper(std::weak_ptr csdkLock, PlatformConfig cfg) { } virtual OCStackResult ListenForResource(const std::string& serviceUrl, const std::string& resourceType, - std::function)>& callback) {return OC_STACK_NOTIMPL;} + FindCallback& callback) {return OC_STACK_NOTIMPL;} virtual OCStackResult GetResourceAttributes(const std::string& host, const std::string& uri, const QueryParamsMap& queryParams, GetCallback& callback){return OC_STACK_NOTIMPL;} @@ -41,17 +41,17 @@ namespace OC virtual OCStackResult ObserveResource(ObserveType observeType, OCDoHandle* handle, const std::string& host, const std::string& uri, const QueryParamsMap& queryParams, - std::function& callback){return OC_STACK_NOTIMPL;} + ObserveCallback& callback){return OC_STACK_NOTIMPL;} virtual OCStackResult CancelObserveResource(OCDoHandle handle, const std::string& host, const std::string& uri){return OC_STACK_NOTIMPL;} virtual std::shared_ptr parseOCResource(IClientWrapper::Ptr clientWrapper, const std::string& host, const boost::property_tree::ptree resourceNode) {return nullptr;} - virtual OCStackResult subscribePresence(OCDoHandle* handle, const std::string& host, - std::function presenceHandler){return OC_STACK_NOTIMPL;} + virtual OCStackResult SubscribePresence(OCDoHandle* handle, const std::string& host, + SubscribeCallback& presenceHandler){return OC_STACK_NOTIMPL;} - virtual OCStackResult unsubscribePresence(OCDoHandle handle){return OC_STACK_NOTIMPL;} + virtual OCStackResult UnsubscribePresence(OCDoHandle handle){return OC_STACK_NOTIMPL;} }; } diff --git a/include/OutOfProcServerWrapper.h b/include/OutOfProcServerWrapper.h index 2503ee5..e276d12 100644 --- a/include/OutOfProcServerWrapper.h +++ b/include/OutOfProcServerWrapper.h @@ -35,7 +35,7 @@ namespace OC std::string& resourceURI, const std::string& resourceTypeName, const std::string& resourceInterface, - std::function entityHandler, + RegisterCallback& entityHandler, uint8_t resourceProperty) { diff --git a/src/InProcClientWrapper.cpp b/src/InProcClientWrapper.cpp index 5393291..6c375a4 100644 --- a/src/InProcClientWrapper.cpp +++ b/src/InProcClientWrapper.cpp @@ -108,7 +108,7 @@ namespace OC struct ListenContext { - std::function)> callback; + FindCallback callback; IClientWrapper::Ptr clientWrapper; }; @@ -202,7 +202,7 @@ namespace OC } } - OCStackResult InProcClientWrapper::ListenForResource(const std::string& serviceUrl, const std::string& resourceType, std::function)>& callback) + OCStackResult InProcClientWrapper::ListenForResource(const std::string& serviceUrl, const std::string& resourceType, FindCallback& callback) { OCStackResult result; @@ -501,7 +501,7 @@ namespace OC struct ObserveContext { - std::function callback; + ObserveCallback callback; }; OCStackApplicationResult observeResourceCallback(void* ctx, OCDoHandle handle, OCClientResponse* clientResponse) @@ -519,7 +519,7 @@ namespace OC } OCStackResult InProcClientWrapper::ObserveResource(ObserveType observeType, OCDoHandle* handle, const std::string& host, - const std::string& uri, const QueryParamsMap& queryParams, std::function& callback) + const std::string& uri, const QueryParamsMap& queryParams, ObserveCallback& callback) { OCStackResult result; OCCallbackData* cbdata = new OCCallbackData(); @@ -566,19 +566,6 @@ namespace OC return result; } - struct UnobserveContext - { - std::function callback; - }; - - OCStackApplicationResult unobserveResourceCallback(void* ctx, OCDoHandle handle, OCClientResponse* clientResponse) - { - UnobserveContext* context = static_cast(ctx); - std::thread exec(context->callback, clientResponse->result); - exec.detach(); - return OC_STACK_DELETE_TRANSACTION; - } - OCStackResult InProcClientWrapper::CancelObserveResource(OCDoHandle handle, const std::string& host, const std::string& uri) { OCStackResult result; @@ -599,7 +586,7 @@ namespace OC struct SubscribePresenceContext { - std::function callback; + SubscribeCallback callback; }; OCStackApplicationResult subscribePresenceCallback(void* ctx, OCDoHandle handle, OCClientResponse* clientResponse) @@ -611,8 +598,8 @@ namespace OC return OC_STACK_KEEP_TRANSACTION; } - OCStackResult InProcClientWrapper::subscribePresence(OCDoHandle* handle, const std::string& host, - std::function presenceHandler) + OCStackResult InProcClientWrapper::SubscribePresence(OCDoHandle* handle, const std::string& host, + SubscribeCallback& presenceHandler) { OCStackResult result; OCCallbackData* cbdata = new OCCallbackData(); @@ -640,7 +627,7 @@ namespace OC return result; } - OCStackResult InProcClientWrapper::unsubscribePresence(OCDoHandle handle) + OCStackResult InProcClientWrapper::UnsubscribePresence(OCDoHandle handle) { OCStackResult result; auto cLock = m_csdkLock.lock(); diff --git a/src/InProcServerWrapper.cpp b/src/InProcServerWrapper.cpp index 0be188c..7721543 100644 --- a/src/InProcServerWrapper.cpp +++ b/src/InProcServerWrapper.cpp @@ -41,7 +41,7 @@ using namespace OC::OCReflect; using namespace std; -std::map > entityHandlerMap; +std::map entityHandlerMap; void defaultEntityHandler(const OC::OCResourceRequest::Ptr request, const OC::OCResourceResponse::Ptr response) { @@ -214,7 +214,7 @@ namespace OC std::string& resourceURI, const std::string& resourceTypeName, const std::string& resourceInterface, - std::function eHandler, + RegisterCallback& eHandler, uint8_t resourceProperties) { diff --git a/src/OCPlatform.cpp b/src/OCPlatform.cpp index dbf5a14..c312a01 100644 --- a/src/OCPlatform.cpp +++ b/src/OCPlatform.cpp @@ -78,8 +78,8 @@ namespace OC } OCResource::Ptr OCPlatform::constructResourceObject(const std::string& host, const std::string& uri, - bool isObservable, const std::vector& resourceTypes, - const std::vector& interfaces) + bool isObservable, const std::vector& resourceTypes, + const std::vector& interfaces) { if(m_client) { @@ -93,7 +93,7 @@ namespace OC } OCStackResult OCPlatform::findResource(const std::string& host, const std::string& resourceName, - std::function resourceHandler) + FindCallback resourceHandler) { if(m_client) { @@ -104,11 +104,11 @@ namespace OC OCStackResult OCPlatform::registerResource(OCResourceHandle& resourceHandle, - std::string& resourceURI, - const std::string& resourceTypeName, - const std::string& resourceInterface, - std::function entityHandler, - uint8_t resourceProperty) + std::string& resourceURI, + const std::string& resourceTypeName, + const std::string& resourceInterface, + RegisterCallback entityHandler, + uint8_t resourceProperty) { OCStackResult result = OC_STACK_OK; @@ -225,7 +225,7 @@ namespace OC } OCStackResult OCPlatform::bindTypeToResource(const OCResourceHandle& resourceHandle, - const std::string& resourceTypeName) const + const std::string& resourceTypeName) const { OCStackResult result = OC_STACK_ERROR; if(m_server) @@ -245,7 +245,7 @@ namespace OC } OCStackResult OCPlatform::bindInterfaceToResource(const OCResourceHandle& resourceHandle, - const std::string& resourceInterfaceName) const + const std::string& resourceInterfaceName) const { OCStackResult result = OC_STACK_ERROR; if(m_server) @@ -290,11 +290,11 @@ namespace OC } OCStackResult OCPlatform::subscribePresence(OCPresenceHandle& presenceHandle, const std::string& host, - std::function presenceHandler) + SubscribeCallback presenceHandler) { if(m_client) { - return m_client->subscribePresence(&presenceHandle, host, presenceHandler); + return m_client->SubscribePresence(&presenceHandle, host, presenceHandler); } else { @@ -306,7 +306,7 @@ namespace OC { if(m_client) { - return m_client->unsubscribePresence(presenceHandle); + return m_client->UnsubscribePresence(presenceHandle); } else { diff --git a/src/OCResource.cpp b/src/OCResource.cpp index 3c841f0..564965f 100644 --- a/src/OCResource.cpp +++ b/src/OCResource.cpp @@ -120,7 +120,7 @@ namespace OC { } OCStackResult OCResource::observe(ObserveType observeType, const QueryParamsMap& queryParametersMap, - std::function observeHandler) + ObserveCallback observeHandler) { if(m_observeHandle != nullptr) { -- 2.7.4