From a389b1a568013d6c4a0a83d9c51ba246fe6f4951 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Wed, 20 Nov 2024 03:03:32 +0100 Subject: [PATCH] QgsProcessingAlgorithm::addParameter(): add a variant that accepts a unique_ptr for QgsProcessingParameterDefinition --- .../processing/qgsprocessingalgorithm.sip.in | 1 + .../processing/qgsprocessingalgorithm.sip.in | 1 + src/core/processing/qgsprocessingalgorithm.cpp | 17 +++++++++++------ src/core/processing/qgsprocessingalgorithm.h | 8 +++++++- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/python/PyQt6/core/auto_generated/processing/qgsprocessingalgorithm.sip.in b/python/PyQt6/core/auto_generated/processing/qgsprocessingalgorithm.sip.in index f2f3cb82ec86..28c54aac178e 100644 --- a/python/PyQt6/core/auto_generated/processing/qgsprocessingalgorithm.sip.in +++ b/python/PyQt6/core/auto_generated/processing/qgsprocessingalgorithm.sip.in @@ -519,6 +519,7 @@ occur. .. seealso:: :py:func:`addOutput` %End + void removeParameter( const QString &name ) /HoldGIL/; %Docstring Removes the parameter with matching ``name`` from the algorithm, and deletes any existing diff --git a/python/core/auto_generated/processing/qgsprocessingalgorithm.sip.in b/python/core/auto_generated/processing/qgsprocessingalgorithm.sip.in index d03819a77452..6d4192efc27f 100644 --- a/python/core/auto_generated/processing/qgsprocessingalgorithm.sip.in +++ b/python/core/auto_generated/processing/qgsprocessingalgorithm.sip.in @@ -519,6 +519,7 @@ occur. .. seealso:: :py:func:`addOutput` %End + void removeParameter( const QString &name ) /HoldGIL/; %Docstring Removes the parameter with matching ``name`` from the algorithm, and deletes any existing diff --git a/src/core/processing/qgsprocessingalgorithm.cpp b/src/core/processing/qgsprocessingalgorithm.cpp index b48a01b0acdc..ea6de0698121 100644 --- a/src/core/processing/qgsprocessingalgorithm.cpp +++ b/src/core/processing/qgsprocessingalgorithm.cpp @@ -391,6 +391,11 @@ QVariantMap QgsProcessingAlgorithm::asMap( const QVariantMap ¶meters, QgsPro } bool QgsProcessingAlgorithm::addParameter( QgsProcessingParameterDefinition *definition, bool createOutput ) +{ + return addParameter( std::unique_ptr( definition ), createOutput ); +} + +bool QgsProcessingAlgorithm::addParameter( std::unique_ptr definition, bool createOutput ) { if ( !definition ) return false; @@ -400,22 +405,22 @@ bool QgsProcessingAlgorithm::addParameter( QgsProcessingParameterDefinition *def if ( existingDef && existingDef->name() == definition->name() ) // parameterDefinition is case-insensitive, but we DO allow case-different duplicate names { QgsMessageLog::logMessage( QObject::tr( "Duplicate parameter %1 registered for alg %2" ).arg( definition->name(), id() ), QObject::tr( "Processing" ) ); - delete definition; return false; } if ( definition->isDestination() && mProvider ) { - QgsProcessingDestinationParameter *destParam = static_cast< QgsProcessingDestinationParameter *>( definition ); + QgsProcessingDestinationParameter *destParam = static_cast< QgsProcessingDestinationParameter *>( definition.get() ); if ( !mProvider->supportsNonFileBasedOutput() ) destParam->setSupportsNonFileBasedOutput( false ); } - mParameters << definition; definition->mAlgorithm = this; + mParameters << definition.release(); + const QgsProcessingParameterDefinition *definitionRawPtr = mParameters.back(); if ( createOutput ) - return createAutoOutputForParameter( definition ); + return createAutoOutputForParameter( definitionRawPtr ); else return true; } @@ -1043,12 +1048,12 @@ bool QgsProcessingAlgorithm::supportInPlaceEdit( const QgsMapLayer *layer ) cons } -bool QgsProcessingAlgorithm::createAutoOutputForParameter( QgsProcessingParameterDefinition *parameter ) +bool QgsProcessingAlgorithm::createAutoOutputForParameter( const QgsProcessingParameterDefinition *parameter ) { if ( !parameter->isDestination() ) return true; // nothing created, but nothing went wrong - so return true - QgsProcessingDestinationParameter *dest = static_cast< QgsProcessingDestinationParameter * >( parameter ); + const QgsProcessingDestinationParameter *dest = static_cast< const QgsProcessingDestinationParameter * >( parameter ); QgsProcessingOutputDefinition *output( dest->toOutputDefinition() ); if ( !output ) return true; // nothing created - but nothing went wrong - so return true diff --git a/src/core/processing/qgsprocessingalgorithm.h b/src/core/processing/qgsprocessingalgorithm.h index be54788d4332..5af4989a706f 100644 --- a/src/core/processing/qgsprocessingalgorithm.h +++ b/src/core/processing/qgsprocessingalgorithm.h @@ -527,6 +527,12 @@ class CORE_EXPORT QgsProcessingAlgorithm */ bool addParameter( QgsProcessingParameterDefinition *parameterDefinition SIP_TRANSFER, bool createOutput = true ) SIP_HOLDGIL; + /** + * Same as above addParameter(QgsProcessingParameterDefinition*, bool), but using + * a smart pointer for safer use. + */ + bool addParameter( std::unique_ptr parameterDefinition, bool createOutput = true ) SIP_SKIP; + /** * Removes the parameter with matching \a name from the algorithm, and deletes any existing * definition. @@ -1110,7 +1116,7 @@ class CORE_EXPORT QgsProcessingAlgorithm bool mHasPostProcessed = false; std::unique_ptr< QgsProcessingContext > mLocalContext; - bool createAutoOutputForParameter( QgsProcessingParameterDefinition *parameter ); + bool createAutoOutputForParameter( const QgsProcessingParameterDefinition *parameter ); friend class QgsProcessingProvider;