elementcarray.cpp

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

CPP
154
字号
/**
*
* @brief Specialisation of CElementList to store the elements in a CArray class
*
* Copyright (c) EMCC Software Ltd 2003
* @version 1.0
*/

// INCLUDE FILES

// Class include
#include "elementcarray.h"


// User includes
#include "chemicalelement.h"


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

CElementCArray::CElementCArray()
{
}

CElementCArray::~CElementCArray()
{
    DeleteAllElements();
    delete iElementArray;
}

void CElementCArray::ConstructL()
{
    iElementArray = new (ELeave) CArrayPtrFlat<CChemicalElement>(KElementListGranularity);
}

CElementCArray* CElementCArray::NewLC()
{
    CElementCArray* self = new (ELeave) CElementCArray;
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
}

CElementCArray* CElementCArray::NewL()
{
    CElementCArray* self = CElementCArray::NewLC();
    CleanupStack::Pop(self);
    return self;
}

//Getters
TInt CElementCArray::NumberOfElements() const
{
    return iElementArray->Count();
}

//Array manipulators and accessors
const CChemicalElement& CElementCArray::At(TInt aPosition) const
{
    //could use At() as in CElementCArray::At() below
    return *(*iElementArray)[aPosition];    //const overload
}

CChemicalElement& CElementCArray::At(TInt aPosition) 
{
    //could use operator[] as in CElementCArray::At() const above
    return *iElementArray->At(aPosition);    //non-const overload
}

void CElementCArray::AppendL(const CChemicalElement* aElement)
{
    //Cast away pointer constness
    iElementArray->AppendL(const_cast<CChemicalElement * const> (aElement));
}

void CElementCArray::DeleteAllElements()
{
    iElementArray->ResetAndDestroy();    //Deletes all the elements owned by the array
}

TInt CElementCArray::DoFindL(TKeyArrayPtr& aKey, const TDesC& aName, const TDesC& aSymbol,
                             TInt aAtomicNumber) const
{
    //A utility function to wrap up the find operation
    CChemicalElement* example = CChemicalElement::NewL(aName, aSymbol, aAtomicNumber, 0);    //The example element to search for
    TInt foundAt = 0;
    TInt error = iElementArray->Find(example, aKey, foundAt);
    delete example;            //No longer need the example element
    User::LeaveIfError(error);
    return foundAt;
}

TInt CElementCArray::FindElementByNameL(const TDesC& aName) const
{
    //Make the key
    TKeyArrayPtr key(_FOFF(CChemicalElement, iName), ECmpCollated);
    //Find the element using the given name only
    return DoFindL(key, aName, KNullDesC, 0);
}

TInt CElementCArray::FindElementBySymbolL(const TDesC& aSymbol) const
{
    //Make the key
    TKeyArrayPtr key(_FOFF(CChemicalElement, iSymbol), ECmpCollated);
    //Find the element using the given symbol only
    return DoFindL(key, KNullDesC, aSymbol, 0);
}

TInt CElementCArray::FindElementByAtomicNumberL(TInt aAtomicNumber) const
{
    //Make the key
    TKeyArrayPtr key(_FOFF(CChemicalElement, iAtomicNumber), ECmpTInt);
    //Find the element using the given atomic number only
    return DoFindL(key, KNullDesC, KNullDesC, aAtomicNumber);
}

void CElementCArray::SortByNameL()
{
    //Not supported, since the name is owned by pointer!
    User::Leave(KErrNotSupported);
}

void CElementCArray::SortBySymbolL()
{
    //Make the key
    TKeyArrayPtr key(_FOFF(CChemicalElement, iSymbol), ECmpFolded);
    User::LeaveIfError(iElementArray->Sort(key));
}

void CElementCArray::SortByAtomicNumberL()
{
    //Make the key
    TKeyArrayPtr key(_FOFF(CChemicalElement, iAtomicNumber), ECmpTInt);
    User::LeaveIfError(iElementArray->Sort(key));
}

void CElementCArray::SortByRelativeAtomicMassL()
{
    //Not supported, since CArrays cannot be sorted on non-integer elements
    User::Leave(KErrNotSupported);
}


TAny* CElementCArray::TKeyArrayPtr::At(TInt aIndex) const
{
    if (aIndex == KIndexPtr)
    {
        return *(TUint8**)iPtr + iKeyOffset;
    }
    return *(TUint8**)iBase->Ptr(aIndex * sizeof(TUint8**)).Ptr() + iKeyOffset;
}

// End of File

⌨️ 快捷键说明

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