Skip to content

Commit 03275bb

Browse files
committed
Split QgsProcessingAlgorithm::create into non virtual-create
and pure virtual createInstance Allows us to add logic which always need applying within create(), leaving createInstance() free to just return a raw new instance of the class
1 parent 7753ba1 commit 03275bb

13 files changed

+61
-32
lines changed

python/core/processing/models/qgsprocessingmodelalgorithm.sip

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class QgsProcessingModelAlgorithm : QgsProcessingAlgorithm
4646

4747
virtual QString asPythonCommand( const QVariantMap &parameters, QgsProcessingContext &context ) const;
4848

49-
virtual QgsProcessingModelAlgorithm *create() const /Factory/;
49+
virtual QgsProcessingModelAlgorithm *createInstance() const /Factory/;
5050

5151

5252
void setName( const QString &name );

python/core/processing/qgsprocessingalgorithm.sip

+9-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ class QgsProcessingAlgorithm
4343
virtual ~QgsProcessingAlgorithm();
4444

4545

46-
47-
virtual QgsProcessingAlgorithm *create() const = 0 /Factory/;
46+
QgsProcessingAlgorithm *create() const /Factory/;
4847
%Docstring
4948
Creates a copy of the algorithm, ready for execution.
5049
:rtype: QgsProcessingAlgorithm
@@ -332,6 +331,14 @@ class QgsProcessingAlgorithm
332331

333332
protected:
334333

334+
virtual QgsProcessingAlgorithm *createInstance() const = 0 /Factory/;
335+
%Docstring
336+
Creates a new instance of the algorithm class.
337+
338+
This method should return a 'pristine' instance of the algorithm class.
339+
:rtype: QgsProcessingAlgorithm
340+
%End
341+
335342
bool addParameter( QgsProcessingParameterDefinition *parameterDefinition /Transfer/ );
336343
%Docstring
337344
Adds a parameter ``definition`` to the algorithm. Ownership of the definition is transferred to the algorithm.

python/plugins/processing/algs/qgis/QgisAlgorithm.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@ def trAlgorithm(self, string, context=''):
4848
context = self.__class__.__name__
4949
return string, QCoreApplication.translate(context, string)
5050

51-
def create(self):
51+
def createInstance(self):
5252
return type(self)()

python/plugins/processing/script/ScriptAlgorithm.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def __init__(self, descriptionFile, script=None):
7979
self.cleaned_script = None
8080
self.results = {}
8181

82-
def create(self):
82+
def createInstance(self):
8383
return ScriptAlgorithm(self.descriptionFile)
8484

8585
def icon(self):

python/plugins/processing/tests/QgisAlgorithmsTest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def name(self):
4848
def displayName(self):
4949
return 'testalg'
5050

51-
def create(self):
51+
def createInstance(self):
5252
return TestAlg()
5353

5454
def processAlgorithm(self, parameters, context, feedback):

src/core/processing/models/qgsprocessingmodelalgorithm.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ QString QgsProcessingModelAlgorithm::asPythonCommand( const QVariantMap &paramet
11001100
return QgsProcessingAlgorithm::asPythonCommand( parameters, context );
11011101
}
11021102

