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.
This commit is contained in:
John Beard 2019-02-02 21:50:36 +00:00 committed by Seth Hillbrand
parent 84d79ec10d
commit 0c6ec7dbb3
4 changed files with 29 additions and 21 deletions

View File

@ -2,7 +2,7 @@
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2017 Wayne Stambaugh <stambaughw@gmail.com>
* 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 );

View File

@ -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];
}
/**

View File

@ -3,7 +3,7 @@
*
* Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* 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 );

View File

@ -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