-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using pImpl to avoid the usage of void* #2
base: master
Are you sure you want to change the base?
Conversation
Please split the first commit into smaller manageable commits; each should focus on one thing. This PR should at least contain 4 commits:
Don't do too many things at once. In addition to that: |
src/LokalSo/Lokal.cpp
Outdated
@@ -143,18 +142,15 @@ std::string Lokal::__curl(std::string path, std::string method, | |||
curl_easy_setopt(ch, CURLOPT_HEADERDATA, &data); | |||
curl_easy_setopt(ch, CURLOPT_USERAGENT, "Lokal Go - github.com/lokal-so/lokal-go"); | |||
curl_easy_setopt(ch, CURLOPT_VERBOSE, 0L); | |||
curl_easy_setopt(ch, CURLOPT_HTTPHEADER, headers); | |||
curl_easy_setopt(ch, CURLOPT_HTTPHEADER, headers.get()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This header wrapping thing is gruesome. Plus, I don't see the need to
do that. The slist_append
and slist_free
here are never touched by
the exception.
It costs extra complexity and an indirect call to the deleter without
code improvement.
If you really want to wrap it up, create the whole CURL class that
handles the header addition. Like:
class Lokal {
private:
CURLImpl c_;
};
std::string Lokal::curl(/* params */)
{
c_->setURL(url);
c_->addHeader("Content-Type: application/json");
c_->setOpt(/* ... */)
}
... and then when the Lokal object is destroyed, the CURLImpl will
also be destroyed cleanly withouth the need for slist_free
and
slist_append
from the caller (do those internally in CURLImpl).
Something like that is the expected improvement, not just blindly
wrapping everything with unique_ptr. Otherwise, keep it as is in
the C fashioned way.
@afrizaloky if you want to wrap the CURL pointer. Please do the following requirements. [ Just for fun, I asked ChatGPT and it yields a nice result. But I am not going to use ChatGPT's code. ] Create a curl class named CurlImpl in C++ that wraps the libcurl Key features:
Additional notes:
[ Edit: Fix (7) and (8) names. ] |
1. Using void* to hide the real implementation is not recommended because it will stop the compiler from being able to enforce type checking 2. Create CurlWrapper to wrap the implementation of cURL 3. Create CurlImpl to hide the implementation of CurlWrapper, so the main header doesnt need to include the header
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please follow the coding style around the code.
Indentation: Tab
Tab width: 8
src/LokalSo/CurlWrapper.cpp
Outdated
namespace LokalSo { | ||
|
||
static void ltrim(std::string &s) { | ||
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { | ||
return !std::isspace(ch); | ||
})); | ||
} | ||
|
||
static void rtrim(std::string &s) { | ||
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { | ||
return !std::isspace(ch); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't add indentation inside the namespace.
src/LokalSo/CurlWrapper.cpp
Outdated
|
||
CurlWrapper::CurlWrapper() { | ||
this->ch = curl_easy_init(); | ||
curl_easy_setopt(this->ch, CURLOPT_WRITEFUNCTION, &res_body_callback); | ||
curl_easy_setopt(this->ch, CURLOPT_WRITEDATA, this); | ||
|
||
curl_easy_setopt(this->ch, CURLOPT_HEADERFUNCTION, &res_hdr_callback); | ||
curl_easy_setopt(this->ch, CURLOPT_HEADERDATA, this); | ||
|
||
} | ||
CurlWrapper::~CurlWrapper() { | ||
if(this->ch) { | ||
curl_easy_cleanup(this->ch); | ||
} | ||
if(this->sList) { | ||
curl_slist_free_all(this->sList); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the bad indentation and add an empty line between function definitions.
src/LokalSo/CurlWrapper.cpp
Outdated
|
||
|
||
curl_easy_reset(this->ch); | ||
// curl_easy_setopt(this->ch, CURLOPT_URL, nullptr); | ||
// curl_easy_setopt(this->ch, CURLOPT_POST, 0L); | ||
// curl_easy_setopt(this->ch, CURLOPT_POSTFIELDSIZE, 0); | ||
// curl_easy_setopt(this->ch, CURLOPT_POSTFIELDS, nullptr); | ||
// curl_easy_setopt(ch, CURLOPT_HTTPHEADER, nullptr); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kill these unnecessary comments.
src/LokalSo/CurlWrapper.cpp
Outdated
static size_t res_hdr_callback(void *contents, size_t size, size_t nmemb, | ||
void *userp) { | ||
CurlWrapper* mem = static_cast<CurlWrapper*>(userp); | ||
size_t full_size = size * nmemb; | ||
|
||
std::string headerStr(static_cast<char *>(contents), full_size); | ||
|
||
auto colon = headerStr.find(':'); | ||
if (colon != std::string::npos) { | ||
std::string key = headerStr.substr(0, colon); | ||
std::string value = headerStr.substr(colon + 1); | ||
// Trim whitespace from key and value | ||
trim(key); | ||
trim(value); | ||
mem->addResponseHeader(key, value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apart from the trim, make the key
lowercase (only for the response header).
Reference