我正在使用卡萨布兰卡C++ Rest SDK进行http连接。下面是生成http请求的基本代码。
从卡萨布兰卡文件复制
// Creates an HTTP request and prints the length of the response stream.
pplx::task<void> HTTPStreamingAsync()
{
http_client client(L"http://www.google.com");
// Make the request and asynchronously process the response.
return client.request(methods::GET).then([](http_response response)
{
// Response received, do whatever here.
});
}这将执行异步请求,并在完成时执行回调。我需要使用这些代码创建自己的类,并将其封装到自己的回调中。
为了简单起见,假设我想要创建一个具有打印google.com的html代码的方法的类。
所以我期待着这样的事情:
MyClass myObject;
myObject.getGoogleHTML([](std::string htmlString)
{
std::cout << htmlString;
});我搜索并阅读了相关的文章,如:
但是我还是有点困惑,因为我已经习惯了completion block in Objective-C。我如何构造这样一个包装回调的类?
发布于 2014-06-26 15:20:24
把灯笼当作一般的类型。作为奖励,它将与任何其他可调用对象一起工作。
template<typename F>
pplx::task<void> MyClass::getGoogleHTML(F f) {
http_client client(L"http://www.google.com");
return client.request(methods::GET).then(f);
}如果您愿意,还可以通过F &&f和.then(std::forward<F>(f))完美地转发.then(std::forward<F>(f))。如果您实际上是想提取一些东西给传入的lambda,那么将一个lambda传递给then,它捕获f并使用提取的数据调用它。
https://stackoverflow.com/questions/24434001
复制相似问题