KiCad PCB EDA Suite
FOOTPRINT_LIST_IMPL Class Reference

#include <footprint_info_impl.h>

Inheritance diagram for FOOTPRINT_LIST_IMPL:
FOOTPRINT_LIST

Public Member Functions

 FOOTPRINT_LIST_IMPL ()
 
virtual ~FOOTPRINT_LIST_IMPL ()
 
void WriteCacheToFile (wxTextFile *aFile) override
 
void ReadCacheFromFile (wxTextFile *aFile) override
 
bool ReadFootprintFiles (FP_LIB_TABLE *aTable, const wxString *aNickname=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr) override
 Read all the footprints provided by the combination of aTable and aNickname. More...
 
unsigned GetCount () const
 
const FPILISTGetList () const
 Was forced to add this by modview_frame.cpp. More...
 
FOOTPRINT_INFOGetModuleInfo (const wxString &aFootprintId)
 Get info for a module by id. More...
 
FOOTPRINT_INFOGetModuleInfo (const wxString &aLibNickname, const wxString &aFootprintName)
 Get info for a module by libNickname/footprintName. More...
 
FOOTPRINT_INFOGetItem (unsigned aIdx)
 Get info for a module by index. More...
 
unsigned GetErrorCount () const
 
std::unique_ptr< IO_ERRORPopError ()
 
void DisplayErrors (wxTopLevelWindow *aCaller=NULL)
 
FP_LIB_TABLEGetTable () const
 

Static Public Member Functions

static FOOTPRINT_LISTGetInstance (KIWAY &aKiway)
 Factory function to return a FOOTPRINT_LIST via Kiway. More...
 

Protected Types

typedef std::vector< std::unique_ptr< FOOTPRINT_INFO > > FPILIST
 
typedef SYNC_QUEUE< std::unique_ptr< IO_ERROR > > ERRLIST
 

Protected Member Functions

void StartWorkers (FP_LIB_TABLE *aTable, wxString const *aNickname, FOOTPRINT_ASYNC_LOADER *aLoader, unsigned aNThreads) override
 Launch worker threads to load footprints. More...
 
bool JoinWorkers () override
 Join worker threads. More...
 
void StopWorkers () override
 Stop worker threads. More...
 
void loader_job ()
 Function loader_job loads footprints from m_queue_in. More...
 

Protected Attributes

FP_LIB_TABLEm_lib_table
 no ownership More...
 
FPILIST m_list
 
ERRLIST m_errors
 some can be PARSE_ERRORs also More...
 

Private Member Functions

bool CatchErrors (const std::function< void()> &aFunc)
 Call aFunc, pushing any IO_ERRORs and std::exceptions it throws onto m_errors. More...
 

Private Attributes

FOOTPRINT_ASYNC_LOADERm_loader
 
std::vector< std::thread > m_threads
 
SYNC_QUEUE< wxString > m_queue_in
 
SYNC_QUEUE< wxString > m_queue_out
 
std::atomic_size_t m_count_finished
 
long long m_list_timestamp
 
PROGRESS_REPORTERm_progress_reporter
 
std::atomic_bool m_cancelled
 
std::mutex m_join
 

Detailed Description

Definition at line 84 of file footprint_info_impl.h.

Member Typedef Documentation

◆ ERRLIST

typedef SYNC_QUEUE<std::unique_ptr<IO_ERROR> > FOOTPRINT_LIST::ERRLIST
protectedinherited

Definition at line 198 of file footprint_info.h.

◆ FPILIST

typedef std::vector<std::unique_ptr<FOOTPRINT_INFO> > FOOTPRINT_LIST::FPILIST
protectedinherited

Definition at line 197 of file footprint_info.h.

Constructor & Destructor Documentation

◆ FOOTPRINT_LIST_IMPL()

FOOTPRINT_LIST_IMPL::FOOTPRINT_LIST_IMPL ( )

Definition at line 325 of file footprint_info_impl.cpp.

325  :
326  m_loader( nullptr ),
327  m_count_finished( 0 ),
328  m_list_timestamp( 0 ),
329  m_progress_reporter( nullptr ),
330  m_cancelled( false )
331 {
332 }
std::atomic_bool m_cancelled
PROGRESS_REPORTER * m_progress_reporter
std::atomic_size_t m_count_finished
FOOTPRINT_ASYNC_LOADER * m_loader

◆ ~FOOTPRINT_LIST_IMPL()

FOOTPRINT_LIST_IMPL::~FOOTPRINT_LIST_IMPL ( )
virtual

Definition at line 335 of file footprint_info_impl.cpp.

336 {
337  StopWorkers();
338 }
void StopWorkers() override
Stop worker threads.

References StopWorkers().

Member Function Documentation

◆ CatchErrors()

