chemicalelement.cpp

来自「series60 应用程序开发的源代码 series60 应用程序开发的源代码」· C++ 代码 · 共 172 行

CPP
172
字号
/**
*
* @brief Class to encapsulate information about chemical elements
*
* Copyright (c) EMCC Software Ltd 2003
* @version 1.0
*/

// INCLUDE FILES

// Class include
#include "chemicalelement.h"

// ================= MEMBER FUNCTIONS =======================

//Construction
CChemicalElement::CChemicalElement(const TDesC& aSymbol, TInt aAtomicNumber, TReal aRelativeAtomicMass,
                                   TElementType aType, TBool aRadioactive) :
iSymbol(aSymbol),
iAtomicNumber(aAtomicNumber),
iRelativeAtomicMass(aRelativeAtomicMass),
iType(aType),
iRadioactive(aRadioactive)
{
}

CChemicalElement::CChemicalElement()
{
}

CChemicalElement* CChemicalElement::NewLC(const TDesC& aName, const TDesC& aSymbol, TInt aAtomicNumber, 
        TReal aRelativeAtomicMass, TElementType aType, TBool aRadioactive)
{
    CChemicalElement* self = new (ELeave) CChemicalElement(aSymbol, aAtomicNumber, aRelativeAtomicMass, aType, aRadioactive);
    CleanupStack::PushL(self);
    self->ConstructL(aName);
    return self;
}

CChemicalElement* CChemicalElement::NewL(const TDesC& aName, const TDesC& aSymbol, TInt aAtomicNumber, 
        TReal aRelativeAtomicMass, TElementType aType, TBool aRadioactive)
{
    CChemicalElement* self = NewLC(aName, aSymbol, aAtomicNumber, aRelativeAtomicMass, aType, aRadioactive);
    CleanupStack::Pop(self);
    return self;
}

CChemicalElement* CChemicalElement::NewLC(RReadStream& aStream)
{
    CChemicalElement* self = new (ELeave) CChemicalElement;    //Use default constructor since we're going to overwrite anyway!
    CleanupStack::PushL(self);
    self->InternalizeL(aStream);
    return self;
}

CChemicalElement* CChemicalElement::NewL(RReadStream& aStream)
{
    CChemicalElement* self = NewLC(aStream);
    CleanupStack::Pop(self);
    return self;
}

void CChemicalElement::ConstructL(const TDesC& aName)
{
    //Allocate a new heap-based descriptor big enough, then copy the buffer of aName to it
    iName = aName.AllocL();        //Can only be called once (by NewL/C) since ConstructL is private
}

//Destructor
CChemicalElement::~CChemicalElement()
{
    delete iName;
}

//Setters
void CChemicalElement::SetNameL(const TDesC& aName)
{
    //first delete the old name
    delete iName;
    iName = NULL;    //in case the next line leaves!
    iName = aName.AllocL();
}

void CChemicalElement::SetSymbol(const TDesC& aSymbol)
{
    iSymbol = aSymbol;    //iSymbol is a TBuf nested within the class, so no allocation
}

void CChemicalElement::SetAtomicNumber(TInt aAtomicNumber)
{
    iAtomicNumber = aAtomicNumber;
}

void CChemicalElement::SetRelativeAtomicMass(const TReal& aRelativeAtomicMass)
{
    iRelativeAtomicMass = aRelativeAtomicMass;
}

void CChemicalElement::SetType(TElementType aType)
{
    iType = aType;
}

void CChemicalElement::SetRadioactive(TBool aRadioactive)
{
    iRadioactive = aRadioactive;
}

//Getters
const TDesC& CChemicalElement::Name() const
{
    return *iName;
}

void CChemicalElement::GetSymbol(TDes& aSymbol) const
{
    aSymbol = iSymbol;
}

const TDesC& CChemicalElement::Symbol() const
{
    return iSymbol;
}

TInt CChemicalElement::AtomicNumber() const
{
    return iAtomicNumber;
}

const TReal& CChemicalElement::RelativeAtomicMass() const
{
    return iRelativeAtomicMass;
}

CChemicalElement::TElementType CChemicalElement::Type() const
{
    return iType;
}

TBool CChemicalElement::Radioactive() const
{
    return iRadioactive;
}

//Stream functions
void CChemicalElement::ExternalizeL(RWriteStream& aStream) const
{
    aStream << *iName;
    aStream << iSymbol;
    aStream.WriteUint8L(static_cast<TUint8> (iAtomicNumber));            //Cannot be greater than 255 or less than zero
    aStream.WriteUint16L(static_cast<TUint16> (iRelativeAtomicMass));    //Cannot be greater than 65535 or less than zero
    aStream.WriteUint8L(static_cast<TUint8> (iRadioactive));            //Boolean
    aStream.WriteUint8L(static_cast<TUint8> (iType));                    //Only three possible states
}

void CChemicalElement::InternalizeL(RReadStream& aStream)
{
    delete iName;        //Just in case this existed previously
    iName = NULL;

    iName = HBufC::NewL(aStream, KMaxTInt);        //KMaxTInt is overridden by the *actual* descriptor length as externalised!

    // Can't stream into a TBufC directly--use Des() to return a modifiable pointer descriptor first.
    TPtr modifiableSymbol = iSymbol.Des();
    aStream >> modifiableSymbol;

    iAtomicNumber = aStream.ReadUint8L();
    iRelativeAtomicMass = aStream.ReadUint16L();
    iRadioactive = static_cast<TBool>(aStream.ReadUint8L());
    iType = static_cast<TElementType>(aStream.ReadUint8L());
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?