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

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

Definition at line 195 of file footprint_info.h.

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

Definition at line 194 of file footprint_info.h.

Constructor & Destructor Documentation

FOOTPRINT_LIST_IMPL::FOOTPRINT_LIST_IMPL ( )

Definition at line 326 of file footprint_info_impl.cpp.

326  :
327  m_loader( nullptr ),
328  m_count_finished( 0 ),
329  m_list_timestamp( 0 ),
330  m_progress_reporter( nullptr ),
331  m_cancelled( false )
332 {
333 }
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 ( )
virtual

Definition at line 336 of file footprint_info_impl.cpp.

References StopWorkers().

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

Member Function Documentation

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 70 of file footprint_info_impl.cpp.

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

Referenced by loader_job().

71 {
72  try
73  {
74  aFunc();
75  }
76  catch( const IO_ERROR& ioe )
77  {
78  m_errors.move_push( std::make_unique<IO_ERROR>( ioe ) );
79  return false;
80  }
81  catch( const std::exception& se )
82  {
83  // This is a round about way to do this, but who knows what THROW_IO_ERROR()
84  // may be tricked out to do someday, keep it in the game.
85  try
86  {
87  THROW_IO_ERROR( se.what() );
88  }
89  catch( const IO_ERROR& ioe )
90  {
91  m_errors.move_push( std::make_unique<IO_ERROR>( ioe ) );
92  }
93  return false;
94  }
95 
96  return true;
97 }
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
void FOOTPRINT_LIST::DisplayErrors ( wxTopLevelWindow *  aCaller = NULL)
inherited

Definition at line 84 of file footprint_info.cpp.

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().

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

Definition at line 215 of file footprint_info.h.

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

216  {
217  return m_list.size();
218  }
unsigned FOOTPRINT_LIST::GetErrorCount ( ) const
inlineinherited

Definition at line 246 of file footprint_info.h.

References SYNC_QUEUE< T >::size().

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

247  {
248  return m_errors.size();
249  }
size_t size() const
Return the size of the queue.
Definition: sync_queue.h:94
ERRLIST m_errors
some can be PARSE_ERRORs also
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 137 of file footprint_info.cpp.

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().

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

Referenced by FOOTPRINT_FILTER::ITERATOR::dereference().

242  {
243  return *m_list[aIdx];
244  }
const FPILIST& FOOTPRINT_LIST::GetList ( ) const
inlineinherited

Was forced to add this by modview_frame.cpp.

Definition at line 221 of file footprint_info.h.

References FOOTPRINT_ASYNC_LOADER::m_list.

Referenced by FP_TREE_MODEL_ADAPTER::getFootprints().

222  {
223  return m_list;
224  }
FOOTPRINT_INFO * FOOTPRINT_LIST::GetModuleInfo ( const wxString &  aFootprintId)
inherited

Get info for a module by id.

Definition at line 64 of file footprint_info.cpp.

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().

65 {
66  if( aFootprintName.IsEmpty() )
67  return NULL;
68 
69  LIB_ID fpid;
70 
71  wxCHECK_MSG( fpid.Parse( aFootprintName, LIB_ID::ID_PCB ) < 0, NULL,
72  wxString::Format( wxT( "\"%s\" is not a valid LIB_ID." ), aFootprintName ) );
73 
74  return GetModuleInfo( fpid.GetLibNickname(), fpid.GetLibItemName() );
75 }
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 & GetLibItemName() const
Definition: lib_id.h:114
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
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Definition: lib_id.h:97
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
FOOTPRINT_INFO * FOOTPRINT_LIST::GetModuleInfo ( const wxString &  aLibNickname,
const wxString &  aFootprintName 
)
inherited

Get info for a module by libNickname/footprintName.

Definition at line 48 of file footprint_info.cpp.

References FOOTPRINT_LIST::m_list.

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

Definition at line 276 of file footprint_info.h.

Referenced by FOOTPRINT_INFO_IMPL::load().

277  {
278  return m_lib_table;
279  }
FP_LIB_TABLE * m_lib_table
no ownership
bool FOOTPRINT_LIST_IMPL::JoinWorkers ( )
overrideprotectedvirtual

Join worker threads.

Part of the FOOTPRINT_ASYNC_LOADER implementation.

Implements FOOTPRINT_LIST.

Definition at line 227 of file footprint_info_impl.cpp.

