Sim: Minor SIM_LIBRARY cleanup

This commit is contained in:
Mikolaj Wielgus 2022-10-06 14:08:01 +02:00
parent b95f4ec6bb
commit 6f2b2d7e19
4 changed files with 25 additions and 29 deletions

View File

@ -574,12 +574,9 @@ void DIALOG_SIM_MODEL<T>::loadLibrary( const wxString& aFilePath )
try
{
for( unsigned i = 0; i < m_library->GetModels().size(); ++i )
for( auto& [baseModelName, baseModel] : m_library->GetModels() )
{
const SIM_MODEL& baseModel = m_library->GetModels().at( i );
wxString baseModelName = m_library->GetModelNames().at( i );
wxString expectedModelName;
expectedModelName =
SIM_MODEL::GetFieldValue( &m_fields, SIM_LIBRARY_KIBIS::NAME_FIELD );
@ -604,8 +601,8 @@ void DIALOG_SIM_MODEL<T>::loadLibrary( const wxString& aFilePath )
}
wxArrayString modelNames;
for( const std::string& name : m_library->GetModelNames() )
modelNames.Add( name );
for( auto& [modelName, model] : m_library->GetModels() )
modelNames.Add( modelName );
auto validator = dynamic_cast<MODEL_NAME_VALIDATOR*>( m_modelNameCombobox->GetValidator() );

View File

@ -50,11 +50,11 @@ void SIM_LIBRARY::ReadFile( const std::string& aFilePath )
SIM_MODEL* SIM_LIBRARY::FindModel( const std::string& aModelName ) const
{
for( int i = 0; i < static_cast<int>( GetModelNames().size() ); ++i )
for( int i = 0; i < static_cast<int>( m_modelNames.size() ); ++i )
{
std::string curModelName = GetModelNames().at( i );
const std::string& modelName = m_modelNames.at( i );
if( curModelName == aModelName )
if( modelName == aModelName )
return m_models.at( i ).get();
}
@ -62,12 +62,12 @@ SIM_MODEL* SIM_LIBRARY::FindModel( const std::string& aModelName ) const
}
std::vector<std::reference_wrapper<SIM_MODEL>> SIM_LIBRARY::GetModels() const
std::vector<SIM_LIBRARY::MODEL> SIM_LIBRARY::GetModels() const
{
std::vector<std::reference_wrapper<SIM_MODEL>> ret;
std::vector<MODEL> result;
for( const std::unique_ptr<SIM_MODEL>& model : m_models )
ret.emplace_back( *model );
for( int i = 0; i < static_cast<int>( m_modelNames.size() ); ++i )
result.push_back( { m_modelNames.at( i ), *m_models.at( i ) } );
return ret;
return result;
}

View File

@ -34,6 +34,12 @@ public:
static constexpr auto LIBRARY_FIELD = "Sim_Library";
static constexpr auto NAME_FIELD = "Sim_Name";
struct MODEL
{
std::string name;
SIM_MODEL& model;
};
virtual ~SIM_LIBRARY() = default;
SIM_LIBRARY() = default;
@ -65,8 +71,7 @@ public:
SIM_MODEL* FindModel( const std::string& aModelName ) const;
std::vector<std::reference_wrapper<SIM_MODEL>> GetModels() const;
const std::vector<std::string>& GetModelNames() const { return m_modelNames; }
std::vector<MODEL> GetModels() const;
std::string GetFilePath() const { return m_filePath; }
std::string GetError() const { return m_error; }
@ -76,8 +81,8 @@ public:
virtual SIM_LIBRARY::LIBRARY_TYPE GetType() { return SIM_LIBRARY::LIBRARY_TYPE::NONE; };
protected:
std::vector<std::unique_ptr<SIM_MODEL>> m_models;
std::vector<std::string> m_modelNames;
std::vector<std::unique_ptr<SIM_MODEL>> m_models;
std::string m_filePath;
std::string m_error;

View File

@ -131,15 +131,13 @@ BOOST_AUTO_TEST_CASE( Diodes )
LoadLibrary( "diodes" );
const std::vector<std::reference_wrapper<SIM_MODEL>> models = m_library->GetModels();
const std::vector<std::string>& modelNames = m_library->GetModelNames();
const std::vector<SIM_LIBRARY::MODEL> models = m_library->GetModels();
BOOST_CHECK_EQUAL( models.size(), 22 );
for( int i = 0; i < models.size(); ++i )
{
const SIM_MODEL& model = models.at( i );
const std::string& modelName = modelNames.at( i );
const auto& [modelName, model] = models.at( i );
switch( i )
{
@ -238,15 +236,13 @@ BOOST_AUTO_TEST_CASE( Bjts )
LoadLibrary( "bjts" );
const std::vector<std::reference_wrapper<SIM_MODEL>> models = m_library->GetModels();
const std::vector<std::string>& modelNames = m_library->GetModelNames();
const std::vector<SIM_LIBRARY::MODEL> models = m_library->GetModels();
BOOST_CHECK_EQUAL( models.size(), 6 );
for( int i = 0; i < models.size(); ++i )
{
const SIM_MODEL& model = models.at( i );
const std::string& modelName = modelNames.at( i );
const auto& [modelName, model] = models.at( i );
switch( i )
{
@ -294,15 +290,13 @@ BOOST_AUTO_TEST_CASE( Fets )
LoadLibrary( "fets" );
const std::vector<std::reference_wrapper<SIM_MODEL>> models = m_library->GetModels();
const std::vector<std::string>& modelNames = m_library->GetModelNames();
const std::vector<SIM_LIBRARY::MODEL> models = m_library->GetModels();
BOOST_CHECK_EQUAL( models.size(), 44 );
for( int i = 0; i < models.size(); ++i )
{
const SIM_MODEL& model = models.at( i );
const std::string& modelName = modelNames.at( i );
const auto& [modelName, model] = models.at( i );
switch( i )
{