KiCad PCB EDA Suite
KICAD_CURL Class Reference

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 ()
 Call curl_global_init for the application. More...
 
static void Cleanup ()
 Call curl_global_cleanup for the application. More...
 
static const char * GetVersion ()
 Wrapper for curl_version(). More...
 
static std::string GetSimpleVersion ()
 Report back curl version only and SSL library support. More...
 

Friends

class KICAD_CURL_EASY
 

Detailed Description

Simple wrapper class to call curl_global_init and curl_global_cleanup for KiCad.

Definition at line 63 of file kicad_curl.h.

Member Function Documentation

◆ Cleanup()

void KICAD_CURL::Cleanup ( )
static

Call curl_global_cleanup for the application.

It must be used only after curl_global_init was called.

Definition at line 72 of file kicad_curl.cpp.

73 {
74  /*
75 
76  Calling lock_guard() from a static destructor will typically be bad, since the
77  s_lock may already have been statically destroyed itself leading to a boost
78  exception. (Remember C++ does not provide certain sequencing of static
79  destructor invocation.)
80 
81  To prevent this we test s_initialized twice, which ensures that the lock_guard
82  is only instantiated on the first call, which should be from
83  PGM_BASE::destroy() which is first called earlier than static destruction.
84  Then when called again from the actual PGM_BASE::~PGM_BASE() function,
85  lock_guard will not be instantiated because s_initialized will be false.
86 
87  */
88 
89  if( s_initialized )
90  {
91  std::lock_guard<std::mutex> lock( s_lock );
92 
93  if( s_initialized )
94  {
95  curl_global_cleanup();
96 
97  atexit( &at_terminate );
98 
99  s_initialized = false;
100  }
101  }
102 }
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:44
static volatile bool s_initialized
Definition: kicad_curl.cpp:38

References at_terminate(), s_initialized, and s_lock.

Referenced by at_terminate().

◆ GetSimpleVersion()

std::string KICAD_CURL::GetSimpleVersion ( )
static

Report back curl version only and SSL library support.

Returns
Generated version string.

Definition at line 105 of file kicad_curl.cpp.

106 {
107  if( !s_initialized )
108  Init();
109 
110  curl_version_info_data* info = curl_version_info( CURLVERSION_NOW );
111 
112  std::string res;
113 
114  if( info->version )
115  {
116  res += "libcurl version: " + std::string( info->version );
117  }
118 
119  res += " (";
120 
121  if( info->features & CURL_VERSION_SSL )
122  {
123  res += "with SSL - ";
124  res += std::string( info->ssl_version );
125  }
126  else
127  {
128  res += "without SSL";
129  }
130  res += ")";
131 
132  return res;
133 }
static void Init()
Call curl_global_init for the application.
Definition: kicad_curl.cpp:50
static volatile bool s_initialized
Definition: kicad_curl.cpp:38

References info, Init(), and s_initialized.

◆ GetVersion()

static const char* KICAD_CURL::GetVersion ( )
inlinestatic

Wrapper for curl_version().

Reports back a short string of loaded libraries.

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

Definition at line 87 of file kicad_curl.h.

88  {
89  return curl_version();
90  }

Referenced by GetKicadCurlVersion().

◆ Init()

void KICAD_CURL::Init ( )
static

Call curl_global_init for the application.

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

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

Definition at line 50 of file kicad_curl.cpp.

51 {
52  // We test s_initialized twice in an effort to avoid
53  // unnecessarily locking s_lock. This understands that the common case
54  // will not need to lock.
55  if( !s_initialized )
56  {
57  std::lock_guard<std::mutex> lock( s_lock );
58 
59  if( !s_initialized )
60  {
61  if( curl_global_init( CURL_GLOBAL_ALL ) != CURLE_OK )
62  {
63  THROW_IO_ERROR( "curl_global_init() failed." );
64  }
65 
66  s_initialized = true;
67  }
68  }
69 }
static std::mutex s_lock
Definition: kicad_curl.cpp:40
static volatile bool s_initialized
Definition: kicad_curl.cpp:38
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:38

References 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 101 of file kicad_curl.h.


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