commonutilities/common/inc/glxsingletonstore.inl
changeset 23 74c9f037fd5d
equal deleted inserted replaced
5:f7f0874bfe7d 23:74c9f037fd5d
       
     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 #include <glxlog.h>
       
    21 #include <glxpanic.h>
       
    22 
       
    23 // -----------------------------------------------------------------------------
       
    24 // Return new or existing instance
       
    25 // -----------------------------------------------------------------------------
       
    26 //
       
    27 template <class T>
       
    28 T* CGlxSingletonStore::InstanceL( T* (*aFactoryFuncL)() )
       
    29 	{
       
    30 	// Get new or existing pointer to store.
       
    31 	// InstanceL stores the pointer in TLS
       
    32     CGlxSingletonStore* store = InstanceLC();
       
    33     	
       
    34     // Try to find existing object of type T
       
    35     TInt count = store->iSingletons.Count();
       
    36     TInt i = 0;
       
    37     T* obj = NULL;
       
    38     while (i < count && !obj) 
       
    39         {
       
    40         obj = dynamic_cast<T*>(store->iSingletons[i].iObject);
       
    41         if (obj)
       
    42             {
       
    43             // Found an existing object of type T
       
    44             GLX_LOG_INFO1("CGlxSingletonStore::InstanceL, Found existing object %x", obj);
       
    45             // Add another reference
       
    46             store->iSingletons[i].iReferenceCount++;
       
    47             }
       
    48         i++;
       
    49         }
       
    50     
       
    51     // Create new object if one did not exist
       
    52     if ( !obj ) 
       
    53         {
       
    54         // If these calls leave, Cleanup stack will make sure tls 
       
    55         // pointer is cleared, if there are no other clients for CGlxSingletonStore
       
    56         // class
       
    57         
       
    58         // Reserve space in the array so that can safely append
       
    59         store->iSingletons.ReserveL( count + 1 );
       
    60         
       
    61         // Create a new object via provided factory function
       
    62         obj = aFactoryFuncL();
       
    63         
       
    64         // Give ownership of the new object to store
       
    65         TSingleton singleton;
       
    66         singleton.iObject = obj;
       
    67         singleton.iReferenceCount = 1;
       
    68         store->iSingletons.Append(singleton);
       
    69 
       
    70         GLX_LOG_INFO1 ("CGlxSingletonStore::InstanceL, Created new object %x", obj);
       
    71         }
       
    72 
       
    73     // Remove singleton store from cleanup stack
       
    74     CleanupStack::Pop(store);
       
    75 
       
    76 	return obj;
       
    77 	}    
       
    78