Skip to content

Commit 3767cd4

Browse files
committed
Debloat QgsExpression header
it's used throughout the project and keeping it slick should keep compile time a little lower. These methods are also normally used while building the request or preparing, so inlining them shouldn't make much difference.
1 parent b642c3f commit 3767cd4

File tree

2 files changed

+181
-123
lines changed

2 files changed

+181
-123
lines changed

src/core/qgsexpression.cpp

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3824,6 +3824,14 @@ QStringList QgsExpression::referencedColumns() const
38243824
return columns;
38253825
}
38263826

3827+
bool QgsExpression::NodeInOperator::needsGeometry() const
3828+
{
3829+
bool needs = false;
3830+
Q_FOREACH ( Node* n, mList->list() )
3831+
needs |= n->needsGeometry();
3832+
return needs;
3833+
}
3834+
38273835
QSet<int> QgsExpression::referencedAttributeIndexes( const QgsFields& fields ) const
38283836
{
38293837
if ( !d->mRootNode )
@@ -4056,6 +4064,13 @@ double QgsExpression::evaluateToDouble( const QString &text, const double fallba
40564064
///////////////////////////////////////////////
40574065
// nodes
40584066

4067+
void QgsExpression::NodeList::append( QgsExpression::NamedNode* node )
4068+
{
4069+
mList.append( node->node );
4070+
mNameList.append( node->name.toLower() );
4071+
mHasNamedNodes = true;
4072+
}
4073+
40594074
QgsExpression::NodeList* QgsExpression::NodeList::clone() const
40604075
{
40614076
NodeList* nl = new NodeList;
@@ -4603,6 +4618,21 @@ QString QgsExpression::NodeBinaryOperator::dump() const
46034618
return fmt.arg( mOpLeft->dump(), BinaryOperatorText[mOp], rdump );
46044619
}
46054620

4621+
QStringList QgsExpression::NodeBinaryOperator::referencedColumns() const
4622+
{
4623+
return mOpLeft->referencedColumns() + mOpRight->referencedColumns();
4624+
}
4625+
4626+
bool QgsExpression::NodeBinaryOperator::needsGeometry() const
4627+
{
4628+
return mOpLeft->needsGeometry() || mOpRight->needsGeometry();
4629+
}
4630+
4631+
void QgsExpression::NodeBinaryOperator::accept( QgsExpression::Visitor& v ) const
4632+
{
4633+
v.visit( *this );
4634+
}
4635+
46064636
QgsExpression::Node* QgsExpression::NodeBinaryOperator::clone() const
46074637
{
46084638
return new NodeBinaryOperator( mOp, mOpLeft->clone(), mOpRight->clone() );
@@ -4718,6 +4748,46 @@ QVariant QgsExpression::NodeFunction::eval( QgsExpression *parent, const QgsExpr
47184748
return res;
47194749
}
47204750

4751+
QgsExpression::NodeFunction::NodeFunction( int fnIndex, QgsExpression::NodeList* args )
4752+
: mFnIndex( fnIndex )
4753+
{
4754+
const ParameterList& functionParams = Functions()[mFnIndex]->parameters();
4755+
if ( !args || functionParams.isEmpty() )
4756+
{
4757+
// no parameters, or function does not support them
4758+
mArgs = args;
4759+
}
4760+
else
4761+
{
4762+
mArgs = new NodeList();
4763+
4764+
int idx = 0;
4765+
//first loop through unnamed arguments
4766+
while ( idx < args->names().size() && args->names().at( idx ).isEmpty() )
4767+
{
4768+
mArgs->append( args->list().at( idx )->clone() );
4769+
idx++;
4770+
}
4771+
4772+
//next copy named parameters in order expected by function
4773+
for ( ; idx < functionParams.count(); ++idx )
4774+
{
4775+
int nodeIdx = args->names().indexOf( functionParams.at( idx ).name().toLower() );
4776+
if ( nodeIdx < 0 )
4777+
{
4778+
//parameter not found - insert default value for parameter
4779+
mArgs->append( new NodeLiteral( functionParams.at( idx ).defaultValue() ) );
4780+
}
4781+
else
4782+
{
4783+
mArgs->append( args->list().at( nodeIdx )->clone() );
4784+
}
4785+
}
4786+
4787+
delete args;
4788+
}
4789+
}
4790+
47214791
bool QgsExpression::NodeFunction::prepare( QgsExpression *parent, const QgsExpressionContext *context )
47224792
{
47234793
Function* fd = Functions()[mFnIndex];
@@ -4762,11 +4832,95 @@ QStringList QgsExpression::NodeFunction::referencedColumns() const
47624832
return functionColumns.toSet().toList();
47634833
}
47644834

4835+
bool QgsExpression::NodeFunction::needsGeometry() const
4836+
{
4837+
bool needs = Functions()[mFnIndex]->usesGeometry();
4838+
if ( mArgs )
4839+
{
4840+
Q_FOREACH ( Node* n, mArgs->list() )
4841+
needs |= n->needsGeometry();
4842+
}
4843+
return needs;
4844+
}
4845+
47654846
QgsExpression::Node* QgsExpression::NodeFunction::clone() const
47664847
{
47674848
return new NodeFunction( mFnIndex, mArgs ? mArgs->clone() : nullptr );
47684849
}
47694850

4851+
bool QgsExpression::NodeFunction::validateParams( int fnIndex, QgsExpression::NodeList* args, QString& error )
4852+
{
4853+
if ( !args || !args->hasNamedNodes() )
4854+
return true;
4855+
4856+
const ParameterList& functionParams = Functions()[fnIndex]->parameters();
4857+
if ( functionParams.isEmpty() )
4858+
{
4859+
error = QString( "%1 does not supported named parameters" ).arg( Functions()[fnIndex]->name() );
4860+
return false;
4861+
}
4862+
else
4863+
{
4864+
QSet< int > providedArgs;
4865+
QSet< int > handledArgs;
4866+
int idx = 0;
4867+
//first loop through unnamed arguments
4868+
while ( args->names().at( idx ).isEmpty() )
4869+
{
4870+
providedArgs << idx;
4871+
handledArgs << idx;
4872+
idx++;
4873+
}
4874+
4875+
//next check named parameters
4876+
for ( ; idx < functionParams.count(); ++idx )
4877+
{
4878+
int nodeIdx = args->names().indexOf( functionParams.at( idx ).name().toLower() );
4879+
if ( nodeIdx < 0 )
4880+
{
4881+
if ( !functionParams.at( idx ).optional() )
4882+
{
4883+
error = QString( "No value specified for parameter '%1' for %2" ).arg( functionParams.at( idx ).name(), Functions()[fnIndex]->name() );
4884+
return false;
4885+
}
4886+
}
4887+
else
4888+
{
4889+
if ( providedArgs.contains( idx ) )
4890+
{
4891+
error = QString( "Duplicate parameter specified for '%1' for %2" ).arg( functionParams.at( idx ).name(), Functions()[fnIndex]->name() );
4892+
return false;
4893+
}
4894+
}
4895+
providedArgs << idx;
4896+
handledArgs << nodeIdx;
4897+
}
4898+
4899+
//last check for bad names
4900+
idx = 0;
4901+
Q_FOREACH ( const QString& name, args->names() )
4902+
{
4903+
if ( !name.isEmpty() && !functionParams.contains( name ) )
4904+
{
4905+
error = QString( "Invalid parameter name '%1' for %2" ).arg( name, Functions()[fnIndex]->name() );
4906+
return false;
4907+
}
4908+
if ( !name.isEmpty() && !handledArgs.contains( idx ) )
4909+
{
4910+
int functionIdx = functionParams.indexOf( name );
4911+
if ( providedArgs.contains( functionIdx ) )
4912+
{
4913+
error = QString( "Duplicate parameter specified for '%1' for %2" ).arg( functionParams.at( functionIdx ).name(), Functions()[fnIndex]->name() );
4914+
return false;
4915+
}
4916+
}
4917+
idx++;
4918+
}
4919+
4920+
}
4921+
return true;
4922+
}
4923+
47704924
//
47714925

47724926
QVariant QgsExpression::NodeLiteral::eval( QgsExpression *parent, const QgsExpressionContext *context )
@@ -5294,3 +5448,19 @@ const QgsExpression::Node* QgsExpression::rootNode() const
52945448
{
52955449
return d->mRootNode;
52965450
}
5451+
5452+
QStringList QgsExpression::NodeInOperator::referencedColumns() const
5453+
{
5454+
QStringList lst( mNode->referencedColumns() );
5455+
Q_FOREACH ( const Node* n, mList->list() )
5456+
lst.append( n->referencedColumns() );
5457+
return lst;
5458+
}
5459+
5460+
bool QgsExpression::Function::operator==( const QgsExpression::Function& other ) const
5461+
{
5462+
if ( QString::compare( mName, other.mName, Qt::CaseInsensitive ) == 0 )
5463+
return true;
5464+
5465+
return false;
5466+
}

src/core/qgsexpression.h

Lines changed: 11 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -555,13 +555,7 @@ class CORE_EXPORT QgsExpression
555555
*/
556556
virtual QVariant func( const QVariantList& values, const QgsExpressionContext* context, QgsExpression* parent ) = 0;
557557

558-
bool operator==( const Function& other ) const
559-
{
560-
if ( QString::compare( mName, other.mName, Qt::CaseInsensitive ) == 0 )
561-
return true;
562-
563-
return false;
564-
}
558+
bool operator==( const Function& other ) const;
565559

566560
virtual bool handlesNull() const { return mHandlesNull; }
567561

@@ -830,7 +824,7 @@ class CORE_EXPORT QgsExpression
830824
/** Adds a named node. Takes ownership of the provided node.
831825
* @note added in QGIS 2.16
832826
*/
833-
void append( NamedNode* node ) { mList.append( node->node ); mNameList.append( node->name.toLower() ); mHasNamedNodes = true; }
827+
void append( NamedNode* node );
834828

835829
/** Returns the number of nodes in the list.
836830
*/
@@ -909,8 +903,8 @@ class CORE_EXPORT QgsExpression
909903
virtual QVariant eval( QgsExpression* parent, const QgsExpressionContext* context ) override;
910904
virtual QString dump() const override;
911905

912-
virtual QStringList referencedColumns() const override { return mOpLeft->referencedColumns() + mOpRight->referencedColumns(); }
913-
virtual bool needsGeometry() const override { return mOpLeft->needsGeometry() || mOpRight->needsGeometry(); }
906+
virtual QStringList referencedColumns() const override;
907+
virtual bool needsGeometry() const override;
914908
virtual Node* clone() const override;
915909

916910
int precedence() const;
@@ -953,8 +947,8 @@ class CORE_EXPORT QgsExpression
953947
virtual QVariant eval( QgsExpression* parent, const QgsExpressionContext* context ) override;
954948
virtual QString dump() const override;
955949

956-
virtual QStringList referencedColumns() const override { QStringList lst( mNode->referencedColumns() ); Q_FOREACH ( const Node* n, mList->list() ) lst.append( n->referencedColumns() ); return lst; }
957-
virtual bool needsGeometry() const override { bool needs = false; Q_FOREACH ( Node* n, mList->list() ) needs |= n->needsGeometry(); return needs; }
950+
virtual QStringList referencedColumns() const override;
951+
virtual bool needsGeometry() const override;
958952
virtual Node* clone() const override;
959953

960954
protected:
@@ -968,44 +962,7 @@ class CORE_EXPORT QgsExpression
968962
class CORE_EXPORT NodeFunction : public Node
969963
{
970964
public:
971-
NodeFunction( int fnIndex, NodeList* args ) : mFnIndex( fnIndex )
972-
{
973-
const ParameterList& functionParams = Functions()[mFnIndex]->parameters();
974-
if ( !args || functionParams.isEmpty() )
975-
{
976-
// no parameters, or function does not support them
977-
mArgs = args;
978-
}
979-
else
980-
{
981-
mArgs = new NodeList();
982-
983-
int idx = 0;
984-
//first loop through unnamed arguments
985-
while ( idx < args->names().size() && args->names().at( idx ).isEmpty() )
986-
{
987-
mArgs->append( args->list().at( idx )->clone() );
988-
idx++;
989-
}
990-
991-
//next copy named parameters in order expected by function
992-
for ( ; idx < functionParams.count(); ++idx )
993-
{
994-
int nodeIdx = args->names().indexOf( functionParams.at( idx ).name().toLower() );
995-
if ( nodeIdx < 0 )
996-
{
997-
//parameter not found - insert default value for parameter
998-
mArgs->append( new NodeLiteral( functionParams.at( idx ).defaultValue() ) );
999-
}
1000-
else
1001-
{
1002-
mArgs->append( args->list().at( nodeIdx )->clone() );
1003-
}
1004-
}
1005-
1006-
delete args;
1007-
}
1008-
}
965+
NodeFunction( int fnIndex, NodeList* args );
1009966

1010967
virtual ~NodeFunction() { delete mArgs; }
1011968

@@ -1018,82 +975,11 @@ class CORE_EXPORT QgsExpression
1018975
virtual QString dump() const override;
1019976

1020977
virtual QStringList referencedColumns() const override;
1021-
virtual bool needsGeometry() const override { bool needs = Functions()[mFnIndex]->usesGeometry(); if ( mArgs ) { Q_FOREACH ( Node* n, mArgs->list() ) needs |= n->needsGeometry(); } return needs; }
978+
virtual bool needsGeometry() const override;
1022979
virtual Node* clone() const override;
1023980

1024981
//! Tests whether the provided argument list is valid for the matching function
1025-
static bool validateParams( int fnIndex, NodeList* args, QString& error )
1026-
{
1027-
if ( !args || !args->hasNamedNodes() )
1028-
return true;
1029-
1030-
const ParameterList& functionParams = Functions()[fnIndex]->parameters();
1031-
if ( functionParams.isEmpty() )
1032-
{
1033-
error = QString( "%1 does not supported named parameters" ).arg( Functions()[fnIndex]->name() );
1034-
return false;
1035-
}
1036-
else
1037-
{
1038-
QSet< int > providedArgs;
1039-
QSet< int > handledArgs;
1040-
int idx = 0;
1041-
//first loop through unnamed arguments
1042-
while ( args->names().at( idx ).isEmpty() )
1043-
{
1044-
providedArgs << idx;
1045-
handledArgs << idx;
1046-
idx++;
1047-
}
1048-
1049-
//next check named parameters
1050-
for ( ; idx < functionParams.count(); ++idx )
1051-
{
1052-
int nodeIdx = args->names().indexOf( functionParams.at( idx ).name().toLower() );
1053-
if ( nodeIdx < 0 )
1054-
{
1055-
if ( !functionParams.at( idx ).optional() )
1056-
{
1057-
error = QString( "No value specified for parameter '%1' for %2" ).arg( functionParams.at( idx ).name(), Functions()[fnIndex]->name() );
1058-
return false;
1059-
}
1060-
}
1061-
else
1062-
{
1063-
if ( providedArgs.contains( idx ) )
1064-
{
1065-
error = QString( "Duplicate parameter specified for '%1' for %2" ).arg( functionParams.at( idx ).name(), Functions()[fnIndex]->name() );
1066-
return false;
1067-
}
1068-
}
1069-
providedArgs << idx;
1070-
handledArgs << nodeIdx;
1071-
}
1072-
1073-
//last check for bad names
1074-
idx = 0;
1075-
Q_FOREACH ( const QString& name, args->names() )
1076-
{
1077-
if ( !name.isEmpty() && !functionParams.contains( name ) )
1078-
{
1079-
error = QString( "Invalid parameter name '%1' for %2" ).arg( name, Functions()[fnIndex]->name() );
1080-
return false;
1081-
}
1082-
if ( !name.isEmpty() && !handledArgs.contains( idx ) )
1083-
{
1084-
int functionIdx = functionParams.indexOf( name );
1085-
if ( providedArgs.contains( functionIdx ) )
1086-
{
1087-
error = QString( "Duplicate parameter specified for '%1' for %2" ).arg( functionParams.at( functionIdx ).name(), Functions()[fnIndex]->name() );
1088-
return false;
1089-
}
1090-
}
1091-
idx++;
1092-
}
1093-
1094-
}
1095-
return true;
1096-
}
982+
static bool validateParams( int fnIndex, NodeList* args, QString& error );
1097983

1098984
protected:
1099985
int mFnIndex;
@@ -1333,6 +1219,8 @@ class CORE_EXPORT QgsExpression
13331219
friend class QgsOgcUtils;
13341220
};
13351221

1222+
1223+
13361224
Q_DECLARE_METATYPE( QgsExpression::Node* )
13371225

13381226
#endif // QGSEXPRESSION_H

0 commit comments

Comments
 (0)