refactored into a proper RAII class for a curl handle
This commit is contained in:
parent
f4f3b5a32f
commit
b9df8cdb02
@ -30,6 +30,28 @@ requester::requester() {
|
|||||||
curl_easy_setopt(_h, CURLOPT_WRITEFUNCTION, write_data);
|
curl_easy_setopt(_h, CURLOPT_WRITEFUNCTION, write_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
requester::~requester() {
|
||||||
|
if (_h) {
|
||||||
|
curl_easy_cleanup(_h);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
requester::requester(requester&& other) {
|
||||||
|
_h = other._h;
|
||||||
|
other._h = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
requester& requester::operator=(requester &&other) {
|
||||||
|
if (this != &other) {
|
||||||
|
if (_h) {
|
||||||
|
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, CURLOPT_URL, url.c_str());
|
||||||
std::vector<char> buf;
|
std::vector<char> buf;
|
||||||
|
@ -14,14 +14,27 @@
|
|||||||
|
|
||||||
class requester {
|
class requester {
|
||||||
public:
|
public:
|
||||||
|
// callback for data receiving
|
||||||
static size_t write_data(char* buf, size_t sz, size_t nmemb, void* user_data);
|
static size_t write_data(char* buf, size_t sz, size_t nmemb, void* user_data);
|
||||||
|
|
||||||
|
// this class is a RAII class for a curl handle
|
||||||
requester();
|
requester();
|
||||||
|
|
||||||
|
// forbid copying
|
||||||
|
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;
|
CURL* _h; // curl handle
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // _requester_H_
|
#endif // _requester_H_
|
||||||
|
Loading…
x
Reference in New Issue
Block a user