33#include <magic_enum.hpp>
57 wxCHECK( aRow->
plugin && ret,
nullptr );
70 wxString msg = wxString::Format(
_(
"Unknown library type %s " ), row->
Type() );
90 wxArrayString dummyList;
91 std::lock_guard lock ( aLib->
mutex );
132 [](
const std::future<void>& aFuture ) {
return aFuture.valid(); } );
136 wxLogTrace(
traceLibraries,
"DB: Cannot AsyncLoad, futures from a previous call remain!" );
147 wxLogTrace(
traceLibraries,
"DB: AsyncLoad: no libraries left to load; exiting" );
154 [](
const wxString& aLib, std::map<wxString, LIB_DATA>& aMap, std::mutex& aMutex )
156 std::lock_guard lock( aMutex );
158 if( aMap.contains( aLib ) )
169 std::set<wxString> libNamesCurrentlyValid;
174 const wxString& nickname = row->Nickname();
176 libNamesCurrentlyValid.insert( nickname );
192 [
this, nickname, scope]()
197 LIBRARY_RESULT<LIB_DATA*> result = loadIfNeeded( nickname );
199 if( result.has_value() )
201 std::optional<LIB_STATUS> loadResult = LoadOne( *result );
204 wxCHECK2( loadResult, ++m_loadCount; return );
208 case LIBRARY_TABLE_SCOPE::GLOBAL:
210 std::lock_guard lock( GlobalLibraryMutex );
211 GlobalLibraries[nickname].status = *loadResult;
215 case LIBRARY_TABLE_SCOPE::PROJECT:
217 std::lock_guard lock( m_libraries_mutex );
218 m_libraries[nickname].status = *loadResult;
223 wxFAIL_MSG(
"Unexpected library table scope" );
230 case LIBRARY_TABLE_SCOPE::GLOBAL:
232 std::lock_guard lock( GlobalLibraryMutex );
234 GlobalLibraries[nickname].status = LIB_STATUS( {
235 .load_status = LOAD_STATUS::LOAD_ERROR,
236 .error = result.error()
242 case LIBRARY_TABLE_SCOPE::PROJECT:
244 wxLogTrace( traceLibraries,
"DB: project library error: %s: %s", nickname, result.error().message );
245 std::lock_guard lock( m_libraries_mutex );
247 m_libraries[nickname].status = LIB_STATUS( {
248 .load_status = LOAD_STATUS::LOAD_ERROR,
249 .error = result.error()
256 wxFAIL_MSG(
"Unexpected library table scope" );
261 }, BS::pr::lowest ) );
266 std::lock_guard lock( GlobalLibraryMutex );
267 std::erase_if( GlobalLibraries, [&](
const auto& pair )
269 return !libNamesCurrentlyValid.contains( pair.first );
274 std::lock_guard lock( m_libraries_mutex );
275 std::erase_if( m_libraries, [&](
const auto& pair )
277 return !libNamesCurrentlyValid.contains( pair.first );
281 if( m_loadTotal > 0 )
282 wxLogTrace(
traceLibraries,
"DB: Started async load of %zu libraries", m_loadTotal );
288 std::vector<DESIGN_BLOCK*> blocks;
290 std::optional<const LIB_DATA*> maybeLib =
fetchIfLoaded( aNickname );
297 wxArrayString blockNames;
308 for(
const wxString& blockName : blockNames )
312 blocks.emplace_back(
dbplugin( lib )->DesignBlockLoad(
getUri( lib->
row ), blockName,
false, &options ) );
316 wxLogTrace(
traceLibraries,
"DB: Exception enumerating design block %s: %s", blockName, e.
What() );
327 wxArrayString namesAS;
328 std::vector<wxString> names;
330 if( std::optional<const LIB_DATA*> maybeLib =
fetchIfLoaded( aNickname ) )
338 for(
const wxString&
name : namesAS )
339 names.emplace_back(
name );
346 const wxString& aDesignBlockName,
bool aKeepUUID )
348 if( std::optional<const LIB_DATA*> maybeLib =
fetchIfLoaded( aNickname ) )
364 if( std::optional<const LIB_DATA*> maybeLib =
fetchIfLoaded( aNickname ) )
377 const wxString& aDesignBlockName )
379 if( std::optional<const LIB_DATA*> maybeLib =
fetchIfLoaded( aNickname ) )
395 if( std::optional<const LIB_DATA*> maybeLib =
fetchIfLoaded( aNickname ) )
411 const wxString& aDesignBlockName )
413 if( std::optional<const LIB_DATA*> maybeLib =
fetchIfLoaded( aNickname ) )
424 if( std::optional<const LIB_DATA*> maybeLib =
fetchIfLoaded( aNickname ) )
440 if( nickname.size() )
@ DESIGN_BLOCK_FILE_UNKNOWN
0 is not a legal menu id on Mac
static DESIGN_BLOCK_FILE_T EnumFromStr(const wxString &aFileType)
static DESIGN_BLOCK_IO * FindPlugin(DESIGN_BLOCK_FILE_T aFileType)
bool DesignBlockExists(const wxString &aLibraryPath, const wxString &aDesignBlockName, const std::map< std::string, UTF8 > *aProperties=nullptr)
void DesignBlockDelete(const wxString &aLibraryPath, const wxString &aDesignBlockName, const std::map< std::string, UTF8 > *aProperties=nullptr)
DESIGN_BLOCK * DesignBlockLoad(const wxString &aLibraryPath, const wxString &aDesignBlockName, bool aKeepUUID=false, const std::map< std::string, UTF8 > *aProperties=nullptr)
void DesignBlockSave(const wxString &aLibraryPath, const DESIGN_BLOCK *aDesignBlock, const std::map< std::string, UTF8 > *aProperties=nullptr)
const DESIGN_BLOCK * GetEnumeratedDesignBlock(const wxString &aLibraryPath, const wxString &aDesignBlockName, const std::map< std::string, UTF8 > *aProperties=nullptr)
void DesignBlockEnumerate(wxArrayString &aDesignBlockNames, const wxString &aLibraryPath, bool aBestEfforts, const std::map< std::string, UTF8 > *aProperties=nullptr)
std::vector< wxString > GetDesignBlockNames(const wxString &aNickname)
SAVE_T
The set of return values from DesignBlockSave() below.
std::vector< DESIGN_BLOCK * > GetDesignBlocks(const wxString &aNickname)
IO_BASE * plugin(const LIB_DATA *aRow) override
static DESIGN_BLOCK_IO * dbplugin(const LIB_DATA *aRow)
Helper to cast the ABC plugin in the LIB_DATA* to a concrete plugin.
void DeleteDesignBlock(const wxString &aNickname, const wxString &aDesignBlockName)
Delete the aDesignBlockName from the library given by aNickname.
static wxString GlobalPathEnvVariableName()
LIBRARY_RESULT< IO_BASE * > createPlugin(const LIBRARY_TABLE_ROW *row) override
Creates a concrete plugin for the given row.
void AsyncLoad() override
Loads all available libraries for this adapter type in the background.
DESIGN_BLOCK * LoadDesignBlock(const wxString &aNickname, const wxString &aDesignBlockName, bool aKeepUUID=false)
Load a design block having aDesignBlockName from the library given by aNickname.
static std::mutex GlobalLibraryMutex
DESIGN_BLOCK * DesignBlockLoadWithOptionalNickname(const LIB_ID &aDesignBlockId, bool aKeepUUID=false)
Load a design block having aDesignBlockId with possibly an empty nickname.
SAVE_T SaveDesignBlock(const wxString &aNickname, const DESIGN_BLOCK *aDesignBlock, bool aOverwrite=true)
Write aDesignBlock to an existing library given by aNickname.
static std::map< wxString, LIB_DATA > GlobalLibraries
DESIGN_BLOCK_LIBRARY_ADAPTER(LIBRARY_MANAGER &aManager)
bool IsDesignBlockLibWritable(const wxString &aNickname)
Return true if the library given by aNickname is writable.
std::optional< LIB_STATUS > LoadOne(LIB_DATA *aLib) override
Loads or reloads the given library, if it exists.
const DESIGN_BLOCK * GetEnumeratedDesignBlock(const wxString &aNickname, const wxString &aDesignBlockName)
A version of #DesignBlockLoad() for use after #DesignBlockEnumerate() for more efficient cache manage...
bool DesignBlockExists(const wxString &aNickname, const wxString &aDesignBlockName)
Indicates whether or not the given design block already exists in the given library.
const LIB_ID & GetLibId() const
wxString GetName() const override
virtual bool IsLibraryWritable(const wxString &aLibraryPath)
Return true if the library at aLibraryPath is writable.
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()
LIBRARY_MANAGER_ADAPTER(LIBRARY_MANAGER &aManager)
Constructs a type-specific adapter into the library manager.
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::map< wxString, LIB_DATA > m_libraries
std::vector< wxString > GetLibraryNames() const
Returns a list of library nicknames that are available (skips any that failed to load)
std::vector< std::future< void > > m_futures
static wxString getUri(const LIBRARY_TABLE_ROW *aRow)
std::mutex m_libraries_mutex
std::atomic< size_t > m_loadCount
LIBRARY_MANAGER & m_manager
std::optional< const LIB_DATA * > fetchIfLoaded(const wxString &aNickname) const
std::map< std::string, UTF8 > GetOptionsMap() const
const wxString & Type() const
const wxString & Nickname() const
A logical library item identifier and consists of various portions much like a URI.
int SetLibNickname(const UTF8 &aLibNickname)
Override the logical library name portion of the LIB_ID to aLibNickname.
const UTF8 & GetLibItemName() const
const UTF8 & GetLibNickname() const
Return the logical library name portion of a LIB_ID.
Functions related to environment variables, including help functions.
const wxChar *const traceLibraries
Flag to enable library table and library manager tracing.
tl::expected< ResultType, LIBRARY_ERROR > LIBRARY_RESULT
KICOMMON_API wxString GetVersionedEnvVarName(const wxString &aBaseName)
Construct a versioned environment variable based on this KiCad major version.
Storage for an actual loaded library (including library content owned by the plugin)
std::unique_ptr< IO_BASE > plugin
const LIBRARY_TABLE_ROW * row
The overall status of a loaded or loading library.
std::optional< LIBRARY_ERROR > error
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.