Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Running an Active Object in OpenC

From Wiki
Jump to: navigation, search
Article Metadata
Article
Created: paipeng (20 Mar 2008)
Last edited: hamishwillee (27 Jul 2012)


I just want to use some functions and classes of Symbian C++ in an Open C project. Because some classes are written as Active Object for asynchronisation. I am trying to find out, if it is possible to run an active class in a Open C program. And finally it works. At least on device, not yet on emulator by compiling.


Here is the code, may be someone needs it. I am using the example 'Periodic' from the SDK as the CActive class.



main.c

#include <stdio.h>
#include <e32base.h>
#ifdef __GCCE__
#include <staticlibinit_gcce.h>
#endif
 
#include "PeriodicTest.h"
 
 
_LIT(KTxtEPOC32EX,"EPOC32EX");
 
 
void activeClassL()
{
CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
CleanupStack::PushL(scheduler);
CActiveScheduler::Install(scheduler);
 
 
CPeriodicRunner* periodic = CPeriodicRunner::NewL(4);
 
periodic->Start();
 
CActiveScheduler::Start();
 
 
periodic = NULL;
// Delete active scheduler
CleanupStack::PopAndDestroy(scheduler);
 
}
 
void runCActiveclass()
{
__UHEAP_MARK;
CTrapCleanup* cleanup = CTrapCleanup::New();
 
TRAPD(error,activeClassL()); // more initialization, then do example
__ASSERT_ALWAYS(!error,User::Panic(KTxtEPOC32EX,error));
 
delete cleanup;
__UHEAP_MARKEND;
}
int main()
{
runCActiveclass();
printf("finished!\n");
getchar();
return 0;
}

PeriodicTest.h

#ifndef PERIODICTEST_H_
#define PERIODICTEST_H_
 
#include <e32base.h>
#include <stdio.h>
 
 
class CPeriodicRunner : public CActive
{
public:
static CPeriodicRunner* NewL(TInt aTotalTicks);
~CPeriodicRunner(); // destruct and give statistics
 
protected:
CPeriodicRunner(TInt aTotalTicks);
private:
void ConstructL(); // second construction phase
 
void RunL();
void DoCancel();
 
void DoTick(); // indirectly called
public:
void Start();
 
private:
TInt iTotalTicks;
RTimer timer;
};
#endif /*PERIODICTEST_H_*/

PeriodicTest.cpp

#include "PeriodicTest.h"
 
 
// protected C++ constructor
CPeriodicRunner::CPeriodicRunner(TInt aTotalTicks)
: CActive(EPriorityStandard), iTotalTicks(aTotalTicks)
{
 
}
 
// private second-phase constructor
void CPeriodicRunner::ConstructL()
{
timer.CreateLocal();
CActiveScheduler::Add( this );
}
 
// construct, add CPeriodic to active scheduler, and start it
CPeriodicRunner* CPeriodicRunner::NewL(TInt aTotalTicks)
{
CPeriodicRunner* self=new (ELeave) CPeriodicRunner(aTotalTicks);
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop();
return self;
}
 
// destruct and give statistics
CPeriodicRunner::~CPeriodicRunner()
{
timer.Close();
CActiveScheduler::Stop();
}
 
 
void CPeriodicRunner::RunL()
{
if (iStatus == KErrNone)
{
DoTick();
}
}
 
void CPeriodicRunner::DoCancel()
{
 
}
 
void CPeriodicRunner::Start()
{
timer.After(iStatus, 1000000);
SetActive();
}
 
// private
void CPeriodicRunner::DoTick()
{
iTotalTicks--;
printf("Periodic timer %d done\n", iTotalTicks);
if(iTotalTicks==0)
{
delete this;
}
else
{
timer.After(iStatus, 1000000);
SetActive();
}
}



--paipeng 13:49, 20 March 2008 (EET)

This page was last modified on 27 July 2012, at 05:16.
22 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×