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.

Revision as of 16:37, 15 September 2009 by sunil.shetty (Talk | contribs)

Implementing a Singleton using Symbian CCoeStatic class

From Wiki
Jump to: navigation, search
{{{width}}}

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.

Using class CCoeStatic to implement a singleton class

A simpler way of implementing singletons than using TLS is possible for those classes which use the CCoeEnv class. Since CCoeEnv is a part of the UI control framework, this concerns only applications, not application engines. This applies also to S60 3rd Edition and later editions.

Singleton implementation based on CCoeStatic

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

// -------------------------------------------------------------------------
// 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;
}
138 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.

×