KiCad PCB EDA Suite
KICAD_CURL Class Reference

Class 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

Class 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 176 of file kicad_curl.cpp.

177 {
178  /*
179 
180  Calling lock_guard() from a static destructor will typically be bad, since the
181  s_lock may already have been statically destroyed itself leading to a boost
182  exception. (Remember C++ does not provide certain sequencing of static
183  destructor invocation.)
184 
185  To prevent this we test s_initialized twice, which ensures that the lock_guard
186  is only instantiated on the first call, which should be from
187  PGM_BASE::destroy() which is first called earlier than static destruction.
188  Then when called again from the actual PGM_BASE::~PGM_BASE() function,
189  lock_guard will not be instantiated because s_initialized will be false.
190 
191  */
192 
193  if( s_initialized )
194  {
195  std::lock_guard<std::mutex> lock( s_lock );
196 
197  if( s_initialized )
198  {
199  curl_global_cleanup();
200 
201  kill_locks();
202 
203  atexit( &at_terminate );
204 
205  s_initialized = false;
206  }
207  }
208 }
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 211 of file kicad_curl.cpp.

212 {
213  if( !s_initialized )
214  Init();
215 
216  curl_version_info_data* info = curl_version_info( CURLVERSION_NOW );
217 
218  std::string res;
219 
220  if( info->version )
221  {
222  res += "libcurl version: " + std::string( info->version );
223  }
224 
225  res += " (";
226 
227  if( info->features & CURL_VERSION_SSL )
228  {
229  res += "with SSL - ";
230  res += std::string( info->ssl_version );
231  }
232  else
233  {
234  res += "without SSL";
235  }
236  res += ")";
237 
238  return res;
239 }
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  //wxLogDebug( "Using %s", GetVersion() );
169 
170  s_initialized = true;
171  }
172  }
173 }
static std::mutex s_lock
Definition: kicad_curl.cpp:40
#define THROW_IO_ERROR(msg)
static volatile bool s_initialized
Definition: kicad_curl.cpp:38
void init_locks()
Definition: kicad_curl.cpp:137

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: