delegated all RAII for curl handle to unique_ptr
This commit is contained in:
parent
159feb0dac
commit
a5c8ddae35
@ -26,33 +26,15 @@ size_t requester::write_data(char *buf, size_t sz, size_t nmemb, void *user_data
|
|||||||
|
|
||||||
requester::requester() {
|
requester::requester() {
|
||||||
curl_global_init(CURL_GLOBAL_ALL);
|
curl_global_init(CURL_GLOBAL_ALL);
|
||||||
_h = curl_easy_init();
|
_h.reset(curl_easy_init());
|
||||||
curl_easy_setopt(_h, CURLOPT_WRITEFUNCTION, write_data);
|
curl_easy_setopt(_h.get(), CURLOPT_WRITEFUNCTION, write_data);
|
||||||
}
|
|
||||||
|
|
||||||
requester::~requester() {
|
|
||||||
curl_easy_cleanup(_h);
|
|
||||||
}
|
|
||||||
|
|
||||||
requester::requester(requester&& other) {
|
|
||||||
_h = other._h;
|
|
||||||
other._h = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
requester& requester::operator=(requester &&other) {
|
|
||||||
if (this != &other) {
|
|
||||||
curl_easy_cleanup(_h);
|
|
||||||
_h = other._h;
|
|
||||||
other._h = nullptr;
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string requester::get(const std::string &url) {
|
std::string requester::get(const std::string &url) {
|
||||||
curl_easy_setopt(_h, CURLOPT_URL, url.c_str());
|
curl_easy_setopt(_h.get(), CURLOPT_URL, url.c_str());
|
||||||
std::vector<char> buf;
|
std::vector<char> buf;
|
||||||
curl_easy_setopt(_h, CURLOPT_WRITEDATA, &buf);
|
curl_easy_setopt(_h.get(), CURLOPT_WRITEDATA, &buf);
|
||||||
auto success = curl_easy_perform(_h);
|
auto success = curl_easy_perform(_h.get());
|
||||||
if (success != CURLE_OK) throw std::runtime_error("could not get remote data");
|
if (success != CURLE_OK) throw std::runtime_error("could not get remote data");
|
||||||
std::string text {buf.data(), buf.size()};
|
std::string text {buf.data(), buf.size()};
|
||||||
return text;
|
return text;
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
#define _requester_H_
|
#define _requester_H_
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <memory>
|
||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
|
|
||||||
class requester {
|
class requester {
|
||||||
@ -20,21 +21,11 @@ class requester {
|
|||||||
// this class is a RAII class for a curl handle
|
// this class is a RAII class for a curl handle
|
||||||
requester();
|
requester();
|
||||||
|
|
||||||
// forbid copying
|
// perform a http get request
|
||||||
requester(const requester& other) = delete;
|
|
||||||
requester& operator=(const requester& other) = delete;
|
|
||||||
|
|
||||||
// allow moving
|
|
||||||
requester(requester&& other);
|
|
||||||
requester& operator=(requester&& other);
|
|
||||||
|
|
||||||
// cleanup
|
|
||||||
~requester();
|
|
||||||
|
|
||||||
std::string get(const std::string& url);
|
std::string get(const std::string& url);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CURL* _h {nullptr}; // curl handle
|
std::unique_ptr<CURL, void(*)(CURL*)> _h {nullptr, curl_easy_cleanup};
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // _requester_H_
|
#endif // _requester_H_
|
||||||
|
Loading…
x
Reference in New Issue
Block a user