Skip to content
Permalink
Browse files

Implement implicit sharing for QgsField

  • Loading branch information
nyalldawson committed May 1, 2015
1 parent e12601e commit fadaf124909e40225f824918c788bf73a104ce73
@@ -34,6 +34,10 @@ public:
int prec = 0,
QString comment = QString() );

/** Copy constructor
*/
QgsField( const QgsField& other );

//! Destructor
~QgsField();

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

/**
Set the field name.
@param nam Name of the field
@param name Name of the field
*/
void setName( const QString & nam );
void setName( const QString& name );

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

/**
Set the field type.
@param typ Field type
@param typeName Field type
*/
void setTypeName( const QString & typ );
void setTypeName( const QString& typeName );

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

/**
Set the field precision.
@param prec Precision of the field
@param precision Precision of the field
*/
void setPrecision( int prec );
void setPrecision( int precision );


/**
Set the field comment
*/
void setComment( const QString & comment );
void setComment( const QString& comment );

/** Formats string for display*/
QString displayString( const QVariant& v ) const;
@@ -504,6 +504,7 @@ SET(QGIS_CORE_HDRS
qgsfeaturerequest.h
qgsfeaturestore.h
qgsfield.h
qgsfield_p.h
qgsfontutils.h
qgsgeometry.h
qgsgeometrycache.h
@@ -15,6 +15,7 @@
***************************************************************************/

#include "qgsfield.h"
#include "qgsfield_p.h"

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

QgsField::QgsField( QString name, QVariant::Type type, QString typeName, int len, int prec, QString comment )
: mName( name ), mType( type ), mTypeName( typeName )
, mLength( len ), mPrecision( prec ), mComment( comment )
{
d = new QgsFieldPrivate( name, type, typeName, len, prec, comment );
}

QgsField::QgsField( const QgsField &other )
: d( other.d )
{

}

QgsField &QgsField::operator =( const QgsField & other )
{
d = other.d;
return *this;
}


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

bool QgsField::operator==( const QgsField& other ) const
{
return (( mName == other.mName ) && ( mType == other.mType )
&& ( mLength == other.mLength ) && ( mPrecision == other.mPrecision ) );
return *( other.d ) == *d;
}

bool QgsField::operator!=( const QgsField& other ) const
{
return !( *this == other );
}


const QString & QgsField::name() const
{
return mName;
return d->name;
}

QVariant::Type QgsField::type() const
{
return mType;
return d->type;
}

const QString & QgsField::typeName() const
{
return mTypeName;
return d->typeName;
}

int QgsField::length() const
{
return mLength;
return d->length;
}

int QgsField::precision() const
{
return mPrecision;
return d->precision;
}

const QString & QgsField::comment() const
{
return mComment;
return d->comment;
}

void QgsField::setName( const QString & nam )
void QgsField::setName( const QString& name )
{
mName = nam;
d->name = name;
}

void QgsField::setType( QVariant::Type type )
{
mType = type;
d->type = type;
}

void QgsField::setTypeName( const QString & typeName )
void QgsField::setTypeName( const QString& typeName )
{
mTypeName = typeName;
d->typeName = typeName;
}

void QgsField::setLength( int len )
{
mLength = len;
d->length = len;
}
void QgsField::setPrecision( int prec )
void QgsField::setPrecision( int precision )
{
mPrecision = prec;
d->precision = precision;
}

void QgsField::setComment( const QString & comment )
void QgsField::setComment( const QString& comment )
{
mComment = comment;
d->comment = comment;
}

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

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

return v.toString();
}
@@ -133,33 +143,33 @@ bool QgsField::convertCompatible( QVariant& v ) const
{
if ( v.isNull() )
{
v.convert( mType );
v.convert( d->type );
return true;
}

if ( mType == QVariant::Int && v.toInt() != v.toLongLong() )
if ( d->type == QVariant::Int && v.toInt() != v.toLongLong() )
{
v = QVariant( mType );
v = QVariant( d->type );
return false;
}

if ( !v.convert( mType ) )
if ( !v.convert( d->type ) )
{
v = QVariant( mType );
v = QVariant( d->type );
return false;
}

if ( mType == QVariant::Double && mPrecision > 0 )
if ( d->type == QVariant::Double && d->precision > 0 )
{
double s = qPow( 10, mPrecision );
double s = qPow( 10, d->precision );
double d = v.toDouble() * s;
v = QVariant(( d < 0 ? ceil( d - 0.5 ) : floor( d + 0.5 ) ) / s );
return true;
}

if ( mType == QVariant::String && mLength > 0 && v.toString().length() > mLength )
if ( d->type == QVariant::String && d->length > 0 && v.toString().length() > d->length )
{
v = v.toString().left( mLength );
v = v.toString().left( d->length );
return false;
}

@@ -19,10 +19,12 @@
#include <QString>
#include <QVariant>
#include <QVector>
#include <QSharedDataPointer>

typedef QList<int> QgsAttributeList;

class QgsExpression;
class QgsFieldPrivate;

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

/** Copy constructor
*/
QgsField( const QgsField& other );

/** Assignment operator
*/
QgsField& operator =( const QgsField &other );

//! Destructor
~QgsField();

bool operator==( const QgsField& other ) const;
bool operator!=( const QgsField& other ) const;

//! Gets the name of the field
const QString & name() const;
const QString& name() const;

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

const QString& typeName() const;

/**
Gets the length of the field.
@return int containing the length of the field
*/
int length() const;


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

/**
Set the field name.
@param nam Name of the field
@param name Name of the field
*/
void setName( const QString & nam );
void setName( const QString& name );

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

/**
Set the field type.
@param typ Field type
@param typeName Field type
*/
void setTypeName( const QString & typ );
void setTypeName( const QString& typeName );

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

/**
Set the field precision.
@param prec Precision of the field
@param precision Precision of the field
*/
void setPrecision( int prec );

void setPrecision( int precision );

/**
Set the field comment
*/
void setComment( const QString & comment );
void setComment( const QString& comment );

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

private:

//! Name
QString mName;

//! Variant type
QVariant::Type mType;

//! Type name from provider
QString mTypeName;

//! Length
int mLength;

//! Precision
int mPrecision;

//! Comment
QString mComment;
QSharedDataPointer<QgsFieldPrivate> d;

}; // class QgsField

0 comments on commit fadaf12

Please sign in to comment.
You can’t perform that action at this time.