gpt4 book ai didi

c++ - 在unique_ptr <>中使用自定义删除器(curl_formfree())

转载 作者:行者123 更新时间:2023-12-02 10:22:37 25 4
gpt4 key购买 nike

我有以下代码使用libcurl发布文件。

bool FileUploadDownload::upload(const std::string& filename, const std::string& url) {

CURLcode res;
CURLFORMcode form_res;
curl_off_t speed_upload = 0;
struct curl_httppost* form_post = nullptr;
struct curl_httppost* last_ptr = nullptr;
struct curl_slist* header_list = nullptr;
static const char buf[] = "Expect: 100-continue";

std::string pure_fname = filename.substr(filename.find_last_of("/\\") + 1);

res = curl_global_init(CURL_GLOBAL_ALL);
if (res != CURLE_OK) {
m_logger->errorf("curl_global_init(CURL_GLOBAL_ALL) failed: %s", curl_easy_strerror(res));
return false;
}

form_res = curl_formadd(&form_post,
&last_ptr,
CURLFORM_COPYNAME, "file",
CURLFORM_FILENAME, pure_fname.c_str(),
CURLFORM_FILE, filename.c_str(),
CURLFORM_CONTENTTYPE, "text/plain",
CURLFORM_END);
if (form_res != 0) {
m_logger->errorf("curl_formadd() failed: %s", curl_easy_strerror((CURLcode)form_res));
curl_formfree(form_post);
return false;
}

form_res = curl_formadd(&form_post,
&last_ptr,
CURLFORM_COPYNAME, "submit",
CURLFORM_COPYCONTENTS, "send",
CURLFORM_END);
if (form_res != 0) {
m_logger->errorf("curl_formadd() failed: %s", curl_easy_strerror((CURLcode)form_res));
curl_formfree(form_post);
return false;
}

if (m_curl) {

header_list = curl_slist_append(header_list, buf);
if(nullptr == header_list){
return false;
}

res = curl_easy_setopt(m_curl, CURLOPT_URL, url.c_str());
if (res != CURLE_OK) {
m_logger->errorf("curl_easy_setopt(CURLOPT_URL) failed: %s", curl_easy_strerror(res));
curl_formfree(form_post);
curl_slist_free_all(header_list);
return false;
}

res = curl_easy_setopt(m_curl, CURLOPT_HTTPHEADER, header_list);
if (res != CURLE_OK) {
m_logger->errorf("curl_easy_setopt(CURLOPT_HTTPHEADER) failed: %s", curl_easy_strerror(res));
curl_formfree(form_post);
curl_slist_free_all(header_list);
return false;
}

res = curl_easy_setopt(m_curl, CURLOPT_HTTPPOST, form_post);
if (res != CURLE_OK) {
m_logger->errorf("curl_easy_setopt(CURLOPT_HTTPPOST) failed: %s", curl_easy_strerror(res));
curl_formfree(form_post);
curl_slist_free_all(header_list);
return false;
}

res = curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
if (res != CURLE_OK) {
m_logger->errorf("curl_easy_setopt(CURLOPT_VERBOSE) failed: %s", curl_easy_strerror(res));
curl_formfree(form_post);
curl_slist_free_all(header_list);
return false;
}

res = curl_easy_perform(m_curl);
if (res != CURLE_OK) {
m_logger->errorf("curl_easy_perform() failed: %s", curl_easy_strerror(res));
curl_formfree(form_post);
curl_slist_free_all(header_list);
return false;
}

res = curl_easy_getinfo(m_curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
if (res != CURLE_OK) {
m_logger->errorf("curl_easy_getinfo(CURLINFO_SPEED_UPLOAD) failed: %s", curl_easy_strerror(res));
curl_formfree(form_post);
curl_slist_free_all(header_list);
return false;
}
m_logger->infof("Average upload speed: %" CURL_FORMAT_CURL_OFF_T
" megabyte/sec.\n", speed_upload / (1024 * 1024));
curl_formfree(form_post);
curl_slist_free_all(header_list);
return true;
}
return false;
}

在很多情况下,上述函数都会失败并返回false。在所有这些情况下,我都需要调用 curl_formfree()curl_slist_free_all()

我想使用 std::unique_ptr<>对此进行优化

我已经如下重写了上述功能。
bool FileUploadDownload::upload(const std::string& filename, const std::string& url) {

CURLcode res;
CURLFORMcode form_res;
curl_off_t speed_upload = 0;
auto form_delete = [&](curl_httppost * p) {
curl_formfree(p);
};
//struct curl_httppost* form_post = nullptr;
//struct curl_httppost* last_ptr = nullptr;
std::unique_ptr<curl_httppost*, decltype(form_delete)> form_post;
std::unique_ptr<curl_httppost*, decltype(form_delete)> last_ptr;

struct curl_slist* header_list = nullptr;
static const char buf[] = "Expect: 100-continue";

std::string pure_fname = filename.substr(filename.find_last_of("/\\") + 1);

res = curl_global_init(CURL_GLOBAL_ALL);
if (res != CURLE_OK) {
m_logger->errorf("curl_global_init(CURL_GLOBAL_ALL) failed: %s", curl_easy_strerror(res));
return false;
}

form_res = curl_formadd(form_post.get(),
last_ptr.get(),
CURLFORM_COPYNAME, "file",
CURLFORM_FILENAME, pure_fname.c_str(),
CURLFORM_FILE, filename.c_str(),
CURLFORM_CONTENTTYPE, "text/plain",
CURLFORM_END);
if (form_res != 0) {
m_logger->errorf("curl_formadd() failed: %s", curl_easy_strerror((CURLcode)form_res));
//curl_formfree(form_post);
return false;
}

form_res = curl_formadd(form_post.get(),
last_ptr.get(),
CURLFORM_COPYNAME, "submit",
CURLFORM_COPYCONTENTS, "send",
CURLFORM_END);
if (form_res != 0) {
m_logger->errorf("curl_formadd() failed: %s", curl_easy_strerror((CURLcode)form_res));
//curl_formfree(form_post);
return false;
}

if (m_curl) {

header_list = curl_slist_append(header_list, buf);
if(nullptr == header_list){
return false;
}

res = curl_easy_setopt(m_curl, CURLOPT_URL, url.c_str());
if (res != CURLE_OK) {
m_logger->errorf("curl_easy_setopt(CURLOPT_URL) failed: %s", curl_easy_strerror(res));
//curl_formfree(form_post);
curl_slist_free_all(header_list);
return false;
}

res = curl_easy_setopt(m_curl, CURLOPT_HTTPHEADER, header_list);
if (res != CURLE_OK) {
m_logger->errorf("curl_easy_setopt(CURLOPT_HTTPHEADER) failed: %s", curl_easy_strerror(res));
//curl_formfree(form_post);
curl_slist_free_all(header_list);
return false;
}

res = curl_easy_setopt(m_curl, CURLOPT_HTTPPOST, form_post.get());
if (res != CURLE_OK) {
m_logger->errorf("curl_easy_setopt(CURLOPT_HTTPPOST) failed: %s", curl_easy_strerror(res));
//curl_formfree(form_post);
curl_slist_free_all(header_list);
return false;
}

res = curl_easy_setopt(m_curl, CURLOPT_VERBOSE, 1L);
if (res != CURLE_OK) {
m_logger->errorf("curl_easy_setopt(CURLOPT_VERBOSE) failed: %s", curl_easy_strerror(res));
//curl_formfree(form_post);
curl_slist_free_all(header_list);
return false;
}

res = curl_easy_perform(m_curl);
if (res != CURLE_OK) {
m_logger->errorf("curl_easy_perform() failed: %s", curl_easy_strerror(res));
//curl_formfree(form_post);
curl_slist_free_all(header_list);
return false;
}

res = curl_easy_getinfo(m_curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
if (res != CURLE_OK) {
m_logger->errorf("curl_easy_getinfo(CURLINFO_SPEED_UPLOAD) failed: %s", curl_easy_strerror(res));
//curl_formfree(form_post);
curl_slist_free_all(header_list);
return false;
}
m_logger->infof("Average upload speed: %" CURL_FORMAT_CURL_OFF_T
" megabyte/sec.\n", speed_upload / (1024 * 1024));
//curl_formfree(form_post);
curl_slist_free_all(header_list);
return true;
}
return false;
}

我收到以下编译错误。
error: no match for call to ‘(std::unique_ptr<curl_httppost*, FileUploadDownload::upload(const string&, const string&)::<lambda(curl_httppost*)> >::deleter_type {aka FileUploadDownload::upload(const string&, const string&)::<lambda(curl_httppost*)>}) (curl_httppost**&)’
get_deleter()(__ptr);

最佳答案

您必须初始化指针。同样,lambda的参数或指针的第一个参数都是错误的。 unique_ptr需要它引用的类型,而deleter接受指向该类型的指针。

std::unique_ptr<curl_httppost, decltype(form_delete)> 
form_post( nullptr /* required by first call of curl_formadd */,
form_delete);

也许您想创建自己的“make”函数来用更少的代码来构造那些指针。
curl_formadd需要访问指针以进行初始化,并且其第一个参数是 curl_httppost**,因此使用 .get()不会对其进行剪切。您要么必须使用中间指针(这会破坏重构的目的?),要么在这些指针周围创建包装器。注意,不建议使用 curl_formadd。笨拙的解决方法是管理指向指针的指针。
auto form_delete = [&](curl_httppost ** p) {
curl_formfree(*p);
delete p;
};

std::unique_ptr<curl_httppost*, decltype(form_delete)>
form_post( new curl_httppost*(nullptr) /* required by first call of curl_formadd */,
form_delete);

form_res = curl_formadd(form_post.get(),

这并不能消除重置指针的要求,因为底层指针是不受管理的。

关于c++ - 在unique_ptr <>中使用自定义删除器(curl_formfree()),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59466077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com