1103-
QgsProcessingModelAlgorithm *QgsProcessingModelAlgorithm::create() const
1103+
QgsProcessingAlgorithm *QgsProcessingModelAlgorithm::createInstance() const
11041104
{
11051105
QgsProcessingModelAlgorithm *alg = new QgsProcessingModelAlgorithm();
11061106
alg->loadVariant( toVariant() );

src/core/processing/models/qgsprocessingmodelalgorithm.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
5252

5353
bool canExecute( QString *errorMessage SIP_OUT = nullptr ) const override;
5454
QString asPythonCommand( const QVariantMap &parameters, QgsProcessingContext &context ) const override;
55-
QgsProcessingModelAlgorithm *create() const override SIP_FACTORY;
55+
QgsProcessingAlgorithm *createInstance() const override SIP_FACTORY;
5656

5757
/**
5858
* Sets the model \a name.

src/core/processing/models/qgsprocessingmodelchildalgorithm.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,6 @@ void QgsProcessingModelChildAlgorithm::setAlgorithmId( const QString &algorithmI
208208
{
209209
mAlgorithmId = algorithmId;
210210
mAlgorithm.reset( QgsApplication::processingRegistry()->createAlgorithmById( mAlgorithmId ) );
211-
if ( mAlgorithm )
212-
{
213-
mAlgorithm->init( QVariantMap() );
214-
}
215211
}
216212

217213
///@endcond

src/core/processing/qgsnativealgorithms.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ QString QgsCentroidAlgorithm::shortHelpString() const
8484
"The attributes associated to each point in the output layer are the same ones associated to the original features." );
8585
}
8686

87-
QgsCentroidAlgorithm *QgsCentroidAlgorithm::create() const
87+
QgsCentroidAlgorithm *QgsCentroidAlgorithm::createInstance() const
8888
{
8989
return new QgsCentroidAlgorithm();
9090
}
@@ -164,7 +164,7 @@ QString QgsBufferAlgorithm::shortHelpString() const
164164
"The mitre limit parameter is only applicable for mitre join styles, and controls the maximum distance from the offset curve to use when creating a mitred join." );
165165
}
166166

167-
QgsBufferAlgorithm *QgsBufferAlgorithm::create() const
167+
QgsBufferAlgorithm *QgsBufferAlgorithm::createInstance() const
168168
{
169169
return new QgsBufferAlgorithm();
170170
}
@@ -273,7 +273,7 @@ QString QgsDissolveAlgorithm::shortHelpString() const
273273
"In case the input is a polygon layer, common boundaries of adjacent polygons being dissolved will get erased." );
274274
}
275275

276-
QgsDissolveAlgorithm *QgsDissolveAlgorithm::create() const
276+
QgsDissolveAlgorithm *QgsDissolveAlgorithm::createInstance() const
277277
{
278278
return new QgsDissolveAlgorithm();
279279
}
@@ -430,7 +430,7 @@ QString QgsClipAlgorithm::shortHelpString() const
430430
"be manually updated." );
431431
}
432432

433-
QgsClipAlgorithm *QgsClipAlgorithm::create() const
433+
QgsClipAlgorithm *QgsClipAlgorithm::createInstance() const
434434
{
435435
return new QgsClipAlgorithm();
436436
}
@@ -583,7 +583,7 @@ QString QgsTransformAlgorithm::shortHelpString() const
583583
"Attributes are not modified by this algorithm." );
584584
}
585585

586-
QgsTransformAlgorithm *QgsTransformAlgorithm::create() const
586+
QgsTransformAlgorithm *QgsTransformAlgorithm::createInstance() const
587587
{
588588
return new QgsTransformAlgorithm();
589589
}
@@ -652,7 +652,7 @@ QString QgsSubdivideAlgorithm::shortHelpString() const
652652
"Curved geometries will be segmentized before subdivision." );
653653
}
654654

655-
QgsSubdivideAlgorithm *QgsSubdivideAlgorithm::create() const
655+
QgsSubdivideAlgorithm *QgsSubdivideAlgorithm::createInstance() const
656656
{
657657
return new QgsSubdivideAlgorithm();
658658
}
@@ -723,7 +723,7 @@ QString QgsMultipartToSinglepartAlgorithm::shortHelpString() const
723723
"contain, and the same attributes are used for each of them." );
724724
}
725725

726-
QgsMultipartToSinglepartAlgorithm *QgsMultipartToSinglepartAlgorithm::create() const
726+
QgsMultipartToSinglepartAlgorithm *QgsMultipartToSinglepartAlgorithm::createInstance() const
727727
{
728728
return new QgsMultipartToSinglepartAlgorithm();
729729
}
@@ -810,7 +810,7 @@ QString QgsExtractByExpressionAlgorithm::shortHelpString() const
810810
"For more information about expressions see the <a href =\"{qgisdocs}/user_manual/working_with_vector/expression.html\">user manual</a>" );
811811
}
812812

813-
QgsExtractByExpressionAlgorithm *QgsExtractByExpressionAlgorithm::create() const
813+
QgsExtractByExpressionAlgorithm *QgsExtractByExpressionAlgorithm::createInstance() const
814814
{
815815
return new QgsExtractByExpressionAlgorithm();
816816
}
@@ -941,7 +941,7 @@ QString QgsExtractByAttributeAlgorithm::shortHelpString() const
941941
"of an attribute from the input layer." );
942942
}
943943

944-
QgsExtractByAttributeAlgorithm *QgsExtractByAttributeAlgorithm::create() const
944+
QgsExtractByAttributeAlgorithm *QgsExtractByAttributeAlgorithm::createInstance() const
945945
{
946946
return new QgsExtractByAttributeAlgorithm();
947947
}

src/core/processing/qgsnativealgorithms.h

+9-9
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class QgsCentroidAlgorithm : public QgsProcessingAlgorithm
6060
virtual QStringList tags() const override { return QObject::tr( "centroid,center,average,point,middle" ).split( ',' ); }
6161
QString group() const override { return QObject::tr( "Vector geometry tools" ); }
6262
QString shortHelpString() const override;
63-
QgsCentroidAlgorithm *create() const override SIP_FACTORY;
63+
QgsCentroidAlgorithm *createInstance() const override SIP_FACTORY;
6464

6565
protected:
6666

@@ -84,7 +84,7 @@ class QgsTransformAlgorithm : public QgsProcessingAlgorithm
8484
virtual QStringList tags() const override { return QObject::tr( "transform,reproject,crs,srs,warp" ).split( ',' ); }
8585
QString group() const override { return QObject::tr( "Vector general tools" ); }
8686
QString shortHelpString() const override;
87-
QgsTransformAlgorithm *create() const override SIP_FACTORY;
87+
QgsTransformAlgorithm *createInstance() const override SIP_FACTORY;
8888

8989
protected:
9090

@@ -108,7 +108,7 @@ class QgsBufferAlgorithm : public QgsProcessingAlgorithm
108108
virtual QStringList tags() const override { return QObject::tr( "buffer,grow" ).split( ',' ); }
109109
QString group() const override { return QObject::tr( "Vector geometry tools" ); }
110110
QString shortHelpString() const override;
111-
QgsBufferAlgorithm *create() const override SIP_FACTORY;
111+
QgsBufferAlgorithm *createInstance() const override SIP_FACTORY;
112112

113113
protected:
114114

@@ -132,7 +132,7 @@ class QgsDissolveAlgorithm : public QgsProcessingAlgorithm
132132
virtual QStringList tags() const override { return QObject::tr( "dissolve,union,combine,collect" ).split( ',' ); }
133133
QString group() const override { return QObject::tr( "Vector geometry tools" ); }
134134
QString shortHelpString() const override;
135-
QgsDissolveAlgorithm *create() const override SIP_FACTORY;
135+
QgsDissolveAlgorithm *createInstance() const override SIP_FACTORY;
136136

137137
protected:
138138

@@ -171,7 +171,7 @@ class QgsExtractByAttributeAlgorithm : public QgsProcessingAlgorithm
171171
virtual QStringList tags() const override { return QObject::tr( "extract,filter,attribute,value,contains,null,field" ).split( ',' ); }
172172
QString group() const override { return QObject::tr( "Vector selection tools" ); }
173173
QString shortHelpString() const override;
174-
QgsExtractByAttributeAlgorithm *create() const override SIP_FACTORY;
174+
QgsExtractByAttributeAlgorithm *createInstance() const override SIP_FACTORY;
175175

176176
protected:
177177

@@ -195,7 +195,7 @@ class QgsExtractByExpressionAlgorithm : public QgsProcessingAlgorithm
195195
virtual QStringList tags() const override { return QObject::tr( "extract,filter,expression,field" ).split( ',' ); }
196196
QString group() const override { return QObject::tr( "Vector selection tools" ); }
197197
QString shortHelpString() const override;
198-
QgsExtractByExpressionAlgorithm *create() const override SIP_FACTORY;
198+
QgsExtractByExpressionAlgorithm *createInstance() const override SIP_FACTORY;
199199

200200
protected:
201201

@@ -219,7 +219,7 @@ class QgsClipAlgorithm : public QgsProcessingAlgorithm
219219
virtual QStringList tags() const override { return QObject::tr( "clip,intersect,intersection,mask" ).split( ',' ); }
220220
QString group() const override { return QObject::tr( "Vector overlay tools" ); }
221221
QString shortHelpString() const override;
222-
QgsClipAlgorithm *create() const override SIP_FACTORY;
222+
QgsClipAlgorithm *createInstance() const override SIP_FACTORY;
223223

224224
protected:
225225

@@ -244,7 +244,7 @@ class QgsSubdivideAlgorithm : public QgsProcessingAlgorithm
244244
virtual QStringList tags() const override { return QObject::tr( "subdivide,segmentize,split,tesselate" ).split( ',' ); }
245245
QString group() const override { return QObject::tr( "Vector geometry tools" ); }
246246
QString shortHelpString() const override;
247-
QgsSubdivideAlgorithm *create() const override SIP_FACTORY;
247+
QgsSubdivideAlgorithm *createInstance() const override SIP_FACTORY;
248248

249249
protected:
250250

@@ -268,7 +268,7 @@ class QgsMultipartToSinglepartAlgorithm : public QgsProcessingAlgorithm
268268
virtual QStringList tags() const override { return QObject::tr( "multi,single,multiple,split,dump" ).split( ',' ); }
269269
QString group() const override { return QObject::tr( "Vector geometry tools" ); }
270270
QString shortHelpString() const override;
271-
QgsMultipartToSinglepartAlgorithm *create() const override SIP_FACTORY;
271+
QgsMultipartToSinglepartAlgorithm *createInstance() const override SIP_FACTORY;
272272

273273
protected:
274274

src/core/processing/qgsprocessingalgorithm.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ QgsProcessingAlgorithm::~QgsProcessingAlgorithm()
3333
qDeleteAll( mOutputs );
3434
}
3535

36+
QgsProcessingAlgorithm *QgsProcessingAlgorithm::create() const
37+
{
38+
std::unique_ptr< QgsProcessingAlgorithm > creation( createInstance() );
39+
creation->setProvider( provider() );
40+
return creation.release();
41+
}
42+
3643
QString QgsProcessingAlgorithm::id() const
3744
{
3845
if ( mProvider )

src/core/processing/qgsprocessingalgorithm.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class CORE_EXPORT QgsProcessingAlgorithm
7070
/**
7171
* Creates a copy of the algorithm, ready for execution.
7272
*/
73-
virtual QgsProcessingAlgorithm *create() const = 0 SIP_FACTORY;
73+
QgsProcessingAlgorithm *create() const SIP_FACTORY;
7474

7575
/**
7676
* Returns the algorithm name, used for identifying the algorithm. This string
@@ -317,6 +317,13 @@ class CORE_EXPORT QgsProcessingAlgorithm
317317

318318
protected:
319319

320+
/**
321+
* Creates a new instance of the algorithm class.
322+
*
323+
* This method should return a 'pristine' instance of the algorithm class.
324+
*/
325+
virtual QgsProcessingAlgorithm *createInstance() const = 0 SIP_FACTORY;
326+
320327
/**
321328
* Adds a parameter \a definition to the algorithm. Ownership of the definition is transferred to the algorithm.
322329
* Returns true if parameter could be successfully added, or false if the parameter could not be added (e.g.

tests/src/core/testqgsprocessing.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class DummyAlgorithm : public QgsProcessingAlgorithm
4545
QVariantMap processAlgorithm( const QVariantMap &, QgsProcessingContext &, QgsProcessingFeedback * ) override { return QVariantMap(); }
4646

4747
virtual Flags flags() const override { return mFlags; }
48-
DummyAlgorithm *create() const override { return new DummyAlgorithm( name() ); }
48+
DummyAlgorithm *createInstance() const override { return new DummyAlgorithm( name() ); }
4949

5050
QString mName;
5151

@@ -334,6 +334,7 @@ class TestQgsProcessing: public QObject
334334
void modelAcceptableValues();
335335
void tempUtils();
336336
void convertCompatible();
337+
void create();
337338

338339
private:
339340

@@ -5126,5 +5127,16 @@ void TestQgsProcessing::convertCompatible()
51265127
QVERIFY( out.startsWith( QgsProcessingUtils::tempFolder() ) );
51275128
}
51285129

5130+
void TestQgsProcessing::create()
5131+
{
5132+
DummyAlgorithm alg( QStringLiteral( "test" ) );
5133+
DummyProvider p( QStringLiteral( "test_provider" ) );
5134+
alg.setProvider( &p );
5135+
5136+
std::unique_ptr< QgsProcessingAlgorithm > newInstance( alg.create() );
5137+
QVERIFY( newInstance.get() );
5138+
QCOMPARE( newInstance->provider(), &p );
5139+
}
5140+
51295141
QGSTEST_MAIN( TestQgsProcessing )
51305142
#include "testqgsprocessing.moc"

0 commit comments

Comments
 (0)