- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下代码使用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;
}
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);
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/
我进行了搜索以查看是否找到了解决此问题的方法,但没有找到答案。我遇到的问题是在我的代码编译时,我没有得到 intellisense 如果我收到一个参数(或声明一个变量),例如使用模板 T : uniq
我想在多态情况下将派生类 unique_ptr 的所有权转移到它的抽象基类 unique_ptr。怎么走? class Fruit { public: virtual void print()
unique_ptr> 之间有什么区别?和一个 list> ?威尔list>导致元素的内存也被自动管理? 最佳答案 说unique_ptr<>就像在说 *但具有自动删除的额外好处。 unique_pt
我第一次在我的项目中使用智能指针。在使用 unique_ptr 时,我对 unique_ptr 和原始指针组合有一些疑问。以及 unique_ptr 内部工作的方式。 有人可以根据我的理解解释/回答如
我创建了一个派生自 std::istream 的自定义 istream,当文件是压缩文件时使用自定义 streambuf,否则使用 std::filebuf。 #mystream.h class my
目前我正在尝试使用 std::unique_ptr,但我在 Visual Studio 2012 中遇到编译器错误。 class A { private: unique_ptr other; pub
我有以下三个代码片段来演示一个容易重现的问题。 using namespace boost::filesystem; using namespace std; int main() { pat
这个问题令人困惑,所以这里是我正在尝试做的事情的精简版: #include #include class A { }; class B : public A { public:
假设 class Owner 有 Member 成员,它还必须有一个指向它的 const 所有者的 const 指针。该指针在 Owner 的构造函数中提供给 member,该构造函数接受指向构成 m
下面的代码会抛出一个警告: 警告 C4239:使用了非标准扩展:“参数”:从“std::unique_ptr”到“std::unique_ptr &”的转换 std::unique_ptr foo()
这个问题在这里已经有了答案: Returning unique_ptr from functions (7 个答案) 关闭 8 年前。 我是 unique_ptr 的新手。一切都很顺利,直到我遇到一
如果 vector 不是 unique_ptr 或者如果我没有 vector 的 unique_ptr(并且不取消引用)它可以工作,但两者都会导致编译错误。我不确定发生了什么。 auto v = st
我正在尝试使用 unique_ptr到接受 unique_ptr 的函数中的派生类到基类。比如: class Base {}; class Derived : public Base {}; void
我是 C++ 和智能指针的新手,尤其是 unique_ptr 的行为。下面是我正在试验的一段代码: unique_ptr u1 = make_unique(2); unique_ptr u2 =
我类有这个成员: static std::unique_ptr[]> changestatecommands; 而且我找不到正确的方法来初始化它。我希望数组被初始化,但元素未初始化,所以我可以随时编写
我编写了以下使用 unique_ptr 的代码其中 unique_ptr预计 class Base { int i; public: Base( int i ) : i(i) {}
我有一个非常具体的需求,需要访问特定于派生类的功能,我在构建包含类时在 unique_ptr 中获得了该派生类的实例。然后,该包含类必须将其基类的 unique_ptr 上转型移动到包含类的基类构造函
有人可以建议如何使用自定义删除器从模板化唯一指针池返回唯一指针。 在下面的代码片段中,我使用 ObjectPool.h 作为我的模板类来获取一堆唯一指针。我正在使用 ObjectPool 在 DBCo
我在 std::vector> 中维护了一些对象池我将这个池中的对象传递给一个函数 void process(...) .我不清楚将这些对象之一传递给 process() 的最佳方式功能。如果我理解我
我用一个对象初始化了一个unique_ptr。因为我想将它的引用传递给函数并且不让函数更改对象内容,所以我必须传递 unique_ptr&给它。但是 gcc 5.4 不允许我初始化 unique_pt
我是一名优秀的程序员,十分优秀!