From 3cade7fc4750c6c0bdaabfd8006fff2c3ddf2d39 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Mon, 21 Aug 2023 14:51:10 +0100 Subject: [PATCH] Don't use string compare for A.NetClass == B.NetClass. (or A.NetName == B.NetName, or != of either) --- pcbnew/pcb_expr_evaluator.cpp | 74 ++++++++++++++++++++++- qa/tests/pcbnew/test_libeval_compiler.cpp | 12 ++-- 2 files changed, 78 insertions(+), 8 deletions(-) diff --git a/pcbnew/pcb_expr_evaluator.cpp b/pcbnew/pcb_expr_evaluator.cpp index 490be93dd7..eeffcf04e4 100644 --- a/pcbnew/pcb_expr_evaluator.cpp +++ b/pcbnew/pcb_expr_evaluator.cpp @@ -89,6 +89,76 @@ protected: }; +class PCB_NETCLASS_VALUE : public LIBEVAL::VALUE +{ +public: + PCB_NETCLASS_VALUE( BOARD_CONNECTED_ITEM* aItem ) : + LIBEVAL::VALUE( wxEmptyString ), + m_item( aItem ) + {}; + + const wxString& AsString() const override + { + const_cast( this )->Set( m_item->GetEffectiveNetClass()->GetName() ); + return LIBEVAL::VALUE::AsString(); + } + + bool EqualTo( LIBEVAL::CONTEXT* aCtx, const VALUE* b ) const override + { + if( const PCB_NETCLASS_VALUE* bValue = dynamic_cast( b ) ) + return m_item->GetEffectiveNetClass() == bValue->m_item->GetEffectiveNetClass(); + else + return LIBEVAL::VALUE::EqualTo( aCtx, b ); + } + + bool NotEqualTo( LIBEVAL::CONTEXT* aCtx, const LIBEVAL::VALUE* b ) const override + { + if( const PCB_NETCLASS_VALUE* bValue = dynamic_cast( b ) ) + return m_item->GetEffectiveNetClass() != bValue->m_item->GetEffectiveNetClass(); + else + return LIBEVAL::VALUE::NotEqualTo( aCtx, b ); + } + +protected: + BOARD_CONNECTED_ITEM* m_item; +}; + + +class PCB_NET_VALUE : public LIBEVAL::VALUE +{ +public: + PCB_NET_VALUE( BOARD_CONNECTED_ITEM* aItem ) : + LIBEVAL::VALUE( wxEmptyString ), + m_item( aItem ) + {}; + + const wxString& AsString() const override + { + const_cast( this )->Set( m_item->GetNetname() ); + return LIBEVAL::VALUE::AsString(); + } + + bool EqualTo( LIBEVAL::CONTEXT* aCtx, const VALUE* b ) const override + { + if( const PCB_NET_VALUE* bValue = dynamic_cast( b ) ) + return m_item->GetNetCode() == bValue->m_item->GetNetCode(); + else + return LIBEVAL::VALUE::EqualTo( aCtx, b ); + } + + bool NotEqualTo( LIBEVAL::CONTEXT* aCtx, const LIBEVAL::VALUE* b ) const override + { + if( const PCB_NET_VALUE* bValue = dynamic_cast( b ) ) + return m_item->GetNetCode() != bValue->m_item->GetNetCode(); + else + return LIBEVAL::VALUE::NotEqualTo( aCtx, b ); + } + +protected: + BOARD_CONNECTED_ITEM* m_item; +}; + + LIBEVAL::VALUE* PCB_EXPR_VAR_REF::GetValue( LIBEVAL::CONTEXT* aCtx ) { PCB_EXPR_CONTEXT* context = static_cast( aCtx ); @@ -156,7 +226,7 @@ LIBEVAL::VALUE* PCB_EXPR_NETCLASS_REF::GetValue( LIBEVAL::CONTEXT* aCtx ) if( !item ) return new LIBEVAL::VALUE(); - return new LIBEVAL::VALUE( item->GetEffectiveNetClass()->GetName() ); + return new PCB_NETCLASS_VALUE( item ); } @@ -167,7 +237,7 @@ LIBEVAL::VALUE* PCB_EXPR_NETNAME_REF::GetValue( LIBEVAL::CONTEXT* aCtx ) if( !item ) return new LIBEVAL::VALUE(); - return new LIBEVAL::VALUE( item->GetNetname() ); + return new PCB_NET_VALUE( item ); } diff --git a/qa/tests/pcbnew/test_libeval_compiler.cpp b/qa/tests/pcbnew/test_libeval_compiler.cpp index 6216288bd6..3408cb8e2f 100644 --- a/qa/tests/pcbnew/test_libeval_compiler.cpp +++ b/qa/tests/pcbnew/test_libeval_compiler.cpp @@ -1,7 +1,7 @@ /* * This program source code file is part of KiCad, a free EDA CAD application. * - * Copyright (C) 2019-2022 KiCad Developers, see AUTHORS.TXT for contributors. + * Copyright (C) 2019-2023 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 @@ -116,21 +116,21 @@ static bool testEvalExpr( const wxString& expr, const LIBEVAL::VALUE& expectedRe if( error ) return true; - LIBEVAL::VALUE result; + LIBEVAL::VALUE* result; if( ok ) { - result = *ucode.Run( &context ); - ok = ( result.EqualTo( &context, &expectedResult ) ); + result = ucode.Run( &context ); + ok = ( result->EqualTo( &context, &expectedResult ) ); } if( expectedResult.GetType() == LIBEVAL::VT_NUMERIC ) { - BOOST_CHECK_EQUAL( result.AsDouble(), expectedResult.AsDouble() ); + BOOST_CHECK_EQUAL( result->AsDouble(), expectedResult.AsDouble() ); } else { - BOOST_CHECK_EQUAL( result.AsString(), expectedResult.AsString() ); + BOOST_CHECK_EQUAL( result->AsString(), expectedResult.AsString() ); } return ok;