Implementing a Singleton using Symbian CCoeStatic class
This article shows how to create a singleton using CCoeStatic that can be used in Symbian C++ application classes (only).
Article Metadata
Compatibility
Platform(s): S60 3rd Edition and later
Platform Security
Signing Required: Self-Signed
Capabilities: None
Article
Keywords: CCoeStatic
Created: giridharn
(23 May 2007)
Last edited: hamishwillee
(04 Dec 2012)
Contents |
Overview
The singleton pattern is one of the best-known patterns in software engineering. Essentially, a singleton is a class which only allows a single instance of itself to be created, and usually gives simple access to that instance.
This article explains how to create a singleton that can be used in classes which use the CCoeEnv class. Since CCoeEnv is a part of the UI control framework, this approach is only available to applications (not their engines). The approach is simpler than using TLS.
Source code
CMySingleton.h
/**
* Example implementation of a singleton class by means of inheriting
* from CCoeStatic.
*/
class CMySingleton : public CCoeStatic
{
public: // constructors and destructor
/**
* Returns an instance of this class. When called for the first
* time, a new instance is created and returned. After that,
* calling InstanceL returns the same instance that was created
* earlier.
*
* @return A pointer to a CMySingleton object
*/
static CMySingleton* InstanceL();
private: // constructor
/**
* Default constructor is private because we are using the
* singleton design pattern.
*/
CMySingleton();
...
};
CMySingleton.cpp
static const TUid KUidMySingleton = { 0x100000E9 };
// -------------------------------------------------------------------------
// CMySingleton::CMySingleton
// C++ default constructor. It is private because we are using the
// singleton design pattern.
// -------------------------------------------------------------------------
CMySingleton::CMySingleton()
: CCoeStatic( KUidMySingleton )
{
}
// -------------------------------------------------------------------------
// CMySingleton::InstanceL
// Returns an instance of this class. When called for the first time,
// a new instance is created and returned. After that, calling
// InstanceL returns the same instance that was created earlier.
// Note that the UID passed to CCoeEnv::Static needs to be unique.
// -------------------------------------------------------------------------
CMySingleton* CMySingleton::InstanceL()
{
CMySingleton* instance = static_cast<CMySingleton*>
( CCoeEnv::Static( KUidMySingleton ) );
if ( !instance )
{
instance = new ( ELeave ) CMySingleton;
CleanupStack::PushL( instance );
instance->ConstructL();
CleanupStack::Pop();
}
return instance;
}


15 Sep
2009
A very helpful code-snippet on usage of a very powerful feature of symbian. Shows how we can initialize a singleton class just once and then later use the same instance throughout the project. Data members of a singleton class
are maintained globally and any changes made to them are reflected across the project. The code is well commented and describes the concept and its usage briefly.
28 Sep
2009
Singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. It is powerful concept of software engineering. Since singleton class have only one instance at a time, it can also be used as a just like global variable in application. The DLL in EKA1 can not support writable static data and so singleton class can not be usable in DLL of EKA1.
The article describes how to create singleton class in symbian. Proper comment on will enable beginners to understand important of each method. Each singleton class must have a unique TUid, so do not forgot to use separate TUid if you have more than one singleton class.