From 0c6ec7dbb3a98e0646e12f9ddbc3607d3fdbf7c9 Mon Sep 17 00:00:00 2001 From: John Beard Date: Sat, 2 Feb 2019 21:50:36 +0000 Subject: [PATCH] LIB_TABLE_BASE: Const and unsigned fixes * Make LIB_TABLE_BASE::GetCount() return unsigned. This is more consistent with the behaviour of STL containers (especially the boost::ptr_vector this is really accessing). Sadly wxGridTableBase() forces an int, so a cast is still required at the WX interface. * Make LIB_TABLE_BASE::At() return a reference. First, this is more consistent with normal STL indexing operator[]'s, and secondly, it allows an idiomatic const index method (so you can access const LIB_TABLE_ROWs from a const LIB_TABLE_BASE). The motivation is to allow use of this class and LIB_TABLE_ROW in a test program, where the LIB_TABLE_BASE is const. --- eeschema/dialogs/panel_sym_lib_table.cpp | 8 ++++---- include/lib_table_base.h | 20 ++++++++++++++------ pcbnew/dialogs/panel_fp_lib_table.cpp | 8 ++++---- qa/common/test_lib_table.cpp | 14 +++++++------- 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/eeschema/dialogs/panel_sym_lib_table.cpp b/eeschema/dialogs/panel_sym_lib_table.cpp index 659df04ba2..daf06975d8 100644 --- a/eeschema/dialogs/panel_sym_lib_table.cpp +++ b/eeschema/dialogs/panel_sym_lib_table.cpp @@ -2,7 +2,7 @@ * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright (C) 2017 Wayne Stambaugh - * Copyright (C) 2017-2018 KiCad Developers, see AUTHORS.txt for contributors. + * Copyright (C) 2017-2019 KiCad Developers, see AUTHORS.txt for contributors. * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the @@ -122,11 +122,11 @@ protected: if( parsed ) { // make sure the table is big enough... - if( tmp_tbl.GetCount() > tbl->GetNumberRows() ) + if( tmp_tbl.GetCount() > (unsigned) tbl->GetNumberRows() ) tbl->AppendRows( tmp_tbl.GetCount() - tbl->GetNumberRows() ); - for( int i = 0; i < tmp_tbl.GetCount(); ++i ) - tbl->rows.replace( i, tmp_tbl.At( i )->clone() ); + for( unsigned i = 0; i < tmp_tbl.GetCount(); ++i ) + tbl->rows.replace( i, tmp_tbl.At( i ).clone() ); } m_grid->AutoSizeColumns( false ); diff --git a/include/lib_table_base.h b/include/lib_table_base.h index c60d6cec16..4f72274980 100644 --- a/include/lib_table_base.h +++ b/include/lib_table_base.h @@ -342,19 +342,27 @@ public: /** * Get the number of rows contained in the table */ - int GetCount() + unsigned GetCount() const { return rows.size(); } /** - * Get the row at the given index. - * @param aIndex row index (must exist) - * @return pointer to the row + * Get the 'n'th #LIB_TABLE_ROW object + * @param aIndex index of row (must exist: from 0 to GetCount() - 1) + * @return reference to the row */ - LIB_TABLE_ROW* At( int aIndex ) + LIB_TABLE_ROW& At( unsigned aIndex ) { - return &rows[aIndex]; + return rows[aIndex]; + } + + /** + * @copydoc At() + */ + const LIB_TABLE_ROW& At( unsigned aIndex ) const + { + return rows[aIndex]; } /** diff --git a/pcbnew/dialogs/panel_fp_lib_table.cpp b/pcbnew/dialogs/panel_fp_lib_table.cpp index e1ef84b1a8..e1a24c2d59 100644 --- a/pcbnew/dialogs/panel_fp_lib_table.cpp +++ b/pcbnew/dialogs/panel_fp_lib_table.cpp @@ -3,7 +3,7 @@ * * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck * Copyright (C) 2013 CERN - * Copyright (C) 2012-2018 KiCad Developers, see AUTHORS.txt for contributors. + * Copyright (C) 2012-2019 KiCad Developers, see AUTHORS.txt for contributors. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -245,11 +245,11 @@ protected: if( parsed ) { // make sure the table is big enough... - if( tmp_tbl.GetCount() > tbl->GetNumberRows() ) + if( tmp_tbl.GetCount() > (unsigned) tbl->GetNumberRows() ) tbl->AppendRows( tmp_tbl.GetCount() - tbl->GetNumberRows() ); - for( int i = 0; i < tmp_tbl.GetCount(); ++i ) - tbl->rows.replace( i, tmp_tbl.At( i )->clone() ); + for( unsigned i = 0; i < tmp_tbl.GetCount(); ++i ) + tbl->rows.replace( i, tmp_tbl.At( i ).clone() ); } m_grid->AutoSizeColumns( false ); diff --git a/qa/common/test_lib_table.cpp b/qa/common/test_lib_table.cpp index 02088020f3..380d0d5e95 100644 --- a/qa/common/test_lib_table.cpp +++ b/qa/common/test_lib_table.cpp @@ -261,7 +261,7 @@ BOOST_AUTO_TEST_CASE( Equal ) BOOST_CHECK_EQUAL( false, m_mainTableNoFb != m_mainTableWithFb ); // Modify one of them - m_mainTableWithFb.At( 1 )->SetNickName( "NewNickname" ); + m_mainTableWithFb.At( 1 ).SetNickName( "NewNickname" ); BOOST_CHECK_EQUAL( false, m_mainTableNoFb == m_mainTableWithFb ); BOOST_CHECK_EQUAL( true, m_mainTableNoFb != m_mainTableWithFb ); @@ -280,15 +280,15 @@ BOOST_AUTO_TEST_CASE( Indexing ) // Filled with the right row count BOOST_CHECK_EQUAL( m_mainTableNoFb.GetCount(), 3 ); - const auto* row0 = m_mainTableNoFb.At( 0 ); - BOOST_CHECK_EQUAL( row0->GetNickName(), "Lib1" ); + const auto& row0 = m_mainTableNoFb.At( 0 ); + BOOST_CHECK_EQUAL( row0.GetNickName(), "Lib1" ); - const auto* row1 = m_mainTableNoFb.At( 1 ); - BOOST_CHECK_EQUAL( row1->GetNickName(), "Lib2" ); + const auto& row1 = m_mainTableNoFb.At( 1 ); + BOOST_CHECK_EQUAL( row1.GetNickName(), "Lib2" ); // disable, but still in the index - const auto* row2 = m_mainTableNoFb.At( 2 ); - BOOST_CHECK_EQUAL( row2->GetNickName(), "Lib3" ); + const auto& row2 = m_mainTableNoFb.At( 2 ); + BOOST_CHECK_EQUAL( row2.GetNickName(), "Lib3" ); // check correct handling of out-of-bounds // TODO: this doesn't work with boost::ptr_vector - that only asserts