Skip to content

Commit

Permalink
Clang-Tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
COM8 committed Sep 26, 2024
1 parent 42a0c5f commit 243bd04
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 29 deletions.
3 changes: 2 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ Checks: '*,
-cppcoreguidelines-avoid-magic-numbers,
-readability-magic-numbers,
-cppcoreguidelines-avoid-do-while,
-cppcoreguidelines-rvalue-reference-param-not-moved
-cppcoreguidelines-rvalue-reference-param-not-moved,
-misc-header-include-cycle
'
WarningsAsErrors: '*'
HeaderFilterRegex: 'src/*.hpp'
Expand Down
8 changes: 4 additions & 4 deletions cpr/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,14 @@ void Session::SetUserAgent(const UserAgent& ua) {
void Session::SetPayload(const Payload& payload) {
hasBodyOrPayload_ = true;
const std::string content = payload.GetContent(*curl_);
curl_easy_setopt(curl_->handle, CURLOPT_POSTFIELDSIZE_LARGE, static_cast<curl_off_t>(content.length()));
curl_easy_setopt(curl_->handle, CURLOPT_POSTFIELDSIZE_LARGE, static_cast<cpr_pf_arg_t>(content.length()));
curl_easy_setopt(curl_->handle, CURLOPT_COPYPOSTFIELDS, content.c_str());
}

void Session::SetPayload(Payload&& payload) {
hasBodyOrPayload_ = true;
const std::string content = payload.GetContent(*curl_);
curl_easy_setopt(curl_->handle, CURLOPT_POSTFIELDSIZE_LARGE, static_cast<curl_off_t>(content.length()));
curl_easy_setopt(curl_->handle, CURLOPT_POSTFIELDSIZE_LARGE, static_cast<cpr_pf_arg_t>(content.length()));
curl_easy_setopt(curl_->handle, CURLOPT_COPYPOSTFIELDS, content.c_str());
}

Expand Down Expand Up @@ -454,13 +454,13 @@ void Session::SetCookies(const Cookies& cookies) {

void Session::SetBody(const Body& body) {
hasBodyOrPayload_ = true;
curl_easy_setopt(curl_->handle, CURLOPT_POSTFIELDSIZE_LARGE, static_cast<curl_off_t>(body.str().length()));
curl_easy_setopt(curl_->handle, CURLOPT_POSTFIELDSIZE_LARGE, static_cast<cpr_pf_arg_t>(body.str().length()));
curl_easy_setopt(curl_->handle, CURLOPT_POSTFIELDS, body.c_str());
}

void Session::SetBody(Body&& body) {
hasBodyOrPayload_ = true;
curl_easy_setopt(curl_->handle, CURLOPT_POSTFIELDSIZE_LARGE, static_cast<curl_off_t>(body.str().length()));
curl_easy_setopt(curl_->handle, CURLOPT_POSTFIELDSIZE_LARGE, static_cast<cpr_pf_arg_t>(body.str().length()));
curl_easy_setopt(curl_->handle, CURLOPT_COPYPOSTFIELDS, body.c_str());
}

Expand Down
7 changes: 1 addition & 6 deletions cpr/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <vector>

#include <curl/curl.h>
#include <curl/curlver.h>

#if defined(_Win32)
#include <Windows.h>
Expand Down Expand Up @@ -162,11 +161,7 @@ size_t writeUserFunction(char* ptr, size_t size, size_t nmemb, const WriteCallba
return (*write)({ptr, size}) ? size : 0;
}

#if LIBCURL_VERSION_NUM < 0x072000
int progressUserFunction(const ProgressCallback* progress, double dltotal, double dlnow, double ultotal, double ulnow) {
#else
int progressUserFunction(const ProgressCallback* progress, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) {
#endif
int progressUserFunction(const ProgressCallback* progress, cpr_pf_arg_t dltotal, cpr_pf_arg_t dlnow, cpr_pf_arg_t ultotal, cpr_pf_arg_t ulnow) {
return (*progress)(dltotal, dlnow, ultotal, ulnow) ? 0 : 1;
}

Expand Down
18 changes: 8 additions & 10 deletions include/cpr/callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

#include "cprtypes.h"

#include <atomic>
#include <cstdint>
#include <functional>
#include <optional>
#include <utility>

namespace cpr {
Expand All @@ -21,7 +19,7 @@ class ReadCallback {
return callback(buffer, buffer_size, userdata);
}

intptr_t userdata;
intptr_t userdata{};
cpr_off_t size{};
std::function<bool(char* buffer, size_t& size, intptr_t userdata)> callback;
};
Expand All @@ -35,7 +33,7 @@ class HeaderCallback {
return callback(std::move(header), userdata);
}

intptr_t userdata;
intptr_t userdata{};
std::function<bool(std::string header, intptr_t userdata)> callback;
};

Expand All @@ -48,21 +46,21 @@ class WriteCallback {
return callback(std::move(data), userdata);
}

intptr_t userdata;
intptr_t userdata{};
std::function<bool(std::string data, intptr_t userdata)> callback;
};

class ProgressCallback {
public:
ProgressCallback() = default;
// NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
ProgressCallback(std::function<bool(cpr_off_t downloadTotal, cpr_off_t downloadNow, cpr_off_t uploadTotal, cpr_off_t uploadNow, intptr_t userdata)> p_callback, intptr_t p_userdata = 0) : userdata(p_userdata), callback(std::move(p_callback)) {}
bool operator()(cpr_off_t downloadTotal, cpr_off_t downloadNow, cpr_off_t uploadTotal, cpr_off_t uploadNow) const {
ProgressCallback(std::function<bool(cpr_pf_arg_t downloadTotal, cpr_pf_arg_t downloadNow, cpr_pf_arg_t uploadTotal, cpr_pf_arg_t uploadNow, intptr_t userdata)> p_callback, intptr_t p_userdata = 0) : userdata(p_userdata), callback(std::move(p_callback)) {}
bool operator()(cpr_pf_arg_t downloadTotal, cpr_pf_arg_t downloadNow, cpr_pf_arg_t uploadTotal, cpr_pf_arg_t uploadNow) const {
return callback(downloadTotal, downloadNow, uploadTotal, uploadNow, userdata);
}

intptr_t userdata;
std::function<bool(cpr_off_t downloadTotal, cpr_off_t downloadNow, cpr_off_t uploadTotal, cpr_off_t uploadNow, intptr_t userdata)> callback;
intptr_t userdata{};
std::function<bool(cpr_pf_arg_t downloadTotal, cpr_pf_arg_t downloadNow, cpr_pf_arg_t uploadTotal, cpr_pf_arg_t uploadNow, intptr_t userdata)> callback;
};

class DebugCallback {
Expand All @@ -83,7 +81,7 @@ class DebugCallback {
callback(type, std::move(data), userdata);
}

intptr_t userdata;
intptr_t userdata{};
std::function<void(InfoType type, std::string data, intptr_t userdata)> callback;
};

Expand Down
12 changes: 11 additions & 1 deletion include/cpr/cprtypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,27 @@
#include <curl/system.h>
#include <initializer_list>
#include <map>
#include <memory>
#include <numeric>
#include <string>

#include <curl/curlver.h>

namespace cpr {

/**
* Wrapper around "curl_off_t" to prevent applications from having to link against libcurl.
**/
using cpr_off_t = curl_off_t;

/**
* The argument type for progress functions, dependent on libcurl version
**/
#if LIBCURL_VERSION_NUM < 0x072000
using cpr_pf_arg_t = double;
#else
using cpr_pf_arg_t = cpr_off_t;
#endif

template <class T>
class StringHolder {
public:
Expand Down
6 changes: 1 addition & 5 deletions include/cpr/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ size_t headerUserFunction(char* ptr, size_t size, size_t nmemb, const HeaderCall
size_t writeFunction(char* ptr, size_t size, size_t nmemb, std::string* data);
size_t writeFileFunction(char* ptr, size_t size, size_t nmemb, std::ofstream* file);
size_t writeUserFunction(char* ptr, size_t size, size_t nmemb, const WriteCallback* write);
#if LIBCURL_VERSION_NUM < 0x072000
int progressUserFunction(const ProgressCallback* progress, double dltotal, double dlnow, double ultotal, double ulnow);
#else
int progressUserFunction(const ProgressCallback* progress, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow);
#endif
int progressUserFunction(const ProgressCallback* progress, cpr_pf_arg_t dltotal, cpr_pf_arg_t dlnow, cpr_pf_arg_t ultotal, cpr_pf_arg_t ulnow);
int debugUserFunction(CURL* handle, curl_infotype type, char* data, size_t size, const DebugCallback* debug);
std::vector<std::string> split(const std::string& to_split, char delimiter);
std::string urlEncode(const std::string& s);
Expand Down
4 changes: 2 additions & 2 deletions test/callback_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ TEST(CallbackDataTests, CallbackWriteFunctionTextTest) {

TEST(CallbackDataTests, CallbackProgressFunctionCancelTest) {
Url url{server->GetBaseUrl() + "/url_post.html"};
Response response = Post(url, ProgressCallback{[](size_t /*downloadTotal*/, size_t /*downloadNow*/, size_t /*uploadTotal*/, size_t /*uploadNow*/, intptr_t /*userdata*/) -> bool { return false; }});
Response response = Post(url, ProgressCallback{[](cpr_pf_arg_t /*downloadTotal*/, cpr_pf_arg_t /*downloadNow*/, cpr_pf_arg_t /*uploadTotal*/, cpr_pf_arg_t /*uploadNow*/, intptr_t /*userdata*/) -> bool { return false; }});
EXPECT_EQ(response.error.code, ErrorCode::REQUEST_CANCELLED);
}

Expand All @@ -905,7 +905,7 @@ TEST(CallbackDataTests, CallbackProgressFunctionTotalTest) {
Body body{"x=5"};
size_t response_upload = 0;
size_t response_download = 0;
Response response = Post(url, body, ProgressCallback{[&](size_t downloadTotal, size_t /*downloadNow*/, size_t uploadTotal, size_t /*uploadNow*/, intptr_t /*userdata*/) -> bool {
Response response = Post(url, body, ProgressCallback{[&](cpr_pf_arg_t downloadTotal, cpr_pf_arg_t /*downloadNow*/, cpr_pf_arg_t uploadTotal, cpr_pf_arg_t /*uploadNow*/, intptr_t /*userdata*/) -> bool {
response_upload = uploadTotal;
response_download = downloadTotal;
return true;
Expand Down

0 comments on commit 243bd04

Please sign in to comment.