photosgallery/common/inc/glxsingletonstore.inl
changeset 0 4e91876724a2
equal deleted inserted replaced
-1:000000000000 0:4e91876724a2
       
     1 /*
       
     2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:    TLS store for singleton objects
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include <glxlog.h>
       
    22 #include <glxpanic.h>
       
    23 
       
    24 // -----------------------------------------------------------------------------
       
    25 // Return new or existing instance
       
    26 // -----------------------------------------------------------------------------
       
    27 //
       
    28 template <class T>
       
    29 T* CGlxSingletonStore::InstanceL( T* (*aFactoryFuncL)() )
       
    30 	{
       
    31 	// Get new or existing pointer to store.
       
    32 	// InstanceL stores the pointer in TLS
       
    33     CGlxSingletonStore* store = InstanceLC();
       
    34     	
       
    35     // Try to find existing object of type T
       
    36     TInt count = store->iSingletons.Count();
       
    37     TInt i = 0;
       
    38     T* obj = NULL;
       
    39     while (i < count && !obj) 
       
    40         {
       
    41         obj = dynamic_cast<T*>(store->iSingletons[i].iObject);
       
    42         if (obj)
       
    43             {
       
    44             // Found an existing object of type T
       
    45             GLX_LOG_INFO1("CGlxSingletonStore::InstanceL, Found existing object %x", obj);
       
    46             // Add another reference
       
    47             store->iSingletons[i].iReferenceCount++;
       
    48             }
       
    49         i++;
       
    50         }
       
    51     
       
    52     // Create new object if one did not exist
       
    53     if ( !obj ) 
       
    54         {
       
    55         // If these calls leave, Cleanup stack will make sure tls 
       
    56         // pointer is cleared, if there are no other clients for CGlxSingletonStore
       
    57         // class
       
    58         
       
    59         // Reserve space in the array so that can safely append
       
    60         store->iSingletons.ReserveL( count + 1 );
       
    61         
       
    62         // Create a new object via provided factory function
       
    63         obj = aFactoryFuncL();
       
    64         
       
    65         // Give ownership of the new object to store
       
    66         TSingleton singleton;
       
    67         singleton.iObject = obj;
       
    68         singleton.iReferenceCount = 1;
       
    69         store->iSingletons.Append(singleton);
       
    70 
       
    71         GLX_LOG_INFO1 ("CGlxSingletonStore::InstanceL, Created new object %x", obj);
       
    72         }
       
    73 
       
    74     // Remove singleton store from cleanup stack
       
    75     CleanupStack::Pop(store);
       
    76 
       
    77 	return obj;
       
    78 	}    
       
    79