From 53e509326cb691a262bbbdac60c579d4d882a0d8 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Fri, 18 Apr 2025 18:41:43 +0100 Subject: [PATCH] Don't put null value in map as a side-effect of using []. --- common/widgets/wx_grid.cpp | 42 ++++++-------------------------------- include/widgets/wx_grid.h | 8 ++++++++ 2 files changed, 14 insertions(+), 36 deletions(-) diff --git a/common/widgets/wx_grid.cpp b/common/widgets/wx_grid.cpp index 0e0741b09a..5c26082366 100644 --- a/common/widgets/wx_grid.cpp +++ b/common/widgets/wx_grid.cpp @@ -375,10 +375,7 @@ void WX_GRID::onCellEditorHidden( wxGridEvent& aEvent ) if( alg::contains( m_autoEvalCols, col ) ) { - UNITS_PROVIDER* unitsProvider = m_unitsProviders[ aEvent.GetCol() ]; - - if( !unitsProvider ) - unitsProvider = m_unitsProviders.begin()->second; + UNITS_PROVIDER* unitsProvider = getUnitsProvider( col ); auto [cellUnits, cellDataType] = getColumnUnits( col ); @@ -723,11 +720,6 @@ void WX_GRID::SetAutoEvalColUnits( const int col, EDA_UNITS aUnit ) int WX_GRID::GetUnitValue( int aRow, int aCol ) { - UNITS_PROVIDER* unitsProvider = m_unitsProviders[ aCol ]; - - if( !unitsProvider ) - unitsProvider = m_unitsProviders.begin()->second; - wxString stringValue = GetCellValue( aRow, aCol ); auto [cellUnits, cellDataType] = getColumnUnits( aCol ); @@ -740,17 +732,12 @@ int WX_GRID::GetUnitValue( int aRow, int aCol ) stringValue = m_eval->Result(); } - return unitsProvider->ValueFromString( stringValue, cellDataType ); + return getUnitsProvider( aCol )->ValueFromString( stringValue, cellDataType ); } std::optional WX_GRID::GetOptionalUnitValue( int aRow, int aCol ) { - UNITS_PROVIDER* unitsProvider = m_unitsProviders[aCol]; - - if( !unitsProvider ) - unitsProvider = m_unitsProviders.begin()->second; - wxString stringValue = GetCellValue( aRow, aCol ); auto [cellUnits, cellDataType] = getColumnUnits( aCol ); @@ -763,17 +750,12 @@ std::optional WX_GRID::GetOptionalUnitValue( int aRow, int aCol ) stringValue = m_eval->Result(); } - return unitsProvider->OptionalValueFromString( stringValue, cellDataType ); + return getUnitsProvider( aCol )->OptionalValueFromString( stringValue, cellDataType ); } void WX_GRID::SetUnitValue( int aRow, int aCol, int aValue ) { - UNITS_PROVIDER* unitsProvider = m_unitsProviders[ aCol ]; - - if( !unitsProvider ) - unitsProvider = m_unitsProviders.begin()->second; - EDA_DATA_TYPE cellDataType; if( m_autoEvalColsUnits.contains( aCol ) ) @@ -781,18 +763,13 @@ void WX_GRID::SetUnitValue( int aRow, int aCol, int aValue ) else cellDataType = EDA_DATA_TYPE::DISTANCE; - SetCellValue( aRow, aCol, unitsProvider->StringFromValue( aValue, true, cellDataType ) ); + SetCellValue( aRow, aCol, getUnitsProvider( aCol )->StringFromValue( aValue, true, cellDataType ) ); } void WX_GRID::SetOptionalUnitValue( int aRow, int aCol, std::optional aValue ) { - UNITS_PROVIDER* unitsProvider = m_unitsProviders[aCol]; - - if( !unitsProvider ) - unitsProvider = m_unitsProviders.begin()->second; - - SetCellValue( aRow, aCol, unitsProvider->StringFromOptionalValue( aValue, true ) ); + SetCellValue( aRow, aCol, getUnitsProvider( aCol )->StringFromOptionalValue( aValue, true ) ); } @@ -877,13 +854,6 @@ std::pair WX_GRID::getColumnUnits( const int aCol ) co if( m_autoEvalColsUnits.contains( aCol ) ) return { m_autoEvalColsUnits.at( aCol ).first, m_autoEvalColsUnits.at( aCol ).second }; - UNITS_PROVIDER* unitsProvider; - - if( m_unitsProviders.contains( aCol ) ) - unitsProvider = m_unitsProviders.at( aCol ); - else - unitsProvider = m_unitsProviders.begin()->second; - // Legacy - default always DISTANCE - return { unitsProvider->GetUserUnits(), EDA_DATA_TYPE::DISTANCE }; + return { getUnitsProvider( aCol )->GetUserUnits(), EDA_DATA_TYPE::DISTANCE }; } diff --git a/include/widgets/wx_grid.h b/include/widgets/wx_grid.h index 4e0b9a55da..7931d6020c 100644 --- a/include/widgets/wx_grid.h +++ b/include/widgets/wx_grid.h @@ -260,6 +260,14 @@ protected: void onDPIChanged(wxDPIChangedEvent& event); + UNITS_PROVIDER* getUnitsProvider( int aCol ) const + { + if( m_unitsProviders.contains( aCol ) ) + return m_unitsProviders.at( aCol ); + else + return m_unitsProviders.begin()->second; + } + /** * Returns the units and data type associated with a given column */