KiCad PCB EDA Suite
SYMBOL_ASYNC_LOADER Class Reference

#include <symbol_async_loader.h>

Public Types

typedef std::pair< wxString, std::vector< LIB_SYMBOL * > > LOADED_PAIR
 

Public Member Functions

 SYMBOL_ASYNC_LOADER (const std::vector< wxString > &aNicknames, SYMBOL_LIB_TABLE *aTable, bool aOnlyPowerSymbols=false, std::unordered_map< wxString, std::vector< LIB_SYMBOL * >> *aOutput=nullptr, PROGRESS_REPORTER *aReporter=nullptr)
 Constructs a loader for symbol libraries. More...
 
 ~SYMBOL_ASYNC_LOADER ()
 
void Start ()
 Spins up threads to load all the libraries in m_nicknames. More...
 
bool Join ()
 Finalizes the threads and combines the output into the target output map. More...
 
bool Done ()
 Returns a string containing any errors generated during the load. More...
 
const wxString & GetErrors () const
 Represents a pair of <nickname, loaded parts list> More...
 

Private Member Functions

std::vector< LOADED_PAIRworker ()
 < Worker job that loads libraries and returns a list of pairs of <nickname, loaded parts> More...
 

Private Attributes

std::vector< wxString > m_nicknames
 Handle to the symbol library table being loaded into. More...
 
SYMBOL_LIB_TABLEm_table
 True if we are loading only power symbols. More...
 
bool m_onlyPowerSymbols
 Handle to map that will be filled with the loaded parts per library. More...
 
std::unordered_map< wxString, std::vector< LIB_SYMBOL * > > * m_output
 Progress reporter (may be null) More...
 
PROGRESS_REPORTERm_reporter
 
size_t m_threadCount
 
std::atomic< size_t > m_nextLibrary
 
wxString m_errors
 
std::mutex m_errorMutex
 
std::vector< std::future< std::vector< LOADED_PAIR > > > m_returns
 

Detailed Description

Definition at line 37 of file symbol_async_loader.h.

Member Typedef Documentation

◆ LOADED_PAIR

typedef std::pair<wxString, std::vector<LIB_SYMBOL*> > SYMBOL_ASYNC_LOADER::LOADED_PAIR

Definition at line 72 of file symbol_async_loader.h.

Constructor & Destructor Documentation

◆ SYMBOL_ASYNC_LOADER()

SYMBOL_ASYNC_LOADER::SYMBOL_ASYNC_LOADER ( const std::vector< wxString > &  aNicknames,
SYMBOL_LIB_TABLE aTable,
bool  aOnlyPowerSymbols = false,
std::unordered_map< wxString, std::vector< LIB_SYMBOL * >> *  aOutput = nullptr,
PROGRESS_REPORTER aReporter = nullptr 
)

Constructs a loader for symbol libraries.

Parameters
aNicknamesis a list of library nicknames to load
aTableis a pointer to the symbol library table to load libraries for
aOnlyPowerSymbols,iftrue, will only return power symbols in the output map
aOutputwill be filled with the loaded parts
aReporterwill be used to repord progress, of not null

Definition at line 29 of file symbol_async_loader.cpp.

32  :
33  m_nicknames( aNicknames ),
34  m_table( aTable ),
35  m_onlyPowerSymbols( aOnlyPowerSymbols ),
36  m_output( aOutput ),
37  m_reporter( aReporter ),
38  m_nextLibrary( 0 )
39 {
40  wxASSERT( m_table );
41  m_threadCount = std::max<size_t>( 1, std::thread::hardware_concurrency() );
42 
43  m_returns.resize( m_threadCount );
44 }
bool m_onlyPowerSymbols
Handle to map that will be filled with the loaded parts per library.
std::atomic< size_t > m_nextLibrary
std::unordered_map< wxString, std::vector< LIB_SYMBOL * > > * m_output
Progress reporter (may be null)
SYMBOL_LIB_TABLE * m_table
True if we are loading only power symbols.
std::vector< wxString > m_nicknames
Handle to the symbol library table being loaded into.
std::vector< std::future< std::vector< LOADED_PAIR > > > m_returns
PROGRESS_REPORTER * m_reporter

References m_returns, m_table, and m_threadCount.

◆ ~SYMBOL_ASYNC_LOADER()

SYMBOL_ASYNC_LOADER::~SYMBOL_ASYNC_LOADER ( )

Definition at line 48 of file symbol_async_loader.cpp.

49 {
50  Join();
51 }
bool Join()
Finalizes the threads and combines the output into the target output map.

References Join().

Member Function Documentation

◆ Done()

bool SYMBOL_ASYNC_LOADER::Done ( )

Returns a string containing any errors generated during the load.

Definition at line 90 of file symbol_async_loader.cpp.

91 {
92  return m_nextLibrary.load() >= m_nicknames.size();
93 }
std::atomic< size_t > m_nextLibrary
std::vector< wxString > m_nicknames
Handle to the symbol library table being loaded into.

References m_nextLibrary, and m_nicknames.

Referenced by SYMBOL_LIBRARY_MANAGER::Preload().

◆ GetErrors()

const wxString& SYMBOL_ASYNC_LOADER::GetErrors ( ) const
inline

Represents a pair of <nickname, loaded parts list>

Definition at line 69 of file symbol_async_loader.h.

References m_errors.

Referenced by SYMBOL_LIBRARY_MANAGER::Preload().

◆ Join()

bool SYMBOL_ASYNC_LOADER::Join ( )

Finalizes the threads and combines the output into the target output map.

Return true if loading is done

Definition at line 61 of file symbol_async_loader.cpp.

62 {
63  for( size_t ii = 0; ii < m_threadCount; ++ii )
64  {
65  if( !m_returns[ii].valid() )
66  continue;
67 
68  m_returns[ii].wait();
69 
70  const std::vector<LOADED_PAIR>& ret = m_returns[ii].get();
71 
72  if( m_output && !ret.empty() )
73  {
74  for( const LOADED_PAIR& pair : ret )
75  {
76  // Don't show libraries that had no power symbols
77  if( m_onlyPowerSymbols && pair.second.empty() )
78  continue;
79 
80  // *Do* show empty libraries in the normal case
81  m_output->insert( pair );
82  }
83  }
84  }
85 
86  return true;
87 }
bool m_onlyPowerSymbols
Handle to map that will be filled with the loaded parts per library.
std::pair< wxString, std::vector< LIB_SYMBOL * > > LOADED_PAIR
std::unordered_map< wxString, std::vector< LIB_SYMBOL * > > * m_output
Progress reporter (may be null)
std::vector< std::future< std::vector< LOADED_PAIR > > > m_returns

References m_onlyPowerSymbols, m_output, m_returns, and m_threadCount.

Referenced by SYMBOL_LIBRARY_MANAGER::Preload(), and ~SYMBOL_ASYNC_LOADER().

◆ Start()

void SYMBOL_ASYNC_LOADER::Start ( )

Spins up threads to load all the libraries in m_nicknames.

Definition at line 54 of file symbol_async_loader.cpp.

55 {
56  for( size_t ii = 0; ii < m_threadCount; ++ii )
57  m_returns[ii] = std::async( std::launch::async, &SYMBOL_ASYNC_LOADER::worker, this );
58 }
std::vector< LOADED_PAIR > worker()
< Worker job that loads libraries and returns a list of pairs of <nickname, loaded parts>
std::vector< std::future< std::vector< LOADED_PAIR > > > m_returns

References m_returns, m_threadCount, and worker().

Referenced by SYMBOL_LIBRARY_MANAGER::Preload().

◆ worker()

std::vector< SYMBOL_ASYNC_LOADER::LOADED_PAIR > SYMBOL_ASYNC_LOADER::worker ( )
private

< Worker job that loads libraries and returns a list of pairs of <nickname, loaded parts>

list of libraries to load

Definition at line 96 of file symbol_async_loader.cpp.

97 {
98  std::vector<LOADED_PAIR> ret;
99 
100  bool onlyPower = m_onlyPowerSymbols;
101 
102  for( size_t libraryIndex = m_nextLibrary++; libraryIndex < m_nicknames.size();
103  libraryIndex = m_nextLibrary++ )
104  {
105  const wxString& nickname = m_nicknames[libraryIndex];
106 
107  if( m_reporter )
108  m_reporter->AdvancePhase( wxString::Format( _( "Loading library %s..." ), nickname ) );
109 
110  if( m_reporter && m_reporter->IsCancelled() )
111  break;
112 
113  LOADED_PAIR pair( nickname, {} );
114 
115  try
116  {
117  m_table->LoadSymbolLib( pair.second, nickname, onlyPower );
118  ret.emplace_back( std::move( pair ) );
119  }
120  catch( const IO_ERROR& ioe )
121  {
122  wxString msg = wxString::Format( _( "Error loading symbol library %s.\n\n%s\n" ),
123  nickname, ioe.What() );
124 
125  std::lock_guard<std::mutex> lock( m_errorMutex );
126  m_errors += msg;
127  }
128  }
129 
130  return ret;
131 }
bool m_onlyPowerSymbols
Handle to map that will be filled with the loaded parts per library.
std::atomic< size_t > m_nextLibrary
std::pair< wxString, std::vector< LIB_SYMBOL * > > LOADED_PAIR
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
virtual void AdvancePhase()=0
Use the next available virtual zone of the dialog progress bar.
#define _(s)
SYMBOL_LIB_TABLE * m_table
True if we are loading only power symbols.
std::vector< wxString > m_nicknames
Handle to the symbol library table being loaded into.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
void LoadSymbolLib(std::vector< LIB_SYMBOL * > &aAliasList, const wxString &aNickname, bool aPowerSymbolsOnly=false)
PROGRESS_REPORTER * m_reporter
virtual bool IsCancelled() const =0
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75

References _, PROGRESS_REPORTER::AdvancePhase(), Format(), PROGRESS_REPORTER::IsCancelled(), SYMBOL_LIB_TABLE::LoadSymbolLib(), m_errorMutex, m_errors, m_nextLibrary, m_nicknames, m_onlyPowerSymbols, m_reporter, m_table, and IO_ERROR::What().

Referenced by Start().

Member Data Documentation

◆ m_errorMutex

std::mutex SYMBOL_ASYNC_LOADER::m_errorMutex
private

Definition at line 96 of file symbol_async_loader.h.

Referenced by worker().

◆ m_errors

wxString SYMBOL_ASYNC_LOADER::m_errors
private

Definition at line 95 of file symbol_async_loader.h.

Referenced by GetErrors(), and worker().

◆ m_nextLibrary

std::atomic<size_t> SYMBOL_ASYNC_LOADER::m_nextLibrary
private

Definition at line 94 of file symbol_async_loader.h.

Referenced by Done(), and worker().

◆ m_nicknames

std::vector<wxString> SYMBOL_ASYNC_LOADER::m_nicknames
private

Handle to the symbol library table being loaded into.

Definition at line 79 of file symbol_async_loader.h.

Referenced by Done(), and worker().

◆ m_onlyPowerSymbols

bool SYMBOL_ASYNC_LOADER::m_onlyPowerSymbols
private

Handle to map that will be filled with the loaded parts per library.

Definition at line 85 of file symbol_async_loader.h.

Referenced by Join(), and worker().

◆ m_output

std::unordered_map<wxString, std::vector<LIB_SYMBOL*> >* SYMBOL_ASYNC_LOADER::m_output
private

Progress reporter (may be null)

Definition at line 88 of file symbol_async_loader.h.

Referenced by Join().

◆ m_reporter

PROGRESS_REPORTER* SYMBOL_ASYNC_LOADER::m_reporter
private

Definition at line 91 of file symbol_async_loader.h.

Referenced by worker().

◆ m_returns

std::vector<std::future<std::vector<LOADED_PAIR> > > SYMBOL_ASYNC_LOADER::m_returns
private

Definition at line 98 of file symbol_async_loader.h.

Referenced by Join(), Start(), and SYMBOL_ASYNC_LOADER().

◆ m_table

SYMBOL_LIB_TABLE* SYMBOL_ASYNC_LOADER::m_table
private

True if we are loading only power symbols.

Definition at line 82 of file symbol_async_loader.h.

Referenced by SYMBOL_ASYNC_LOADER(), and worker().

◆ m_threadCount

size_t SYMBOL_ASYNC_LOADER::m_threadCount
private

Definition at line 93 of file symbol_async_loader.h.

Referenced by Join(), Start(), and SYMBOL_ASYNC_LOADER().


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