Porting to QSharedPointer and co, this shares implicitly.
Result:
==8109== LEAK SUMMARY:
==8109== definitely lost: 980 bytes in 17 blocks.
==8109== indirectly lost: 14,505 bytes in 245 blocks.
==8109== possibly lost: 792 bytes in 6 bloc
svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=599978
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
-#include "chemicaldataobject.h"
-
-int ChemicalDataObject::mycount = 0;
-
#include <kdebug.h>
+#include "chemicaldataobject.h"
-class ChemicalDataObjectPrivate
+//########################
+ChemicalDataObjectPrivate::ChemicalDataObjectPrivate()
{
- public:
- QVariant m_value;
- QVariant m_errorValue;
- ChemicalDataObject::BlueObelisk m_type;
- ChemicalDataObject::BlueObeliskUnit m_unit;
+}
- ~ChemicalDataObjectPrivate();
-};
+ChemicalDataObjectPrivate::ChemicalDataObjectPrivate(const ChemicalDataObjectPrivate &other)
+: QSharedData(other)
+{
+}
ChemicalDataObjectPrivate::~ChemicalDataObjectPrivate()
{
}
+//##############
ChemicalDataObject::ChemicalDataObject( const QVariant& v, BlueObelisk type, const QVariant& errorValue )
-: d(new ChemicalDataObjectPrivate())
{
- mycount++;
- kDebug() << "CDO non-default contructor. There are now " << mycount
- << " allocated CDOs" << endl;
-
- d->m_value = v;
- d->m_errorValue = errorValue;
- d->m_type = type;
- d->m_unit = ChemicalDataObject::noUnit;
+ d = new ChemicalDataObjectPrivate;
+ d->m_value = v;
+ d->m_errorValue = errorValue;
+ d->m_type = type;
+ d->m_unit = ChemicalDataObject::noUnit;
}
ChemicalDataObject::ChemicalDataObject()
-: d(new ChemicalDataObjectPrivate())
{
- mycount++;
- kDebug() << "CDO default contructor. There are now " << mycount
- << " allocated CDOs" << endl; d->m_value = QVariant();
- d->m_errorValue = QVariant();
- d->m_unit = ChemicalDataObject::noUnit;
+ d = new ChemicalDataObjectPrivate;
+ d->m_errorValue = QVariant();
+ d->m_unit = ChemicalDataObject::noUnit;
}
bool ChemicalDataObject::operator==( const int v ) const
return d->m_value.toString() == v;
}
-ChemicalDataObject::~ChemicalDataObject()
-{
- mycount--;
- kDebug() << "CDO destructor. There are now " << mycount
- << " allocated CDOs" << endl;
- delete d;
-}
-
QString ChemicalDataObject::valueAsString() const
{
return d->m_value.toString();
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
+#include <QSharedData>
+#include <QSharedDataPointer>
#include <QVariant>
#include <QString>
#include <libkdeedu_science_export.h>
class ChemicalDataObjectPrivate;
-
/**
* A ChemicalDataObject is an object which contains information about
* a chemical element. This can for example be a boiling point. The information
* @param v the value of the object
*/
void setErrorValue( const QVariant& v );
-
- /**
- * Destructor.
- */
- ~ChemicalDataObject();
/**
* Every ChemicalDataObject contains one data. For example a
static BlueObeliskUnit unit( const QString& unitname );
private:
- ChemicalDataObjectPrivate *d;
- static int mycount;
+ QSharedDataPointer<ChemicalDataObjectPrivate> d;
};
+class ChemicalDataObjectPrivate : public QSharedData
+{
+ public:
+ ChemicalDataObjectPrivate();
+ ChemicalDataObjectPrivate(const ChemicalDataObjectPrivate &other);
+ ~ChemicalDataObjectPrivate();
+
+ QVariant m_value;
+ QVariant m_errorValue;
+ ChemicalDataObject::BlueObelisk m_type;
+ ChemicalDataObject::BlueObeliskUnit m_unit;
+};
#endif // CHEMICALDATAOBJECT_H
QVariant Element::dataAsVariant( ChemicalDataObject::BlueObelisk type ) const
{
- foreach( ChemicalDataObject* o, dataList ) {
- if ( o->type() == type )
- return o->value();
+ foreach( ChemicalDataObject o, dataList ) {
+ if ( o.type() == type )
+ return o.value();
}
return QVariant();
}
QString Element::dataAsString(ChemicalDataObject::BlueObelisk type) const
{
- foreach( ChemicalDataObject*o, dataList ) {
- if ( o->type() == type )
- return o->valueAsString();
+ foreach( ChemicalDataObject o, dataList ) {
+ if ( o.type() == type )
+ return o.valueAsString();
}
return QString();
}
Element::~Element()
{
- qDeleteAll(dataList);
-
delete m_spectrum;
m_spectrum = 0;
}
-void Element::addData( ChemicalDataObject*o )
+void Element::addData( const ChemicalDataObject& o )
{
- // skip null pointers
- if ( !o ) return;
dataList.append( o );
}
void Element::addData( const QVariant& value, ChemicalDataObject::BlueObelisk type )
{
- ChemicalDataObject* tmp = new ChemicalDataObject( value, type );
+ ChemicalDataObject tmp( value, type );
dataList.append( tmp );
}
* Add the ChemicalDataObject @p o to this Element
* @param o the ChemicalDataObject to be added
*/
- void addData( ChemicalDataObject*o );
+ void addData( const ChemicalDataObject& o );
/**
* Add a ChemicalDataObject with @p value of @p type to this
/**
* @return the data of the Element
*/
- QList<ChemicalDataObject*> data()const{
+ QList<ChemicalDataObject> data()const{
return dataList;
}
/**
* this QList stores all information about an element
*/
- QList<ChemicalDataObject*> dataList;
+ QList<ChemicalDataObject> dataList;
Spectrum *m_spectrum;
};
{
public:
Private()
- : currentDataObject(0),
- currentUnit(ChemicalDataObject::noUnit),
+ : currentUnit(ChemicalDataObject::noUnit),
currentElement(0),
inElement(false),
inName(false),
~Private()
{
- delete currentDataObject;
delete currentElement;
//qDeleteAll(elements);
}
- ChemicalDataObject *currentDataObject;
+ ChemicalDataObject currentDataObject;
ChemicalDataObject::BlueObeliskUnit currentUnit;
Element *currentElement;
for (int i = 0; i < attrs.length(); ++i)
{
if (attrs.localName(i) == "value") {
- d->currentDataObject->setData( attrs.value(i) );
- d->currentDataObject->setType( ChemicalDataObject::symbol );
+ d->currentDataObject.setData( attrs.value(i) );
+ d->currentDataObject.setType( ChemicalDataObject::symbol );
if ( d->currentElement )
d->currentElement->addData( d->currentDataObject );
for (int i = 0; i < attrs.length(); ++i)
{
if (attrs.localName(i) == "value") {
- d->currentDataObject->setData( attrs.value(i) );
- d->currentDataObject->setType( ChemicalDataObject::name );
+ d->currentDataObject.setData( attrs.value(i) );
+ d->currentDataObject.setType( ChemicalDataObject::name );
if ( d->currentElement )
d->currentElement->addData( d->currentDataObject );
d->elements.append(d->currentElement);
d->currentElement = 0;
- d->currentDataObject = 0;
d->inElement = false;
}
else if ( localName == "scalar" || localName == "label" || localName == "array" )
{
- d->currentDataObject->setUnit( d->currentUnit );
+ d->currentDataObject.setUnit( d->currentUnit );
}
return true;
}
bool ElementSaxParser::characters(const QString &ch)
{
- d->currentDataObject = new ChemicalDataObject();
+ d->currentDataObject = ChemicalDataObject();
ChemicalDataObject::BlueObelisk type;
QVariant value;
else//it is a non known value. Do not create a wrong object but return
return true;
- d->currentDataObject->setData( value );
- d->currentDataObject->setType( type );
+ d->currentDataObject.setData( value );
+ d->currentDataObject.setType( type );
if ( d->currentElement )
d->currentElement->addData( d->currentDataObject );
#include <kdebug.h>
Isotope::Isotope()
-: m_parentElementSymbol( 0 ), m_mass( 0 ), m_identifier( 0 ),
- m_spin( 0 ), m_magmoment( 0 ), m_abundance( 0 ), m_halflife( 0 ), m_ecdecay( 0 ), m_eclikeliness( 0 ), m_betaplusdecay( 0 ), m_betapluslikeliness( 0 ), m_betaminusdecay( 0 ), m_betaminuslikeliness( 0 ), m_alphadecay( 0 ), m_alphalikeliness( 0 )
{
}
Isotope::~Isotope()
{
- delete m_parentElementSymbol;
- delete m_mass;
- delete m_identifier;
- delete m_spin;
- delete m_magmoment;
- delete m_abundance;
- delete m_halflife;
- delete m_ecdecay;
- delete m_betaplusdecay;
- delete m_betaminusdecay;
- delete m_alphadecay;
- delete m_eclikeliness;
- delete m_betapluslikeliness;
- delete m_betaminuslikeliness;
- delete m_alphalikeliness;
-}
-
-void Isotope::addData( ChemicalDataObject* o )
-{
- if ( o->type() == ChemicalDataObject::exactMass )
+}
+
+void Isotope::addData( ChemicalDataObject o )
+{
+ if ( o.type() == ChemicalDataObject::exactMass )
m_mass = o;
- else if ( o->type() == ChemicalDataObject::atomicNumber )
+ else if ( o.type() == ChemicalDataObject::atomicNumber )
m_identifier = o;
- else if ( o->type() == ChemicalDataObject::symbol )
+ else if ( o.type() == ChemicalDataObject::symbol )
m_parentElementSymbol = o;
- else if ( o->type() == ChemicalDataObject::spin )
+ else if ( o.type() == ChemicalDataObject::spin )
m_spin = o;
- else if ( o->type() == ChemicalDataObject::magneticMoment )
+ else if ( o.type() == ChemicalDataObject::magneticMoment )
m_magmoment = o;
- else if ( o->type() == ChemicalDataObject::relativeAbundance )
+ else if ( o.type() == ChemicalDataObject::relativeAbundance )
m_abundance = o;
- else if ( o->type() == ChemicalDataObject::halfLife )
+ else if ( o.type() == ChemicalDataObject::halfLife )
m_halflife = o;
- else if ( o->type() == ChemicalDataObject::ecDecay )
+ else if ( o.type() == ChemicalDataObject::ecDecay )
m_ecdecay = o;
- else if ( o->type() == ChemicalDataObject::ecDecayLikeliness )
+ else if ( o.type() == ChemicalDataObject::ecDecayLikeliness )
m_eclikeliness = o;
- else if ( o->type() == ChemicalDataObject::betaplusDecay )
+ else if ( o.type() == ChemicalDataObject::betaplusDecay )
m_betaplusdecay = o;
- else if ( o->type() == ChemicalDataObject::betaplusDecayLikeliness )
+ else if ( o.type() == ChemicalDataObject::betaplusDecayLikeliness )
m_betapluslikeliness = o;
- else if ( o->type() == ChemicalDataObject::betaminusDecay )
+ else if ( o.type() == ChemicalDataObject::betaminusDecay )
m_betaminusdecay = o;
- else if ( o->type() == ChemicalDataObject::betaminusDecayLikeliness )
+ else if ( o.type() == ChemicalDataObject::betaminusDecayLikeliness )
m_betaminuslikeliness = o;
- else if ( o->type() == ChemicalDataObject::alphaDecay )
+ else if ( o.type() == ChemicalDataObject::alphaDecay )
m_alphadecay = o;
- else if ( o->type() == ChemicalDataObject::alphaDecayLikeliness )
+ else if ( o.type() == ChemicalDataObject::alphaDecayLikeliness )
m_alphalikeliness = o;
}
double Isotope::mass() const
{
- return m_mass ? m_mass->value().toDouble() : -1.0;
+ //return m_mass ? m_mass.value().toDouble() : -1.0;
}
QString Isotope::errorMargin() const
{
- return m_mass ? m_mass->errorValue().toString() : QString();
+ //return m_mass ? m_mass.errorValue().toString() : QString();
}
int Isotope::parentElementNumber() const
{
- return m_identifier ? m_identifier->value().toInt() : -1;
+// return m_identifier ? m_identifier.value().toInt() : -1;
}
QString Isotope::spin() const
{
- return m_spin ? m_spin->value().toString() : QString();
+ //return m_spin ? m_spin.value().toString() : QString();
}
QString Isotope::magmoment() const
{
- return m_magmoment ? m_magmoment->value().toString() : QString();
+ //return m_magmoment ? m_magmoment.value().toString() : QString();
}
QString Isotope::abundance() const
{
- return m_abundance ? m_abundance->value().toString() : QString();
+ //return m_abundance ? m_abundance.value().toString() : QString();
}
double Isotope::halflife() const
{
- return m_halflife ? m_halflife->value().toDouble() : -1.0;
+ //return m_halflife ? m_halflife.value().toDouble() : -1.0;
}
double Isotope::ecdecay() const
{
- return m_ecdecay ? m_ecdecay->value().toDouble() : -1.0;
+ //return m_ecdecay ? m_ecdecay.value().toDouble() : -1.0;
}
double Isotope::eclikeliness() const
{
- return m_eclikeliness ? m_eclikeliness->value().toDouble() : -1.0;
+ //return m_eclikeliness ? m_eclikeliness.value().toDouble() : -1.0;
}
double Isotope::betaplusdecay() const
{
- return m_betaplusdecay ? m_betaplusdecay->value().toDouble() : -1.0;
+ //return m_betaplusdecay ? m_betaplusdecay.value().toDouble() : -1.0;
}
double Isotope::betapluslikeliness() const
{
- return m_betapluslikeliness ? m_betapluslikeliness->value().toDouble() : -1.0;
+ //return m_betapluslikeliness ? m_betapluslikeliness.value().toDouble() : -1.0;
}
double Isotope::betaminusdecay() const
{
- return m_betaminusdecay ? m_betaminusdecay->value().toDouble() : -1.0;
+ //return m_betaminusdecay ? m_betaminusdecay.value().toDouble() : -1.0;
}
double Isotope::betaminuslikeliness() const
{
- return m_betaminuslikeliness ? m_betaminuslikeliness->value().toDouble() : -1.0;
+ //return m_betaminuslikeliness ? m_betaminuslikeliness.value().toDouble() : -1.0;
}
double Isotope::alphadecay() const
{
- return m_alphadecay ? m_alphadecay->value().toDouble() : -1.0;
+ //return m_alphadecay ? m_alphadecay.value().toDouble() : -1.0;
}
double Isotope::alphalikeliness() const
{
- return m_alphalikeliness ? m_alphalikeliness->value().toDouble() : -1.0;
+ //return m_alphalikeliness ? m_alphalikeliness.value().toDouble() : -1.0;
}
QString Isotope::parentElementSymbol() const
{
- return m_parentElementSymbol ? m_parentElementSymbol->value().toString() : QString();
+ //return m_parentElementSymbol ? m_parentElementSymbol.value().toString() : QString();
}
void Isotope::setNucleons( int number )
{
- m_numberOfNucleons = number;
+ //m_numberOfNucleons = number;
}
int Isotope::nucleons() const
{
- return m_numberOfNucleons;
+ //return m_numberOfNucleons;
}
Isotope::Nucleons Isotope::nucleonsAfterDecay( Decay kind )
{
Isotope::Nucleons n;
- int protons = m_identifier->value().toInt();
+ int protons = m_identifier.value().toInt();
int neutrons = m_numberOfNucleons - protons;
n.protons = protons;
n.neutrons = neutrons;
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
-class ChemicalDataObject;
-
+#include "chemicaldataobject.h"
#include <libkdeedu_science_export.h>
#include <QString>
/**
* add the ChemicalDataObject @p o
*/
- void addData( ChemicalDataObject* o );
+ void addData( ChemicalDataObject o );
void setNucleons( int number );
/**
* the symbol of the element the isotope belongs to
*/
- ChemicalDataObject* m_parentElementSymbol;
+ ChemicalDataObject m_parentElementSymbol;
/**
* stores the information about the mass of the Isotope
*/
- ChemicalDataObject* m_mass;
+ ChemicalDataObject m_mass;
/**
* stores the atomicNumber of the Isotope
*/
- ChemicalDataObject* m_identifier;
+ ChemicalDataObject m_identifier;
/**
* stores the spin of the Isotope
*/
- ChemicalDataObject* m_spin;
+ ChemicalDataObject m_spin;
/**
* stores the magneticMoment of the Isotope
*/
- ChemicalDataObject* m_magmoment;
+ ChemicalDataObject m_magmoment;
/**
* stores the relative abundance of the Isotope
*/
- ChemicalDataObject* m_abundance;
+ ChemicalDataObject m_abundance;
/**
* stores the halfLife of the Isotope
*/
- ChemicalDataObject* m_halflife;
+ ChemicalDataObject m_halflife;
/**
* stores decay energy of the isotope
*/
- ChemicalDataObject* m_ecdecay;
- ChemicalDataObject* m_betaplusdecay;
- ChemicalDataObject* m_betaminusdecay;
- ChemicalDataObject* m_alphadecay;
+ ChemicalDataObject m_ecdecay;
+ ChemicalDataObject m_betaplusdecay;
+ ChemicalDataObject m_betaminusdecay;
+ ChemicalDataObject m_alphadecay;
/**
* stores the likeliness of a decay of the isotope
*/
- ChemicalDataObject* m_eclikeliness;
- ChemicalDataObject* m_betapluslikeliness;
- ChemicalDataObject* m_betaminuslikeliness;
- ChemicalDataObject* m_alphalikeliness;
+ ChemicalDataObject m_eclikeliness;
+ ChemicalDataObject m_betapluslikeliness;
+ ChemicalDataObject m_betaminuslikeliness;
+ ChemicalDataObject m_alphalikeliness;
int m_numberOfNucleons;
};
{
public:
Private()
- : currentDataObject(0),
- currentUnit(ChemicalDataObject::noUnit),
+ : currentUnit(ChemicalDataObject::noUnit),
currentErrorValue(QVariant()),
currentElementSymbol(QString()),
currentIsotope(0),
~Private(){
delete currentIsotope;
- delete currentDataObject;
//qDeleteAll(isotopes);
}
- ChemicalDataObject *currentDataObject;
+ ChemicalDataObject currentDataObject;
ChemicalDataObject::BlueObeliskUnit currentUnit;
QVariant currentErrorValue;
QString currentElementSymbol;
{
//X kDebug() << "setting inIsotope true!" << endl;
d->currentIsotope = new Isotope();
- d->currentIsotope->addData( new ChemicalDataObject( QVariant( d->currentElementSymbol ), ChemicalDataObject::symbol ) );
+ d->currentIsotope->addData( ChemicalDataObject( QVariant( d->currentElementSymbol ), ChemicalDataObject::symbol ) );
d->inIsotope = true;
for (int i = 0; i < attrs.length(); ++i)
{
{
if ( attrs.localName( i ) == "unit" )
{
- d->currentDataObject->setUnit( d->currentUnit );
+ d->currentDataObject.setUnit( d->currentUnit );
}
else
{
{
d->isotopes.append(d->currentIsotope);
- d->currentDataObject = 0;
d->currentIsotope = 0;
d->inIsotope = false;
}
bool IsotopeParser::characters(const QString &ch)
{
- d->currentDataObject = new ChemicalDataObject();
+ d->currentDataObject = ChemicalDataObject();
ChemicalDataObject::BlueObelisk type;
QVariant value;
if ( type == ChemicalDataObject::exactMass )
{
- d->currentDataObject->setErrorValue( d->currentErrorValue );
+ d->currentDataObject.setErrorValue( d->currentErrorValue );
}
- d->currentDataObject->setData( value );
- d->currentDataObject->setType( type );
+ d->currentDataObject.setData( value );
+ d->currentDataObject.setType( type );
if ( d->currentIsotope )
{
d->currentIsotope->addData( d->currentDataObject );
- d->currentDataObject = 0;
}
return true;
//X isotopes_.append(currentSpectrum_);
//X
//X currentSpectrum_ = 0;
-//X currentDataObject_ = 0;
//X inSpectrum_ = false;
}
else if ( localName == "scalar" )
{
-//X if ( currentDataObject_->type() == ChemicalDataObject::exactMass ){
-//X currentDataObject_->setErrorValue( currentErrorValue_ );
+//X if ( currentDataObject_.type() == ChemicalDataObject::exactMass ){
+//X currentDataObject_.setErrorValue( currentErrorValue_ );
//X }
}
bool SpectrumParser::characters(const QString &ch)
{
- currentDataObject_ = new ChemicalDataObject();
+ ChemicalDataObject currentDataObject_;
ChemicalDataObject::BlueObelisk type;
QVariant value;
else//it is a non known value. Do not create a wrong object but return
return true;
- currentDataObject_->setData( value );
- currentDataObject_->setType( type );
+ currentDataObject_.setData( value );
+ currentDataObject_.setType( type );
//X if ( currentSpectrum_ )
//X currentSpectrum_->addData( currentDataObject_ );
QList<Spectrum*> getSpectrums();
private:
- ChemicalDataObject *currentDataObject_;
+ ChemicalDataObject currentDataObject_;
ChemicalDataObject::BlueObeliskUnit currentUnit_;
QVariant currentErrorValue_;
foreach( Element* e, v ){
if ( e )
{
- QList<ChemicalDataObject*> list = e->data();
+ QList<ChemicalDataObject> list = e->data();
//Test: give me all data available
- foreach( ChemicalDataObject*o, list ){
- if ( o )
- {
- QString unit = o->unitAsString();
+ foreach( ChemicalDataObject o, list ){
+ QString unit = o.unitAsString();
if ( unit == "bo:noUnit" )
unit = "";
- kDebug() << "Name: " << o->dictRef() << " " << o->valueAsString() <<" " << unit << endl;
- }
+ kDebug() << "Name: " << o.dictRef() << " " << o.valueAsString() <<" " << unit << endl;
}
}
}