Running an Active Object in OpenC

来源:互联网 发布:淘宝可以办假的毕业证 编辑:程序博客网 时间:2024/05/24 00:14

From Forum Nokia Wiki

http://wiki.forum.nokia.com/index.php/Running_an_Active_Object_in_OpenC

 

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)