Symbian3/SDK/Source/GUID-7250950C-5502-5ACE-864B-0EFD5C253053-GENID-1-8-1-3-1-1-7-1-10-1.dita
changeset 8 ae94777fff8f
parent 7 51a74ef9ed63
child 9 59758314f811
equal deleted inserted replaced
7:51a74ef9ed63 8:ae94777fff8f
     1 <?xml version="1.0" encoding="utf-8"?>
       
     2 <!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
       
     3 <!-- This component and the accompanying materials are made available under the terms of the License 
       
     4 "Eclipse Public License v1.0" which accompanies this distribution, 
       
     5 and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
       
     6 <!-- Initial Contributors:
       
     7     Nokia Corporation - initial contribution.
       
     8 Contributors: 
       
     9 -->
       
    10 <!DOCTYPE concept
       
    11   PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
       
    12 <concept id="GUID-7250950C-5502-5ACE-864B-0EFD5C253053-GENID-1-8-1-3-1-1-7-1-10-1" xml:lang="en"><title>How
       
    13 to create a singleton</title><prolog><metadata><keywords/></metadata></prolog><conbody>
       
    14 <p>The <xref href="GUID-F0C084E2-C683-39E2-A131-77FE3206A02C.dita"><apiname>CCoeStatic</apiname></xref> class allows singleton classes to be
       
    15 created and stored by the environment (<xref href="GUID-12A9389D-363B-3F54-857F-89EE0EDCDF40.dita"><apiname>CCoeEnv</apiname></xref>) in Thread
       
    16 Local Storage (TLS). This provides a means of creating writeable global static
       
    17 data. </p>
       
    18 <p> <filepath>MySingleton.h</filepath>  </p>
       
    19 <codeblock id="GUID-B526F5B3-FBC7-5563-8F5C-37B01A0EC28D-GENID-1-8-1-3-1-1-7-1-10-1-2-3" xml:space="preserve">
       
    20 class CMySingleton : public CCoeStatic
       
    21     {
       
    22 public:
       
    23     static CMySingleton* SelfL() ;                // Slightly slower
       
    24     static CMySingleton* SelfL( CCoeEnv* aCoeEnv ) ;    // Slightly faster
       
    25     ...
       
    26 private:
       
    27     CMySingleton() ;
       
    28     ~CMySingleton() ;
       
    29     } ;
       
    30 </codeblock>
       
    31 <p> <filepath>MySingleton.cpp</filepath>  </p>
       
    32 <codeblock id="GUID-FD7053EB-8769-5C04-98E2-3C8EC8589A10-GENID-1-8-1-3-1-1-7-1-10-1-2-5" xml:space="preserve">
       
    33 const TUid KUidMySingleton = {0x10204232} ;
       
    34 
       
    35 CMySingleton::CMySingleton() : CCoeStatic( KUidMySingleton, CCoeStatic::EThread /*or EApp*/ )
       
    36     {
       
    37     }
       
    38 
       
    39 CMySingleton* CMySingleton::SelfL()
       
    40     {
       
    41     CMySingleton* self = static_cast&lt;CMySingleton*&gt;( CCoeStatic::Static( KUidMySingleton ) ) ; 
       
    42     if(!self)
       
    43         {
       
    44         self = new( ELeave ) CMySingleton() ;
       
    45         }
       
    46     return self ;
       
    47     }
       
    48 
       
    49 CMySingleton* CMySingleton::SelfL( CCoeEnv* aCoeEnv )
       
    50     {
       
    51     CMySingleton* self = static_cast&lt;CMySingleton*&gt;( aCoeEnv-&gt;FindStatic( KUidMySingleton ) ) ;
       
    52     if( !self )
       
    53         {
       
    54         self = new( ELeave ) CMySingleton() ;
       
    55         }
       
    56     return self ;
       
    57     }
       
    58 </codeblock>
       
    59 <p>A singleton must be given a UID. When it is instantiated for the first
       
    60 time the base class constructor adds it to the list of singletons in the environment.
       
    61 Any subsequent attempts to instantiate the same singleton will result in a
       
    62 panic. </p>
       
    63 <p>Singletons may be given a <b>destruction priority</b> and a <b>scope</b>. </p>
       
    64 <p>The <b>destruction priority</b> determines when the singleton is destroyed
       
    65 relative to any other singletons in the environment's list and relative to
       
    66 the App Ui. The default priority, <codeph>EDefaultDestructionPriority</codeph>,
       
    67 is 100. The higher the priority, the earlier the singleton will be destroyed.
       
    68 A negative value indicates that the singleton should be destroyed after the
       
    69 AppUi. The more negative the value, the later the destruction. </p>
       
    70 <p>The <b>scope</b> may be <codeph>EThread</codeph> (the default) or <codeph>EApp</codeph> and
       
    71 determines the visibility of the singleton. </p>
       
    72 <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>
       
    73 <codeblock id="GUID-7B6E1AE6-30E6-585C-84AC-5C385F0894D3-GENID-1-8-1-3-1-1-7-1-10-1-2-11" xml:space="preserve">// Singleton access
       
    74 IMPORT_C static CCoeStatic* Static( TUid aUid ) ;
       
    75 IMPORT_C CCoeStatic* FindStatic( TUid aUid ) ;</codeblock>
       
    76 </conbody></concept>