Sim: Create missing SIM_MODEL factories in SIM_LIB_MGR

This commit is contained in:
Mikolaj Wielgus 2022-11-12 18:11:11 +01:00
parent 248181e62d
commit 0d42003247
2 changed files with 42 additions and 25 deletions

View File

@ -35,22 +35,47 @@ SIM_LIB_MGR::SIM_LIB_MGR( const PROJECT& aPrj ) : m_project( aPrj )
} }
SIM_LIBRARY& SIM_LIB_MGR::CreateLibrary( const std::string& aLibraryPath )
{
auto it = m_libraries.try_emplace( aLibraryPath, SIM_LIBRARY::Create( aLibraryPath ) ).first;
return *it->second;
}
SIM_MODEL& SIM_LIB_MGR::CreateModel( SIM_MODEL::TYPE aType, int aSymbolPinCount )
{
m_models.push_back( SIM_MODEL::Create( aType, aSymbolPinCount ) );
return *m_models.back();
}
SIM_MODEL& SIM_LIB_MGR::CreateModel( const SIM_MODEL& aBaseModel, int aSymbolPinCount )
{
m_models.push_back( SIM_MODEL::Create( aBaseModel, aSymbolPinCount ) );
return *m_models.back();
}
SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel( SCH_SYMBOL& aSymbol ) SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel( SCH_SYMBOL& aSymbol )
{ {
std::vector<LIB_PIN*> pins = aSymbol.GetLibPins(); return CreateModel( aSymbol.GetFields(), static_cast<int>( aSymbol.GetLibPins().size() ) );
SCH_FIELD* libraryField = aSymbol.FindField( SIM_LIBRARY::LIBRARY_FIELD ); }
SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel( const std::vector<SCH_FIELD>& aFields,
int aSymbolPinCount )
{
std::string libraryPath = SIM_MODEL::GetFieldValue( &aFields, SIM_LIBRARY::LIBRARY_FIELD );
std::string baseModelName; std::string baseModelName;
if( libraryField ) if( libraryPath != "" )
{ {
wxString path = libraryField->GetShownText(); std::string absolutePath = std::string( m_project.AbsolutePath( libraryPath ).ToUTF8() );
wxString absolutePath = m_project.AbsolutePath( path );
SIM_LIBRARY* library = nullptr; SIM_LIBRARY* library = nullptr;
try try
{ {
auto it = m_libraries.try_emplace( std::string( path.ToUTF8() ), auto it = m_libraries.try_emplace( libraryPath,
SIM_LIBRARY::Create( std::string( absolutePath.ToUTF8() ) ) ).first; SIM_LIBRARY::Create( absolutePath ) ).first;
library = &*it->second; library = &*it->second;
} }
catch( const IO_ERROR& e ) catch( const IO_ERROR& e )
@ -61,15 +86,14 @@ SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel( SCH_SYMBOL& aSymbol )
e.What() ) ); e.What() ) );
} }
SCH_FIELD* nameField = aSymbol.FindField( SIM_LIBRARY::NAME_FIELD ); baseModelName = SIM_MODEL::GetFieldValue( &aFields, SIM_LIBRARY::NAME_FIELD );
if( !nameField ) if( baseModelName == "" )
{ {
THROW_IO_ERROR( wxString::Format( _( "Error loading simulation model: no '%s' field" ), THROW_IO_ERROR( wxString::Format( _( "Error loading simulation model: no '%s' field" ),
SIM_LIBRARY::NAME_FIELD ) ); SIM_LIBRARY::NAME_FIELD ) );
} }
baseModelName = std::string( nameField->GetShownText().ToUTF8() );
SIM_MODEL* baseModel = library->FindModel( baseModelName ); SIM_MODEL* baseModel = library->FindModel( baseModelName );
if( !baseModel ) if( !baseModel )
@ -80,27 +104,15 @@ SIM_LIBRARY::MODEL SIM_LIB_MGR::CreateModel( SCH_SYMBOL& aSymbol )
absolutePath ) ); absolutePath ) );
} }
m_models.push_back( SIM_MODEL::Create( *baseModel, m_models.push_back( SIM_MODEL::Create( *baseModel, aSymbolPinCount, aFields ) );
static_cast<int>( pins.size() ),
aSymbol.GetFields() ) );
} }
else else
{ m_models.push_back( SIM_MODEL::Create( aSymbolPinCount, aFields ) );
m_models.push_back( SIM_MODEL::Create( static_cast<int>( pins.size() ),
aSymbol.GetFields() ) );
}
return { baseModelName, *m_models.back() }; return { baseModelName, *m_models.back() };
} }
SIM_LIBRARY& SIM_LIB_MGR::CreateLibrary( const std::string& aLibraryPath )
{
auto it = m_libraries.try_emplace( aLibraryPath, SIM_LIBRARY::Create( aLibraryPath ) ).first;
return *it->second;
}
std::map<std::string, std::reference_wrapper<const SIM_LIBRARY>> SIM_LIB_MGR::GetLibraries() const std::map<std::string, std::reference_wrapper<const SIM_LIBRARY>> SIM_LIB_MGR::GetLibraries() const
{ {
std::map<std::string, std::reference_wrapper<const SIM_LIBRARY>> result; std::map<std::string, std::reference_wrapper<const SIM_LIBRARY>> result;

View File

@ -43,10 +43,15 @@ public:
SIM_LIB_MGR( const PROJECT& aPrj ); SIM_LIB_MGR( const PROJECT& aPrj );
virtual ~SIM_LIB_MGR() = default; virtual ~SIM_LIB_MGR() = default;
SIM_LIBRARY& CreateLibrary( const std::string& aLibraryPath );
SIM_MODEL& CreateModel( SIM_MODEL::TYPE aType, int aSymbolPinCount );
SIM_MODEL& CreateModel( const SIM_MODEL& aBaseModel, int aSymbolPinCount );
// TODO: The argument can be made const. // TODO: The argument can be made const.
SIM_LIBRARY::MODEL CreateModel( SCH_SYMBOL& aSymbol ); SIM_LIBRARY::MODEL CreateModel( SCH_SYMBOL& aSymbol );
SIM_LIBRARY& CreateLibrary( const std::string& aLibraryPath ); SIM_LIBRARY::MODEL CreateModel( const std::vector<SCH_FIELD>& aFields, int aSymbolPinCount );
std::map<std::string, std::reference_wrapper<const SIM_LIBRARY>> GetLibraries() const; std::map<std::string, std::reference_wrapper<const SIM_LIBRARY>> GetLibraries() const;