Add option to not call global init / cleanup.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
tastytea 2021-02-08 23:16:20 +01:00
parent d9756db965
commit fe5707b610
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
2 changed files with 23 additions and 11 deletions

View File

@ -1,5 +1,5 @@
/* This file is part of curl_wrapper.
* Copyright © 2020 tastytea <tastytea@tastytea.de>
* Copyright © 2020, 2021 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
@ -29,16 +29,21 @@
namespace curl_wrapper
{
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
inline static std::atomic<std::uint64_t> curlwrapper_instances{0};
CURLWrapper::CURLWrapper()
CURLWrapper::CURLWrapper(const bool init)
: _init{init}
{
if (curlwrapper_instances == 0)
if (_init)
{
// NOLINTNEXTLINE(hicpp-signed-bitwise)
check(curl_global_init(CURL_GLOBAL_ALL));
if (curlwrapper_instances == 0)
{
// NOLINTNEXTLINE(hicpp-signed-bitwise)
check(curl_global_init(CURL_GLOBAL_ALL));
}
++curlwrapper_instances;
}
++curlwrapper_instances;
_connection = curl_easy_init();
if (_connection == nullptr)
@ -69,11 +74,14 @@ CURLWrapper::CURLWrapper()
CURLWrapper::~CURLWrapper() noexcept
{
curl_easy_cleanup(_connection);
--curlwrapper_instances;
if (curlwrapper_instances == 0)
if (_init)
{
curl_global_cleanup();
--curlwrapper_instances;
if (curlwrapper_instances == 0)
{
curl_global_cleanup();
}
}
}

View File

@ -1,5 +1,5 @@
/* This file is part of curl_wrapper.
* Copyright © 2020 tastytea <tastytea@tastytea.de>
* Copyright © 2020, 2021 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
@ -49,9 +49,12 @@ public:
*
* May throw CURLException or std::runtime_error.
*
* @param init If false, do not do global init and cleanup. Default is
* true.
*
* @since 0.1.0
*/
CURLWrapper();
explicit CURLWrapper(bool init = true);
/*!
* @brief Cleans up curl and connection.
@ -171,6 +174,7 @@ public:
void set_maxredirs(long redirections); // NOLINT(google-runtime-int)
private:
const bool _init;
CURL *_connection{};
char _buffer_error[CURL_ERROR_SIZE]{};
string _buffer_headers;