KiCad PCB EDA Suite
Loading...
Searching...
No Matches
library_manager.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 * @author Jon Evans <[email protected]>
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#ifndef LIBRARY_MANAGER_H
22#define LIBRARY_MANAGER_H
23
24#include <future>
25#include <memory>
26#include <mutex>
27#include <shared_mutex>
28
29#include <kicommon.h>
31#include <io/io_base.h>
32
33
34class LIBRARY_MANAGER;
36class PROJECT;
37
38
47
50{
52 std::optional<LIBRARY_ERROR> error;
53};
54
55
58{
59 std::unique_ptr<IO_BASE> plugin;
60 const LIBRARY_TABLE_ROW* row = nullptr;
62
63 // For an entry in the process-wide globalLibs() cache, the adapter that loaded it and so owns
64 // the lifetime of row. Its destructor evicts the entry; identity is by adapter, not by the
65 // reusable row pointer.
67
68 int modify_hash = -1;
69 std::vector<wxString> available_fields_cache;
70};
71
72
78{
79public:
88
90
91 LIBRARY_MANAGER& Manager() const;
92
94 virtual LIBRARY_TABLE_TYPE Type() const = 0;
95
98
100 std::optional<LIBRARY_TABLE*> ProjectTable() const;
101
102 std::optional<wxString> FindLibraryByURI( const wxString& aURI ) const;
103
105 std::vector<wxString> GetLibraryNames() const;
106
113 bool HasLibrary( const wxString& aNickname, bool aCheckEnabled = false ) const;
114
116 bool DeleteLibrary( const wxString& aNickname );
117
118 std::optional<wxString> GetLibraryDescription( const wxString& aNickname ) const;
119
121 std::vector<LIBRARY_TABLE_ROW*> Rows( LIBRARY_TABLE_SCOPE aScope = LIBRARY_TABLE_SCOPE::BOTH,
122 bool aIncludeInvalid = false ) const;
123
125 std::optional<LIBRARY_TABLE_ROW*> GetRow( const wxString& aNickname,
127
129 std::optional<LIBRARY_TABLE_ROW*> FindRowByURI( const wxString& aUri,
131
133 virtual void ProjectChanged();
134
136 void GlobalTablesChanged( std::initializer_list<LIBRARY_TABLE_TYPE> aChangedTables = {} );
137
144 void ProjectTablesChanged( std::initializer_list<LIBRARY_TABLE_TYPE> aChangedTables = {} );
145
150 void ProjectTablesReloaded( std::initializer_list<LIBRARY_TABLE_TYPE> aChangedTables = {} );
151
152 void CheckTableRow( LIBRARY_TABLE_ROW& aRow );
153
155 void AsyncLoad();
156
157 virtual std::optional<LIB_STATUS> LoadOne( LIB_DATA* aLib ) = 0;
158
160 std::optional<float> AsyncLoadProgress() const;
161
162 void BlockUntilLoaded();
163
166 void AbortAsyncLoad();
167
168 bool IsLibraryLoaded( const wxString& aNickname );
169
171 std::optional<LIB_STATUS> GetLibraryStatus( const wxString& aNickname ) const;
172
174 std::vector<std::pair<wxString, LIB_STATUS>> GetLibraryStatuses() const;
175
177 wxString GetLibraryLoadErrors() const;
178
179 void ReloadLibraryEntry( const wxString& aNickname,
181
184 std::optional<LIB_STATUS> LoadLibraryEntry( const wxString& aNickname );
185
187 virtual bool IsWritable( const wxString& aNickname ) const;
188
190 bool CreateLibrary( const wxString& aNickname );
191
192 virtual bool SupportsConfigurationDialog( const wxString& aNickname ) const { return false; }
193
194 virtual void ShowConfigurationDialog( const wxString& aNickname, wxWindow* aParent ) const {};
195
196 virtual std::optional<LIBRARY_ERROR> LibraryError( const wxString& aNickname ) const;
197
198protected:
199 virtual std::map<wxString, LIB_DATA>& globalLibs() = 0;
200 virtual std::map<wxString, LIB_DATA>& globalLibs() const = 0;
201 virtual std::shared_mutex& globalLibsMutex() = 0;
202 virtual std::shared_mutex& globalLibsMutex() const = 0;
203
207 virtual void enumerateLibrary( LIB_DATA* aLib, const wxString& aUri ) = 0;
208
209 static wxString getUri( const LIBRARY_TABLE_ROW* aRow );
210
211 std::optional<const LIB_DATA*> fetchIfLoaded( const wxString& aNickname ) const;
212
213 std::optional<LIB_DATA*> fetchIfLoaded( const wxString& aNickname );
214
216 LIBRARY_RESULT<LIB_DATA*> loadIfNeeded( const wxString& aNickname );
217
218 LIBRARY_RESULT<LIB_DATA*> loadFromScope( const wxString& aNickname,
219 LIBRARY_TABLE_SCOPE aScope,
220 std::map<wxString, LIB_DATA>& aTarget,
221 std::shared_mutex& aMutex );
222
229
231 void abortLoad();
232
240 void resetProjectCache();
241
244
245 virtual IO_BASE* plugin( const LIB_DATA* aRow ) = 0;
246
252 static std::mutex& pluginMutex( const wxString& aNickname );
253
255
256 // The actual library content is held in an associated IO plugin
257 std::map<wxString, LIB_DATA> m_libraries;
258
259 mutable std::shared_mutex m_librariesMutex;
260
261 std::atomic_bool m_abort;
262 std::vector<std::future<void>> m_futures;
263
264 std::atomic<size_t> m_loadCount{ 0 };
265 std::atomic<size_t> m_loadTotal{ 0 };
266 std::mutex m_loadMutex;
267};
268
269
271{
272public:
274
276
279
280 static wxString DefaultGlobalTablePath( LIBRARY_TABLE_TYPE aType );
281
282 static wxString StockTablePath( LIBRARY_TABLE_TYPE aType );
283
291 static wxString StockTableTokenizedURI( LIBRARY_TABLE_TYPE aType );
292
303 static wxString StockTableReferenceURI( LIBRARY_TABLE_TYPE aType );
304
305 static bool IsTableValid( const wxString& aPath );
306
308 static bool GlobalTablesValid();
309
311 static std::vector<LIBRARY_TABLE_TYPE> InvalidGlobalTables();
312
313 static bool CreateGlobalTable( LIBRARY_TABLE_TYPE aType, bool aPopulateDefaultLibraries );
314
316 void LoadGlobalTables( std::initializer_list<LIBRARY_TABLE_TYPE> aTablesToLoad = {} );
317
319 void LoadProjectTables( std::initializer_list<LIBRARY_TABLE_TYPE> aTablesToLoad = {} );
320
321 void ReloadTables( LIBRARY_TABLE_SCOPE aScope, std::initializer_list<LIBRARY_TABLE_TYPE> aTablesToLoad = {} );
322
324 void ProjectChanged();
325
329 void AbortAsyncLoads();
330
331 void RegisterAdapter( LIBRARY_TABLE_TYPE aType,
332 std::unique_ptr<LIBRARY_MANAGER_ADAPTER>&& aAdapter );
333
334 bool RemoveAdapter( LIBRARY_TABLE_TYPE aType, LIBRARY_MANAGER_ADAPTER* aAdapter );
335
336 std::optional<LIBRARY_MANAGER_ADAPTER*> Adapter( LIBRARY_TABLE_TYPE aType ) const;
337
346 std::optional<LIBRARY_TABLE*> Table( LIBRARY_TABLE_TYPE aType,
347 LIBRARY_TABLE_SCOPE aScope );
348
356 std::vector<LIBRARY_TABLE_ROW*> Rows( LIBRARY_TABLE_TYPE aType,
358 bool aIncludeInvalid = false ) const;
359
367 std::optional<LIBRARY_TABLE_ROW*> GetRow( LIBRARY_TABLE_TYPE aType, const wxString &aNickname,
369
370 std::optional<LIBRARY_TABLE_ROW*> FindRowByURI( LIBRARY_TABLE_TYPE aType, const wxString &aUri,
372
373 void ReloadLibraryEntry( LIBRARY_TABLE_TYPE aType, const wxString& aNickname,
375
378 std::optional<LIB_STATUS> LoadLibraryEntry( LIBRARY_TABLE_TYPE aType,
379 const wxString& aNickname );
380
381 void LoadProjectTables( const wxString& aProjectPath,
382 std::initializer_list<LIBRARY_TABLE_TYPE> aTablesToLoad = {} );
383
393 std::optional<wxString> GetFullURI( LIBRARY_TABLE_TYPE aType, const wxString& aNickname,
394 bool aSubstituted = false );
395
396 static wxString GetFullURI( const LIBRARY_TABLE_ROW* aRow, bool aSubstituted = false );
397
398 static wxString ExpandURI( const wxString& aShortURI, const PROJECT& aProject );
399
400 static bool UrisAreEquivalent( const wxString& aURI1, const wxString& aURI2 );
401
413 static bool IsPcmManagedRow( const LIBRARY_TABLE_ROW& aRow );
414
418 void ApplyLibOverrides( LIBRARY_TABLE& aTable );
419
428 void SetLibOverride( const wxString& aTablePath, const wxString& aNickname,
429 bool aDisabled, bool aHidden );
430
432 void ClearLibOverride( const wxString& aTablePath, const wxString& aNickname );
433
434private:
435 void loadTables( const wxString& aTablePath, LIBRARY_TABLE_SCOPE aScope,
436 std::vector<LIBRARY_TABLE_TYPE> aTablesToLoad = {} );
437
438 void loadNestedTables( LIBRARY_TABLE& aTable );
439
441 void applyLibOverrides( LIBRARY_TABLE& aTable );
442
443 static wxString tableFileName( LIBRARY_TABLE_TYPE aType );
444
445 void createEmptyTable( LIBRARY_TABLE_TYPE aType, LIBRARY_TABLE_SCOPE aScope );
446
447 std::map<LIBRARY_TABLE_TYPE, std::unique_ptr<LIBRARY_TABLE>> m_tables;
448
450 std::map<wxString, std::unique_ptr<LIBRARY_TABLE>> m_childTables;
451
452 // TODO: support multiple projects
453 std::map<LIBRARY_TABLE_TYPE, std::unique_ptr<LIBRARY_TABLE>> m_projectTables;
454
455 std::map<LIBRARY_TABLE_TYPE, std::unique_ptr<LIBRARY_MANAGER_ADAPTER>> m_adapters;
456
457 mutable std::mutex m_adaptersMutex;
458
459 typedef std::tuple<LIBRARY_TABLE_TYPE, LIBRARY_TABLE_SCOPE, wxString> ROW_CACHE_KEY;
460
461 std::map<ROW_CACHE_KEY, LIBRARY_TABLE_ROW*> m_rowCache;
462 mutable std::mutex m_rowCacheMutex;
463};
464
465#endif //LIBRARY_MANAGER_H
The interface used by the classes that actually can load IO plugins for the different parts of KiCad ...
std::optional< float > AsyncLoadProgress() const
Returns async load progress between 0.0 and 1.0, or nullopt if load is not in progress.
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,...
void ReloadLibraryEntry(const wxString &aNickname, LIBRARY_TABLE_SCOPE aScope=LIBRARY_TABLE_SCOPE::BOTH)
virtual std::shared_mutex & globalLibsMutex() const =0
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)
std::optional< LIBRARY_TABLE * > ProjectTable() const
Retrieves the project library table for this adapter type, or nullopt if one doesn't exist.
void AbortAsyncLoad()
Aborts any async load in progress; blocks until fully done aborting.
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.
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.
static std::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::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.
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.
wxString GetLibraryLoadErrors() const
Returns all library load errors as newline-separated strings for display.
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.
virtual void ShowConfigurationDialog(const wxString &aNickname, wxWindow *aParent) const
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)
std::vector< std::future< void > > m_futures
virtual bool SupportsConfigurationDialog(const wxString &aNickname) const
static wxString getUri(const LIBRARY_TABLE_ROW *aRow)
bool CreateLibrary(const wxString &aNickname)
Creates the library (i.e. saves to disk) for the given row if it exists.
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
virtual std::map< wxString, LIB_DATA > & globalLibs() const =0
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.
std::optional< const LIB_DATA * > fetchIfLoaded(const wxString &aNickname) const
static wxString StockTableTokenizedURI(LIBRARY_TABLE_TYPE aType)
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
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
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)
std::map< LIBRARY_TABLE_TYPE, std::unique_ptr< LIBRARY_MANAGER_ADAPTER > > m_adapters
Container for project specific data.
Definition project.h:63
void ProjectChanged() override
#define KICOMMON_API
Definition kicommon.h:27
LOAD_STATUS
Status of a library load managed by a library adapter.
tl::expected< ResultType, LIBRARY_ERROR > LIBRARY_RESULT
LIBRARY_TABLE_TYPE
LIBRARY_TABLE_SCOPE
Storage for an actual loaded library (including library content owned by the plugin)
std::vector< wxString > available_fields_cache
LIB_STATUS status
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
LOAD_STATUS load_status