关于C++ 将lambda表达式作为参数传递

https://blog.csdn.net/register_man/article/details/71330743

项目里面用到lambda,需要穿参数,看了这篇博文,便动手试了试。

先定义一个typedef void (^responsefunc)(Poco::Net::HTTPResponse::HTTPStatus result, const string& responseContent);

然后建立一个方法- (void) sendRequest:(URI &)sendUri recieveFunc:(responsefunc)recieveFunc;

调用过程如下:

auto recieveResponseDataFunc = ^(Poco::Net::HTTPResponse::HTTPStatus result, const string& responseContent){
#if defined(DEBUG)||defined(_DEBUG)
        std::cout << responseContent << std::endl;
#endif
        NSString *jsonString = [NSString stringWithUTF8String:responseContent.c_str()];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [[NSNotificationCenter defaultCenter] postNotificationName:LOGINRESULT object:@[@(result),jsonString]];
        });
    };
    //auto recieveResponseData = [recieveResponseDataFunc](Poco::Net::HTTPResponse::HTTPStatus result, const string& responseContent{recieveResponseDataFunc(result,responseContent);};
   

NSString *urlStr =

[StringProcessing AssembleParamsAndValue:@API_BASE_URL_LOGIN_STRING,@SERVERURL,loginUserId.text,loginPassWord.text];
    SLSTHTTPSessionManager *getServiceAreaDataManager = [SLSTHTTPSessionManager manager];
    URI senduri([urlStr UTF8String]);
    [getServiceAreaDataManager sendRequest:senduri recieveFunc:recieveResponseDataFunc];
    [getServiceAreaDataManager release];getServiceAreaDataManager = nil;

在dispatch_after里面还是可以发消息,但是如果在里面调用自定义的OC的类的实例再传给C++类的话,会崩溃掉,我试了很多次,就算在外部加上__block typeof(self) selfObject = self;然后selfObject调用方法也会崩溃,最后发现只能发消息。

然后是- (void)sendRequest:(URI &)sendUri recieveFunc:(responsefunc)recieveFunc方法的实现:

OC里面的auto还是传给C++的,我试了试,转成std::function,然后再传给给C++方法

- (void)sendRequest:(URI &)sendUri recieveFunc:(responsefunc)recieveFunc
{
    slstss::SLSTHttpHandler *httpHandler = new slstss::SLSTHttpHandler();
    std::function func = recieveFunc;
    httpHandler->setHandlerFunc(func);
    slstss::SLSTHttpExchange *saDataHttpExchange = new slstss::SLSTHttpExchange();
    saDataHttpExchange->SetUri(sendUri.toString());
    saDataHttpExchange->SetTimeOut(20);
    saDataHttpExchange->SetHandler(httpHandler);
    if(true == self.needCookie){
        saDataHttpExchange->SetNeedCookie(true);
        LevelDB *oaDb = [LevelDB levelDBWithDbName:FreeWayServiceOALDB];
        if(ObjectIsNilOrNull(oaDb)){
            return;
        }
        NSArray *cookiesData = [oaDb arrayForKey:OACOOKIES];
        [oaDb release];oaDb = nil;
        if(ObjectIsNilOrNull(cookiesData)){
            return;
        }
        Poco::Net::NameValueCollection cookiesNVC;
        for(NSDictionary *dict in cookiesData){
            NSArray *keys = dict.allKeys;
            NSString *key = [keys objectAtIndex:0];
            NSString *value = [dict objectForKey:key];
            cookiesNVC.add([key UTF8String], [value UTF8String]);
        }
        saDataHttpExchange->SetCookieNVC(cookiesNVC);
    }
    slstss::SLSTHttpClient saDataHttpClient;
    saDataHttpClient.Send(saDataHttpExchange);
    saDataHttpExchange = NULL;
    httpHandler = NULL;
}

你可能感兴趣的:(Objective-C,C++)