References PROGRESS_REPORTER::AdvanceProgress(), SYNC_QUEUE< T >::clear(), SYNC_QUEUE< T >::empty(), FOOTPRINT_INFO_IMPL::FOOTPRINT_INFO_IMPL(), 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.

228 {
229  {
230  std::lock_guard<std::mutex> lock1( m_join );
231 
232  for( auto& i : m_threads )
233  i.join();
234 
235  m_threads.clear();
236  m_queue_in.clear();
237  m_count_finished.store( 0 );
238  }
239 
240  size_t total_count = m_queue_out.size();
241 
242  LOCALE_IO toggle_locale;
243 
244  // Parse the footprints in parallel. WARNING! This requires changing the locale, which is
245  // GLOBAL. It is only threadsafe to construct the LOCALE_IO before the threads are created,
246  // destroy it after they finish, and block the main (GUI) thread while they work. Any deviation
247  // from this will cause nasal demons.
248  //
249  // TODO: blast LOCALE_IO into the sun
250 
252  std::vector<std::thread> threads;
253 
254  for( size_t ii = 0; ii < std::thread::hardware_concurrency() + 1; ++ii )
255  {
256  threads.push_back( std::thread( [this, &queue_parsed]() {
257  wxString nickname;
258 
259  while( this->m_queue_out.pop( nickname ) && !m_cancelled )
260  {
261  wxArrayString fpnames;
262 
263  try
264  {
265  m_lib_table->FootprintEnumerate( fpnames, nickname );
266  }
267  catch( const IO_ERROR& ioe )
268  {
269  m_errors.move_push( std::make_unique<IO_ERROR>( ioe ) );
270  }
271  catch( const std::exception& se )
272  {
273  // This is a round about way to do this, but who knows what THROW_IO_ERROR()
274  // may be tricked out to do someday, keep it in the game.
275  try
276  {
277  THROW_IO_ERROR( se.what() );
278  }
279  catch( const IO_ERROR& ioe )
280  {
281  m_errors.move_push( std::make_unique<IO_ERROR>( ioe ) );
282  }
283  }
284 
285  for( unsigned jj = 0; jj < fpnames.size() && !m_cancelled; ++jj )
286  {
287  wxString fpname = fpnames[jj];
288  FOOTPRINT_INFO* fpinfo = new FOOTPRINT_INFO_IMPL( this, nickname, fpname );
289  queue_parsed.move_push( std::unique_ptr<FOOTPRINT_INFO>( fpinfo ) );
290  }
291 
292  if( m_progress_reporter )
294 
295  m_count_finished.fetch_add( 1 );
296  }
297  } ) );
298  }
299 
300  while( !m_cancelled && (size_t)m_count_finished.load() < total_count )
301  {
303  m_cancelled = true;
304 
305  wxMilliSleep( 30 );
306  }
307 
308  for( auto& thr : threads )
309  thr.join();
310 
311  std::unique_ptr<FOOTPRINT_INFO> fpi;
312 
313  while( queue_parsed.pop( fpi ) )
314  m_list.push_back( std::move( fpi ) );
315 
316  std::sort( m_list.begin(), m_list.end(), []( std::unique_ptr<FOOTPRINT_INFO> const& lhs,
317  std::unique_ptr<FOOTPRINT_INFO> const& rhs ) -> bool
318  {
319  return *lhs < *rhs;
320  } );
321 
322  return m_errors.empty();
323 }
SYNC_QUEUE< wxString > m_queue_out
size_t size() const
Return the size of the queue.
Definition: sync_queue.h:94
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:179
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
bool empty() const
Return true iff the queue is empty.
Definition: sync_queue.h:85
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
std::atomic_size_t m_count_finished
#define THROW_IO_ERROR(msg)
YYCODETYPE lhs
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
void FOOTPRINT_LIST_IMPL::loader_job ( )
protected

Function loader_job loads footprints from m_queue_in.

Definition at line 100 of file footprint_info_impl.cpp.

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().

101 {
102  wxString nickname;
103 
104  while( m_queue_in.pop( nickname ) && !m_cancelled )
105  {
106  CatchErrors( [this, &nickname]() {
107  m_lib_table->PrefetchLib( nickname );
108  m_queue_out.push( nickname );
109  } );
110 
111  m_count_finished.fetch_add( 1 );
112 
113  if( m_progress_reporter )
115  }
116 }
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)
std::unique_ptr<IO_ERROR> FOOTPRINT_LIST::PopError ( )
inlineinherited

Definition at line 251 of file footprint_info.h.

References SYNC_QUEUE< T >::pop().

Referenced by FOOTPRINT_LIST::DisplayErrors().

252  {
253  std::unique_ptr<IO_ERROR> error;
254 
255  m_errors.pop( error );
256  return error;
257  }
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
void FOOTPRINT_LIST_IMPL::ReadCacheFromFile ( wxTextFile *  aFile)
overridevirtual

Reimplemented from FOOTPRINT_LIST.

Definition at line 372 of file footprint_info_impl.cpp.

References FOOTPRINT_LIST::m_list, m_list_timestamp, and name.

Referenced by PCB_BASE_EDIT_FRAME::PCB_BASE_EDIT_FRAME().

373 {
374  try
375  {
376  m_list.clear();
377 
378  if( aCacheFile->Exists() )
379  aCacheFile->Open();
380  else
381  return;
382 
383  aCacheFile->GetFirstLine().ToLongLong( &m_list_timestamp );
384 
385  while( aCacheFile->GetCurrentLine() + 6 < aCacheFile->GetLineCount() )
386  {
387  wxString libNickname = aCacheFile->GetNextLine();
388  wxString name = aCacheFile->GetNextLine();
389  wxString description = aCacheFile->GetNextLine();
390  wxString keywords = aCacheFile->GetNextLine();
391  int orderNum = wxAtoi( aCacheFile->GetNextLine() );
392  unsigned int padCount = (unsigned) wxAtoi( aCacheFile->GetNextLine() );
393  unsigned int uniquePadCount = (unsigned) wxAtoi( aCacheFile->GetNextLine() );
394 
395  auto* fpinfo = new FOOTPRINT_INFO_IMPL( libNickname, name, description, keywords,
396  orderNum, padCount, uniquePadCount );
397  m_list.emplace_back( std::unique_ptr<FOOTPRINT_INFO>( fpinfo ) );
398  }
399  }
400  catch( ... )
401  {
402  // whatever went wrong, invalidate the cache
403  m_list_timestamp = 0;
404  }
405 
406  if( aCacheFile->IsOpened() )
407  aCacheFile->Close();
408 }
const char * name
Definition: DXF_plotter.cpp:61
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 119 of file footprint_info_impl.cpp.

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().

121 {
122  long long int generatedTimestamp = aTable->GenerateTimestamp( aNickname );
123 
124  if( generatedTimestamp == m_list_timestamp )
125  return true;
126 
127  m_progress_reporter = aProgressReporter;
128  m_cancelled = false;
129 
130  FOOTPRINT_ASYNC_LOADER loader;
131 
132  loader.SetList( this );
133  loader.Start( aTable, aNickname );
134 
135  if( m_progress_reporter )
136  {
138  m_progress_reporter->Report( _( "Fetching Footprint Libraries" ) );
139  }
140 
141  while( !m_cancelled && (int)m_count_finished.load() < m_loader->m_total_libs )
142  {
144  m_cancelled = true;
145 
146  wxMilliSleep( 20 );
147  }
148 
149  if( m_cancelled )
150  {
151  loader.Abort();
152  }
153  else
154  {
155  if( m_progress_reporter )
156  {
159  m_progress_reporter->Report( _( "Loading Footprints" ) );
160  }
161 
162  loader.Join();
163 
164  if( m_progress_reporter )
166  }
167 
168  if( m_cancelled )
169  m_list_timestamp = 0; // God knows what we got before we were cancelled
170  else
171  m_list_timestamp = generatedTimestamp;
172 
173  return m_errors.empty();
174 }
void AdvancePhase()
Uses the next vailable virtual zone of the dialog progress bar.
SYNC_QUEUE< wxString > m_queue_out
size_t size() const
Return the size of the queue.
Definition: sync_queue.h:94
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.
bool empty() const
Return true iff the queue is empty.
Definition: sync_queue.h:85
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.
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...
FOOTPRINT_ASYNC_LOADER * m_loader
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.
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 177 of file footprint_info_impl.cpp.

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().

179 {
180  m_loader = aLoader;
181  m_lib_table = aTable;
182 
183  // Clear data before reading files
184  m_count_finished.store( 0 );
185  m_errors.clear();
186  m_list.clear();
187  m_threads.clear();
188  m_queue_in.clear();
189  m_queue_out.clear();
190 
191  if( aNickname )
192  m_queue_in.push( *aNickname );
193  else
194  {
195  for( auto const& nickname : aTable->GetLogicalLibs() )
196  m_queue_in.push( nickname );
197  }
198 
200 
201  for( unsigned i = 0; i < aNThreads; ++i )
202  {
203  m_threads.push_back( std::thread( &FOOTPRINT_LIST_IMPL::loader_job, this ) );
204  }
205 }
void push(T const &aValue)
Push a value onto the queue.
Definition: sync_queue.h:45
SYNC_QUEUE< wxString > m_queue_out
size_t size() const
Return the size of the queue.
Definition: sync_queue.h:94
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 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...
void FOOTPRINT_LIST_IMPL::StopWorkers ( )
overrideprotectedvirtual

Stop worker threads.

Part of the FOOTPRINT_ASYNC_LOADER implementation.

Implements FOOTPRINT_LIST.

Definition at line 207 of file footprint_info_impl.cpp.

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().

208 {
209  std::lock_guard<std::mutex> lock1( m_join );
210 
211  // To safely stop our workers, we set the cancellation flag (they will each
212  // exit on their next safe loop location when this is set). Then we need to wait
213  // for all threads to finish as closing the implementation will free the queues
214  // that the threads write to.
215  for( auto& i : m_threads )
216  i.join();
217 
218  m_threads.clear();
219  m_queue_in.clear();
220  m_count_finished.store( 0 );
221 
222  // If we have cancelled in the middle of a load, clear our timestamp to re-load next time
223  if( m_cancelled )
224  m_list_timestamp = 0;
225 }
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
void FOOTPRINT_LIST_IMPL::WriteCacheToFile ( wxTextFile *  aFile)
overridevirtual

Reimplemented from FOOTPRINT_LIST.

Definition at line 342 of file footprint_info_impl.cpp.

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

Referenced by PCB_BASE_EDIT_FRAME::~PCB_BASE_EDIT_FRAME().

343 {
344  if( aCacheFile->Exists() )
345  {
346  aCacheFile->Open();
347  aCacheFile->Clear();
348  }
349  else
350  {
351  aCacheFile->Create();
352  }
353 
354  aCacheFile->AddLine( wxString::Format( "%lld", m_list_timestamp ) );
355 
356  for( auto& fpinfo : m_list )
357  {
358  aCacheFile->AddLine( fpinfo->GetLibNickname() );
359  aCacheFile->AddLine( fpinfo->GetName() );
360  aCacheFile->AddLine( fpinfo->GetDescription() );
361  aCacheFile->AddLine( fpinfo->GetKeywords() );
362  aCacheFile->AddLine( wxString::Format( "%d", fpinfo->GetOrderNum() ) );
363  aCacheFile->AddLine( wxString::Format( "%u", fpinfo->GetPadCount() ) );
364  aCacheFile->AddLine( wxString::Format( "%u", fpinfo->GetUniquePadCount() ) );
365  }
366 
367  aCacheFile->Write();
368  aCacheFile->Close();
369 }
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

Member Data Documentation

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().

std::atomic_size_t FOOTPRINT_LIST_IMPL::m_count_finished
private
ERRLIST FOOTPRINT_LIST::m_errors
protectedinherited

some can be PARSE_ERRORs also

Definition at line 198 of file footprint_info.h.

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

std::mutex FOOTPRINT_LIST_IMPL::m_join
private

Definition at line 94 of file footprint_info_impl.h.

Referenced by JoinWorkers(), and StopWorkers().

FP_LIB_TABLE* FOOTPRINT_LIST::m_lib_table
protectedinherited

no ownership

Definition at line 192 of file footprint_info.h.

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

FPILIST FOOTPRINT_LIST::m_list
protectedinherited
long long FOOTPRINT_LIST_IMPL::m_list_timestamp
private
FOOTPRINT_ASYNC_LOADER* FOOTPRINT_LIST_IMPL::m_loader
private

Definition at line 86 of file footprint_info_impl.h.

Referenced by ReadFootprintFiles(), and StartWorkers().

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().

SYNC_QUEUE<wxString> FOOTPRINT_LIST_IMPL::m_queue_in
private
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().

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: