Symbian3/SDK/Source/GUID-7250950C-5502-5ACE-864B-0EFD5C253053.dita
author Dominic Pinkman <dominic.pinkman@nokia.com>
Fri, 11 Jun 2010 12:39:03 +0100
changeset 8 ae94777fff8f
permissions -rw-r--r--
Week 23 contribution of SDK documentation content. See release notes for details. Fixes bugs Bug 2714, Bug 462.

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
<!-- This component and the accompanying materials are made available under the terms of the License 
"Eclipse Public License v1.0" which accompanies this distribution, 
and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
<!-- Initial Contributors:
    Nokia Corporation - initial contribution.
Contributors: 
-->
<!DOCTYPE concept
  PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
<concept id="GUID-7250950C-5502-5ACE-864B-0EFD5C253053" xml:lang="en"><title>How
to create a singleton</title><prolog><metadata><keywords/></metadata></prolog><conbody>
<p>The <xref href="GUID-F0C084E2-C683-39E2-A131-77FE3206A02C.dita"><apiname>CCoeStatic</apiname></xref> class allows singleton classes to be
created and stored by the environment (<xref href="GUID-12A9389D-363B-3F54-857F-89EE0EDCDF40.dita"><apiname>CCoeEnv</apiname></xref>) in Thread
Local Storage (TLS). This provides a means of creating writeable global static
data. </p>
<p> <filepath>MySingleton.h</filepath>  </p>
<codeblock id="GUID-B526F5B3-FBC7-5563-8F5C-37B01A0EC28D" xml:space="preserve">
class CMySingleton : public CCoeStatic
    {
public:
    static CMySingleton* SelfL() ;                // Slightly slower
    static CMySingleton* SelfL( CCoeEnv* aCoeEnv ) ;    // Slightly faster
    ...
private:
    CMySingleton() ;
    ~CMySingleton() ;
    } ;
</codeblock>
<p> <filepath>MySingleton.cpp</filepath>  </p>
<codeblock id="GUID-FD7053EB-8769-5C04-98E2-3C8EC8589A10" xml:space="preserve">
const TUid KUidMySingleton = {0x10204232} ;

CMySingleton::CMySingleton() : CCoeStatic( KUidMySingleton, CCoeStatic::EThread /*or EApp*/ )
    {
    }

CMySingleton* CMySingleton::SelfL()
    {
    CMySingleton* self = static_cast&lt;CMySingleton*&gt;( CCoeStatic::Static( KUidMySingleton ) ) ; 
    if(!self)
        {
        self = new( ELeave ) CMySingleton() ;
        }
    return self ;
    }

CMySingleton* CMySingleton::SelfL( CCoeEnv* aCoeEnv )
    {
    CMySingleton* self = static_cast&lt;CMySingleton*&gt;( aCoeEnv-&gt;FindStatic( KUidMySingleton ) ) ;
    if( !self )
        {
        self = new( ELeave ) CMySingleton() ;
        }
    return self ;
    }
</codeblock>
<p>A singleton must be given a UID. When it is instantiated for the first
time the base class constructor adds it to the list of singletons in the environment.
Any subsequent attempts to instantiate the same singleton will result in a
panic. </p>
<p>Singletons may be given a <b>destruction priority</b> and a <b>scope</b>. </p>
<p>The <b>destruction priority</b> determines when the singleton is destroyed
relative to any other singletons in the environment's list and relative to
the App Ui. The default priority, <codeph>EDefaultDestructionPriority</codeph>,
is 100. The higher the priority, the earlier the singleton will be destroyed.
A negative value indicates that the singleton should be destroyed after the
AppUi. The more negative the value, the later the destruction. </p>
<p>The <b>scope</b> may be <codeph>EThread</codeph> (the default) or <codeph>EApp</codeph> and
determines the visibility of the singleton. </p>
<p>Once a singleton has been created it may be accessed through the <xref href="GUID-12A9389D-363B-3F54-857F-89EE0EDCDF40.dita"><apiname>CCoeEnv</apiname></xref> API. </p>
<codeblock id="GUID-7B6E1AE6-30E6-585C-84AC-5C385F0894D3" xml:space="preserve">// Singleton access
IMPORT_C static CCoeStatic* Static( TUid aUid ) ;
IMPORT_C CCoeStatic* FindStatic( TUid aUid ) ;</codeblock>
</conbody></concept>