diff --git a/.clang-tidy b/.clang-tidy
index 55570bf18..34e684a65 100644
--- a/.clang-tidy
+++ b/.clang-tidy
@@ -38,8 +38,14 @@ Checks: '*,
-cppcoreguidelines-avoid-do-while,
-llvmlibc-inline-function-decl,
-altera-struct-pack-align,
--boost-use-ranges
+-boost-use-ranges,
+-cppcoreguidelines-special-member-functions,
+-hicpp-special-member-functions,
+-misc-header-include-cycle,
+-cppcoreguidelines-avoid-const-or-ref-data-members,
+-google-explicit-constructor,
+-hicpp-explicit-conversions
'
WarningsAsErrors: '*'
-HeaderFilterRegex: 'src\/*.hpp'
+HeaderFilterRegex: 'include\/cpr\/.*\.h(pp)?'
FormatStyle: file
diff --git a/cppcheck-suppressions.xml b/cppcheck-suppressions.xml
index 8995984be..6376624d1 100644
--- a/cppcheck-suppressions.xml
+++ b/cppcheck-suppressions.xml
@@ -39,4 +39,8 @@
postfixOperator
+
+ syntaxError
+ */cpr/util.cpp
+
\ No newline at end of file
diff --git a/cpr/async.cpp b/cpr/async.cpp
index e10d09e1f..b4adab778 100644
--- a/cpr/async.cpp
+++ b/cpr/async.cpp
@@ -3,6 +3,6 @@
namespace cpr {
// NOLINTNEXTLINE (cppcoreguidelines-avoid-non-const-global-variables)
-CPR_SINGLETON_IMPL(GlobalThreadPool)
+CPR_SINGLETON_IMPL(GlobalThreadPool);
} // namespace cpr
diff --git a/cpr/callback.cpp b/cpr/callback.cpp
index f2d257a2c..c08215eff 100644
--- a/cpr/callback.cpp
+++ b/cpr/callback.cpp
@@ -3,12 +3,12 @@
#include
namespace cpr {
-
void CancellationCallback::SetProgressCallback(ProgressCallback& u_cb) {
user_cb.emplace(std::reference_wrapper{u_cb});
}
+
bool CancellationCallback::operator()(cpr_pf_arg_t dltotal, cpr_pf_arg_t dlnow, cpr_pf_arg_t ultotal, cpr_pf_arg_t ulnow) const {
- const bool cont_operation{!cancellation_state->load()};
- return user_cb ? (cont_operation && (*user_cb)(dltotal, dlnow, ultotal, ulnow)) : cont_operation;
+ const bool const_operation = !(cancellation_state->load());
+ return user_cb ? (const_operation && (*user_cb)(dltotal, dlnow, ultotal, ulnow)) : const_operation;
}
} // namespace cpr
diff --git a/cpr/connection_pool.cpp b/cpr/connection_pool.cpp
index 6afb8643e..e645e82d8 100644
--- a/cpr/connection_pool.cpp
+++ b/cpr/connection_pool.cpp
@@ -7,33 +7,32 @@ namespace cpr {
ConnectionPool::ConnectionPool() {
CURLSH* curl_share = curl_share_init();
this->connection_mutex_ = std::make_shared();
-
+
auto lock_f = +[](CURL* /*handle*/, curl_lock_data /*data*/, curl_lock_access /*access*/, void* userptr) {
std::mutex* lock = static_cast(userptr);
lock->lock(); // cppcheck-suppress localMutex // False positive: mutex is used as callback for libcurl, not local scope
};
-
+
auto unlock_f = +[](CURL* /*handle*/, curl_lock_data /*data*/, void* userptr) {
std::mutex* lock = static_cast(userptr);
lock->unlock();
};
-
+
curl_share_setopt(curl_share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
curl_share_setopt(curl_share, CURLSHOPT_USERDATA, this->connection_mutex_.get());
curl_share_setopt(curl_share, CURLSHOPT_LOCKFUNC, lock_f);
curl_share_setopt(curl_share, CURLSHOPT_UNLOCKFUNC, unlock_f);
-
- this->curl_sh_ = std::shared_ptr(curl_share,
- [](CURLSH* ptr) {
- // Make sure to reset callbacks before cleanup to avoid deadlocks
- curl_share_setopt(ptr, CURLSHOPT_LOCKFUNC, nullptr);
- curl_share_setopt(ptr, CURLSHOPT_UNLOCKFUNC, nullptr);
- curl_share_cleanup(ptr);
- });
+
+ this->curl_sh_ = std::shared_ptr(curl_share, [](CURLSH* ptr) {
+ // Make sure to reset callbacks before cleanup to avoid deadlocks
+ curl_share_setopt(ptr, CURLSHOPT_LOCKFUNC, nullptr);
+ curl_share_setopt(ptr, CURLSHOPT_UNLOCKFUNC, nullptr);
+ curl_share_cleanup(ptr);
+ });
}
void ConnectionPool::SetupHandler(CURL* easy_handler) const {
curl_easy_setopt(easy_handler, CURLOPT_SHARE, this->curl_sh_.get());
}
-} // namespace cpr
\ No newline at end of file
+} // namespace cpr
\ No newline at end of file
diff --git a/cpr/cprtypes.cpp b/cpr/cprtypes.cpp
index 43df13491..d3cb78d92 100644
--- a/cpr/cprtypes.cpp
+++ b/cpr/cprtypes.cpp
@@ -1,9 +1,9 @@
#include "cpr/cprtypes.h"
-
#include
#include
#include
+
namespace cpr {
bool CaseInsensitiveCompare::operator()(const std::string& a, const std::string& b) const noexcept {
return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end(), [](unsigned char ac, unsigned char bc) { return std::tolower(ac) < std::tolower(bc); });
diff --git a/cpr/curlholder.cpp b/cpr/curlholder.cpp
index 4c8084634..3c397864a 100644
--- a/cpr/curlholder.cpp
+++ b/cpr/curlholder.cpp
@@ -22,7 +22,7 @@ CurlHolder::CurlHolder() {
assert(handle);
}
-CurlHolder::CurlHolder(CurlHolder&& old) noexcept : handle(old.handle), chunk(old.chunk), resolveCurlList(old.resolveCurlList), multipart(old.multipart), error(std::move(old.error)) {
+CurlHolder::CurlHolder(CurlHolder&& old) noexcept : handle(old.handle), chunk(old.chunk), resolveCurlList(old.resolveCurlList), multipart(old.multipart), error(old.error) {
// Avoid double free
old.handle = nullptr;
old.chunk = nullptr;
@@ -49,7 +49,7 @@ CurlHolder& CurlHolder::operator=(CurlHolder&& old) noexcept {
chunk = old.chunk;
resolveCurlList = old.resolveCurlList;
multipart = old.multipart;
- error = std::move(old.error);
+ error = old.error;
// Avoid double free
old.handle = nullptr;
diff --git a/cpr/error.cpp b/cpr/error.cpp
index 4c533a670..3ce918fa6 100644
--- a/cpr/error.cpp
+++ b/cpr/error.cpp
@@ -4,220 +4,223 @@
#include
namespace cpr {
+// NOLINTBEGIN(bugprone-branch-clone) Fine in this case
ErrorCode Error::getErrorCodeForCurlError(std::int32_t curl_code) {
- switch (curl_code)
- {
+ switch (curl_code) {
case CURLE_OK:
- return ErrorCode::OK;
+ return ErrorCode::OK;
case CURLE_UNSUPPORTED_PROTOCOL:
- return ErrorCode::UNSUPPORTED_PROTOCOL;
+ return ErrorCode::UNSUPPORTED_PROTOCOL;
case CURLE_FAILED_INIT:
- return ErrorCode::FAILED_INIT;
+ return ErrorCode::FAILED_INIT;
case CURLE_URL_MALFORMAT:
- return ErrorCode::URL_MALFORMAT;
+ return ErrorCode::URL_MALFORMAT;
case CURLE_NOT_BUILT_IN:
- return ErrorCode::NOT_BUILT_IN;
+ return ErrorCode::NOT_BUILT_IN;
case CURLE_COULDNT_RESOLVE_PROXY:
- return ErrorCode::COULDNT_RESOLVE_PROXY;
+ return ErrorCode::COULDNT_RESOLVE_PROXY;
case CURLE_COULDNT_RESOLVE_HOST:
- return ErrorCode::COULDNT_RESOLVE_HOST;
+ return ErrorCode::COULDNT_RESOLVE_HOST;
case CURLE_COULDNT_CONNECT:
- return ErrorCode::COULDNT_CONNECT;
-
+ return ErrorCode::COULDNT_CONNECT;
+
// Name changed in curl >= 7.51.0.
#if LIBCURL_VERSION_NUM >= 0x073300
case CURLE_WEIRD_SERVER_REPLY:
- return ErrorCode::WEIRD_SERVER_REPLY;
+ return ErrorCode::WEIRD_SERVER_REPLY;
#else
case CURLE_FTP_WEIRD_SERVER_REPLY:
- return ErrorCode::WEIRD_SERVER_REPLY;
+ return ErrorCode::WEIRD_SERVER_REPLY;
#endif
+
case CURLE_REMOTE_ACCESS_DENIED:
- return ErrorCode::REMOTE_ACCESS_DENIED;
+ return ErrorCode::REMOTE_ACCESS_DENIED;
case CURLE_HTTP2:
- return ErrorCode::HTTP2;
+ return ErrorCode::HTTP2;
case CURLE_QUOTE_ERROR:
- return ErrorCode::QUOTE_ERROR;
+ return ErrorCode::QUOTE_ERROR;
case CURLE_HTTP_RETURNED_ERROR:
- return ErrorCode::HTTP_RETURNED_ERROR;
+ return ErrorCode::HTTP_RETURNED_ERROR;
case CURLE_WRITE_ERROR:
- return ErrorCode::WRITE_ERROR;
+ return ErrorCode::WRITE_ERROR;
case CURLE_UPLOAD_FAILED:
- return ErrorCode::UPLOAD_FAILED;
+ return ErrorCode::UPLOAD_FAILED;
case CURLE_READ_ERROR:
- return ErrorCode::READ_ERROR;
+ return ErrorCode::READ_ERROR;
case CURLE_OUT_OF_MEMORY:
- return ErrorCode::OUT_OF_MEMORY;
+ return ErrorCode::OUT_OF_MEMORY;
case CURLE_OPERATION_TIMEDOUT:
- return ErrorCode::OPERATION_TIMEDOUT;
+ return ErrorCode::OPERATION_TIMEDOUT;
case CURLE_RANGE_ERROR:
- return ErrorCode::RANGE_ERROR;
+ return ErrorCode::RANGE_ERROR;
case CURLE_HTTP_POST_ERROR:
- return ErrorCode::HTTP_POST_ERROR;
+ return ErrorCode::HTTP_POST_ERROR;
case CURLE_SSL_CONNECT_ERROR:
- return ErrorCode::SSL_CONNECT_ERROR;
+ return ErrorCode::SSL_CONNECT_ERROR;
case CURLE_BAD_DOWNLOAD_RESUME:
- return ErrorCode::BAD_DOWNLOAD_RESUME;
+ return ErrorCode::BAD_DOWNLOAD_RESUME;
case CURLE_FILE_COULDNT_READ_FILE:
- return ErrorCode::FILE_COULDNT_READ_FILE;
+ return ErrorCode::FILE_COULDNT_READ_FILE;
case CURLE_FUNCTION_NOT_FOUND:
- return ErrorCode::FUNCTION_NOT_FOUND;
+ return ErrorCode::FUNCTION_NOT_FOUND;
case CURLE_ABORTED_BY_CALLBACK:
- return ErrorCode::ABORTED_BY_CALLBACK;
+ return ErrorCode::ABORTED_BY_CALLBACK;
case CURLE_BAD_FUNCTION_ARGUMENT:
- return ErrorCode::BAD_FUNCTION_ARGUMENT;
+ return ErrorCode::BAD_FUNCTION_ARGUMENT;
case CURLE_INTERFACE_FAILED:
- return ErrorCode::INTERFACE_FAILED;
+ return ErrorCode::INTERFACE_FAILED;
case CURLE_TOO_MANY_REDIRECTS:
- return ErrorCode::TOO_MANY_REDIRECTS;
+ return ErrorCode::TOO_MANY_REDIRECTS;
case CURLE_UNKNOWN_OPTION:
- return ErrorCode::UNKNOWN_OPTION;
+ return ErrorCode::UNKNOWN_OPTION;
+
// Added in curl 7.78.0.
#if LIBCURL_VERSION_NUM >= 0x074E00
case CURLE_SETOPT_OPTION_SYNTAX:
- return ErrorCode::SETOPT_OPTION_SYNTAX;
+ return ErrorCode::SETOPT_OPTION_SYNTAX;
#endif
+
case CURLE_GOT_NOTHING:
- return ErrorCode::GOT_NOTHING;
+ return ErrorCode::GOT_NOTHING;
case CURLE_SSL_ENGINE_NOTFOUND:
- return ErrorCode::SSL_ENGINE_NOTFOUND;
+ return ErrorCode::SSL_ENGINE_NOTFOUND;
case CURLE_SSL_ENGINE_SETFAILED:
- return ErrorCode::SSL_ENGINE_SETFAILED;
+ return ErrorCode::SSL_ENGINE_SETFAILED;
case CURLE_SEND_ERROR:
- return ErrorCode::SEND_ERROR;
+ return ErrorCode::SEND_ERROR;
case CURLE_RECV_ERROR:
- return ErrorCode::RECV_ERROR;
+ return ErrorCode::RECV_ERROR;
case CURLE_SSL_CERTPROBLEM:
- return ErrorCode::SSL_CERTPROBLEM;
+ return ErrorCode::SSL_CERTPROBLEM;
case CURLE_SSL_CIPHER:
- return ErrorCode::SSL_CIPHER;
+ return ErrorCode::SSL_CIPHER;
case CURLE_PEER_FAILED_VERIFICATION:
- return ErrorCode::PEER_FAILED_VERIFICATION;
+ return ErrorCode::PEER_FAILED_VERIFICATION;
case CURLE_BAD_CONTENT_ENCODING:
- return ErrorCode::BAD_CONTENT_ENCODING;
+ return ErrorCode::BAD_CONTENT_ENCODING;
case CURLE_FILESIZE_EXCEEDED:
- return ErrorCode::FILESIZE_EXCEEDED;
+ return ErrorCode::FILESIZE_EXCEEDED;
case CURLE_USE_SSL_FAILED:
- return ErrorCode::USE_SSL_FAILED;
+ return ErrorCode::USE_SSL_FAILED;
case CURLE_SEND_FAIL_REWIND:
- return ErrorCode::SEND_FAIL_REWIND;
+ return ErrorCode::SEND_FAIL_REWIND;
case CURLE_SSL_ENGINE_INITFAILED:
- return ErrorCode::SSL_ENGINE_INITFAILED;
+ return ErrorCode::SSL_ENGINE_INITFAILED;
// Added in curl 7.13.1.
#if LIBCURL_VERSION_NUM >= 0x070D01
case CURLE_LOGIN_DENIED:
- return ErrorCode::LOGIN_DENIED;
+ return ErrorCode::LOGIN_DENIED;
#endif
// Added in curl 7.16.0.
#if LIBCURL_VERSION_NUM >= 0x071000
case CURLE_SSL_CACERT_BADFILE:
- return ErrorCode::SSL_CACERT_BADFILE;
+ return ErrorCode::SSL_CACERT_BADFILE;
#endif
// Added in curl 7.16.1.
#if LIBCURL_VERSION_NUM >= 0x071001
case CURLE_SSL_SHUTDOWN_FAILED:
- return ErrorCode::SSL_SHUTDOWN_FAILED;
+ return ErrorCode::SSL_SHUTDOWN_FAILED;
#endif
// Added in curl 7.18.2.
#if LIBCURL_VERSION_NUM >= 0x071202
case CURLE_AGAIN:
- return ErrorCode::AGAIN;
+ return ErrorCode::AGAIN;
#endif
// Added in curl 7.19.0.
#if LIBCURL_VERSION_NUM >= 0x071300
case CURLE_SSL_CRL_BADFILE:
- return ErrorCode::SSL_CRL_BADFILE;
+ return ErrorCode::SSL_CRL_BADFILE;
case CURLE_SSL_ISSUER_ERROR:
- return ErrorCode::SSL_ISSUER_ERROR;
+ return ErrorCode::SSL_ISSUER_ERROR;
#endif
// Added in curl 7.21.0.
#if LIBCURL_VERSION_NUM >= 0x071500
case CURLE_CHUNK_FAILED:
- return ErrorCode::CHUNK_FAILED;
+ return ErrorCode::CHUNK_FAILED;
#endif
// Added in curl 7.30.0.
#if LIBCURL_VERSION_NUM >= 0x071E00
case CURLE_NO_CONNECTION_AVAILABLE:
- return ErrorCode::NO_CONNECTION_AVAILABLE;
+ return ErrorCode::NO_CONNECTION_AVAILABLE;
#endif
// Added in curl 7.39.0.
#if LIBCURL_VERSION_NUM >= 0x072700
case CURLE_SSL_PINNEDPUBKEYNOTMATCH:
- return ErrorCode::SSL_PINNEDPUBKEYNOTMATCH;
+ return ErrorCode::SSL_PINNEDPUBKEYNOTMATCH;
#endif
// Added in curl 7.41.0.
#if LIBCURL_VERSION_NUM >= 0x072900
case CURLE_SSL_INVALIDCERTSTATUS:
- return ErrorCode::SSL_INVALIDCERTSTATUS;
+ return ErrorCode::SSL_INVALIDCERTSTATUS;
#endif
// Added in curl 7.49.0.
#if LIBCURL_VERSION_NUM >= 0x073100
case CURLE_HTTP2_STREAM:
- return ErrorCode::HTTP2_STREAM;
+ return ErrorCode::HTTP2_STREAM;
#endif
case CURLE_PARTIAL_FILE:
- return ErrorCode::PARTIAL_FILE;
+ return ErrorCode::PARTIAL_FILE;
// Added in curl 7.59.0.
#if LIBCURL_VERSION_NUM >= 0x073B00
case CURLE_RECURSIVE_API_CALL:
- return ErrorCode::RECURSIVE_API_CALL;
+ return ErrorCode::RECURSIVE_API_CALL;
#endif
// Added in curl 7.66.0.
#if LIBCURL_VERSION_NUM >= 0x074200
case CURLE_AUTH_ERROR:
- return ErrorCode::AUTH_ERROR;
+ return ErrorCode::AUTH_ERROR;
#endif
// Added in curl 7.68.0.
#if LIBCURL_VERSION_NUM >= 0x074400
case CURLE_HTTP3:
- return ErrorCode::HTTP3;
+ return ErrorCode::HTTP3;
#endif
// Added in curl 7.69.0.
#if LIBCURL_VERSION_NUM >= 0x074500
case CURLE_QUIC_CONNECT_ERROR:
- return ErrorCode::QUIC_CONNECT_ERROR;
+ return ErrorCode::QUIC_CONNECT_ERROR;
#endif
// Added in curl 7.73.0.
#if LIBCURL_VERSION_NUM >= 0x074900
case CURLE_PROXY:
- return ErrorCode::PROXY;
+ return ErrorCode::PROXY;
#endif
// Added in curl 7.77.0.
#if LIBCURL_VERSION_NUM >= 0x074D00
case CURLE_SSL_CLIENTCERT:
- return ErrorCode::SSL_CLIENTCERT;
+ return ErrorCode::SSL_CLIENTCERT;
#endif
// Added in curl 7.84.0.
#if LIBCURL_VERSION_NUM >= 0x075400
case CURLE_UNRECOVERABLE_POLL:
- return ErrorCode::UNRECOVERABLE_POLL;
+ return ErrorCode::UNRECOVERABLE_POLL;
#endif
// Added in curl 7.6.0.
#if LIBCURL_VERSION_NUM >= 0x080600
case CURLE_TOO_LARGE:
- return ErrorCode::TOO_LARGE;
+ return ErrorCode::TOO_LARGE;
#endif
default:
- return ErrorCode::UNKNOWN_ERROR;
- }
+ return ErrorCode::UNKNOWN_ERROR;
+ }
}
-
+// NOLINTEND(bugprone-branch-clone)
} // namespace cpr
\ No newline at end of file
diff --git a/cpr/response.cpp b/cpr/response.cpp
index 4bfa3d5ed..19f75858e 100644
--- a/cpr/response.cpp
+++ b/cpr/response.cpp
@@ -21,7 +21,7 @@ Response::Response(std::shared_ptr curl, std::string&& p_text, std::
assert(curl_->handle);
curl_easy_getinfo(curl_->handle, CURLINFO_RESPONSE_CODE, &status_code);
curl_easy_getinfo(curl_->handle, CURLINFO_TOTAL_TIME, &elapsed);
- char* url_string{nullptr};
+ const char* url_string{nullptr};
curl_easy_getinfo(curl_->handle, CURLINFO_EFFECTIVE_URL, &url_string);
url = Url(url_string);
#if LIBCURL_VERSION_NUM >= 0x073700 // 7.55.0
@@ -36,7 +36,7 @@ Response::Response(std::shared_ptr curl, std::string&& p_text, std::
#endif
curl_easy_getinfo(curl_->handle, CURLINFO_REDIRECT_COUNT, &redirect_count);
#if LIBCURL_VERSION_NUM >= 0x071300 // 7.19.0
- char* ip_ptr{nullptr};
+ const char* ip_ptr{nullptr};
if (curl_easy_getinfo(curl_->handle, CURLINFO_PRIMARY_IP, &ip_ptr) == CURLE_OK && ip_ptr) {
primary_ip = ip_ptr;
}
@@ -54,7 +54,7 @@ Response::Response(std::shared_ptr curl, std::string&& p_text, std::
std::vector Response::GetCertInfos() const {
assert(curl_);
assert(curl_->handle);
- curl_certinfo* ci{nullptr};
+ const curl_certinfo* ci{nullptr};
curl_easy_getinfo(curl_->handle, CURLINFO_CERTINFO, &ci);
std::vector cert_infos;
diff --git a/cpr/session.cpp b/cpr/session.cpp
index a62c8dd53..108ff2adf 100644
--- a/cpr/session.cpp
+++ b/cpr/session.cpp
@@ -143,7 +143,7 @@ void Session::SetBearer(const Bearer& token) {
Session::Session() : curl_(new CurlHolder()) {
// Set up some sensible defaults
- curl_version_info_data* version_info = curl_version_info(CURLVERSION_NOW);
+ const curl_version_info_data* version_info = curl_version_info(CURLVERSION_NOW);
const std::string version = "curl/" + std::string{version_info->version};
curl_easy_setopt(curl_->handle, CURLOPT_USERAGENT, version.c_str());
SetRedirect(Redirect());
@@ -1125,7 +1125,7 @@ void Session::SetOption(const LocalPortRange& local_port_range) { SetLocalPortRa
void Session::SetOption(const HttpVersion& version) { SetHttpVersion(version); }
void Session::SetOption(const Range& range) { SetRange(range); }
void Session::SetOption(const MultiRange& multi_range) { SetMultiRange(multi_range); }
-void Session::SetOption(const ReserveSize& reserve_size) { SetReserveSize(reserve_size.size); }
+void Session::SetOption(const ReserveSize& reserve_size) { SetReserveSize(reserve_size); }
void Session::SetOption(const AcceptEncoding& accept_encoding) { SetAcceptEncoding(accept_encoding); }
void Session::SetOption(AcceptEncoding&& accept_encoding) { SetAcceptEncoding(std::move(accept_encoding)); }
void Session::SetOption(const ConnectionPool& pool) { SetConnectionPool(pool); }
diff --git a/cpr/ssl_ctx.cpp b/cpr/ssl_ctx.cpp
index da5949270..60223edb8 100644
--- a/cpr/ssl_ctx.cpp
+++ b/cpr/ssl_ctx.cpp
@@ -67,7 +67,7 @@ CURLcode sslctx_function_load_ca_cert_from_buffer(CURL* /*curl*/, void* sslctx,
// Create a memory BIO using the data of cert_buf
// Note: It is assumed, that cert_buf is nul terminated and its length is determined by strlen
- char* cert_buf = static_cast(raw_cert_buf);
+ const char* cert_buf = static_cast(raw_cert_buf);
BIO* bio = BIO_new_mem_buf(cert_buf, -1);
// Get a pointer to the current certificate verification storage
diff --git a/cpr/threadpool.cpp b/cpr/threadpool.cpp
index 105c02039..04a5adc94 100644
--- a/cpr/threadpool.cpp
+++ b/cpr/threadpool.cpp
@@ -16,10 +16,10 @@ ThreadPool::~ThreadPool() {
}
int ThreadPool::Start(size_t start_threads) {
- if (status != STOP) {
+ if (status != Status::STOP) {
return -1;
}
- status = RUNNING;
+ status = Status::RUNNING;
start_threads = std::clamp(start_threads, min_thread_num, max_thread_num);
for (size_t i = 0; i < start_threads; ++i) {
CreateThread();
@@ -29,11 +29,11 @@ int ThreadPool::Start(size_t start_threads) {
int ThreadPool::Stop() {
const std::unique_lock status_lock(status_wait_mutex);
- if (status == STOP) {
+ if (status == Status::STOP) {
return -1;
}
- status = STOP;
+ status = Status::STOP;
status_wait_cond.notify_all();
task_cond.notify_all();
@@ -50,29 +50,28 @@ int ThreadPool::Stop() {
}
int ThreadPool::Pause() {
- if (status == RUNNING) {
- status = PAUSE;
+ if (status == Status::RUNNING) {
+ status = Status::PAUSE;
}
return 0;
}
int ThreadPool::Resume() {
const std::unique_lock status_lock(status_wait_mutex);
- if (status == PAUSE) {
- status = RUNNING;
+ if (status == Status::PAUSE) {
+ status = Status::RUNNING;
status_wait_cond.notify_all();
}
return 0;
}
-int ThreadPool::Wait() const {
+void ThreadPool::Wait() {
while (true) {
- if (status == STOP || (tasks.empty() && idle_thread_num == cur_thread_num)) {
+ if (status == Status::STOP || (tasks.empty() && idle_thread_num == cur_thread_num)) {
break;
}
std::this_thread::yield();
}
- return 0;
}
bool ThreadPool::CreateThread() {
@@ -81,7 +80,7 @@ bool ThreadPool::CreateThread() {
}
auto thread = std::make_shared([this] {
bool initialRun = true;
- while (status != STOP) {
+ while (status != Status::STOP) {
{
std::unique_lock status_lock(status_wait_mutex);
status_wait_cond.wait(status_lock, [this]() { return status != Status::PAUSE; });
@@ -90,8 +89,8 @@ bool ThreadPool::CreateThread() {
Task task;
{
std::unique_lock locker(task_mutex);
- task_cond.wait_for(locker, std::chrono::milliseconds(max_idle_time), [this]() { return status == STOP || !tasks.empty(); });
- if (status == STOP) {
+ task_cond.wait_for(locker, std::chrono::milliseconds(max_idle_time), [this]() { return status == Status::STOP || !tasks.empty(); });
+ if (status == Status::STOP) {
return;
}
if (tasks.empty()) {
@@ -124,7 +123,7 @@ void ThreadPool::AddThread(const std::shared_ptr& thread) {
ThreadData data;
data.thread = thread;
data.id = thread->get_id();
- data.status = RUNNING;
+ data.status = Status::RUNNING;
data.start_time = std::chrono::steady_clock::now();
data.stop_time = std::chrono::steady_clock::time_point::max();
threads.emplace_back(data);
@@ -139,14 +138,14 @@ void ThreadPool::DelThread(std::thread::id id) {
--idle_thread_num;
auto iter = threads.begin();
while (iter != threads.end()) {
- if (iter->status == STOP && now > iter->stop_time) {
+ if (iter->status == Status::STOP && now > iter->stop_time) {
if (iter->thread->joinable()) {
iter->thread->join();
iter = threads.erase(iter);
continue;
}
} else if (iter->id == id) {
- iter->status = STOP;
+ iter->status = Status::STOP;
iter->stop_time = std::chrono::steady_clock::now();
}
++iter;
diff --git a/cpr/util.cpp b/cpr/util.cpp
index 8bf5426a2..a89552c4f 100644
--- a/cpr/util.cpp
+++ b/cpr/util.cpp
@@ -18,7 +18,7 @@
#include
#include
-#if defined(_Win32)
+#ifdef _Win32
#include
#else
#ifdef __clang__
@@ -52,7 +52,7 @@ enum class CurlHTTPCookieField : uint8_t {
Cookies parseCookies(curl_slist* raw_cookies) {
const int CURL_HTTP_COOKIE_SIZE = static_cast(CurlHTTPCookieField::Value) + 1;
Cookies cookies;
- for (curl_slist* nc = raw_cookies; nc; nc = nc->next) {
+ for (const curl_slist* nc = raw_cookies; nc; nc = nc->next) {
std::vector tokens = cpr::util::split(nc->data, '\t');
while (tokens.size() < CURL_HTTP_COOKIE_SIZE) {
tokens.emplace_back("");
diff --git a/include/cpr/accept_encoding.h b/include/cpr/accept_encoding.h
index 167d7c2cf..65a08e058 100644
--- a/include/cpr/accept_encoding.h
+++ b/include/cpr/accept_encoding.h
@@ -1,15 +1,17 @@
#ifndef CPR_ACCEPT_ENCODING_H
#define CPR_ACCEPT_ENCODING_H
+#include
#include
#include
#include