photosgallery/common/src/glxsingletonstore.cpp
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:    Store for singletons
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include "glxsingletonstore.h"
       
    23 
       
    24 #include <glxlog.h>
       
    25 
       
    26 // -----------------------------------------------------------------------------
       
    27 // Constructor
       
    28 // -----------------------------------------------------------------------------
       
    29 //
       
    30 CGlxSingletonStore::CGlxSingletonStore()
       
    31 	{
       
    32     GLX_LOG_INFO("CGlxSingletonStore::CGlxSingletonStore");
       
    33 	}
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 // return new instance
       
    37 // -----------------------------------------------------------------------------
       
    38 //
       
    39 EXPORT_C CGlxSingletonStore* CGlxSingletonStore::InstanceLC()
       
    40 	{
       
    41     CGlxSingletonStore* store = reinterpret_cast<CGlxSingletonStore*>(Dll::Tls());
       
    42     
       
    43     // Create new object, if one has not been created previously
       
    44     if ( !store )
       
    45         {
       
    46         store = new (ELeave) CGlxSingletonStore;
       
    47         
       
    48         // Store in tls pointer
       
    49         Dll::SetTls( reinterpret_cast<TAny*>(store));
       
    50         }
       
    51     
       
    52     // Push to cleanup stack - will make sure tls pointer is cleared if 
       
    53     // something leaves
       
    54     CleanupStack::PushL( TCleanupItem(&Cleanup, store) );
       
    55     
       
    56     return store;
       
    57 	}
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // Cleanup function
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 void CGlxSingletonStore::Cleanup( TAny* /*aStore*/ )
       
    64     {   
       
    65     // In case of one singleton owning another there can be nested calls to
       
    66     // CGlxSingletonStore::InstanceL, and so multiple Cleanup items on the
       
    67     // stack to delete the store.
       
    68     // Get the store pointer from TLS to ensure it hasn't already been deleted
       
    69     CGlxSingletonStore* obj
       
    70                     = reinterpret_cast<CGlxSingletonStore*>( Dll::Tls() );
       
    71     
       
    72     // Delete the object if instance was just created, and singleton
       
    73     // was _not_ successfully added
       
    74     if ( obj && obj->iSingletons.Count() == 0 ) 
       
    75         {
       
    76         delete obj;
       
    77         }   
       
    78 
       
    79     GLX_LOG_WARNING("Singleton factory function has (probably) left");
       
    80     }
       
    81  		
       
    82 // -----------------------------------------------------------------------------
       
    83 // Destructor
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 EXPORT_C CGlxSingletonStore::~CGlxSingletonStore()
       
    87 	{
       
    88     GLX_LOG_INFO("CGlxSingletonStore::~CGlxSingletonStore");
       
    89     
       
    90     // Delete remaining objects, if any
       
    91     TInt count = iSingletons.Count();
       
    92     for (TInt i = 0; i < count; i++) 
       
    93         {
       
    94         delete iSingletons[i].iObject;
       
    95         }
       
    96         
       
    97     // Close the singletons array
       
    98     iSingletons.Close();
       
    99     
       
   100     // Clear tls pointer
       
   101     Dll::SetTls( NULL );
       
   102 	}
       
   103 	
       
   104 // -----------------------------------------------------------------------------
       
   105 // Decrement reference count, and potentially delete object(s)
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 EXPORT_C void CGlxSingletonStore::Close(CBase* aObj)
       
   109     {
       
   110     CGlxSingletonStore* store = reinterpret_cast<CGlxSingletonStore*>(Dll::Tls());
       
   111     if ( store ) 
       
   112         {
       
   113         // Find an object by type
       
   114         TInt count = store->iSingletons.Count();
       
   115         TInt i = 0;
       
   116         while ( i < count ) 
       
   117             {
       
   118             TSingleton& singleton = store->iSingletons[i];
       
   119             if ( aObj == singleton.iObject )
       
   120                 {
       
   121                 // Found an existing object of type T
       
   122                 // Remove reference
       
   123                 singleton.iReferenceCount--;
       
   124                 GLX_LOG_INFO2("CGlxSingletonStore::Close, Ref count %d, %x", singleton.iReferenceCount, singleton.iObject);
       
   125                 
       
   126                 // Delete object if no more references
       
   127                 if ( !singleton.iReferenceCount )
       
   128                     {
       
   129                     store->iSingletons.Remove(i);
       
   130 
       
   131                     // Delete store object if last singleton
       
   132                     if ( store->iSingletons.Count() == 0 )
       
   133                     	{
       
   134                     	// CGlxSingletonStore destructor will clean up DLL's TLS pointer
       
   135                 	    delete store;
       
   136                     	}
       
   137 
       
   138                     delete aObj;
       
   139                     }
       
   140                 break;
       
   141                 }
       
   142             i++;
       
   143             }
       
   144         }    
       
   145     else 
       
   146         {
       
   147         GLX_LOG_ERROR("CGlxSingletonStore::Close - Too many calls to Close()");
       
   148         }
       
   149     }