From a9ace2505e1c7b616426fe7811c4420a2b01b888 Mon Sep 17 00:00:00 2001 From: Hojin Choi Date: Fri, 26 Apr 2013 12:42:50 +0900 Subject: [PATCH] Introduce HttpResponse callback selector type. to resolve #2365 --- extensions/network/HttpClient.cpp | 4 ++-- extensions/network/HttpRequest.h | 39 +++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/extensions/network/HttpClient.cpp b/extensions/network/HttpClient.cpp index 47cb9429d1..b194271dd9 100644 --- a/extensions/network/HttpClient.cpp +++ b/extensions/network/HttpClient.cpp @@ -479,11 +479,11 @@ void CCHttpClient::dispatchResponseCallbacks(float delta) CCHttpRequest *request = response->getHttpRequest(); CCObject *pTarget = request->getTarget(); - SEL_CallFuncND pSelector = request->getSelector(); + SEL_HttpResponse pSelector = request->getSelector(); if (pTarget && pSelector) { - (pTarget->*pSelector)((CCNode *)this, response); + (pTarget->*pSelector)(this, response); } response->release(); diff --git a/extensions/network/HttpRequest.h b/extensions/network/HttpRequest.h index 3addad2a6e..28dbf6b762 100644 --- a/extensions/network/HttpRequest.h +++ b/extensions/network/HttpRequest.h @@ -30,11 +30,17 @@ NS_CC_EXT_BEGIN +class CCHttpClient; +class CCHttpResponse; +typedef void (CCObject::*SEL_HttpResponse)(CCHttpClient* client, CCHttpResponse* response); +#define httpresponse_selector(_SELECTOR) (SEL_HttpResponse)(&_SELECTOR) + /** @brief defines the object which users must packed for CCHttpClient::send(HttpRequest*) method. Please refer to samples/TestCpp/Classes/ExtensionTest/NetworkTest/HttpClientTest.cpp as a sample @since v2.0.2 */ + class CCHttpRequest : public CCObject { public: @@ -155,7 +161,12 @@ public: /** Required field. You should set the callback selector function at ack the http request completed */ - inline void setResponseCallback(CCObject* pTarget, SEL_CallFuncND pSelector) + inline void setResponseCallback(CCObject* pTarget, SEL_CallFuncND pSelector) CC_DEPRECATED_ATTRIBUTE + { + setResponseCallback(pTarget, (SEL_HttpResponse) pSelector); + } + + inline void setResponseCallback(CCObject* pTarget, SEL_HttpResponse pSelector) { _pTarget = pTarget; _pSelector = pSelector; @@ -170,12 +181,26 @@ public: { return _pTarget; } - /** Get the selector function pointer, mainly used by CCHttpClient */ - inline SEL_CallFuncND getSelector() - { - return _pSelector; - } + /* This sub class is just for migration SEL_CallFuncND to SEL_HttpResponse, + someday this way will be removed */ + class _prxy + { + public: + _prxy( SEL_HttpResponse cb ) :_cb(cb) {} + ~_prxy(){}; + operator SEL_HttpResponse() const { return _cb; } + operator SEL_CallFuncND() const CC_DEPRECATED_ATTRIBUTE { return (SEL_CallFuncND) _cb; } + protected: + SEL_HttpResponse _cb; + }; + + /** Get the selector function pointer, mainly used by CCHttpClient */ + inline _prxy getSelector() + { + return _prxy(_pSelector); + } + /** Set any custom headers **/ inline void setHeaders(std::vector pHeaders) { @@ -196,7 +221,7 @@ protected: std::vector _requestData; /// used for POST std::string _tag; /// user defined tag, to identify different requests in response callback CCObject* _pTarget; /// callback target of pSelector function - SEL_CallFuncND _pSelector; /// callback function, e.g. MyLayer::onHttpResponse(CCObject *sender, void *data) + SEL_HttpResponse _pSelector; /// callback function, e.g. MyLayer::onHttpResponse(CCHttpClient *sender, CCHttpResponse * response) void* _pUserData; /// You can add your customed data here std::vector _headers; /// custom http headers };