Qt'fy access to pimpl objects: use macros Q_DECLARE_PRIVATE & Q_D

Summary:
* enforces constness some more
  (see issue spotted with GeoDataTrack::extendedData())
* delivers inline-ness of the access methods
* code should be more familiar to Qt developers

Reviewers: #marble, nienhueser

Reviewed By: #marble, nienhueser

Subscribers: nienhueser

Differential Revision: https://phabricator.kde.org/D2951
parent 92e6692c
......@@ -28,102 +28,102 @@ namespace Marble
{
GeoDataGeometry::GeoDataGeometry()
: d( new GeoDataGeometryPrivate() )
: d_ptr(new GeoDataGeometryPrivate())
{
d->ref.ref();
d_ptr->ref.ref();
}
GeoDataGeometry::GeoDataGeometry( const GeoDataGeometry& other )
: GeoDataObject(),
d( other.d )
d_ptr(other.d_ptr)
{
d->ref.ref();
d_ptr->ref.ref();
}
GeoDataGeometry::GeoDataGeometry( GeoDataGeometryPrivate* priv )
: GeoDataObject(),
d( priv )
d_ptr(priv)
{
d->ref.ref();
d_ptr->ref.ref();
}
GeoDataGeometry::~GeoDataGeometry()
{
if (!d->ref.deref())
delete d;
if (!d_ptr->ref.deref())
delete d_ptr;
}
void GeoDataGeometry::detach()
{
if(d->ref.load() == 1) {
if(d_ptr->ref.load() == 1) {
return;
}
GeoDataGeometryPrivate* new_d = d->copy();
GeoDataGeometryPrivate* new_d = d_ptr->copy();
if (!d->ref.deref())
delete d;
if (!d_ptr->ref.deref())
delete d_ptr;
d = new_d;
d->ref.ref();
d_ptr = new_d;
d_ptr->ref.ref();
}
const char* GeoDataGeometry::nodeType() const
{
return d->nodeType();
return d_ptr->nodeType();
}
EnumGeometryId GeoDataGeometry::geometryId() const
{
return d->geometryId();
return d_ptr->geometryId();
}
GeoDataGeometry& GeoDataGeometry::operator=( const GeoDataGeometry& other )
{
GeoDataObject::operator=( other );
if (!d->ref.deref())
delete d;
if (!d_ptr->ref.deref())
delete d_ptr;
d = other.d;
d->ref.ref();
d_ptr = other.d_ptr;
d_ptr->ref.ref();
return *this;
}
bool GeoDataGeometry::extrude() const
{
return d->m_extrude;
return d_ptr->m_extrude;
}
void GeoDataGeometry::setExtrude( bool extrude )
{
detach();
d->m_extrude = extrude;
d_ptr->m_extrude = extrude;
}
AltitudeMode GeoDataGeometry::altitudeMode() const
{
return d->m_altitudeMode;
return d_ptr->m_altitudeMode;
}
void GeoDataGeometry::setAltitudeMode( const AltitudeMode altitudeMode )
{
detach();
d->m_altitudeMode = altitudeMode;
d_ptr->m_altitudeMode = altitudeMode;
}
const GeoDataLatLonAltBox& GeoDataGeometry::latLonAltBox() const
{
return d->m_latLonAltBox;
return d_ptr->m_latLonAltBox;
}
void GeoDataGeometry::pack( QDataStream& stream ) const
{
GeoDataObject::pack( stream );
stream << d->m_extrude;
stream << d->m_altitudeMode;
stream << d_ptr->m_extrude;
stream << d_ptr->m_altitudeMode;
}
void GeoDataGeometry::unpack( QDataStream& stream )
......@@ -132,16 +132,16 @@ void GeoDataGeometry::unpack( QDataStream& stream )
GeoDataObject::unpack( stream );
int am;
stream >> d->m_extrude;
stream >> d_ptr->m_extrude;
stream >> am;
d->m_altitudeMode = (AltitudeMode) am;
d_ptr->m_altitudeMode = (AltitudeMode) am;
}
bool GeoDataGeometry::equals(const GeoDataGeometry &other) const
{
return GeoDataObject::equals(other) &&
d->m_extrude == other.d->m_extrude &&
d->m_altitudeMode == other.d->m_altitudeMode;
d_ptr->m_extrude == other.d_ptr->m_extrude &&
d_ptr->m_altitudeMode == other.d_ptr->m_altitudeMode;
}
}
......@@ -76,7 +76,7 @@ class GEODATA_EXPORT GeoDataGeometry : public GeoDataObject
using GeoDataObject::equals;
protected:
GeoDataGeometryPrivate *d;
GeoDataGeometryPrivate *d_ptr;
};
}
......
......@@ -385,8 +385,7 @@ class GEODATA_EXPORT GeoDataLineString : public GeoDataGeometry
explicit GeoDataLineString(GeoDataLineStringPrivate* priv);
private:
inline GeoDataLineStringPrivate *p();
inline const GeoDataLineStringPrivate *p() const;
Q_DECLARE_PRIVATE(GeoDataLineString)
};
}
......
......@@ -78,15 +78,18 @@ GeoDataModel &GeoDataModel::operator=( const GeoDataModel &other )
bool GeoDataModel::operator==( const GeoDataModel &other ) const
{
Q_D(const GeoDataModel);
const GeoDataModelPrivate *other_d = other.d_func();
return equals(other) &&
p()->m_coordinates == other.p()->m_coordinates &&
p()->m_scale == other.p()->m_scale &&
p()->m_orientation == other.p()->m_orientation &&
p()->m_location == other.p()->m_location &&
p()->m_link == other.p()->m_link &&
p()->m_map == other.p()->m_map &&
p()->m_targetHref == other.p()->m_targetHref &&
p()->m_sourceHref == other.p()->m_sourceHref;
d->m_coordinates == other_d->m_coordinates &&
d->m_scale == other_d->m_scale &&
d->m_orientation == other_d->m_orientation &&
d->m_location == other_d->m_location &&
d->m_link == other_d->m_link &&
d->m_map == other_d->m_map &&
d->m_targetHref == other_d->m_targetHref &&
d->m_sourceHref == other_d->m_sourceHref;
}
bool GeoDataModel::operator!=( const GeoDataModel &other ) const
......@@ -100,133 +103,162 @@ GeoDataModel::~GeoDataModel()
const GeoDataCoordinates &GeoDataModel::coordinates() const
{
return p()->m_coordinates;
Q_D(const GeoDataModel);
return d->m_coordinates;
}
GeoDataCoordinates &GeoDataModel::coordinates()
{
return p()->m_coordinates;
detach();
Q_D(GeoDataModel);
return d->m_coordinates;
}
const GeoDataLocation &GeoDataModel::location() const
{
return p()->m_location;
Q_D(const GeoDataModel);
return d->m_location;
}
GeoDataLocation &GeoDataModel::location()
{
return p()->m_location;
detach();
Q_D(GeoDataModel);
return d->m_location;
}
void GeoDataModel::setCoordinates(const GeoDataCoordinates &coordinates)
{
detach();
p()->m_coordinates = coordinates;
Q_D(GeoDataModel);
d->m_coordinates = coordinates;
}
void GeoDataModel::setLocation(const GeoDataLocation &location)
{
detach();
p()->m_location = location;
Q_D(GeoDataModel);
d->m_location = location;
}
const GeoDataLink &GeoDataModel::link() const
{
return p()->m_link;
Q_D(const GeoDataModel);
return d->m_link;
}
GeoDataLink &GeoDataModel::link()
{
return p()->m_link;
detach();
Q_D(GeoDataModel);
return d->m_link;
}
void GeoDataModel::setLink( const GeoDataLink &link )
{
detach();
p()->m_link = link;
Q_D(GeoDataModel);
d->m_link = link;
}
const GeoDataScale &GeoDataModel::scale() const
{
return p()->m_scale;
Q_D(const GeoDataModel);
return d->m_scale;
}
GeoDataScale &GeoDataModel::scale()
{
return p()->m_scale;
detach();
Q_D(GeoDataModel);
return d->m_scale;
}
void GeoDataModel::setScale(const GeoDataScale &scale)
{
detach();
p()->m_scale=scale;
Q_D(GeoDataModel);
d->m_scale = scale;
}
const GeoDataOrientation &GeoDataModel::orientation() const
{
return p()->m_orientation;
Q_D(const GeoDataModel);
return d->m_orientation;
}
GeoDataOrientation &GeoDataModel::orientation()
{
return p()->m_orientation;
detach();
Q_D(GeoDataModel);
return d->m_orientation;
}
void GeoDataModel::setOrientation(const GeoDataOrientation &orientation)
{
detach();
p()->m_orientation=orientation;
Q_D(GeoDataModel);
d->m_orientation = orientation;
}
const GeoDataResourceMap &GeoDataModel::resourceMap() const
{
return p()->m_map;
Q_D(const GeoDataModel);
return d->m_map;
}
GeoDataResourceMap &GeoDataModel::resourceMap()
{
return p()->m_map;
detach();
Q_D(GeoDataModel);
return d->m_map;
}
void GeoDataModel::setResourceMap(const GeoDataResourceMap &map)
{
detach();
p()->m_map=map;
Q_D(GeoDataModel);
d->m_map = map;
}
QString GeoDataModel::targetHref() const
{
return p()->m_map.targetHref();
Q_D(const GeoDataModel);
return d->m_map.targetHref();
}
void GeoDataModel::setTargetHref(const QString &targetHref)
{
detach();
p()->m_map.setTargetHref( targetHref );
Q_D(GeoDataModel);
d->m_map.setTargetHref( targetHref );
}
QString GeoDataModel::sourceHref() const
{
return p()->m_map.sourceHref();
Q_D(const GeoDataModel);
return d->m_map.sourceHref();
}
void GeoDataModel::setSourceHref(const QString &sourceHref)
{
detach();
p()->m_map.setSourceHref( sourceHref );
}
GeoDataModelPrivate *GeoDataModel::p() const
{
return static_cast<GeoDataModelPrivate *>( d );
Q_D(GeoDataModel);
d->m_map.setSourceHref( sourceHref );
}
}
......@@ -70,7 +70,7 @@ public:
void setTargetHref(const QString &targetHref);
private:
GeoDataModelPrivate *p() const;
Q_DECLARE_PRIVATE(GeoDataModel)
};
}
......
......@@ -39,46 +39,40 @@ GeoDataMultiGeometry::~GeoDataMultiGeometry()
{
}
GeoDataMultiGeometryPrivate* GeoDataMultiGeometry::p()
{
return static_cast<GeoDataMultiGeometryPrivate*>(d);
}
const GeoDataMultiGeometryPrivate* GeoDataMultiGeometry::p() const
{
return static_cast<GeoDataMultiGeometryPrivate*>(d);
}
const GeoDataLatLonAltBox& GeoDataMultiGeometry::latLonAltBox() const
{
QVector<GeoDataGeometry*>::const_iterator it = p()->m_vector.constBegin();
QVector<GeoDataGeometry*>::const_iterator end = p()->m_vector.constEnd();
Q_D(const GeoDataMultiGeometry);
p()->m_latLonAltBox.clear();
QVector<GeoDataGeometry*>::const_iterator it = d->m_vector.constBegin();
QVector<GeoDataGeometry*>::const_iterator end = d->m_vector.constEnd();
d->m_latLonAltBox.clear();
for (; it != end; ++it) {
if ( !(*it)->latLonAltBox().isEmpty() ) {
if ( p()->m_latLonAltBox.isEmpty() ) {
p()->m_latLonAltBox = (*it)->latLonAltBox();
if ( d->m_latLonAltBox.isEmpty() ) {
d->m_latLonAltBox = (*it)->latLonAltBox();
}
else {
p()->m_latLonAltBox |= (*it)->latLonAltBox();
d->m_latLonAltBox |= (*it)->latLonAltBox();
}
}
}
return p()->m_latLonAltBox;
return d->m_latLonAltBox;
}
int GeoDataMultiGeometry::size() const
{
return p()->m_vector.size();
Q_D(const GeoDataMultiGeometry);
return d->m_vector.size();
}
QVector<GeoDataGeometry> GeoDataMultiGeometry::vector() const
{
Q_D(const GeoDataMultiGeometry);
QVector<GeoDataGeometry> results;
QVector<GeoDataGeometry*>::const_iterator it = p()->m_vector.constBegin();
QVector<GeoDataGeometry*>::const_iterator end = p()->m_vector.constEnd();
QVector<GeoDataGeometry*>::const_iterator it = d->m_vector.constBegin();
QVector<GeoDataGeometry*>::const_iterator end = d->m_vector.constEnd();
for (; it != end; ++it) {
GeoDataGeometry f = **it;
......@@ -92,67 +86,85 @@ GeoDataGeometry& GeoDataMultiGeometry::at( int pos )
{
mDebug() << "detaching!";
detach();
return *(p()->m_vector[ pos ]);
Q_D(GeoDataMultiGeometry);
return *(d->m_vector[pos]);
}
const GeoDataGeometry& GeoDataMultiGeometry::at( int pos ) const
{
return *(p()->m_vector.at( pos ));
Q_D(const GeoDataMultiGeometry);
return *(d->m_vector.at(pos));
}
GeoDataGeometry& GeoDataMultiGeometry::operator[]( int pos )
{
detach();
return *(p()->m_vector[ pos ]);
Q_D(GeoDataMultiGeometry);
return *(d->m_vector[pos]);
}
const GeoDataGeometry& GeoDataMultiGeometry::operator[]( int pos ) const
{
return *(p()->m_vector[ pos ]);
Q_D(const GeoDataMultiGeometry);
return *(d->m_vector[pos]);
}
GeoDataGeometry& GeoDataMultiGeometry::last()
{
detach();
return *(p()->m_vector.last());
Q_D(GeoDataMultiGeometry);
return *(d->m_vector.last());
}
GeoDataGeometry& GeoDataMultiGeometry::first()
{
detach();
return *(p()->m_vector.first());
Q_D(GeoDataMultiGeometry);
return *(d->m_vector.first());
}
const GeoDataGeometry& GeoDataMultiGeometry::last() const
{
return *(p()->m_vector.last());
Q_D(const GeoDataMultiGeometry);
return *(d->m_vector.last());
}
const GeoDataGeometry& GeoDataMultiGeometry::first() const
{
return *(p()->m_vector.first());
Q_D(const GeoDataMultiGeometry);
return *(d->m_vector.first());
}
QVector<GeoDataGeometry*>::Iterator GeoDataMultiGeometry::begin()
{
detach();
return p()->m_vector.begin();
Q_D(GeoDataMultiGeometry);
return d->m_vector.begin();
}
QVector<GeoDataGeometry*>::Iterator GeoDataMultiGeometry::end()
{
detach();
return p()->m_vector.end();
Q_D(GeoDataMultiGeometry);
return d->m_vector.end();
}
QVector<GeoDataGeometry*>::ConstIterator GeoDataMultiGeometry::constBegin() const
{
return p()->m_vector.constBegin();
Q_D(const GeoDataMultiGeometry);
return d->m_vector.constBegin();
}
QVector<GeoDataGeometry*>::ConstIterator GeoDataMultiGeometry::constEnd() const
{
return p()->m_vector.constEnd();
Q_D(const GeoDataMultiGeometry);
return d->m_vector.constEnd();
}
/**
......@@ -161,12 +173,15 @@ QVector<GeoDataGeometry*>::ConstIterator GeoDataMultiGeometry::constEnd() const
GeoDataGeometry* GeoDataMultiGeometry::child( int i )
{
detach();
return p()->m_vector.at( i );
Q_D(GeoDataMultiGeometry);
return d->m_vector.at(i);
}
const GeoDataGeometry* GeoDataMultiGeometry::child( int i ) const
{
return p()->m_vector.at( i );
Q_D(const GeoDataMultiGeometry);
return d->m_vector.at(i);
}
/**
......@@ -174,10 +189,9 @@ const GeoDataGeometry* GeoDataMultiGeometry::child( int i ) const
*/
int GeoDataMultiGeometry::childPosition( const GeoDataGeometry *object ) const
{
for ( int i=0; i< p()->m_vector.size(); i++ )
{
if ( p()->m_vector.at( i ) == object )
{
Q_D(const GeoDataMultiGeometry);
for (int i = 0; i < d->m_vector.size(); ++i) {
if (d->m_vector.at(i) == object) {
return i;
}
}
......@@ -190,36 +204,44 @@ int GeoDataMultiGeometry::childPosition( const GeoDataGeometry *object ) const
void GeoDataMultiGeometry::append( GeoDataGeometry *other )
{
detach();
Q_D(GeoDataMultiGeometry);
other->setParent( this );
p()->m_vector.append( other );
d->m_vector.append(other);
}
GeoDataMultiGeometry& GeoDataMultiGeometry::operator << ( const GeoDataGeometry& value )
{
detach();
Q_D(GeoDataMultiGeometry);
GeoDataGeometry *g = new GeoDataGeometry( value );
g->setParent( this );
p()->m_vector.append( g );
d->m_vector.append(g);
return *this;
}
void GeoDataMultiGeometry::clear()
{
detach();
qDeleteAll(p()->m_vector);
p()->m_vector.clear();
Q_D(GeoDataMultiGeometry);
qDeleteAll(d->m_vector);
d->m_vector.clear();
}
void GeoDataMultiGeometry::pack( QDataStream& stream ) const
{
Q_D(const GeoDataMultiGeometry);
GeoDataGeometry::pack( stream );
stream << p()->m_vector.size();
stream << d->m_vector.size();
for( QVector<GeoDataGeometry*>::const_iterator iterator
= p()->m_vector.constBegin();
iterator != p()->m_vector.constEnd();
= d->m_vector.constBegin();
iterator != d->m_vector.constEnd();
++iterator ) {
const GeoDataGeometry *geometry = *iterator;
stream << geometry->geometryId();
......@@ -230,6 +252,8 @@ void GeoDataMultiGeometry::pack( QDataStream& stream ) const
void GeoDataMultiGeometry::unpack( QDataStream& stream )
{
detach();
Q_D(GeoDataMultiGeometry);
GeoDataGeometry::unpack( stream );
int size = 0;
......@@ -246,35 +270,35 @@ void GeoDataMultiGeometry::unpack( QDataStream& stream )
{
GeoDataPoint *point = new GeoDataPoint;
point->unpack( stream );
p()->m_vector.append( point );
d->m_vector.append(point);
}
break;
case GeoDataLineStringId:
{
GeoDataLineString *lineString = new GeoDataLineString;
lineString->unpack( stream );