diff --git a/.clang-tidy b/.clang-tidy index a1a824617..e950962a5 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -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' diff --git a/cpr/session.cpp b/cpr/session.cpp index 30a3e709c..c693c0216 100644 --- a/cpr/session.cpp +++ b/cpr/session.cpp @@ -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(content.length())); + curl_easy_setopt(curl_->handle, CURLOPT_POSTFIELDSIZE_LARGE, static_cast(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(content.length())); + curl_easy_setopt(curl_->handle, CURLOPT_POSTFIELDSIZE_LARGE, static_cast(content.length())); curl_easy_setopt(curl_->handle, CURLOPT_COPYPOSTFIELDS, content.c_str()); } @@ -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(body.str().length())); + curl_easy_setopt(curl_->handle, CURLOPT_POSTFIELDSIZE_LARGE, static_cast(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(body.str().length())); + curl_easy_setopt(curl_->handle, CURLOPT_POSTFIELDSIZE_LARGE, static_cast(body.str().length())); curl_easy_setopt(curl_->handle, CURLOPT_COPYPOSTFIELDS, body.c_str()); } diff --git a/cpr/util.cpp b/cpr/util.cpp index 1e0473aa9..4aa5deb09 100644 --- a/cpr/util.cpp +++ b/cpr/util.cpp @@ -17,7 +17,6 @@ #include #include -#include #if defined(_Win32) #include @@ -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; } diff --git a/include/cpr/callback.h b/include/cpr/callback.h index 3c043e1c9..1442e491e 100644 --- a/include/cpr/callback.h +++ b/include/cpr/callback.h @@ -3,10 +3,8 @@ #include "cprtypes.h" -#include #include #include -#include #include namespace cpr { @@ -21,7 +19,7 @@ class ReadCallback { return callback(buffer, buffer_size, userdata); } - intptr_t userdata; + intptr_t userdata{}; cpr_off_t size{}; std::function callback; }; @@ -35,7 +33,7 @@ class HeaderCallback { return callback(std::move(header), userdata); } - intptr_t userdata; + intptr_t userdata{}; std::function callback; }; @@ -48,7 +46,7 @@ class WriteCallback { return callback(std::move(data), userdata); } - intptr_t userdata; + intptr_t userdata{}; std::function callback; }; @@ -56,13 +54,13 @@ class ProgressCallback { public: ProgressCallback() = default; // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) - ProgressCallback(std::function 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 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 callback; + intptr_t userdata{}; + std::function callback; }; class DebugCallback { @@ -83,7 +81,7 @@ class DebugCallback { callback(type, std::move(data), userdata); } - intptr_t userdata; + intptr_t userdata{}; std::function callback; }; diff --git a/include/cpr/cprtypes.h b/include/cpr/cprtypes.h index bbd2385d5..76c582063 100644 --- a/include/cpr/cprtypes.h +++ b/include/cpr/cprtypes.h @@ -4,10 +4,11 @@ #include #include #include -#include #include #include +#include + namespace cpr { /** @@ -15,6 +16,15 @@ namespace cpr { **/ 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 StringHolder { public: diff --git a/include/cpr/util.h b/include/cpr/util.h index f35ad473d..55a9ad626 100644 --- a/include/cpr/util.h +++ b/include/cpr/util.h @@ -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 split(const std::string& to_split, char delimiter); std::string urlEncode(const std::string& s); diff --git a/test/callback_tests.cpp b/test/callback_tests.cpp index 9a0ceaea8..813e4e767 100644 --- a/test/callback_tests.cpp +++ b/test/callback_tests.cpp @@ -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); } @@ -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;