Wednesday, October 01, 2008

How to simplify Symbian Classes using Template.

As you all know we have to have 2-Phase consutsction in heap based classes. And also know that it's repetitive code that we have to write for each and every classes. If you can minimize the coding effort that will help us in long run, for that we can use the Symbian Templates.

Define a class which is base class

// baseclass.h
#ifndef __BASE_CLASS_H__
#define __BASE_CLASS_H__

template <class T>
class CBaseClass : public CBase
{
public:
static T* NewL(RWsSession& aWsSession)
{
T *self = T::NewLC(aWsSession);
CleanupStack::Pop(self);
return self;
}

static T* NewLC(RWsSession& aWsSession)
{
T *self = new (ELeave) T(aWsSession);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}

public:
virtual void ConstructL() = 0;

};
#endif
Now We can use in any class like below

#ifndef __MYCLASS_H__
#define __MYCLASS_H__

#include "baseclass.h"
class CMyClass;
class CMyClass : public CBaseClass<CMyClass>
{
public:
void ConstructL();
public:
CMyClass()
{
}
~CMyClass();
};
#endif
note: you can improve the above implemetaion as per your requirement.

1 comment:

kumari said...

I actually enjoyed reading through this posting.Many thanks.


Symbian Application Development

stats counter