bool FOOTPRINT_LIST_IMPL::CatchErrors ( const std::function< void()> &  aFunc)
private

Call aFunc, pushing any IO_ERRORs and std::exceptions it throws onto m_errors.

Returns
true if no error occurred.

Definition at line 69 of file footprint_info_impl.cpp.

70 {
71  try
72  {
73  aFunc();
74  }
75  catch( const IO_ERROR& ioe )
76  {
77  m_errors.move_push( std::make_unique<IO_ERROR>( ioe ) );
78  return false;
79  }
80  catch( const std::exception& se )
81  {
82  // This is a round about way to do this, but who knows what THROW_IO_ERROR()
83  // may be tricked out to do someday, keep it in the game.
84  try
85  {
86  THROW_IO_ERROR( se.what() );
87  }
88  catch( const IO_ERROR& ioe )
89  {
90  m_errors.move_push( std::make_unique<IO_ERROR>( ioe ) );
91  }
92  return false;
93  }
94 
95  return true;
96 }
ERRLIST m_errors
some can be PARSE_ERRORs also
void move_push(T &&aValue)
Move a value onto the queue.
Definition: sync_queue.h:54
#define THROW_IO_ERROR(msg)
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76

References FOOTPRINT_LIST::m_errors, SYNC_QUEUE< T >::move_push(), and THROW_IO_ERROR.

Referenced by loader_job().

◆ DisplayErrors()

void FOOTPRINT_LIST::DisplayErrors ( wxTopLevelWindow *  aCaller = NULL)
inherited

Definition at line 83 of file footprint_info.cpp.

84 {
85  // @todo: go to a more HTML !<table>! ? centric output, possibly with
86  // recommendations for remedy of errors. Add numeric error codes
87  // to PARSE_ERROR, and switch on them for remedies, etc. Full
88  // access is provided to everything in every exception!
89 
90  HTML_MESSAGE_BOX dlg( aWindow, _( "Load Error" ) );
91 
92  dlg.MessageSet( _( "Errors were encountered loading footprints:" ) );
93 
94  wxString msg;
95 
96  while( auto error = PopError() )
97  {
98  wxString tmp = error->Problem();
99 
100  // Preserve new lines in error messages so queued errors don't run together.
101  tmp.Replace( "\n", "<BR>" );
102  msg += wxT( "<p>" ) + tmp + wxT( "</p>" );
103  }
104 
105  dlg.AddHTML_Text( msg );
106 
107  dlg.ShowModal();
108 }
Class HTML_MESSAGE_BOX.
#define _(s)
std::unique_ptr< IO_ERROR > PopError()

References _, HTML_MESSAGE_BOX::AddHTML_Text(), HTML_MESSAGE_BOX::MessageSet(), and FOOTPRINT_LIST::PopError().

Referenced by FOOTPRINT_EDIT_FRAME::initLibraryTree(), CVPCB_MAINFRAME::LoadFootprintFiles(), and PCB_BASE_FRAME::SelectFootprintFromLibTree().

◆ GetCount()

unsigned FOOTPRINT_LIST::GetCount ( ) const
inlineinherited
Returns
the number of items stored in list

Definition at line 218 of file footprint_info.h.

219  {
220  return m_list.size();
221  }

References FOOTPRINT_ASYNC_LOADER::m_list.

Referenced by FOOTPRINT_FILTER::ITERATOR::dereference(), FOOTPRINT_FILTER::end(), FOOTPRINT_LIST::GetInstance(), and PCB_BASE_EDIT_FRAME::PCB_BASE_EDIT_FRAME().

◆ GetErrorCount()

unsigned FOOTPRINT_LIST::GetErrorCount ( ) const
inlineinherited

Definition at line 249 of file footprint_info.h.

250  {
251  return m_errors.size();
252  }
ERRLIST m_errors
some can be PARSE_ERRORs also
size_t size() const
Return the size of the queue.
Definition: sync_queue.h:94

References SYNC_QUEUE< T >::size().

Referenced by FOOTPRINT_EDIT_FRAME::initLibraryTree(), CVPCB_MAINFRAME::LoadFootprintFiles(), and PCB_BASE_FRAME::SelectFootprintFromLibTree().

◆ GetInstance()

FOOTPRINT_LIST * FOOTPRINT_LIST::GetInstance ( KIWAY aKiway)
staticinherited

Factory function to return a FOOTPRINT_LIST via Kiway.

NOT guaranteed to succeed; will return null if the kiface is not available.

Parameters
aKiway- active kiway instance

Definition at line 136 of file footprint_info.cpp.

