From b445ce663257bfe09dce51649b130f1b88e5698b Mon Sep 17 00:00:00 2001 From: Ian McInerney Date: Sun, 12 Jan 2020 19:45:37 +0000 Subject: [PATCH] Clean up pointer casting * Make note of when dynamic cast is actually needed * Convert some to static cast when we know the type for sure --- common/widgets/grid_text_button_helpers.cpp | 3 ++- eeschema/dialogs/dialog_edit_one_field.cpp | 20 +++++++++---------- .../dialog_global_edit_text_and_graphics.cpp | 4 +++- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/common/widgets/grid_text_button_helpers.cpp b/common/widgets/grid_text_button_helpers.cpp index b779a098e6..dc689c29e4 100644 --- a/common/widgets/grid_text_button_helpers.cpp +++ b/common/widgets/grid_text_button_helpers.cpp @@ -70,7 +70,8 @@ void GRID_CELL_TEXT_BUTTON::StartingKey( wxKeyEvent& event ) // Do it ourselves instead. We know that if we get this far that we have // a valid character, so not a whole lot of testing needs to be done. - wxTextEntry* textEntry = dynamic_cast( Combo() ); + // wxComboCtrl inherits from wxTextEntry, so can staticly cast + wxTextEntry* textEntry = static_cast( Combo() ); int ch; bool isPrintable; diff --git a/eeschema/dialogs/dialog_edit_one_field.cpp b/eeschema/dialogs/dialog_edit_one_field.cpp index 7961a33357..853d83c6f7 100644 --- a/eeschema/dialogs/dialog_edit_one_field.cpp +++ b/eeschema/dialogs/dialog_edit_one_field.cpp @@ -46,6 +46,8 @@ DIALOG_EDIT_ONE_FIELD::DIALOG_EDIT_ONE_FIELD( SCH_BASE_FRAME* aParent, const wxS m_posY( aParent, m_yPosLabel, m_yPosCtrl, m_yPosUnits, true ), m_textSize( aParent, m_textSizeLabel, m_textSizeCtrl, m_textSizeUnits, true ) { + wxASSERT( aTextItem ); + SetTitle( aTitle ); // The field ID and power status are Initialized in the derived object's ctor. @@ -201,10 +203,9 @@ void DIALOG_EDIT_ONE_FIELD::updateText( EDA_TEXT* aText ) } -DIALOG_LIB_EDIT_ONE_FIELD::DIALOG_LIB_EDIT_ONE_FIELD( SCH_BASE_FRAME* aParent, - const wxString& aTitle, - const LIB_FIELD* aField ) : - DIALOG_EDIT_ONE_FIELD( aParent, aTitle, dynamic_cast< const EDA_TEXT* >( aField ) ) +DIALOG_LIB_EDIT_ONE_FIELD::DIALOG_LIB_EDIT_ONE_FIELD( + SCH_BASE_FRAME* aParent, const wxString& aTitle, const LIB_FIELD* aField ) + : DIALOG_EDIT_ONE_FIELD( aParent, aTitle, static_cast( aField ) ) { m_fieldId = aField->GetId(); @@ -214,10 +215,9 @@ DIALOG_LIB_EDIT_ONE_FIELD::DIALOG_LIB_EDIT_ONE_FIELD( SCH_BASE_FRAME* aParent, } -DIALOG_SCH_EDIT_ONE_FIELD::DIALOG_SCH_EDIT_ONE_FIELD( SCH_BASE_FRAME* aParent, - const wxString& aTitle, - const SCH_FIELD* aField ) : - DIALOG_EDIT_ONE_FIELD( aParent, aTitle, dynamic_cast< const EDA_TEXT* >( aField ) ) +DIALOG_SCH_EDIT_ONE_FIELD::DIALOG_SCH_EDIT_ONE_FIELD( + SCH_BASE_FRAME* aParent, const wxString& aTitle, const SCH_FIELD* aField ) + : DIALOG_EDIT_ONE_FIELD( aParent, aTitle, static_cast( aField ) ) { m_fieldId = aField->GetId(); @@ -242,11 +242,11 @@ void DIALOG_SCH_EDIT_ONE_FIELD::UpdateField( SCH_FIELD* aField, SCH_SHEET_PATH* { if( aField->GetId() == REFERENCE ) { - wxASSERT( aSheetPath ); + wxASSERT( aSheetPath ); SCH_COMPONENT* component = dynamic_cast< SCH_COMPONENT* >( aField->GetParent() ); - wxASSERT( component ); + wxASSERT( component ); if( component ) component->SetRef( aSheetPath, m_text ); diff --git a/pcbnew/dialogs/dialog_global_edit_text_and_graphics.cpp b/pcbnew/dialogs/dialog_global_edit_text_and_graphics.cpp index 98be70770f..fcb1ad6ed3 100644 --- a/pcbnew/dialogs/dialog_global_edit_text_and_graphics.cpp +++ b/pcbnew/dialogs/dialog_global_edit_text_and_graphics.cpp @@ -369,6 +369,8 @@ bool DIALOG_GLOBAL_EDIT_TEXT_AND_GRAPHICS::TransferDataFromWindow() { if( boardItem->Type() == PCB_MODULE_TEXT_T ) { + // We are guaranteed to always get an EDA_TEXT in this statement, but we must + // use the dynamic_cast to move through the type tree anyway. const wxString text = dynamic_cast( boardItem )->GetText(); if( m_references->GetValue() && text == wxT( "%R" ) ) @@ -412,7 +414,7 @@ int GLOBAL_EDIT_TOOL::EditTextAndGraphics( const TOOL_EVENT& aEvent ) { PCB_EDIT_FRAME* editFrame = getEditFrame(); DIALOG_GLOBAL_EDIT_TEXT_AND_GRAPHICS dlg( editFrame ); - + dlg.ShowModal(); return 0; }