KiCad PCB EDA Suite
KICAD_CURL Class Reference

KICAD_CURL simple wrapper class to call curl_global_init and curl_global_cleanup for KiCad. More...

#include <kicad_curl.h>

Static Public Member Functions

static void Init ()
 Function Init calls curl_global_init for the application. More...
 
static void Cleanup ()
 Function Cleanup calls curl_global_cleanup for the application. More...
 
static const char * GetVersion ()
 Function GetVersion wrapper for curl_version(). More...
 
static std::string GetSimpleVersion ()
 Function GetSimpleVersion Reports back curl version only and SSL library support. More...
 

Friends

class KICAD_CURL_EASY
 

Detailed Description

KICAD_CURL simple wrapper class to call curl_global_init and curl_global_cleanup for KiCad.

Definition at line 64 of file kicad_curl.h.

Member Function Documentation

◆ Cleanup()

void KICAD_CURL::Cleanup ( )
static

Function Cleanup calls curl_global_cleanup for the application.

It must be used only after curl_global_init was called.

Definition at line 174 of file kicad_curl.cpp.

175 {
176  /*
177 
178  Calling lock_guard() from a static destructor will typically be bad, since the
179  s_lock may already have been statically destroyed itself leading to a boost
180  exception. (Remember C++ does not provide certain sequencing of static
181  destructor invocation.)
182 
183  To prevent this we test s_initialized twice, which ensures that the lock_guard
184  is only instantiated on the first call, which should be from
185  PGM_BASE::destroy() which is first called earlier than static destruction.
186  Then when called again from the actual PGM_BASE::~PGM_BASE() function,
187  lock_guard will not be instantiated because s_initialized will be false.
188 
189  */
190 
191  if( s_initialized )
192  {
193  std::lock_guard<std::mutex> lock( s_lock );
194 
195  if( s_initialized )
196  {
197  curl_global_cleanup();
198 
199  kill_locks();
200 
201  atexit( &at_terminate );
202 
203  s_initialized = false;
204  }
205  }
206 }
static std::mutex s_lock
Definition: kicad_curl.cpp:40
static void at_terminate()
At process termination, using atexit() keeps the CURL stuff out of the singletops and PGM_BASE.
Definition: kicad_curl.cpp:144
static volatile bool s_initialized
Definition: kicad_curl.cpp:38
void kill_locks()
Definition: kicad_curl.cpp:138

References at_terminate(), kill_locks(), s_initialized, and s_lock.

Referenced by at_terminate().

◆ GetSimpleVersion()

std::string KICAD_CURL::GetSimpleVersion ( )
static

Function GetSimpleVersion Reports back curl version only and SSL library support.

Returns
std::string - Generated version string

Definition at line 209 of file kicad_curl.cpp.

210 {
211  if( !s_initialized )
212  Init();
213 
214  curl_version_info_data* info = curl_version_info( CURLVERSION_NOW );
215 
216  std::string res;
217 
218  if( info->version )
219  {
220  res += "libcurl version: " + std::string( info->version );
221  }
222 
223  res += " (";
224 
225  if( info->features & CURL_VERSION_SSL )
226  {
227  res += "with SSL - ";
228  res += std::string( info->ssl_version );
229  }
230  else
231  {
232  res += "without SSL";
233  }
234  res += ")";
235 
236  return res;
237 }
static void Init()
Function Init calls curl_global_init for the application.
Definition: kicad_curl.cpp:150
static volatile bool s_initialized
Definition: kicad_curl.cpp:38

References Init(), and s_initialized.

◆ GetVersion()

static const char* KICAD_CURL::GetVersion ( )
inlinestatic

Function GetVersion wrapper for curl_version().

Reports back a short string of loaded libraries.

Returns
const char* - String reported by libcurl and owned by it.
Exceptions
IO_ERRORon failure, hopefully with helpful text in it.

Definition at line 93 of file kicad_curl.h.

94  {
95  return curl_version();
96  }

Referenced by GetKicadCurlVersion().

◆ Init()

void KICAD_CURL::Init ( )
static

Function Init calls curl_global_init for the application.

It must be used only once and before any curl functions that perform requests.

Returns
bool - True if successful, false if CURL returned an error
Exceptions
IO_ERRORon failure, hopefully with helpful text in it.

Definition at line 150 of file kicad_curl.cpp.

151 {
152  // We test s_initialized twice in an effort to avoid
153  // unnecessarily locking s_lock. This understands that the common case
154  // will not need to lock.
155  if( !s_initialized )
156  {
157  std::lock_guard<std::mutex> lock( s_lock );
158 
159  if( !s_initialized )
160  {
161  if( curl_global_init( CURL_GLOBAL_ALL ) != CURLE_OK )
162  {
163  THROW_IO_ERROR( "curl_global_init() failed." );
164  }
165 
166  init_locks();
167 
168  s_initialized = true;
169  }
170  }
171 }
static std::mutex s_lock
Definition: kicad_curl.cpp:40
static volatile bool s_initialized
Definition: kicad_curl.cpp:38
void init_locks()
Definition: kicad_curl.cpp:137
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38

References init_locks(), s_initialized, s_lock, and THROW_IO_ERROR.

Referenced by GetSimpleVersion(), and KICAD_CURL_EASY::KICAD_CURL_EASY().

Friends And Related Function Documentation

◆ KICAD_CURL_EASY

friend class KICAD_CURL_EASY
friend

Definition at line 66 of file kicad_curl.h.


The documentation for this class was generated from the following files: