25#include <magic_enum.hpp>
30#include <unordered_set>
42using namespace std::chrono_literals;
59 static std::mutex& registryLock = *
new std::mutex;
60 static std::map<wxString, std::unique_ptr<std::recursive_mutex>>& registry =
61 *
new std::map<wxString, std::unique_ptr<std::recursive_mutex>>;
63 std::lock_guard guard( registryLock );
64 std::unique_ptr<std::recursive_mutex>& slot = registry[aNickname];
67 slot = std::make_unique<std::recursive_mutex>();
81 std::map<LIBRARY_TABLE_TYPE, std::unique_ptr<LIBRARY_MANAGER_ADAPTER>> adapters;
97 std::vector<LIBRARY_TABLE_TYPE> aTablesToLoad )
116 wxCHECK_MSG(
false,
m_tables,
"Invalid scope passed to loadTables" );
120 std::map<LIBRARY_TABLE_TYPE, std::unique_ptr<LIBRARY_TABLE>>& aTarget = getTarget();
122 if( aTablesToLoad.size() == 0 )
127 aTarget.erase( type );
131 if( fn.IsFileReadable() )
133 std::unique_ptr<LIBRARY_TABLE>
table = std::make_unique<LIBRARY_TABLE>( fn, aScope );
135 if(
table->Type() != type )
137 auto actualName = magic_enum::enum_name(
table->Type() );
138 auto expectedName = magic_enum::enum_name( type );
139 wxLogWarning( wxS(
"Library table '%s' has type %s but expected %s; skipping" ),
141 wxString( actualName.data(), actualName.size() ),
142 wxString( expectedName.data(), expectedName.size() ) );
146 aTarget[type] = std::move(
table );
151 wxLogTrace(
traceLibraries,
"No library table found at %s", fn.GetFullPath() );
159 std::unordered_set<wxString> seenTables;
164 seenTables.insert( aTable.Path() );
176 file.MakeAbsolute( wxFileName( aTable.Path() ).GetPath() );
179 wxString src = file.GetFullPath();
181 if( seenTables.contains( src ) )
183 wxLogTrace(
traceLibraries,
"Library table %s has already been loaded!", src );
185 row.SetErrorDescription(
_(
"A reference to this library table already exists" ) );
189 auto child = std::make_unique<LIBRARY_TABLE>( file, aRootTable.
Scope() );
191 processOneTable( *child );
196 row.SetErrorDescription( child->ErrorDescription() );
201 m_childTables.insert_or_assign( row.URI(), std::move( child ) );
206 processOneTable( aRootTable );
221 wxString tablePath = aTable.
Path();
228 const std::map<wxString, LIB_OVERRIDE>& overrides = it->second;
232 auto overIt = overrides.find( row.Nickname() );
234 if( overIt != overrides.end() )
236 row.SetDisabled( overIt->second.disabled );
237 row.SetHidden( overIt->second.hidden );
250 bool aDisabled,
bool aHidden )
255 wxCHECK( settings, );
257 if( !aDisabled && !aHidden )
263 settings->
m_LibOverrides[aTablePath][aNickname] = { aDisabled, aHidden };
272 wxCHECK( settings, );
279 tableIt->second.erase( aNickname );
281 if( tableIt->second.empty() )
293 default: wxCHECK(
false, wxEmptyString );
302 wxCHECK( !
m_tables.contains( aType ), );
324 const wxString& aPrefix ) :
330 wxFileName f( aBasePath,
"" );
336 .value_or(
nullptr );
340 wxDirTraverseResult
OnFile(
const wxString& aFilePath )
override
342 wxFileName file = wxFileName::FileName( aFilePath );
346 if( file.GetExt() == wxT(
"kicad_sym" )
353 return wxDIR_CONTINUE;
357 wxDirTraverseResult
OnDir(
const wxString& dirPath )
override
360 wxFileName dir = wxFileName::DirName( dirPath );
364 if( dirPath.EndsWith( wxS(
".pretty" ) )
370 else if( dirPath.EndsWith( designBlockExt )
377 return wxDIR_CONTINUE;
385 if( aTable->
HasRow( aNickname ) )
391 aNickname = wxString::Format(
"%s%s_%d",
m_lib_prefix, aBaseName, increment );
393 }
while( aTable->
HasRow( aNickname ) );
404 int aExtensionLength )
410 wxString versionedPath = wxString::Format( wxS(
"${%s}" ),
413 wxArrayString parts = aSource.GetDirs();
415 parts.Insert( versionedPath, 0 );
418 parts.Add( aSource.GetFullName() );
420 wxString libPath = wxJoin( parts,
'/' );
424 wxString
name = parts.Last().substr( 0, parts.Last().length() - aExtensionLength );
429 wxLogTrace(
traceLibraries,
"Manager: Adding PCM lib '%s' as '%s'", libPath, nickname );
441 wxLogTrace(
traceLibraries,
"Manager: Not adding existing PCM lib '%s'", libPath );
466 return fn.GetFullPath();
477 return fn.GetFullPath();
485 return wxString::Format( wxS(
"${%s}/%s" ), templateDirVar,
tableFileName( aType ) );
499 auto it = common->m_Env.vars.find( templateDirVar );
501 if( it != common->m_Env.vars.end() && it->second.GetDefinedExternally() )
511 if( wxFileName fn( aPath ); fn.IsFileReadable() )
531 std::vector<LIBRARY_TABLE_TYPE> invalidTables;
541 invalidTables.emplace_back( tableType );
544 return invalidTables;
553 table.SetType( aType );
554 table.Rows().clear();
558 if( aPopulateDefaultLibraries && defaultLib.IsFileReadable() )
566 else if( aPopulateDefaultLibraries )
568 auto typeName = magic_enum::enum_name( aType );
569 wxLogTrace(
traceLibraries,
"No stock library table for %s at '%s'; creating empty global table",
570 wxString( typeName.data(), typeName.size() ), defaultLib.GetFullPath() );
576 table.Format( &formatter );
595 for(
const std::unique_ptr<LIBRARY_MANAGER_ADAPTER>& adapter :
m_adapters | std::views::values )
596 adapter->GlobalTablesChanged( aTablesToLoad );
608 wxCHECK( settings, );
616 wxFileName d( *packagesPath,
"" );
621 wxDir dir( d.GetPath() );
623 dir.Traverse( traverser );
627 table->Save().map_error(
630 wxLogTrace(
traceLibraries, wxT(
"Warning: save failed after PCM auto-add: %s" ),
637 auto cleanupRemovedPCMLibraries =
645 auto toErase = std::ranges::remove_if(
table->Rows(),
648 if( !IsPcmManagedRow( aRow ) )
651 wxString path = GetFullURI( &aRow, true );
652 return !wxFileName::Exists( path );
655 bool hadRemovals = !toErase.empty();
656 table->Rows().erase( toErase.begin(), toErase.end() );
660 table->Save().map_error(
663 wxLogTrace(
traceLibraries, wxT(
"Warning: save failed after PCM auto-remove: %s" ),
695 for(
const std::unique_ptr<LIBRARY_MANAGER_ADAPTER>& adapter :
m_adapters | std::views::values )
696 adapter->ProjectChanged();
704 for(
const std::unique_ptr<LIBRARY_MANAGER_ADAPTER>& adapter :
m_adapters | std::views::values )
705 adapter->AbortAsyncLoad( aCancelPlugins );
713 for(
const std::unique_ptr<LIBRARY_MANAGER_ADAPTER>& adapter :
m_adapters | std::views::values )
714 adapter->RestartAsyncLoad();
722 for(
const std::unique_ptr<LIBRARY_MANAGER_ADAPTER>& adapter :
m_adapters | std::views::values )
723 adapter->LeakCachesAtExit();
728 std::unique_ptr<LIBRARY_MANAGER_ADAPTER>&& aAdapter )
732 wxCHECK_MSG( !
m_adapters.contains( aType ), ,
"You should only register an adapter once!" );
740 std::unique_ptr<LIBRARY_MANAGER_ADAPTER> retired;
773 wxCHECK_MSG(
false, std::nullopt,
"Table() requires a single scope" );
780 magic_enum::enum_name( aType ) );
791 if( !
Project().IsNullProject() )
806 bool aIncludeInvalid )
const
808 std::map<wxString, LIBRARY_TABLE_ROW*>
rows;
809 std::vector<wxString> rowOrder;
811 std::list<std::ranges::ref_view<const std::map<LIBRARY_TABLE_TYPE, std::unique_ptr<LIBRARY_TABLE>>>> tables;
816 tables = { std::views::all(
m_tables ) };
831 std::function<void(
const std::unique_ptr<LIBRARY_TABLE>&,
bool parentHidden)> processTable =
832 [&](
const std::unique_ptr<LIBRARY_TABLE>& aTable,
const bool parentHidden )
834 if( aTable->Type() != aType )
837 if( aTable->IsOk() || aIncludeInvalid )
841 if( row.IsOk() || aIncludeInvalid )
845 row.SetHidden(
true );
860 if( !
rows.contains( row.Nickname() ) )
861 rowOrder.emplace_back( row.Nickname() );
863 rows[ row.Nickname() ] = &row;
870 for(
const std::unique_ptr<LIBRARY_TABLE>&
table :
871 std::views::join( tables ) | std::views::values )
873 processTable(
table,
false );
876 std::vector<LIBRARY_TABLE_ROW*> ret;
878 for(
const wxString& row : rowOrder )
879 ret.emplace_back(
rows[row] );
890 auto key = std::make_tuple( aType, aScope, aNickname );
898 if( row->Nickname() == aNickname )
901 m_rowCache[std::make_tuple( aType, aScope, aNickname )] = row;
911 const wxString& aUri,
927 if( std::optional<LIBRARY_MANAGER_ADAPTER*> adapter =
Adapter( aType ); adapter )
928 ( *adapter )->ReloadLibraryEntry( aNickname, aScope );
933 const wxString& aNickname )
935 if( std::optional<LIBRARY_MANAGER_ADAPTER*> adapter =
Adapter( aType ); adapter )
936 return ( *adapter )->LoadLibraryEntry( aNickname );
943 std::initializer_list<LIBRARY_TABLE_TYPE> aTablesToLoad )
952 for(
const std::unique_ptr<LIBRARY_MANAGER_ADAPTER>& adapter :
m_adapters | std::views::values )
953 adapter->ProjectTablesChanged( aTablesToLoad );
956 if( wxFileName::IsDirReadable( aProjectPath ) )
970 if( aTablesToLoad.size() == 0 )
980 wxLogTrace(
traceLibraries,
"New project path %s is not readable, not loading project tables", aProjectPath );
990 for(
const std::unique_ptr<LIBRARY_MANAGER_ADAPTER>& adapter :
m_adapters | std::views::values )
991 adapter->ProjectTablesReloaded( aTablesToLoad );
997 std::initializer_list<LIBRARY_TABLE_TYPE> aTablesToLoad )
1014 if( std::optional<const LIBRARY_TABLE_ROW*>
result =
GetRow( aType, aNickname ) )
1017 return std::nullopt;
1036 path.MakeAbsolute();
1037 return path.GetFullPath();
1052 const wxString& uri = aRow.
URI();
1054 if( !uri.StartsWith( wxS(
"${" ) ) )
1057 size_t end = uri.find( wxS(
'}' ) );
1059 if(
end == wxString::npos ||
end <= 2 )
1062 wxString varName = uri.SubString( 2,
end - 1 );
1070 if(
end + 1 >= uri.length() || uri[
end + 1] != wxS(
'/' ) )
1077 wxArrayString parts = wxSplit( uri.Mid(
end + 2 ),
'/',
'\0' );
1079 if( parts.size() < 3 )
1082 for(
const wxString& part : parts )
1084 if( part.IsEmpty() )
1088 wxString category = parts[0];
1089 wxString leaf = parts.Last();
1094 auto hasLibExtension = [&leaf](
const wxString& aExt )
1096 return leaf.length() > aExt.length() && leaf.EndsWith( aExt );
1099 if( category == wxS(
"symbols" ) )
1100 return hasLibExtension( wxS(
".kicad_sym" ) );
1102 if( category == wxS(
"footprints" ) )
1103 return hasLibExtension( wxS(
".pretty" ) );
1105 if( category == wxS(
"design_blocks" ) )
1107 static const wxString designBlockExt =
1110 return hasLibExtension( designBlockExt );
1120 if( aURI1.Find(
"://" ) != wxNOT_FOUND )
1123 return aURI1 == aURI2;
1127 const wxFileName fn1( aURI1 );
1128 const wxFileName fn2( aURI2 );
1175 std::vector<std::unique_ptr<IO_BASE>> retired;
1186 retired.push_back( std::move( entry.second.plugin ) );
1219 lib.plugin->LeakCacheAtExit();
1227 if( lib.global_owner ==
this && lib.plugin )
1228 lib.plugin->LeakCacheAtExit();
1235 bool me = aChangedTables.size() == 0;
1239 if( type ==
Type() )
1254 std::map<wxString, LIB_DATA> retired;
1268 [
this](
const wxString&,
const LIB_DATA& aLib )
1274 std::vector<std::map<wxString, LIB_DATA>::node_type> retired;
1281 if( it->second.global_owner ==
this )
1282 retired.push_back(
globalLibs().extract( it++ ) );
1296 [
this](
const wxString&,
const LIB_DATA& aLib )
1307 std::map<wxString, LIB_DATA>& aLibs, std::shared_mutex& aMutex,
1308 const std::function<
bool(
const wxString& aNickname,
const LIB_DATA& aLib )>& aRetiring )
1310 std::vector<IO_BASE*> plugins;
1313 std::shared_lock lock( aMutex );
1315 for(
const auto& [nickname, lib] : aLibs )
1317 if( lib.plugin && ( !aRetiring || aRetiring( nickname, lib ) ) )
1318 plugins.push_back( lib.plugin.get() );
1324 plugin->StopBackgroundRefresh();
1330 bool me = aChangedTables.size() == 0;
1334 if( type ==
Type() )
1349 std::initializer_list<LIBRARY_TABLE_TYPE> aChangedTables )
1351 bool me = aChangedTables.size() == 0;
1355 if( type ==
Type() )
1372 [
this](
const wxString& aNickname )
1378 [&](
const wxString& aNickname,
const LIB_DATA& )
1380 return pruned( aNickname );
1384 std::vector<std::map<wxString, LIB_DATA>::node_type> retired;
1389 if( pruned( it->first ) )
1414 if( loadedRow->
URI() == aRow.
URI() && loadedRow->
Type() == aRow.
Type() )
1431 std::optional<LIB_STATUS> status =
CheckLibrary( &lib );
1433 if( status.has_value() )
1437 if( status.value().error.has_value() )
1448 aRow.
SetOk(
false );
1473 return row->Nickname();
1476 return std::nullopt;
1482 std::vector<wxString> ret;
1485 wxLogTrace(
traceLibraries,
"GetLibraryNames: checking %zu rows from table",
rows.size() );
1489 wxString nickname = row->Nickname();
1490 std::optional<const LIB_DATA*> loaded =
fetchIfLoaded( nickname );
1493 ret.emplace_back( nickname );
1498 wxLogTrace(
traceLibraries,
"GetLibraryNames: returning %zu of %zu libraries", ret.size(),
rows.size() );
1505 std::optional<const LIB_DATA*> r =
fetchIfLoaded( aNickname );
1508 return !aCheckEnabled || !r.value()->row->Disabled();
1523 std::lock_guard pluginGuard(
pluginMutex( aNickname ) );
1541 if( std::optional<const LIB_DATA*> optRow =
fetchIfLoaded( aNickname ); optRow )
1542 return ( *optRow )->row->Description();
1544 return std::nullopt;
1549 bool aIncludeInvalid )
const
1563 const wxString& aUri,
1579 if(
auto it = libs.find( aKey.first ); it != libs.end() && it->second.plugin )
1580 aAction( *it->second.plugin );
1624 []( std::future<void>& aFuture )
1626 return aFuture.valid() && aFuture.wait_for( 0s ) == std::future_status::ready;
1635 if( aCancelPlugins )
1649 std::vector<LOADING_KEY> cancelled;
1659 adapter.
m_abort.store(
false );
1665 } scope{ *
this, {} };
1667 if( aCancelPlugins )
1679 return std::nullopt;
1682 return loaded /
static_cast<float>( total );
1688 constexpr std::chrono::milliseconds interval( 150 );
1692 while( !aAbort.load() )
1694 std::this_thread::sleep_for( interval );
1699 if( progress.value_or( 1 ) >= 1 )
1707 aAbort.store(
false );
1717 wxLogTrace(
traceLibraries,
"BlockUntilLoaded: entry, acquiring m_loadMutex" );
1718 std::unique_lock<std::mutex> asyncLock(
m_loadMutex );
1722 for(
const std::future<void>& future :
m_futures )
1725 wxLogTrace(
traceLibraries,
"BlockUntilLoaded: all futures complete, loadCount=%zu, loadTotal=%zu",
1756 return it->second.status.error;
1763 return it->second.status.error;
1766 return std::nullopt;
1772 std::vector<std::pair<wxString, LIB_STATUS>> ret;
1776 if( row->Disabled() )
1781 ret.emplace_back( std::make_pair( row->Nickname(), *
result ) );
1791 ret.emplace_back( std::make_pair( row->Nickname(),
1802 std::vector<KI_ERROR> errors;
1808 wxString title = status.error->message.BeforeFirst(
'\n' );
1809 title.StartsWith( wxS(
"IO_ERROR: " ), &title );
1810 errors.emplace_back(
1812 .SetDescription( title )
1814 .SetDebugText( status.error->details )
1831 return std::nullopt;
1843 std::shared_mutex& aMutex )
1845 bool wasLoaded =
false;
1846 std::map<wxString, LIB_DATA>::node_type retired;
1849 std::unique_lock lock( aMutex );
1850 auto it = aTarget.find( aNickname );
1852 if( it != aTarget.end() && it->second.plugin )
1855 retired = aTarget.extract( it );
1870 else if( !
result.has_value() )
1872 wxLogTrace(
traceLibraries,
"ReloadLibraryEntry: failed to reload %s (%s): %s",
1873 aNickname, magic_enum::enum_name( aScopeToReload ),
1874 result.error().message );
1909 std::lock_guard pluginGuard(
pluginMutex( aNickname ) );
1926 std::lock_guard pluginGuard(
pluginMutex( aNickname ) );
1935 wxLogTrace(
traceLibraries,
"CreateLibrary: IO_ERROR for %s: %s",
1936 aNickname, ioe.
What() );
1939 catch(
const std::exception& e )
1941 wxLogTrace(
traceLibraries,
"CreateLibrary: std::exception for %s: %s",
1942 aNickname, e.what() );
1947 wxLogTrace(
traceLibraries,
"CreateLibrary: library row '%s' not found", aNickname );
1968 return std::nullopt;
1973 return std::nullopt;
1983 return std::nullopt;
1987 return std::nullopt;
2001 return std::nullopt;
2006 return std::nullopt;
2016 return std::nullopt;
2020 return std::nullopt;
2026 std::map<wxString, LIB_DATA>& aTarget,
2027 std::shared_mutex& aMutex )
2029 bool present =
false;
2032 std::shared_lock lock( aMutex );
2033 present = aTarget.contains( aNickname ) && aTarget.at( aNickname ).plugin;
2041 wxLogTrace(
traceLibraries,
"Library %s (%s) not yet loaded, will attempt...",
2042 aNickname, magic_enum::enum_name( aScope ) );
2047 std::unique_ptr<IO_BASE> candidate( *
plugin );
2048 std::unique_lock lock( aMutex );
2056 slot.
plugin = std::move( candidate );
2066 return tl::unexpected(
plugin.error() );
2073 std::shared_lock lock( aMutex );
2074 return &aTarget.at( aNickname );
2091 wxString msg = wxString::Format(
_(
"Library %s not found" ), aNickname );
2102 return it->second.status;
2109 return it->second.status;
2112 return std::nullopt;
2118 std::unique_lock<std::mutex> asyncLock(
m_loadMutex, std::try_to_lock );
2130 []( std::future<void>& aFuture )
2132 return aFuture.valid()
2133 && aFuture.wait_for( 0s ) == std::future_status::ready;
2138 wxLogTrace(
traceLibraries,
"Cannot AsyncLoad, futures from a previous call remain!" );
2149 wxLogTrace(
traceLibraries,
"AsyncLoad: no libraries left to load; exiting" );
2157 [&](
const wxString& aLib, std::map<wxString, LIB_DATA>& aMap, std::shared_mutex& aMutex )
2159 std::shared_lock lock( aMutex );
2161 if(
auto it = aMap.find( aLib ); it != aMap.end() )
2163 LOAD_STATUS status = it->second.status.load_status;
2174 auto workQueue = std::make_shared<std::vector<LOAD_WORK>>();
2175 workQueue->reserve(
rows.size() );
2179 wxString nickname = row->Nickname();
2184 if( row->Disabled() )
2202 workQueue->emplace_back(
LOAD_WORK{ nickname, scope,
getUri( row ) } );
2205 if( workQueue->empty() )
2207 wxLogTrace(
traceLibraries,
"AsyncLoad: all libraries already loaded; exiting" );
2215 size_t poolSize =
tp.get_thread_count();
2216 size_t maxLoadThreads = std::max<size_t>( 1, poolSize > 2 ? poolSize - 2 : 1 );
2217 size_t numWorkers = std::min( maxLoadThreads, workQueue->size() );
2219 auto workIndex = std::make_shared<std::atomic<size_t>>( 0 );
2221 wxLogTrace(
traceLibraries,
"AsyncLoad: %zu libraries to load, using %zu worker threads (pool has %zu)",
2222 workQueue->size(), numWorkers, poolSize );
2224 for(
size_t w = 0; w < numWorkers; ++w )
2227 [
this, workQueue, workIndex]()
2231 if( m_abort.load() )
2234 size_t idx = workIndex->fetch_add( 1 );
2236 if( idx >= workQueue->size() )
2239 const LOAD_WORK& work = ( *workQueue )[idx];
2245 if( !loadQueuedRow( work ) )
2248 catch( std::exception& e )
2250 std::unique_lock lock( scopeMutex( work.scope ) );
2252 LIB_STATUS& status = queuedRowEntry( work ).status;
2255 if( m_cancelling.load() )
2257 status.load_status = LOAD_STATUS::INVALID;
2258 status.error.reset();
2262 status.load_status = LOAD_STATUS::LOAD_ERROR;
2263 status.error = LIBRARY_ERROR( wxString::FromUTF8( e.what() ) );
2273 m_rearmPending =
false;
2274 wxLogTrace(
traceLibraries,
"Started async load of %zu libraries", workQueue->size() );
2288 struct LOADING_ENTRY
virtual bool DeleteLibrary(const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr)
Delete an existing library and returns true, or if library does not exist returns false,...
virtual bool IsLibraryWritable(const wxString &aLibraryPath)
Return true if the library at aLibraryPath is writable.
virtual void CancelLoad()
Ask a library load running on this plugin to end early.
virtual void CreateLibrary(const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr)
Create a new empty library at aLibraryPath empty.
virtual void ResumeLoad() noexcept
Called after the loads that were cancelled have ended, to clear what CancelLoad() left behind.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
virtual const wxString Problem() const
what was the problem?
virtual const wxString Where() const
where did the Problem() occur?
std::map< wxString, std::map< wxString, LIB_OVERRIDE > > m_LibOverrides
Overrides for libraries in read-only nested tables.
Holds a structured error message.
The interface used by the classes that actually can load IO plugins for the different parts of KiCad ...
std::optional< float > AsyncLoadProgress() const
Returns the fraction of the libraries queued by AsyncLoad() that have been processed,...
virtual std::optional< LIB_STATUS > LoadOne(LIB_DATA *aLib)=0
std::vector< KI_ERROR > GetLibraryLoadErrors() const
Returns all library load errors as newline-separated strings for display.
void resetProjectCache(bool aCancelPlugins=false)
Aborts pending loads and resets every project-scope cache entry in place (plugin and row cleared,...
bool loadQueuedRow(const LOAD_WORK &aWork)
Loads one queued library on a worker thread.
std::atomic_bool m_cancelling
Set with m_abort by an abort that cancels, so only such an abort turns a failed load into INVALID.
virtual std::optional< LIBRARY_ERROR > LibraryError(const wxString &aNickname) const
void ReloadLibraryEntry(const wxString &aNickname, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH)
Synchronously recreates the plugin for and reloads the given library.
std::optional< LIB_STATUS > GetLibraryStatus(const wxString &aNickname) const
Returns the status of a loaded library, or nullopt if the library hasn't been loaded (yet)
void stopRefreshThreads(std::map< wxString, LIB_DATA > &aLibs, std::shared_mutex &aMutex, const std::function< bool(const wxString &aNickname, const LIB_DATA &aLib)> &aRetiring={})
Stops the refresh threads of the plugins in aLibs that aRetiring selects, or of all of them when it i...
void AbortAsyncLoad(bool aCancelPlugins=false)
Aborts any async load in progress; blocks until fully done aborting.
std::pair< wxString, LIBRARY_TABLE_SCOPE > LOADING_KEY
virtual ~LIBRARY_MANAGER_ADAPTER()
std::optional< LIBRARY_TABLE * > ProjectTable() const
Retrieves the project library table for this adapter type, or nullopt if one doesn't exist.
void ProjectTablesChanged(std::initializer_list< LIBRARY_TABLE_TYPE > aChangedTables={})
Notify the adapter that the project library tables are about to be rebuilt.
virtual void enumerateLibrary(LIB_DATA *aLib, const wxString &aUri)=0
Override in derived class to perform library-specific enumeration.
LIBRARY_TABLE * GlobalTable() const
Retrieves the global library table for this adapter type.
virtual std::shared_mutex & globalLibsMutex()=0
std::atomic< unsigned > m_abortGeneration
Counts cancelling aborts, so a load can tell one that began after it started from one that already en...
void evictOwnedGlobalEntries()
Erases this adapter's own entries (LIB_DATA::global_owner == this) from the process-wide globalLibs()...
LIBRARY_MANAGER_ADAPTER(LIBRARY_MANAGER &aManager)
Constructs a type-specific adapter into the library manager.
void CheckTableRow(LIBRARY_TABLE_ROW &aRow)
bool IsLibraryLoaded(const wxString &aNickname)
std::vector< LIBRARY_TABLE_ROW * > Rows(LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH, bool aIncludeInvalid=false) const
Like LIBRARY_MANAGER::Rows but filtered to the LIBRARY_TABLE_TYPE of this adapter.
std::optional< LIBRARY_TABLE_ROW * > FindRowByURI(const wxString &aUri, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH) const
Like LIBRARY_MANAGER::FindRowByURI but filtered to the LIBRARY_TABLE_TYPE of this adapter.
wxString getUri(const LIBRARY_TABLE_ROW *aRow) const
bool RunPreload(PROGRESS_REPORTER &aReporter, std::atomic_bool &aAbort)
Starts AsyncLoad() and waits for it on the calling thread, reporting progress to aReporter.
virtual std::map< wxString, LIB_DATA > & globalLibs()=0
void cancelLoadingPlugins(const std::vector< LOADING_KEY > &aSnapshot)
Asks the plugins in aSnapshot to end their load early, so abortLoad() does not wait out their waits.
virtual LIBRARY_TABLE_TYPE Type() const =0
The type of library table this adapter works with.
void retireAllLibraries()
For derived destructors, while globalLibs() is still resolvable and plugin worker threads can still c...
bool DeleteLibrary(const wxString &aNickname)
Deletes the given library from disk if it exists; returns true if deleted.
void asyncLoadLocked()
The body of AsyncLoad(), run with m_loadMutex already held by the caller.
LIBRARY_RESULT< LIB_DATA * > loadIfNeeded(const wxString &aNickname)
Fetches a loaded library, triggering a load of that library if it isn't loaded yet.
std::optional< wxString > FindLibraryByURI(const wxString &aURI) const
std::mutex m_loadingMutex
Libraries an async worker is enumerating, so a cancel reaches exactly those plugins.
std::vector< LOADING_KEY > loadingSnapshot()
The libraries an async worker is enumerating right now.
LIB_DATA & queuedRowEntry(const LOAD_WORK &aWork)
The cache entry of a queued library, created and owned by this adapter when it is missing,...
void LeakCachesAtExit()
Ends every load, then marks the cache of each library this adapter owns to be left unfreed when its p...
std::shared_mutex m_librariesMutex
std::set< LOADING_KEY > m_loading
LIBRARY_MANAGER & Manager() const
std::optional< wxString > GetLibraryDescription(const wxString &aNickname) const
virtual LIBRARY_RESULT< IO_BASE * > createPlugin(const LIBRARY_TABLE_ROW *row)=0
Creates a concrete plugin for the given row.
void GlobalTablesChanged(std::initializer_list< LIBRARY_TABLE_TYPE > aChangedTables={})
Notify the adapter that the global library tables have changed.
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library tables.
std::optional< LIBRARY_TABLE_ROW * > GetRow(const wxString &aNickname, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH) const
Like LIBRARY_MANAGER::GetRow but filtered to the LIBRARY_TABLE_TYPE of this adapter.
std::map< wxString, LIB_DATA > m_libraries
virtual IO_BASE * plugin(const LIB_DATA *aRow)=0
std::vector< wxString > GetLibraryNames() const
Returns a list of library nicknames that are available (skips any that failed to load)
virtual void ProjectChanged()
Notify the adapter that the active project has changed.
LIBRARY_RESULT< LIB_DATA * > loadFromScope(const wxString &aNickname, LIBRARY_TABLE_SCOPE aScope, std::map< wxString, LIB_DATA > &aTarget, std::shared_mutex &aMutex)
virtual std::optional< LIB_STATUS > CheckLibrary(LIB_DATA *aLib)
Validates that a library is loadable. May not necessarily load the library!
std::vector< std::future< void > > m_futures
void withPlugin(const LOADING_KEY &aKey, const std::function< void(IO_BASE &)> &aAction)
Runs aAction on the plugin of a library when it is still in the map.
static std::recursive_mutex & pluginMutex(const wxString &aNickname)
Serializes access to a library's shared plugin instance so its single mutable cache is not raced by c...
std::shared_mutex & scopeMutex(LIBRARY_TABLE_SCOPE aScope)
bool CreateLibrary(const wxString &aNickname)
Creates the library (i.e. saves to disk) for the given row if it exists.
void resumeLoadingPlugins(const std::vector< LOADING_KEY > &aCancelled)
Tells the plugins cancelLoadingPlugins() reached that the loads they were asked to end have ended.
void AsyncLoad()
Loads all available libraries for this adapter type in the background.
virtual bool IsWritable(const wxString &aNickname) const
Return true if the given nickname exists and is not a read-only library.
std::vector< std::pair< wxString, LIB_STATUS > > GetLibraryStatuses() const
Returns a list of all library nicknames and their status (even if they failed to load)
std::atomic< size_t > m_loadTotal
void ProjectTablesReloaded(std::initializer_list< LIBRARY_TABLE_TYPE > aChangedTables={})
Complements ProjectTablesChanged by erasing project-scope cache entries whose nicknames no longer app...
void abortLoad(bool aCancelPlugins=false)
Aborts any async load in progress; blocks until fully done aborting.
void RestartAsyncLoad()
Queues again the libraries a cancelling abort left INVALID, when one has cut a load short since the l...
std::atomic< size_t > m_loadCount
LIBRARY_MANAGER & m_manager
std::optional< LIB_STATUS > LoadLibraryEntry(const wxString &aNickname)
Synchronously loads the named library to LOADED state.
bool m_rearmPending
Set when a cancelling abort cut a worker short, and cleared only by an AsyncLoad() that starts.
std::optional< const LIB_DATA * > fetchIfLoaded(const wxString &aNickname) const
void ReloadTables(LIBRARY_TABLE_SCOPE aScope, std::initializer_list< LIBRARY_TABLE_TYPE > aTablesToLoad={})
static wxString StockTableTokenizedURI(LIBRARY_TABLE_TYPE aType)
static wxString ExpandURI(const wxString &aShortURI, const PROJECT &aProject)
const PROJECT *const m_project
std::mutex m_rowCacheMutex
void RestartAsyncLoads()
Queues again the libraries that any cancelling abort left INVALID, for every registered adapter.
void ClearLibOverride(const wxString &aTablePath, const wxString &aNickname)
Removes any override for a library that no longer needs one.
std::optional< LIBRARY_MANAGER_ADAPTER * > Adapter(LIBRARY_TABLE_TYPE aType) const
void applyLibOverrides(LIBRARY_TABLE &aTable)
Applies user overrides (disabled/hidden) to rows of a read-only nested table.
std::map< wxString, std::unique_ptr< LIBRARY_TABLE > > m_childTables
Map of full URI to table object for tables that are referenced by global or project tables.
void loadNestedTables(LIBRARY_TABLE &aTable)
std::map< ROW_CACHE_KEY, LIBRARY_TABLE_ROW * > m_rowCache
bool RemoveAdapter(LIBRARY_TABLE_TYPE aType, LIBRARY_MANAGER_ADAPTER *aAdapter)
const PROJECT & Project() const
void ReloadLibraryEntry(LIBRARY_TABLE_TYPE aType, const wxString &aNickname, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH)
Synchronously recreates the plugin for and reloads the given library.
static bool UrisAreEquivalent(const wxString &aURI1, const wxString &aURI2)
void AbortAsyncLoads(bool aCancelPlugins=false)
Abort any async library loading operations in progress.
std::mutex m_adaptersMutex
std::optional< LIBRARY_TABLE * > Table(LIBRARY_TABLE_TYPE aType, LIBRARY_TABLE_SCOPE aScope)
Retrieves a given table; creating a new empty project table if a valid project is loaded and the give...
void ApplyLibOverrides(LIBRARY_TABLE &aTable)
Applies stored user overrides (disabled/hidden) to rows of a read-only table.
void RegisterAdapter(LIBRARY_TABLE_TYPE aType, std::unique_ptr< LIBRARY_MANAGER_ADAPTER > &&aAdapter)
static wxString DefaultGlobalTablePath(LIBRARY_TABLE_TYPE aType)
std::optional< wxString > GetFullURI(LIBRARY_TABLE_TYPE aType, const wxString &aNickname, bool aSubstituted=false)
Return the full location specifying URI for the LIB, either in original UI form or in environment var...
static std::vector< LIBRARY_TABLE_TYPE > InvalidGlobalTables()
void createEmptyTable(LIBRARY_TABLE_TYPE aType, LIBRARY_TABLE_SCOPE aScope)
std::optional< LIB_STATUS > LoadLibraryEntry(LIBRARY_TABLE_TYPE aType, const wxString &aNickname)
Synchronously loads the named library to LOADED state for the given type.
static bool GlobalTablesValid()
std::map< LIBRARY_TABLE_TYPE, std::unique_ptr< LIBRARY_TABLE > > m_projectTables
std::optional< LIBRARY_TABLE_ROW * > FindRowByURI(LIBRARY_TABLE_TYPE aType, const wxString &aUri, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH) const
void SetLibOverride(const wxString &aTablePath, const wxString &aNickname, bool aDisabled, bool aHidden)
Set a user override for a library in a read-only nested table.
bool IsProjectScoped() const
void LoadProjectTables(std::initializer_list< LIBRARY_TABLE_TYPE > aTablesToLoad={})
(Re)loads the project library tables in the given list, or all tables if no list is given
static bool CreateGlobalTable(LIBRARY_TABLE_TYPE aType, bool aPopulateDefaultLibraries)
void loadTables(const wxString &aTablePath, LIBRARY_TABLE_SCOPE aScope, std::vector< LIBRARY_TABLE_TYPE > aTablesToLoad={})
static wxString tableFileName(LIBRARY_TABLE_TYPE aType)
static bool IsPcmManagedRow(const LIBRARY_TABLE_ROW &aRow)
Return true if a library table row was added by the Plugin and Content Manager.
std::vector< LIBRARY_TABLE_ROW * > Rows(LIBRARY_TABLE_TYPE aType, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH, bool aIncludeInvalid=false) const
Returns a flattened list of libraries of the given type.
void LoadGlobalTables(std::initializer_list< LIBRARY_TABLE_TYPE > aTablesToLoad={})
(Re)loads the global library tables in the given list, or all tables if no list is given
std::map< LIBRARY_TABLE_TYPE, std::unique_ptr< LIBRARY_TABLE > > m_tables
void LeakCachesAtExit()
Marks the library caches of every adapter to be left unfreed when their plugins are destroyed.
static wxString StockTableReferenceURI(LIBRARY_TABLE_TYPE aType)
static wxString StockTablePath(LIBRARY_TABLE_TYPE aType)
std::optional< LIBRARY_TABLE_ROW * > GetRow(LIBRARY_TABLE_TYPE aType, const wxString &aNickname, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH)
void ProjectChanged()
Notify all adapters that the project has changed.
static bool IsTableValid(const wxString &aPath)
LIBRARY_MANAGER(const PROJECT *aProject=nullptr)
An explicit project is borrowed until this manager and its adapters are destroyed.
std::map< LIBRARY_TABLE_TYPE, std::unique_ptr< LIBRARY_MANAGER_ADAPTER > > m_adapters
void SetNickname(const wxString &aNickname)
void SetOk(bool aOk=true)
void SetType(const wxString &aType)
void SetErrorDescription(const wxString &aDescription)
std::map< std::string, UTF8 > GetOptionsMap() const
void SetDescription(const wxString &aDescription)
const wxString & Type() const
static const wxString TABLE_TYPE_NAME
void SetURI(const wxString &aUri)
const wxString & URI() const
const wxString & Nickname() const
bool IsReadOnly() const
Returns true if the underlying file exists but is not writable.
LIBRARY_TABLE_ROW & InsertRow()
Builds a new row and inserts it at the end of the table; returning a reference to the row.
const wxString & Path() const
LIBRARY_TABLE_SCOPE Scope() const
bool HasRow(const wxString &aNickname) const
bool HasRowWithURI(const wxString &aUri, const PROJECT &aProject, bool aSubstituted=false) const
Returns true if the given (fully-expanded) URI exists as a library in this table.
const std::deque< LIBRARY_TABLE_ROW > & Rows() const
static wxString GetStockTemplatesPath()
Gets the stock (install) templates path.
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
LIBRARY_TABLE * m_designBlockTable
std::set< LIBRARY_TABLE * > m_modified
LIBRARY_MANAGER & m_manager
std::set< LIBRARY_TABLE * > Modified() const
const PROJECT & m_project
PCM_LIB_TRAVERSER(const wxString &aBasePath, LIBRARY_MANAGER &aManager, const wxString &aPrefix)
void ensureUnique(LIBRARY_TABLE *aTable, const wxString &aBaseName, wxString &aNickname) const
LIBRARY_TABLE * m_symbolTable
wxDirTraverseResult OnDir(const wxString &dirPath) override
Handles footprint library and design block library directories, minimum nest level 3.
void addRowIfNecessary(LIBRARY_TABLE *aTable, const wxFileName &aSource, ADD_MODE aMode, int aExtensionLength)
LIBRARY_TABLE * m_fpTable
wxDirTraverseResult OnFile(const wxString &aFilePath) override
Handles symbol library files, minimum nest level 2.
size_t m_prefix_dir_count
virtual ENV_VAR_MAP & GetLocalEnvVariables() const
virtual SETTINGS_MANAGER & GetSettingsManager() const
A progress reporter interface for use in multi-threaded environments.
virtual void SetCurrentProgress(double aProgress)=0
Set the progress value to aProgress (0..1).
Container for project specific data.
T * GetAppSettings(const char *aFilename)
Return a handle to the a given settings by type.
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
static void ResolvePossibleSymlinks(wxFileName &aFilename)
const wxString ExpandEnvVarSubstitutions(const wxString &aString, const PROJECT *aProject, bool aAllowEscapes)
Replace any environment variable & text variable references with their values.
Functions related to environment variables, including help functions.
static const std::string KiCadDesignBlockLibPathExtension
static const std::string SymbolLibraryTableFileName
static const std::string DesignBlockLibraryTableFileName
static const std::string FootprintLibraryTableFileName
const wxChar *const traceLibraries
Flag to enable library table and library manager tracing.
std::map< wxString, ENV_VAR_ITEM > ENV_VAR_MAP
LOAD_STATUS
Status of a library load managed by a library adapter.
tl::expected< ResultType, LIBRARY_ERROR > LIBRARY_RESULT
KICOMMON_API bool IsVersionedEnvVar(const wxString &aName, const wxString &aBaseName)
Test whether a name is a versioned KiCad environment variable for the given base, i....
KICOMMON_API std::optional< wxString > GetVersionedEnvVarValue(const std::map< wxString, ENV_VAR_ITEM > &aMap, const wxString &aBaseName)
Attempt to retrieve the value of a versioned environment variable, such as KICAD8_TEMPLATE_DIR.
KICOMMON_API wxString GetVersionedEnvVarName(const wxString &aBaseName)
Construct a versioned environment variable based on this KiCad major version.
PGM_BASE & Pgm()
The global program "get" accessor.
void StrNumSort(T &aList, CMP_T aCaseSensitivity)
Sort a container of wxString objects, in place, using the StrNumCmp() function.
A library queued by AsyncLoad(), with its URI resolved on the calling thread.
LIBRARY_TABLE_SCOPE scope
std::vector< LIBRARY_TABLE > tables
Storage for an actual loaded library (including library content owned by the plugin)
std::unique_ptr< IO_BASE > plugin
Never destroyed while holding the map's mutex; a plugin's worker thread may be blocked on it.
const LIBRARY_MANAGER_ADAPTER * global_owner
const LIBRARY_TABLE_ROW * row
The overall status of a loaded or loading library.
std::optional< LIBRARY_ERROR > error
std::vector< DATABASE_CONNECTION::ROW > rows
wxString result
Test unit parsing edge cases and error handling.
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
BS::priority_thread_pool thread_pool
wxLogTrace helper definitions.
Definition of file extensions used in Kicad.
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().