137 {
138  FOOTPRINT_LIST* footprintInfo = get_instance_from_id( aKiway, KIFACE_FOOTPRINT_LIST );
139 
140  if( ! footprintInfo )
141  return nullptr;
142 
143  if( !footprintInfo->GetCount() )
144  {
145  wxTextFile footprintInfoCache( aKiway.Prj().GetProjectPath() + "fp-info-cache" );
146  footprintInfo->ReadCacheFromFile( &footprintInfoCache );
147  }
148 
149  return footprintInfo;
150 }
static FOOTPRINT_LIST * get_instance_from_id(KIWAY &aKiway, int aId)
VTBL_ENTRY PROJECT & Prj() const
Function Prj returns the PROJECT associated with this KIWAY.
Definition: kiway.cpp:163
VTBL_ENTRY const wxString GetProjectPath() const
Function GetProjectPath returns the full path of the project.
Definition: project.cpp:102
virtual void ReadCacheFromFile(wxTextFile *aFile)
unsigned GetCount() const
Holds a list of FOOTPRINT_INFO objects, along with a list of IO_ERRORs or PARSE_ERRORs that were thro...
Return a pointer to the global instance of FOOTPRINT_LIST from pcbnew.
Definition: kiface_ids.h:39

References get_instance_from_id(), FOOTPRINT_LIST::GetCount(), PROJECT::GetProjectPath(), KIFACE_FOOTPRINT_LIST, KIWAY::Prj(), and FOOTPRINT_LIST::ReadCacheFromFile().

Referenced by DIALOG_CHOOSE_COMPONENT::ConstructRightPanel(), CVPCB_MAINFRAME::CVPCB_MAINFRAME(), FOOTPRINT_SELECT_WIDGET::Load(), and FOOTPRINT_VIEWER_FRAME::ReCreateFootprintList().

◆ GetItem()

FOOTPRINT_INFO& FOOTPRINT_LIST::GetItem ( unsigned  aIdx)
inlineinherited

Get info for a module by index.

Parameters
aIdx= index of the given item
Returns
the aIdx item in list

Definition at line 244 of file footprint_info.h.

245  {
246  return *m_list[aIdx];
247  }

References FOOTPRINT_ASYNC_LOADER::m_list.

◆ GetList()

const FPILIST& FOOTPRINT_LIST::GetList ( ) const
inlineinherited

Was forced to add this by modview_frame.cpp.

Definition at line 224 of file footprint_info.h.

225  {
226  return m_list;
227  }

References FOOTPRINT_ASYNC_LOADER::m_list.

Referenced by FP_TREE_MODEL_ADAPTER::getFootprints().

◆ GetModuleInfo() [1/2]

FOOTPRINT_INFO * FOOTPRINT_LIST::GetModuleInfo ( const wxString &  aFootprintId)
inherited

Get info for a module by id.

Definition at line 63 of file footprint_info.cpp.

64 {
65  if( aFootprintName.IsEmpty() )
66  return NULL;
67 
68  LIB_ID fpid;
69 
70  wxCHECK_MSG( fpid.Parse( aFootprintName, LIB_ID::ID_PCB ) < 0, NULL,
71  wxString::Format( wxT( "\"%s\" is not a valid LIB_ID." ), aFootprintName ) );
72 
73  return GetModuleInfo( fpid.GetLibNickname(), fpid.GetLibItemName() );
74 }
const UTF8 & GetLibItemName() const
Definition: lib_id.h:114
FOOTPRINT_INFO * GetModuleInfo(const wxString &aFootprintId)
Get info for a module by id.
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:97
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
int Parse(const UTF8 &aId, LIB_ID_TYPE aType, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition: lib_id.cpp:122

References Format(), LIB_ID::GetLibItemName(), LIB_ID::GetLibNickname(), LIB_ID::ID_PCB, and LIB_ID::Parse().

Referenced by CVPCB_MAINFRAME::AutomaticFootprintMatching(), CVPCB_MAINFRAME::DisplayStatus(), DISPLAY_FOOTPRINTS_FRAME::InitDisplay(), and PCB_BASE_FRAME::SelectFootprintFromLibTree().

◆ GetModuleInfo() [2/2]

FOOTPRINT_INFO * FOOTPRINT_LIST::GetModuleInfo ( const wxString &  aLibNickname,
const wxString &  aFootprintName 
)
inherited

Get info for a module by libNickname/footprintName.

Definition at line 47 of file footprint_info.cpp.

49 {
50  if( aFootprintName.IsEmpty() )
51  return NULL;
52 
53  for( auto& fp : m_list )
54  {
55  if( aLibNickname == fp->GetLibNickname() && aFootprintName == fp->GetFootprintName() )
56  return &*fp;
57  }
58 
59  return NULL;
60 }

References FOOTPRINT_LIST::m_list.

◆ GetTable()

FP_LIB_TABLE* FOOTPRINT_LIST::GetTable ( ) const
inlineinherited

Definition at line 279 of file footprint_info.h.

280  {
281  return m_lib_table;
282  }
FP_LIB_TABLE * m_lib_table
no ownership

Referenced by FOOTPRINT_INFO_IMPL::load().

◆ JoinWorkers()

bool FOOTPRINT_LIST_IMPL::JoinWorkers ( )
overrideprotectedvirtual

Join worker threads.

Part of the FOOTPRINT_ASYNC_LOADER implementation.

Implements FOOTPRINT_LIST.

Definition at line 226 of file footprint_info_impl.cpp.

227 {
228  {
229  std::lock_guard<std::mutex> lock1( m_join );
230 
231  for( auto& i : m_threads )
232  i.join();
233 
234  m_threads.clear();
235  m_queue_in.clear();
236  m_count_finished.store( 0 );
237  }
238 
239  size_t total_count = m_queue_out.size();
240 
241  LOCALE_IO toggle_locale;
242 
243  // Parse the footprints in parallel. WARNING! This requires changing the locale, which is
244  // GLOBAL. It is only threadsafe to construct the LOCALE_IO before the threads are created,
245  // destroy it after they finish, and block the main (GUI) thread while they work. Any deviation
246  // from this will cause nasal demons.
247  //
248  // TODO: blast LOCALE_IO into the sun
249 
251  std::vector<std::thread> threads;
252 
253  for( size_t ii = 0; ii < std::thread::hardware_concurrency() + 1; ++ii )
254  {
255  threads.push_back( std::thread( [this, &queue_parsed]() {
256  wxString nickname;
257 
258  while( this->m_queue_out.pop( nickname ) && !m_cancelled )
259  {
260  wxArrayString fpnames;
261 
262  try
263  {
264  m_lib_table->FootprintEnumerate( fpnames, nickname );
265  }
266  catch( const IO_ERROR& ioe )
267  {
268  m_errors.move_push( std::make_unique<IO_ERROR>( ioe ) );
269  }
270  catch( const std::exception& se )
271  {
272  // This is a round about way to do this, but who knows what THROW_IO_ERROR()
273  // may be tricked out to do someday, keep it in the game.
274  try
275  {
276  THROW_IO_ERROR( se.what() );
277  }
278  catch( const IO_ERROR& ioe )
279  {
280  m_errors.move_push( std::make_unique<IO_ERROR>( ioe ) );
281  }
282  }
283 
284  for( unsigned jj = 0; jj < fpnames.size() && !m_cancelled; ++jj )
285  {
286  wxString fpname = fpnames[jj];
287  FOOTPRINT_INFO* fpinfo = new FOOTPRINT_INFO_IMPL( this, nickname, fpname );
288  queue_parsed.move_push( std::unique_ptr<FOOTPRINT_INFO>( fpinfo ) );
289  }
290 
291  if( m_progress_reporter )
293 
294  m_count_finished.fetch_add( 1 );
295  }
296  } ) );
297  }
298 
299  while( !m_cancelled && (size_t)m_count_finished.load() < total_count )
300  {
302  m_cancelled = true;
303 
304  wxMilliSleep( 30 );
305  }
306 
307  for( auto& thr : threads )
308  thr.join();
309 
310  std::unique_ptr<FOOTPRINT_INFO> fpi;
311 
312  while( queue_parsed.pop( fpi ) )
313  m_list.push_back( std::move( fpi ) );
314 
315  std::sort( m_list.begin(), m_list.end(), []( std::unique_ptr<FOOTPRINT_INFO> const& lhs,
316  std::unique_ptr<FOOTPRINT_INFO> const& rhs ) -> bool
317  {
318  return *lhs < *rhs;
319  } );
320 
321  return m_errors.empty();
322 }
SYNC_QUEUE< wxString > m_queue_out
bool pop(T &aReceiver)
Pop a value off the queue into the provided variable.
Definition: sync_queue.h:66
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:175
std::atomic_bool m_cancelled
ERRLIST m_errors
some can be PARSE_ERRORs also
FP_LIB_TABLE * m_lib_table
no ownership
Synchronized, locking queue.
Definition: sync_queue.h:30
std::vector< std::thread > m_threads
void move_push(T &&aValue)
Move a value onto the queue.
Definition: sync_queue.h:54
SYNC_QUEUE< wxString > m_queue_in
PROGRESS_REPORTER * m_progress_reporter
bool empty() const
Return true iff the queue is empty.
Definition: sync_queue.h:85
std::atomic_size_t m_count_finished
#define THROW_IO_ERROR(msg)
YYCODETYPE lhs
size_t size() const
Return the size of the queue.
Definition: sync_queue.h:94
size_t i
Definition: json11.cpp:597
bool KeepRefreshing(bool aWait=false)
Update the UI dialog.
void FootprintEnumerate(wxArrayString &aFootprintNames, const wxString &aNickname)
Return a list of footprint names contained within the library given by aNickname.
void AdvanceProgress()
Increment the progress bar length (inside the current virtual zone)
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76
void clear()
Clear the queue.
Definition: sync_queue.h:103

References PROGRESS_REPORTER::AdvanceProgress(), SYNC_QUEUE< T >::clear(), SYNC_QUEUE< T >::empty(), FP_LIB_TABLE::FootprintEnumerate(), i, PROGRESS_REPORTER::KeepRefreshing(), numEval::lhs, m_cancelled, m_count_finished, FOOTPRINT_LIST::m_errors, m_join, FOOTPRINT_LIST::m_lib_table, FOOTPRINT_LIST::m_list, m_progress_reporter, m_queue_in, m_queue_out, m_threads, SYNC_QUEUE< T >::move_push(), SYNC_QUEUE< T >::pop(), SYNC_QUEUE< T >::size(), and THROW_IO_ERROR.

◆ loader_job()

void FOOTPRINT_LIST_IMPL::loader_job ( )
protected

Function loader_job loads footprints from m_queue_in.

Definition at line 99 of file footprint_info_impl.cpp.

100 {
101  wxString nickname;
102 
103  while( m_queue_in.pop( nickname ) && !m_cancelled )
104  {
105  CatchErrors( [this, &nickname]() {
106  m_lib_table->PrefetchLib( nickname );
107  m_queue_out.push( nickname );
108  } );
109 
110  m_count_finished.fetch_add( 1 );
111 
112  if( m_progress_reporter )
114  }
115 }
void push(T const &aValue)
Push a value onto the queue.
Definition: sync_queue.h:45
SYNC_QUEUE< wxString > m_queue_out
bool pop(T &aReceiver)
Pop a value off the queue into the provided variable.
Definition: sync_queue.h:66
std::atomic_bool m_cancelled
FP_LIB_TABLE * m_lib_table
no ownership
SYNC_QUEUE< wxString > m_queue_in
PROGRESS_REPORTER * m_progress_reporter
void PrefetchLib(const wxString &aNickname)
Function PrefetchLib If possible, prefetches the specified library (e.g.
std::atomic_size_t m_count_finished
bool CatchErrors(const std::function< void()> &aFunc)
Call aFunc, pushing any IO_ERRORs and std::exceptions it throws onto m_errors.
void AdvanceProgress()
Increment the progress bar length (inside the current virtual zone)

References PROGRESS_REPORTER::AdvanceProgress(), CatchErrors(), m_cancelled, m_count_finished, FOOTPRINT_LIST::m_lib_table, m_progress_reporter, m_queue_in, m_queue_out, SYNC_QUEUE< T >::pop(), FP_LIB_TABLE::PrefetchLib(), and SYNC_QUEUE< T >::push().

Referenced by StartWorkers().

◆ PopError()

std::unique_ptr<IO_ERROR> FOOTPRINT_LIST::PopError ( )
inlineinherited

Definition at line 254 of file footprint_info.h.

255  {
256  std::unique_ptr<IO_ERROR> error;
257 
258  m_errors.pop( error );
259  return error;
260  }
bool pop(T &aReceiver)
Pop a value off the queue into the provided variable.
Definition: sync_queue.h:66
ERRLIST m_errors
some can be PARSE_ERRORs also

References SYNC_QUEUE< T >::pop().

Referenced by FOOTPRINT_LIST::DisplayErrors().

◆ ReadCacheFromFile()

void FOOTPRINT_LIST_IMPL::ReadCacheFromFile ( wxTextFile *  aFile)
overridevirtual

Reimplemented from FOOTPRINT_LIST.

Definition at line 371 of file footprint_info_impl.cpp.

372 {
373  m_list_timestamp = 0;
374  m_list.clear();
375 
376  try
377  {
378  if( aCacheFile->Exists() )
379  {
380  aCacheFile->Open();
381 
382  aCacheFile->GetFirstLine().ToLongLong( &m_list_timestamp );
383 
384  while( aCacheFile->GetCurrentLine() + 6 < aCacheFile->GetLineCount() )
385  {
386  wxString libNickname = aCacheFile->GetNextLine();
387  wxString name = aCacheFile->GetNextLine();
388  wxString description = UnescapeString( aCacheFile->GetNextLine() );
389  wxString keywords = UnescapeString( aCacheFile->GetNextLine() );
390  int orderNum = wxAtoi( aCacheFile->GetNextLine() );
391  unsigned int padCount = (unsigned) wxAtoi( aCacheFile->GetNextLine() );
392  unsigned int uniquePadCount = (unsigned) wxAtoi( aCacheFile->GetNextLine() );
393 
394  auto* fpinfo = new FOOTPRINT_INFO_IMPL( libNickname, name, description, keywords,
395  orderNum, padCount, uniquePadCount );
396  m_list.emplace_back( std::unique_ptr<FOOTPRINT_INFO>( fpinfo ) );
397  }
398  }
399  }
400  catch( ... )
401  {
402  // whatever went wrong, invalidate the cache
403  m_list_timestamp = 0;
404  }
405 
406  // Sanity check: an empty list is very unlikely to be correct.
407  if( m_list.size() == 0 )
408  m_list_timestamp = 0;
409 
410  if( aCacheFile->IsOpened() )
411  aCacheFile->Close();
412 }
const char * name
Definition: DXF_plotter.cpp:61
wxString UnescapeString(const wxString &aSource)
Definition: string.cpp:123

References FOOTPRINT_LIST::m_list, m_list_timestamp, name, and UnescapeString().

Referenced by PCB_BASE_EDIT_FRAME::PCB_BASE_EDIT_FRAME().

◆ ReadFootprintFiles()

bool FOOTPRINT_LIST_IMPL::ReadFootprintFiles ( FP_LIB_TABLE aTable,
const wxString *  aNickname = nullptr,
PROGRESS_REPORTER aProgressReporter = nullptr 
)
overridevirtual

Read all the footprints provided by the combination of aTable and aNickname.

Parameters
aTabledefines all the libraries.
aNicknameis the library to read from, or if NULL means read all footprints from all known libraries in aTable.
aProgressReporteris an optional progress reporter. ReadFootprintFiles() will use 2 phases within the reporter.
Returns
bool - true if it ran to completion, else false if it aborted after some number of errors. If true, it does not mean there were no errors, check GetErrorCount() for that, should be zero to indicate success.

Implements FOOTPRINT_LIST.

Definition at line 118 of file footprint_info_impl.cpp.

120 {
121  long long int generatedTimestamp = aTable->GenerateTimestamp( aNickname );
122 
123  if( generatedTimestamp == m_list_timestamp )
124  return true;
125 
126  m_progress_reporter = aProgressReporter;
127  m_cancelled = false;
128 
129  FOOTPRINT_ASYNC_LOADER loader;
130 
131  loader.SetList( this );
132  loader.Start( aTable, aNickname );
133 
134  if( m_progress_reporter )
135  {
137  m_progress_reporter->Report( _( "Fetching Footprint Libraries" ) );
138  }
139 
140  while( !m_cancelled && (int)m_count_finished.load() < m_loader->m_total_libs )
141  {
143  m_cancelled = true;
144 
145  wxMilliSleep( 20 );
146  }
147 
148  if( m_cancelled )
149  {
150  loader.Abort();
151  }
152  else
153  {
154  if( m_progress_reporter )
155  {
158  m_progress_reporter->Report( _( "Loading Footprints" ) );
159  }
160 
161  loader.Join();
162 
163  if( m_progress_reporter )
165  }
166 
167  if( m_cancelled )
168  m_list_timestamp = 0; // God knows what we got before we were cancelled
169  else
170  m_list_timestamp = generatedTimestamp;
171 
172  return m_errors.empty();
173 }
void AdvancePhase()
Uses the next vailable virtual zone of the dialog progress bar.
SYNC_QUEUE< wxString > m_queue_out
std::atomic_bool m_cancelled
ERRLIST m_errors
some can be PARSE_ERRORs also
void Report(const wxString &aMessage)
Display aMessage in the progress bar dialog.
This class can be used to populate a FOOTPRINT_LIST asynchronously.
SYNC_QUEUE< wxString > m_queue_in
long long GenerateTimestamp(const wxString *aNickname)
Generate a hashed timestamp representing the last-mod-times of the library indicated by aNickname,...
PROGRESS_REPORTER * m_progress_reporter
void Start(FP_LIB_TABLE *aTable, wxString const *aNickname=nullptr, unsigned aNThreads=DEFAULT_THREADS)
Launch the worker threads.
bool empty() const
Return true iff the queue is empty.
Definition: sync_queue.h:85
std::atomic_size_t m_count_finished
bool Join()
Wait until the worker threads are finished, and then perform any required single-threaded finishing o...
#define _(s)
FOOTPRINT_ASYNC_LOADER * m_loader
size_t size() const
Return the size of the queue.
Definition: sync_queue.h:94
bool KeepRefreshing(bool aWait=false)
Update the UI dialog.
void SetMaxProgress(int aMaxProgress)
Fix the value thar gives the 100 precent progress bar length (inside the current virtual zone)
void SetList(FOOTPRINT_LIST *aList)
Assign a FOOTPRINT_LIST to the loader.
void Abort()
Safely stop the current process.

References _, FOOTPRINT_ASYNC_LOADER::Abort(), PROGRESS_REPORTER::AdvancePhase(), SYNC_QUEUE< T >::empty(), FP_LIB_TABLE::GenerateTimestamp(), FOOTPRINT_ASYNC_LOADER::Join(), PROGRESS_REPORTER::KeepRefreshing(), m_cancelled, m_count_finished, FOOTPRINT_LIST::m_errors, m_list_timestamp, m_loader, m_progress_reporter, m_queue_in, m_queue_out, FOOTPRINT_ASYNC_LOADER::m_total_libs, PROGRESS_REPORTER::Report(), FOOTPRINT_ASYNC_LOADER::SetList(), PROGRESS_REPORTER::SetMaxProgress(), SYNC_QUEUE< T >::size(), and FOOTPRINT_ASYNC_LOADER::Start().

Referenced by FOOTPRINT_EDIT_FRAME::initLibraryTree(), PCB_BASE_FRAME::SelectFootprintFromLibTree(), and FOOTPRINT_EDIT_FRAME::SyncLibraryTree().

◆ StartWorkers()

void FOOTPRINT_LIST_IMPL::StartWorkers ( FP_LIB_TABLE aTable,
wxString const *  aNickname,
FOOTPRINT_ASYNC_LOADER aLoader,
unsigned  aNThreads 
)
overrideprotectedvirtual

Launch worker threads to load footprints.

Part of the FOOTPRINT_ASYNC_LOADER implementation.

Implements FOOTPRINT_LIST.

Definition at line 176 of file footprint_info_impl.cpp.

178 {
179  m_loader = aLoader;
180  m_lib_table = aTable;
181 
182  // Clear data before reading files
183  m_count_finished.store( 0 );
184  m_errors.clear();
185  m_list.clear();
186  m_threads.clear();
187  m_queue_in.clear();
188  m_queue_out.clear();
189 
190  if( aNickname )
191  m_queue_in.push( *aNickname );
192  else
193  {
194  for( auto const& nickname : aTable->GetLogicalLibs() )
195  m_queue_in.push( nickname );
196  }
197 
199 
200  for( unsigned i = 0; i < aNThreads; ++i )
201  {
202  m_threads.push_back( std::thread( &FOOTPRINT_LIST_IMPL::loader_job, this ) );
203  }
204 }
void push(T const &aValue)
Push a value onto the queue.
Definition: sync_queue.h:45
SYNC_QUEUE< wxString > m_queue_out
ERRLIST m_errors
some can be PARSE_ERRORs also
void loader_job()
Function loader_job loads footprints from m_queue_in.
FP_LIB_TABLE * m_lib_table
no ownership
std::vector< std::thread > m_threads
SYNC_QUEUE< wxString > m_queue_in
std::atomic_size_t m_count_finished
FOOTPRINT_ASYNC_LOADER * m_loader
size_t size() const
Return the size of the queue.
Definition: sync_queue.h:94
size_t i
Definition: json11.cpp:597
void clear()
Clear the queue.
Definition: sync_queue.h:103
std::vector< wxString > GetLogicalLibs()
Return the logical library names, all of them that are pertinent to a look up done on this LIB_TABLE.

References SYNC_QUEUE< T >::clear(), LIB_TABLE::GetLogicalLibs(), i, loader_job(), m_count_finished, FOOTPRINT_LIST::m_errors, FOOTPRINT_LIST::m_lib_table, FOOTPRINT_LIST::m_list, m_loader, m_queue_in, m_queue_out, m_threads, FOOTPRINT_ASYNC_LOADER::m_total_libs, SYNC_QUEUE< T >::push(), and SYNC_QUEUE< T >::size().

◆ StopWorkers()

void FOOTPRINT_LIST_IMPL::StopWorkers ( )
overrideprotectedvirtual

Stop worker threads.

Part of the FOOTPRINT_ASYNC_LOADER implementation.

Implements FOOTPRINT_LIST.

Definition at line 206 of file footprint_info_impl.cpp.

207 {
208  std::lock_guard<std::mutex> lock1( m_join );
209 
210  // To safely stop our workers, we set the cancellation flag (they will each
211  // exit on their next safe loop location when this is set). Then we need to wait
212  // for all threads to finish as closing the implementation will free the queues
213  // that the threads write to.
214  for( auto& i : m_threads )
215  i.join();
216 
217  m_threads.clear();
218  m_queue_in.clear();
219  m_count_finished.store( 0 );
220 
221  // If we have cancelled in the middle of a load, clear our timestamp to re-load next time
222  if( m_cancelled )
223  m_list_timestamp = 0;
224 }
std::atomic_bool m_cancelled
std::vector< std::thread > m_threads
SYNC_QUEUE< wxString > m_queue_in
std::atomic_size_t m_count_finished
size_t i
Definition: json11.cpp:597
void clear()
Clear the queue.
Definition: sync_queue.h:103

References SYNC_QUEUE< T >::clear(), i, m_cancelled, m_count_finished, m_join, m_list_timestamp, m_queue_in, and m_threads.

Referenced by ~FOOTPRINT_LIST_IMPL().

◆ WriteCacheToFile()

void FOOTPRINT_LIST_IMPL::WriteCacheToFile ( wxTextFile *  aFile)
overridevirtual

Reimplemented from FOOTPRINT_LIST.

Definition at line 341 of file footprint_info_impl.cpp.

342 {
343  if( aCacheFile->Exists() )
344  {
345  aCacheFile->Open();
346  aCacheFile->Clear();
347  }
348  else
349  {
350  aCacheFile->Create();
351  }
352 
353  aCacheFile->AddLine( wxString::Format( "%lld", m_list_timestamp ) );
354 
355  for( auto& fpinfo : m_list )
356  {
357  aCacheFile->AddLine( fpinfo->GetLibNickname() );
358  aCacheFile->AddLine( fpinfo->GetName() );
359  aCacheFile->AddLine( EscapeString( fpinfo->GetDescription(), CTX_DELIMITED_STR ) );
360  aCacheFile->AddLine( EscapeString( fpinfo->GetKeywords(), CTX_DELIMITED_STR ) );
361  aCacheFile->AddLine( wxString::Format( "%d", fpinfo->GetOrderNum() ) );
362  aCacheFile->AddLine( wxString::Format( "%u", fpinfo->GetPadCount() ) );
363  aCacheFile->AddLine( wxString::Format( "%u", fpinfo->GetUniquePadCount() ) );
364  }
365 
366  aCacheFile->Write();
367  aCacheFile->Close();
368 }
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
These Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which ar...
Definition: string.cpp:50

References CTX_DELIMITED_STR, EscapeString(), Format(), FOOTPRINT_LIST::m_list, and m_list_timestamp.

Referenced by PCB_BASE_EDIT_FRAME::~PCB_BASE_EDIT_FRAME().

Member Data Documentation

◆ m_cancelled

std::atomic_bool FOOTPRINT_LIST_IMPL::m_cancelled
private

Definition at line 93 of file footprint_info_impl.h.

Referenced by JoinWorkers(), loader_job(), ReadFootprintFiles(), and StopWorkers().

◆ m_count_finished

std::atomic_size_t FOOTPRINT_LIST_IMPL::m_count_finished
private

◆ m_errors

ERRLIST FOOTPRINT_LIST::m_errors
protectedinherited

some can be PARSE_ERRORs also

Definition at line 201 of file footprint_info.h.

Referenced by CatchErrors(), JoinWorkers(), ReadFootprintFiles(), and StartWorkers().

◆ m_join

std::mutex FOOTPRINT_LIST_IMPL::m_join
private

Definition at line 94 of file footprint_info_impl.h.

Referenced by JoinWorkers(), and StopWorkers().

◆ m_lib_table

FP_LIB_TABLE* FOOTPRINT_LIST::m_lib_table
protectedinherited

no ownership

Definition at line 195 of file footprint_info.h.

Referenced by JoinWorkers(), loader_job(), and StartWorkers().

◆ m_list

FPILIST FOOTPRINT_LIST::m_list
protectedinherited

◆ m_list_timestamp

long long FOOTPRINT_LIST_IMPL::m_list_timestamp
private

◆ m_loader

FOOTPRINT_ASYNC_LOADER* FOOTPRINT_LIST_IMPL::m_loader
private

Definition at line 86 of file footprint_info_impl.h.

Referenced by ReadFootprintFiles(), and StartWorkers().

◆ m_progress_reporter

PROGRESS_REPORTER* FOOTPRINT_LIST_IMPL::m_progress_reporter
private

Definition at line 92 of file footprint_info_impl.h.

Referenced by JoinWorkers(), loader_job(), and ReadFootprintFiles().

◆ m_queue_in

SYNC_QUEUE<wxString> FOOTPRINT_LIST_IMPL::m_queue_in
private

◆ m_queue_out

SYNC_QUEUE<wxString> FOOTPRINT_LIST_IMPL::m_queue_out
private

Definition at line 89 of file footprint_info_impl.h.

Referenced by JoinWorkers(), loader_job(), ReadFootprintFiles(), and StartWorkers().

◆ m_threads

std::vector<std::thread> FOOTPRINT_LIST_IMPL::m_threads
private

Definition at line 87 of file footprint_info_impl.h.

Referenced by JoinWorkers(), StartWorkers(), and StopWorkers().


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