×
Namespaces

Variants
Actions
(Difference between revisions)

Implementing a Singleton using Symbian CCoeStatic class

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
hamishwillee (Talk | contribs)
 
(2 intermediate revisions by one user not shown)
Line 1: Line 1:
 +
[[Category:Symbian C++]][[Category:Code Snippet]][[Category:Mobile Design Patterns]][[Category:S60 3rd Edition (initial release)]][[Category:S60 5th Edition]][[Category:Symbian^3]][[Category:General Programming]]
 +
{{Abstract|This article shows how to create a singleton using {{Icode|CCoeStatic}} that can be used in Symbian C++ application classes (only). }}
 +
 
{{ArticleMetaData <!-- v1.1 -->
 
{{ArticleMetaData <!-- v1.1 -->
 
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
Line 4: Line 7:
 
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Qt SDK 1.1.4]) -->
 
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Qt SDK 1.1.4]) -->
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
+
|platform= S60 3rd Edition and later
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
+
|signing= Self-Signed
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
+
|capabilities= None
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
+
|keywords= CCoeStatic
 
|id= <!-- Article Id (Knowledge base articles only) -->
 
|id= <!-- Article Id (Knowledge base articles only) -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
Line 22: Line 25:
 
|author= [[User:Giridharn]]
 
|author= [[User:Giridharn]]
 
}}
 
}}
[[Category:Symbian C++]][[Category:Code Snippet]]
 
 
 
{{SeeAlso|
 
{{SeeAlso|
 
* [[WSD and the Singleton Pattern]]
 
* [[WSD and the Singleton Pattern]]
Line 29: Line 30:
 
* [[Implementing a Singleton Class in Symbian OS]]
 
* [[Implementing a Singleton Class in Symbian OS]]
 
}}
 
}}
== 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 ==
+
== Overview ==
  
A simpler way of implementing singletons than using TLS is possible for those classes which use the
+
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.  
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
+
This article explains how to create a singleton that can be used in classes which use the {{Icode|CCoeEnv}} class. Since {{Icode|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.
  
== CMySingleton.h ==
+
== Source code ==
 +
 
 +
=== CMySingleton.h ===
  
 
<code cpp>
 
<code cpp>
Line 74: Line 70:
  
  
== CMySingleton.cpp ==
+
=== CMySingleton.cpp ===
  
 
<code cpp>
 
<code cpp>
Line 109: Line 105:
 
return instance;
 
return instance;
 
}
 
}
 
 
</code>
 
</code>

Latest revision as of 06:55, 30 May 2013

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
Symbian^3
S60 5th Edition
S60 3rd Edition (initial release)
Platform Security
Signing Required: Self-Signed
Capabilities: None
Article
Keywords: CCoeStatic
Created: giridharn (23 May 2007)
Last edited: hamishwillee (30 May 2013)

Contents

[edit] 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.

[edit] Source code

[edit] 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();
...
};


[edit] 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;
}
This page was last modified on 30 May 2013, at 06:55.
145 page views in the last 30 days.
×