diff --git a/requester.cpp b/requester.cpp index 5a80f9c..75e30f4 100644 --- a/requester.cpp +++ b/requester.cpp @@ -26,33 +26,15 @@ size_t requester::write_data(char *buf, size_t sz, size_t nmemb, void *user_data requester::requester() { curl_global_init(CURL_GLOBAL_ALL); - _h = curl_easy_init(); - curl_easy_setopt(_h, 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; + _h.reset(curl_easy_init()); + curl_easy_setopt(_h.get(), CURLOPT_WRITEFUNCTION, write_data); } 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 buf; - curl_easy_setopt(_h, CURLOPT_WRITEDATA, &buf); - auto success = curl_easy_perform(_h); + curl_easy_setopt(_h.get(), CURLOPT_WRITEDATA, &buf); + auto success = curl_easy_perform(_h.get()); if (success != CURLE_OK) throw std::runtime_error("could not get remote data"); std::string text {buf.data(), buf.size()}; return text; diff --git a/requester.hpp b/requester.hpp index 50769c7..95288f1 100644 --- a/requester.hpp +++ b/requester.hpp @@ -10,6 +10,7 @@ #define _requester_H_ #include +#include #include class requester { @@ -20,21 +21,11 @@ class requester { // this class is a RAII class for a curl handle 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(); - + // perform a http get request std::string get(const std::string& url); private: - CURL* _h {nullptr}; // curl handle + std::unique_ptr _h {nullptr, curl_easy_cleanup}; }; #endif // _requester_H_