Skip to content

Commit 1194b5a

Browse files
committed
Use inheritance rather than composition for QgsReferencedGeometries
See #4720 (comment) for discussion of the rationale
1 parent e926f34 commit 1194b5a

7 files changed

+94
-60
lines changed

python/core/geometry/qgsrectangle.sip

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ Construct a rectangle from a QRectF. The rectangle is normalized after construct
4242
Copy constructor
4343
%End
4444

45+
~QgsRectangle();
46+
4547
void set( const QgsPointXY &p1, const QgsPointXY &p2 );
4648
%Docstring
4749
Sets the rectangle from two QgsPoints. The rectangle is

python/core/geometry/qgsreferencedgeometry.sip

+26-28
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,27 @@
99

1010

1111

12-
template<T>
13-
class QgsReferencedGeometryPrimitive
12+
class QgsReferencedGeometryBase
1413
{
1514
%Docstring
16-
A template based class for storing geometry primitives with an associated reference system.
15+
A base class for geometry primitives which are stored with an associated reference system.
1716

18-
QgsReferencedGeometryPrimitive classes represent some form of geometry primitive
17+
QgsReferencedGeometryBase classes represent some form of geometry primitive
1918
(such as rectangles) which have an optional coordinate reference system
2019
associated with them.
2120

2221
.. versionadded:: 3.0
2322
.. seealso:: QgsReferencedRectangle
24-
.. note::
25-
26-
Not available in Python bindings (although SIP file is present for specific implementations).
2723
%End
2824

2925
%TypeHeaderCode
3026
#include "qgsreferencedgeometry.h"
3127
%End
3228
public:
3329

34-
QgsReferencedGeometryPrimitive( T primitive, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
35-
%Docstring
36-
Constructor for QgsReferencedGeometryPrimitive, for the specified ``primitive`` and ``crs``.
37-
%End
38-
39-
40-
T &primitive();
30+
QgsReferencedGeometryBase( const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
4131
%Docstring
42-
Returns the geometry primitive.
43-
:rtype: T
32+
Constructor for QgsReferencedGeometryBase, with the specified ``crs``.
4433
%End
4534

4635
QgsCoordinateReferenceSystem crs() const;
@@ -60,11 +49,7 @@ class QgsReferencedGeometryPrimitive
6049

6150
};
6251

63-
64-
65-
typedef QgsReferencedGeometryPrimitive<QgsRectangle> QgsReferencedGeometryPrimitiveQgsRectangleBase;
66-
67-
class QgsReferencedRectangle : QgsReferencedGeometryPrimitiveQgsRectangleBase
52+
class QgsReferencedRectangle : QgsRectangle, QgsReferencedGeometryBase
6853
{
6954
%Docstring
7055
A QgsRectangle with associated coordinate reference system.
@@ -73,20 +58,33 @@ class QgsReferencedRectangle : QgsReferencedGeometryPrimitiveQgsRectangleBase
7358

7459
%TypeHeaderCode
7560
#include "qgsreferencedgeometry.h"
76-
typedef QgsReferencedGeometryPrimitive<QgsRectangle> QgsReferencedGeometryPrimitiveQgsRectangleBase;
7761
%End
7862
public:
7963

80-
QgsReferencedRectangle( const QgsRectangle &rect );
64+
QgsReferencedRectangle( const QgsRectangle &rectangle, const QgsCoordinateReferenceSystem &crs );
65+
%Docstring
66+
Constructor for QgsReferencedRectangle, with the specified initial ``rectangle``
67+
and ``crs``.
68+
%End
69+
70+
};
71+
72+
class QgsReferencedPointXY : QgsPointXY, QgsReferencedGeometryBase
73+
{
8174
%Docstring
82-
Construct a default optional expression.
83-
It will be disabled and with an empty expression.
75+
A QgsPointXY with associated coordinate reference system.
76+
.. versionadded:: 3.0
8477
%End
8578

86-
QgsRectangle &rect();
79+
%TypeHeaderCode
80+
#include "qgsreferencedgeometry.h"
81+
%End
82+
public:
83+
84+
QgsReferencedPointXY( const QgsPointXY &point, const QgsCoordinateReferenceSystem &crs );
8785
%Docstring
88-
Returns the rectangles
89-
:rtype: QgsRectangle
86+
Constructor for QgsReferencedPointXY, with the specified initial ``point``
87+
and ``crs``.
9088
%End
9189

9290
};

src/core/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ SET(QGIS_CORE_SRCS
447447
geometry/qgspoint.cpp
448448
geometry/qgspolygon.cpp
449449
geometry/qgsrectangle.cpp
450+
geometry/qgsreferencedgeometry.cpp
450451
geometry/qgsregularpolygon.cpp
451452
geometry/qgstriangle.cpp
452453
geometry/qgswkbptr.cpp

src/core/geometry/qgsrectangle.h

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ class CORE_EXPORT QgsRectangle
4747
//! Copy constructor
4848
QgsRectangle( const QgsRectangle &other );
4949

50+
// IMPORTANT - while QgsRectangle is inherited by QgsReferencedRectangle, we do NOT want a virtual destructor here
51+
// because this class MUST be lightweight and we don't want the cost of the vtable here.
52+
// see https://github.com/qgis/QGIS/pull/4720#issuecomment-308652392
53+
~QgsRectangle() = default;
54+
5055
/**
5156
* Sets the rectangle from two QgsPoints. The rectangle is
5257
* normalised after construction.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/***************************************************************************
2+
qgsreferencedgeometry.cpp
3+
------------------------
4+
begin : June 2017
5+
copyright : (C) 2017 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#include "qgsreferencedgeometry.h"
19+
20+
QgsReferencedGeometryBase::QgsReferencedGeometryBase( const QgsCoordinateReferenceSystem &crs )
21+
: mCrs( crs )
22+
{}
23+
24+
QgsReferencedRectangle::QgsReferencedRectangle( const QgsRectangle &rect, const QgsCoordinateReferenceSystem &crs )
25+
: QgsRectangle( rect )
26+
, QgsReferencedGeometryBase( crs )
27+
{}
28+
29+
QgsReferencedPointXY::QgsReferencedPointXY( const QgsPointXY &point, const QgsCoordinateReferenceSystem &crs )
30+
: QgsPointXY( point )
31+
, QgsReferencedGeometryBase( crs )
32+
{}

src/core/geometry/qgsreferencedgeometry.h

+24-30
Original file line numberDiff line numberDiff line change
@@ -25,40 +25,25 @@
2525
#include "qgsrectangle.h"
2626

2727
/**
28-
* \class QgsReferencedGeometryPrimitive
28+
* \class QgsReferencedGeometryBase
2929
* \ingroup core
30-
* A template based class for storing geometry primitives with an associated reference system.
30+
* A base class for geometry primitives which are stored with an associated reference system.
3131
*
32-
* QgsReferencedGeometryPrimitive classes represent some form of geometry primitive
32+
* QgsReferencedGeometryBase classes represent some form of geometry primitive
3333
* (such as rectangles) which have an optional coordinate reference system
3434
* associated with them.
3535
*
3636
* \since QGIS 3.0
3737
* \see QgsReferencedRectangle
38-
* \note Not available in Python bindings (although SIP file is present for specific implementations).
3938
*/
40-
template<typename T>
41-
class CORE_EXPORT QgsReferencedGeometryPrimitive
39+
class CORE_EXPORT QgsReferencedGeometryBase
4240
{
4341
public:
4442

4543
/**
46-
* Constructor for QgsReferencedGeometryPrimitive, for the specified \a primitive and \a crs.
44+
* Constructor for QgsReferencedGeometryBase, with the specified \a crs.
4745
*/
48-
QgsReferencedGeometryPrimitive( T primitive, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() )
49-
: mPrimitive( primitive )
50-
, mCrs( crs )
51-
{}
52-
53-
/**
54-
* Returns the geometry primitive.
55-
*/
56-
T primitive() const { return mPrimitive; } SIP_SKIP
57-
58-
/**
59-
* Returns the geometry primitive.
60-
*/
61-
T &primitive() { return mPrimitive; }
46+
QgsReferencedGeometryBase( const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
6247

6348
/**
6449
* Returns the associated coordinate reference system, or an invalid CRS if
@@ -76,32 +61,41 @@ class CORE_EXPORT QgsReferencedGeometryPrimitive
7661

7762
private:
7863

79-
T mPrimitive;
8064
QgsCoordinateReferenceSystem mCrs;
8165

8266
};
8367

84-
//template class QgsReferencedGeometryPrimitive< QgsRectangle > QgsReferencedRectangle;
85-
8668
/**
8769
* \ingroup core
8870
* A QgsRectangle with associated coordinate reference system.
8971
* \since QGIS 3.0
9072
*/
91-
class CORE_EXPORT QgsReferencedRectangle : public QgsReferencedGeometryPrimitive< QgsRectangle >
73+
class CORE_EXPORT QgsReferencedRectangle : public QgsRectangle, public QgsReferencedGeometryBase
9274
{
9375
public:
9476

9577
/**
96-
* Construct a default optional expression.
97-
* It will be disabled and with an empty expression.
78+
* Constructor for QgsReferencedRectangle, with the specified initial \a rectangle
79+
* and \a crs.
9880
*/
99-
QgsReferencedRectangle( const QgsRectangle &rect ) : QgsReferencedGeometryPrimitive( rect ) {}
81+
QgsReferencedRectangle( const QgsRectangle &rectangle, const QgsCoordinateReferenceSystem &crs );
82+
83+
};
84+
85+
/**
86+
* \ingroup core
87+
* A QgsPointXY with associated coordinate reference system.
88+
* \since QGIS 3.0
89+
*/
90+
class CORE_EXPORT QgsReferencedPointXY : public QgsPointXY, public QgsReferencedGeometryBase
91+
{
92+
public:
10093

10194
/**
102-
* Returns the rectangles
95+
* Constructor for QgsReferencedPointXY, with the specified initial \a point
96+
* and \a crs.
10397
*/
104-
QgsRectangle &rect() { return primitive(); }
98+
QgsReferencedPointXY( const QgsPointXY &point, const QgsCoordinateReferenceSystem &crs );
10599

106100
};
107101

src/core/qgspointxy.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,10 @@ class CORE_EXPORT QgsPointXY
9191
*/
9292
QgsPointXY( const QgsPoint &point );
9393

94-
~QgsPointXY()
95-
{}
94+
// IMPORTANT - while QgsPointXY is inherited by QgsReferencedPointXY, we do NOT want a virtual destructor here
95+
// because this class MUST be lightweight and we don't want the cost of the vtable here.
96+
// see https://github.com/qgis/QGIS/pull/4720#issuecomment-308652392
97+
~QgsPointXY() = default;
9698

9799
/** Sets the x value of the point
98100
* \param x x coordinate

0 commit comments

Comments
 (0)