Symbian Developer Library

SYMBIAN OS V6.1 EDITION FOR C++

[Index] [Glossary] [Previous] [Next]



How to construct an active object

The following examples outline the practical framework required when implementing an active object


Active objects framework

The following example code demonstrates how to construct the framework for a simple active object which encapsulates a service-provider.

First define and implement the service-provider class. The following code shows the typical features:

class CExampleServiceProvider
    {
public:
    ...
    void RequestTheService(TRequestStatus& aStatus);
    void CancelTheRequest();
private:
    TInt iResult ;
    };

Now define and implement the active object which wraps around the service provider. The following code shows the the main features of an active object class.

class CExampleActiveObject : public CActive;
    {
public:
    CExampleActiveObject(CExampleServiceProvider* aServiceProvider);
    ~CExampleActiveObject;
    void IssueRequest();
private:
    void DoCancel();
    void RunL();
    TInt RunError(TInt aError);
private:
    CExampleServiceProvider* iServiceProvider ;
    };

When the class codes a DoCancel() then the destructor must call the Cancel() member function of the active object.

[Top]


How to construct an active object

The following code shows the typical elements required when constructing an active object.

CExampleActiveObject::CExampleActiveObject(CExampleServiceProvider* aServiceProvider) : CActive(EPriorityStandard)
    {
    iServiceProvider = aServiceProvider; // Store pointer to service provider
    CActiveScheduler::Add(this) ; // add to active scheduler
    }

In practice there are many variations on this basic construction theme.

CTimer::CTimer(TInt aPriority)
    : CActive(aPriority)
    {
    }

void CTimer::ConstructL()
    {
    TInt r=iTimer.CreateLocal();
    if (r!=KErrNone)
        {
        User::Leave(r);
        }
    }