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 );
118 [](
const std::future<void>& aFuture ) {
return aFuture.valid(); } );
122 wxLogTrace(
traceLibraries,
"DB: Cannot AsyncLoad, futures from a previous call remain!" );
133 wxLogTrace(
traceLibraries,
"DB: AsyncLoad: no libraries left to load; exiting" );
140 [](
const wxString& aLib, std::map<wxString, LIB_DATA>& aMap, std::mutex& aMutex )
142 std::lock_guard lock( aMutex );
144 if( aMap.contains( aLib ) )
155 std::set<wxString> libNamesCurrentlyValid;
160 const wxString& nickname = row->Nickname();
162 libNamesCurrentlyValid.insert( nickname );
178 [
this, nickname, scope]()
183 LIBRARY_RESULT<LIB_DATA*> result = loadIfNeeded( nickname );
185 if( result.has_value() )
187 std::optional<LIB_STATUS> loadResult = LoadOne( *result );
190 wxCHECK2( loadResult, ++m_loadCount; return );
194 case LIBRARY_TABLE_SCOPE::GLOBAL:
196 std::lock_guard lock( GlobalLibraryMutex );
197 GlobalLibraries[nickname].status = *loadResult;
201 case LIBRARY_TABLE_SCOPE::PROJECT:
203 std::lock_guard lock( m_libraries_mutex );
204 m_libraries[nickname].status = *loadResult;
209 wxFAIL_MSG(
"Unexpected library table scope" );
216 case LIBRARY_TABLE_SCOPE::GLOBAL:
218 std::lock_guard lock( GlobalLibraryMutex );
220 GlobalLibraries[nickname].status = LIB_STATUS( {
221 .load_status = LOAD_STATUS::LOAD_ERROR,
222 .error = result.error()
228 case LIBRARY_TABLE_SCOPE::PROJECT:
230 wxLogTrace( traceLibraries,
"DB: project library error: %s: %s", nickname, result.error().message );
231 std::lock_guard lock( m_libraries_mutex );
233 m_libraries[nickname].status = LIB_STATUS( {
234 .load_status = LOAD_STATUS::LOAD_ERROR,
235 .error = result.error()
242 wxFAIL_MSG(
"Unexpected library table scope" );
247 }, BS::pr::lowest ) );
252 std::lock_guard lock( GlobalLibraryMutex );
253 std::erase_if( GlobalLibraries, [&](
const auto& pair )
255 return !libNamesCurrentlyValid.contains( pair.first );
260 std::lock_guard lock( m_libraries_mutex );
261 std::erase_if( m_libraries, [&](
const auto& pair )
263 return !libNamesCurrentlyValid.contains( pair.first );
267 if( m_loadTotal > 0 )
268 wxLogTrace(
traceLibraries,
"DB: Started async load of %zu libraries", m_loadTotal );
274 std::vector<DESIGN_BLOCK*> blocks;
276 std::optional<const LIB_DATA*> maybeLib =
fetchIfLoaded( aNickname );
283 wxArrayString blockNames;
294 for(
const wxString& blockName : blockNames )
298 blocks.emplace_back(
dbplugin( lib )->DesignBlockLoad(
getUri( lib->
row ), blockName,
false, &options ) );
302 wxLogTrace(
traceLibraries,
"DB: Exception enumerating design block %s: %s", blockName, e.
What() );
313 wxArrayString namesAS;
314 std::vector<wxString> names;
316 if( std::optional<const LIB_DATA*> maybeLib =
fetchIfLoaded( aNickname ) )
324 for(
const wxString&
name : namesAS )
325 names.emplace_back(
name );
332 const wxString& aDesignBlockName,
bool aKeepUUID )
334 if( std::optional<const LIB_DATA*> maybeLib =
fetchIfLoaded( aNickname ) )
350 if( std::optional<const LIB_DATA*> maybeLib =
fetchIfLoaded( aNickname ) )
363 const wxString& aDesignBlockName )
365 if( std::optional<const LIB_DATA*> maybeLib =
fetchIfLoaded( aNickname ) )
381 if( std::optional<const LIB_DATA*> maybeLib =
fetchIfLoaded( aNickname ) )
397 const wxString& aDesignBlockName )
399 if( std::optional<const LIB_DATA*> maybeLib =
fetchIfLoaded( aNickname ) )
410 if( std::optional<const LIB_DATA*> maybeLib =
fetchIfLoaded( aNickname ) )
426 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.
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
std::optional< LIBRARY_ERROR > error
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
BS::priority_thread_pool thread_pool
wxLogTrace helper definitions.