Skip to content

Commit fadaf12

Browse files
committed
Implement implicit sharing for QgsField
1 parent e12601e commit fadaf12

File tree

7 files changed

+319
-69
lines changed

7 files changed

+319
-69
lines changed

python/core/qgsfield.sip

+11-7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public:
3434
int prec = 0,
3535
QString comment = QString() );
3636

37+
/** Copy constructor
38+
*/
39+
QgsField( const QgsField& other );
40+
3741
//! Destructor
3842
~QgsField();
3943

@@ -75,9 +79,9 @@ public:
7579

7680
/**
7781
Set the field name.
78-
@param nam Name of the field
82+
@param name Name of the field
7983
*/
80-
void setName( const QString & nam );
84+
void setName( const QString& name );
8185

8286
/**
8387
Set variant type.
@@ -86,9 +90,9 @@ public:
8690

8791
/**
8892
Set the field type.
89-
@param typ Field type
93+
@param typeName Field type
9094
*/
91-
void setTypeName( const QString & typ );
95+
void setTypeName( const QString& typeName );
9296

9397
/**
9498
Set the field length.
@@ -98,15 +102,15 @@ public:
98102

99103
/**
100104
Set the field precision.
101-
@param prec Precision of the field
105+
@param precision Precision of the field
102106
*/
103-
void setPrecision( int prec );
107+
void setPrecision( int precision );
104108

105109

106110
/**
107111
Set the field comment
108112
*/
109-
void setComment( const QString & comment );
113+
void setComment( const QString& comment );
110114

111115
/** Formats string for display*/
112116
QString displayString( const QVariant& v ) const;

