21#ifndef LIBRARY_MANAGER_H
22#define LIBRARY_MANAGER_H
27#include <shared_mutex>
53 std::optional<LIBRARY_ERROR>
error = std::nullopt;
114 bool HasLibrary(
const wxString& aNickname,
bool aCheckEnabled =
false )
const;
123 bool aIncludeInvalid =
false )
const;
126 std::optional<LIBRARY_TABLE_ROW*>
GetRow(
const wxString& aNickname,
130 std::optional<LIBRARY_TABLE_ROW*>
FindRowByURI(
const wxString& aUri,
145 void ProjectTablesChanged( std::initializer_list<LIBRARY_TABLE_TYPE> aChangedTables = {} );
151 void ProjectTablesReloaded( std::initializer_list<LIBRARY_TABLE_TYPE> aChangedTables = {} );
153 void CheckTableRow( LIBRARY_TABLE_ROW& aRow );
164 std::optional<float> AsyncLoadProgress()
const;
166 void BlockUntilLoaded();
170 void AbortAsyncLoad();
172 bool IsLibraryLoaded(
const wxString& aNickname );
175 std::optional<LIB_STATUS> GetLibraryStatus(
const wxString& aNickname )
const;
178 std::vector<std::pair<wxString, LIB_STATUS>> GetLibraryStatuses()
const;
181 std::vector<KI_ERROR> GetLibraryLoadErrors()
const;
183 void ReloadLibraryEntry(
const wxString& aNickname,
188 std::optional<LIB_STATUS> LoadLibraryEntry(
const wxString& aNickname );
191 virtual bool IsWritable(
const wxString& aNickname )
const;
194 bool CreateLibrary(
const wxString& aNickname );
200 virtual std::optional<LIBRARY_ERROR> LibraryError(
const wxString& aNickname )
const;
204 virtual std::map<wxString, LIB_DATA>&
globalLibs()
const = 0;
215 std::optional<const LIB_DATA*>
fetchIfLoaded(
const wxString& aNickname )
const;
217 std::optional<LIB_DATA*>
fetchIfLoaded(
const wxString& aNickname );
224 std::map<wxString, LIB_DATA>& aTarget,
225 std::shared_mutex& aMutex );
258 static std::recursive_mutex&
pluginMutex(
const wxString& aNickname );
326 void LoadGlobalTables( std::initializer_list<LIBRARY_TABLE_TYPE> aTablesToLoad = {} );
329 void LoadProjectTables( std::initializer_list<LIBRARY_TABLE_TYPE> aTablesToLoad = {} );
331 void ReloadTables(
LIBRARY_TABLE_SCOPE aScope, std::initializer_list<LIBRARY_TABLE_TYPE> aTablesToLoad = {} );
339 void AbortAsyncLoads();
342 std::unique_ptr<LIBRARY_MANAGER_ADAPTER>&& aAdapter );
368 bool aIncludeInvalid =
false )
const;
377 std::optional<LIBRARY_TABLE_ROW*> GetRow(
LIBRARY_TABLE_TYPE aType,
const wxString &aNickname,
380 std::optional<LIBRARY_TABLE_ROW*> FindRowByURI(
LIBRARY_TABLE_TYPE aType,
const wxString &aUri,
389 const wxString& aNickname );
391 void LoadProjectTables(
const wxString& aProjectPath,
392 std::initializer_list<LIBRARY_TABLE_TYPE> aTablesToLoad = {} );
403 std::optional<wxString> GetFullURI(
LIBRARY_TABLE_TYPE aType,
const wxString& aNickname,
404 bool aSubstituted =
false );
406 static wxString GetFullURI(
const LIBRARY_TABLE_ROW* aRow,
bool aSubstituted =
false,
407 const PROJECT* aProject =
nullptr );
409 static wxString ExpandURI(
const wxString& aShortURI,
const PROJECT& aProject );
411 static bool UrisAreEquivalent(
const wxString& aURI1,
const wxString& aURI2 );
424 static bool IsPcmManagedRow(
const LIBRARY_TABLE_ROW& aRow );
429 void ApplyLibOverrides( LIBRARY_TABLE& aTable );
439 void SetLibOverride(
const wxString& aTablePath,
const wxString& aNickname,
440 bool aDisabled,
bool aHidden );
443 void ClearLibOverride(
const wxString& aTablePath,
const wxString& aNickname );
447 std::vector<LIBRARY_TABLE_TYPE> aTablesToLoad = {} );
449 void loadNestedTables( LIBRARY_TABLE& aTable );
452 void applyLibOverrides( LIBRARY_TABLE& aTable );
459 std::map<LIBRARY_TABLE_TYPE, std::unique_ptr<LIBRARY_TABLE>>
m_tables;
467 std::map<LIBRARY_TABLE_TYPE, std::unique_ptr<LIBRARY_MANAGER_ADAPTER>>
m_adapters;
471 typedef std::tuple<LIBRARY_TABLE_TYPE, LIBRARY_TABLE_SCOPE, wxString>
ROW_CACHE_KEY;
The interface used by the classes that actually can load IO plugins for the different parts of KiCad ...
virtual std::optional< LIB_STATUS > LoadOne(LIB_DATA *aLib)=0
void resetProjectCache()
Aborts pending loads and resets every project-scope cache entry in place (plugin and row cleared,...
virtual std::shared_mutex & globalLibsMutex() const =0
std::optional< LIBRARY_TABLE * > ProjectTable() const
Retrieves the project library table for this adapter type, or nullopt if one doesn't exist.
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
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.
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
virtual std::map< wxString, LIB_DATA > & globalLibs()=0
virtual LIBRARY_TABLE_TYPE Type() const =0
The type of library table this adapter works with.
bool DeleteLibrary(const wxString &aNickname)
Deletes the given library from disk if it exists; returns true if deleted.
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::shared_mutex m_librariesMutex
LIBRARY_MANAGER & Manager() const
void abortLoad()
Aborts any async load in progress; blocks until fully done aborting.
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)
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
virtual bool SupportsConfigurationDialog(const wxString &aNickname) const
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::atomic< size_t > m_loadTotal
virtual int ShowConfigurationDialog(const wxString &aNickname, wxWindow *aParent) const
virtual std::map< wxString, LIB_DATA > & globalLibs() const =0
std::atomic< size_t > m_loadCount
LIBRARY_MANAGER & m_manager
std::optional< const LIB_DATA * > fetchIfLoaded(const wxString &aNickname) const
static wxString StockTableTokenizedURI(LIBRARY_TABLE_TYPE aType)
const PROJECT *const m_project
std::mutex m_rowCacheMutex
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.
std::map< ROW_CACHE_KEY, LIBRARY_TABLE_ROW * > m_rowCache
const PROJECT & Project() const
std::mutex m_adaptersMutex
static wxString DefaultGlobalTablePath(LIBRARY_TABLE_TYPE aType)
static std::vector< LIBRARY_TABLE_TYPE > InvalidGlobalTables()
static bool GlobalTablesValid()
std::map< LIBRARY_TABLE_TYPE, std::unique_ptr< LIBRARY_TABLE > > m_projectTables
LIBRARY_MANAGER(const LIBRARY_MANAGER &)=delete
bool IsProjectScoped() const
static bool CreateGlobalTable(LIBRARY_TABLE_TYPE aType, bool aPopulateDefaultLibraries)
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
LIBRARY_MANAGER & operator=(const LIBRARY_MANAGER &)=delete
std::tuple< LIBRARY_TABLE_TYPE, LIBRARY_TABLE_SCOPE, wxString > ROW_CACHE_KEY
static wxString StockTableReferenceURI(LIBRARY_TABLE_TYPE aType)
static wxString StockTablePath(LIBRARY_TABLE_TYPE aType)
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
Container for project specific data.
void ProjectChanged() override
LOAD_STATUS
Status of a library load managed by a library adapter.
tl::expected< ResultType, LIBRARY_ERROR > LIBRARY_RESULT
Storage for an actual loaded library (including library content owned by the plugin)
std::vector< wxString > available_fields_cache
std::unique_ptr< IO_BASE > plugin
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