src/core/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ SET(QGIS_CORE_HDRS
504504
qgsfeaturerequest.h
505505
qgsfeaturestore.h
506506
qgsfield.h
507+
qgsfield_p.h
507508
qgsfontutils.h
508509
qgsgeometry.h
509510
qgsgeometrycache.h

src/core/qgsfield.cpp

+42-32
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
***************************************************************************/
1616

1717
#include "qgsfield.h"
18+
#include "qgsfield_p.h"
1819

1920
#include <QSettings>
2021
#include <QtCore/qmath.h>
@@ -34,9 +35,20 @@ QgsField::QgsField( QString nam, QString typ, int len, int prec, bool num,
3435
#endif
3536

3637
QgsField::QgsField( QString name, QVariant::Type type, QString typeName, int len, int prec, QString comment )
37-
: mName( name ), mType( type ), mTypeName( typeName )
38-
, mLength( len ), mPrecision( prec ), mComment( comment )
3938
{
39+
d = new QgsFieldPrivate( name, type, typeName, len, prec, comment );
40+
}
41+
42+
QgsField::QgsField( const QgsField &other )
43+
: d( other.d )
44+
{
45+
46+
}
47+
48+
QgsField &QgsField::operator =( const QgsField & other )
49+
{
50+
d = other.d;
51+
return *this;
4052
}
4153

4254

@@ -46,73 +58,71 @@ QgsField::~QgsField()
4658

4759
bool QgsField::operator==( const QgsField& other ) const
4860
{
49-
return (( mName == other.mName ) && ( mType == other.mType )
50-
&& ( mLength == other.mLength ) && ( mPrecision == other.mPrecision ) );
61+
return *( other.d ) == *d;
5162
}
5263

5364
bool QgsField::operator!=( const QgsField& other ) const
5465
{
5566
return !( *this == other );
5667
}
5768

58-
5969
const QString & QgsField::name() const
6070
{
61-
return mName;
71+
return d->name;
6272
}
6373

6474
QVariant::Type QgsField::type() const
6575
{
66-
return mType;
76+
return d->type;
6777
}
6878

6979
const QString & QgsField::typeName() const
7080
{
71-
return mTypeName;
81+
return d->typeName;
7282
}
7383

7484
int QgsField::length() const
7585
{
76-
return mLength;
86+
return d->length;
7787
}
7888

7989
int QgsField::precision() const
8090
{
81-
return mPrecision;
91+
return d->precision;
8292
}
8393

8494
const QString & QgsField::comment() const
8595
{
86-
return mComment;
96+
return d->comment;
8797
}
8898

89-
void QgsField::setName( const QString & nam )
99+
void QgsField::setName( const QString& name )
90100
{
91-
mName = nam;
101+
d->name = name;
92102
}
93103

94104
void QgsField::setType( QVariant::Type type )
95105
{
96-
mType = type;
106+
d->type = type;
97107
}
98108

99-
void QgsField::setTypeName( const QString & typeName )
109+
void QgsField::setTypeName( const QString& typeName )
100110
{
101-
mTypeName = typeName;
111+
d->typeName = typeName;
102112
}
103113

104114
void QgsField::setLength( int len )
105115
{
106-
mLength = len;
116+
d->length = len;
107117
}
108-
void QgsField::setPrecision( int prec )
118+
void QgsField::setPrecision( int precision )
109119
{
110-
mPrecision = prec;
120+
d->precision = precision;
111121
}
112122

113-
void QgsField::setComment( const QString & comment )
123+
void QgsField::setComment( const QString& comment )
114124
{
115-
mComment = comment;
125+
d->comment = comment;
116126
}
117127

118128
QString QgsField::displayString( const QVariant& v ) const
@@ -123,8 +133,8 @@ QString QgsField::displayString( const QVariant& v ) const
123133
return settings.value( "qgis/nullValue", "NULL" ).toString();
124134
}
125135

126-
if ( mType == QVariant::Double && mPrecision > 0 )
127-
return QString::number( v.toDouble(), 'f', mPrecision );
136+
if ( d->type == QVariant::Double && d->precision > 0 )
137+
return QString::number( v.toDouble(), 'f', d->precision );
128138

129139
return v.toString();
130140
}
@@ -133,33 +143,33 @@ bool QgsField::convertCompatible( QVariant& v ) const
133143
{
134144
if ( v.isNull() )
135145
{
136-
v.convert( mType );
146+
v.convert( d->type );
137147
return true;
138148
}
139149

140-
if ( mType == QVariant::Int && v.toInt() != v.toLongLong() )
150+
if ( d->type == QVariant::Int && v.toInt() != v.toLongLong() )
141151
{
142-
v = QVariant( mType );
152+
v = QVariant( d->type );
143153
return false;
144154
}
145155

146-
if ( !v.convert( mType ) )
156+
if ( !v.convert( d->type ) )
147157
{
148-
v = QVariant( mType );
158+
v = QVariant( d->type );
149159
return false;
150160
}
151161

152-
if ( mType == QVariant::Double && mPrecision > 0 )
162+
if ( d->type == QVariant::Double && d->precision > 0 )
153163
{
154-
double s = qPow( 10, mPrecision );
164+
double s = qPow( 10, d->precision );
155165
double d = v.toDouble() * s;
156166
v = QVariant(( d < 0 ? ceil( d - 0.5 ) : floor( d + 0.5 ) ) / s );
157167
return true;
158168
}
159169

160-
if ( mType == QVariant::String && mLength > 0 && v.toString().length() > mLength )
170+
if ( d->type == QVariant::String && d->length > 0 && v.toString().length() > d->length )
161171
{
162-
v = v.toString().left( mLength );
172+
v = v.toString().left( d->length );
163173
return false;
164174
}
165175

src/core/qgsfield.h

+21-30
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
#include <QString>
2020
#include <QVariant>
2121
#include <QVector>
22+
#include <QSharedDataPointer>
2223

2324
typedef QList<int> QgsAttributeList;
2425

2526
class QgsExpression;
27+
class QgsFieldPrivate;
2628

2729
/** \ingroup core
2830
* Encapsulate a field in an attribute table or data source.
@@ -52,14 +54,22 @@ class CORE_EXPORT QgsField
5254
int prec = 0,
5355
QString comment = QString() );
5456

57+
/** Copy constructor
58+
*/
59+
QgsField( const QgsField& other );
60+
61+
/** Assignment operator
62+
*/
63+
QgsField& operator =( const QgsField &other );
64+
5565
//! Destructor
5666
~QgsField();
5767

5868
bool operator==( const QgsField& other ) const;
5969
bool operator!=( const QgsField& other ) const;
6070

6171
//! Gets the name of the field
62-
const QString & name() const;
72+
const QString& name() const;
6373

6474
//! Gets variant type of the field as it will be retrieved from data source
6575
QVariant::Type type() const;
@@ -70,16 +80,14 @@ class CORE_EXPORT QgsField
7080
the data store reports it, with no attempt to standardize the value.
7181
@return QString containing the field type
7282
*/
73-
const QString & typeName() const;
74-
83+
const QString& typeName() const;
7584

7685
/**
7786
Gets the length of the field.
7887
@return int containing the length of the field
7988
*/
8089
int length() const;
8190

82-
8391
/**
8492
Gets the precision of the field. Not all field types have a related precision.
8593
@return int containing the precision or zero if not applicable to the field type.
@@ -89,13 +97,13 @@ class CORE_EXPORT QgsField
8997
/**
9098
Returns the field comment
9199
*/
92-
const QString & comment() const;
100+
const QString& comment() const;
93101

94102
/**
95103
Set the field name.
96-
@param nam Name of the field
104+
@param name Name of the field
97105
*/
98-
void setName( const QString & nam );
106+
void setName( const QString& name );
99107

100108
/**
101109
Set variant type.
@@ -104,9 +112,9 @@ class CORE_EXPORT QgsField
104112

105113
/**
106114
Set the field type.
107-
@param typ Field type
115+
@param typeName Field type
108116
*/
109-
void setTypeName( const QString & typ );
117+
void setTypeName( const QString& typeName );
110118

111119
/**
112120
Set the field length.
@@ -116,15 +124,14 @@ class CORE_EXPORT QgsField
116124

117125
/**
118126
Set the field precision.
119-
@param prec Precision of the field
127+
@param precision Precision of the field
120128
*/
121-
void setPrecision( int prec );
122-
129+
void setPrecision( int precision );
123130

124131
/**
125132
Set the field comment
126133
*/
127-
void setComment( const QString & comment );
134+
void setComment( const QString& comment );
128135

129136
/** Formats string for display*/
130137
QString displayString( const QVariant& v ) const;
@@ -140,23 +147,7 @@ class CORE_EXPORT QgsField
140147

141148
private:
142149

143-
//! Name
144-
QString mName;
145-
146-
//! Variant type
147-
QVariant::Type mType;
148-
149-
//! Type name from provider
150-
QString mTypeName;
151-
152-
//! Length
153-
int mLength;
154-
155-
//! Precision
156-
int mPrecision;
157-
158-
//! Comment
159-
QString mComment;
150+
QSharedDataPointer<QgsFieldPrivate> d;
160151

161152
}; // class QgsField
162153

0 commit comments

Comments
 (0)