ncdengine/engine/src/catalogsinterfacebase.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2006 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:   Contains CCatalogsInterfaceBase class implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "catalogsinterfacebase.h"
       
    20 #include "catalogsinterfaceidentifier.h"
       
    21 #include "catalogsdebug.h"
       
    22 
       
    23 
       
    24 CCatalogsInterfaceBase::CCatalogsInterfaceBase( CCatalogsInterfaceBase* aParent )
       
    25 : CBase(),
       
    26   iParent( aParent )
       
    27     {
       
    28     
       
    29     }
       
    30   
       
    31 
       
    32 CCatalogsInterfaceBase::~CCatalogsInterfaceBase()
       
    33     {
       
    34     DLTRACE(("this-ptr: %x", this));
       
    35     iInterfaces.ResetAndDestroy();    
       
    36     }
       
    37 
       
    38 
       
    39 void CCatalogsInterfaceBase::SetParentL( CCatalogsInterfaceBase* aParent )
       
    40     {
       
    41     DLTRACEIN(("this-ptr: %x", this));
       
    42     
       
    43     if ( aParent == this )
       
    44         {
       
    45         // We do not accept this object to be its own parent.
       
    46         DLTRACEOUT(("Parent is same as the object itself"))
       
    47         return;
       
    48         }
       
    49 
       
    50     if ( iParent != NULL )
       
    51         {
       
    52         DLERROR(("Object already had a parent"));
       
    53         
       
    54         // This option can not be accepted, because problems would arise when
       
    55         // transferring interfaces from this class object to the new parent. Or,
       
    56         // if this class object should be made the top root, then it would not be
       
    57         // known which interfaces should be moved here.
       
    58         // Only parent knows the interfaces and children do not know their own.
       
    59         // So, we do not know which interfaces should be removed from the parent
       
    60         // and which should be left.
       
    61         
       
    62         DASSERT( EFalse );
       
    63         
       
    64         User::Leave( KErrArgument );
       
    65         }
       
    66     else if ( aParent == NULL )
       
    67         {
       
    68         DLTRACEOUT(("Nothing needs to be done, because NULL parent was given"));
       
    69 
       
    70         // Because the parent of this object is NULL and the new parent is NULL
       
    71         // then nothing needs to be done. In other words, this object remains to be
       
    72         // the top parent.
       
    73         
       
    74         return;
       
    75         }
       
    76 
       
    77     // This class object was top parent itself and the new parent seems to be acceptable.
       
    78     // Continue by moving interfaces from this class object to the new top parent.
       
    79         
       
    80     DLINFO(("New parent object was given"));
       
    81 
       
    82     // This class object was a top parent itself.
       
    83     // So, move all the interface elements from this list to the
       
    84     // list of the new parent.
       
    85     CCatalogsInterfaceIdentifier* interfaceIdentifier( NULL );
       
    86     TInt interfaceError( KErrNone );
       
    87     while ( iInterfaces.Count() > 0 )
       
    88         {
       
    89         interfaceIdentifier = iInterfaces[ 0 ];
       
    90         // Notice that new parent takes the ownership of the interface.
       
    91         // If the new parent is not the top parent, these interfaces will
       
    92         // be forwarded to the top parent automatically.
       
    93         // TRAP updates the error value to KErrNone everytime if no error occurs.
       
    94         TRAP( interfaceError, aParent->AddInterfaceL( interfaceIdentifier ) );
       
    95         if ( interfaceError != KErrNone )
       
    96             {
       
    97             DLERROR(("Could not add interface to the parent: %d", interfaceError));
       
    98             DASSERT( EFalse );
       
    99             // Because the ownership of the interface could not been transferred.
       
   100             // delete the interface here. There is nothing much else that we could do.
       
   101             delete interfaceIdentifier;
       
   102             interfaceIdentifier = NULL;
       
   103             }
       
   104         // Remove the interface from the list because it has been moved above.
       
   105         iInterfaces.Remove( 0 );
       
   106         }
       
   107     
       
   108     // The old parent value was NULL because this function is allower only for the
       
   109     // top parent. Update the parent pointer.
       
   110     iParent = aParent;
       
   111 
       
   112     // The top parent also keeps track of the reference counts of all its children.
       
   113     // Update the reference counts of the new parent.
       
   114     // Notice that the parent reference count always contains the sum of all the reference
       
   115     // counts of its children and their children etc. So, add this information to the new
       
   116     // parent.
       
   117     for ( TInt i = 0; i < iRefCount; ++i )
       
   118         {
       
   119         iParent->AddRef();
       
   120         }
       
   121     for ( TInt i = 0; i < iInternalRefCount; ++i )
       
   122         {
       
   123         iParent->InternalAddRef();
       
   124         }        
       
   125 
       
   126     DLTRACEOUT((""));
       
   127     }
       
   128 
       
   129 
       
   130 void CCatalogsInterfaceBase::RemoveFromParent()
       
   131     {
       
   132     DLTRACEIN(("this-ptr: %x", this));
       
   133     
       
   134     if ( iParent == NULL )
       
   135         {
       
   136         DLTRACEOUT(("This is a top parent itself."));
       
   137         // Nothing to do here.
       
   138         return;
       
   139         }
       
   140 
       
   141     // This class object has a parent
       
   142     // Release old parent counter values according 
       
   143     // to the values this object has. Do not change the values
       
   144     // of this object.
       
   145     for ( TInt i = 0; i < iRefCount; ++i )
       
   146         {
       
   147         iParent->Release();
       
   148         }
       
   149     for ( TInt i = 0; i < iInternalRefCount; ++i )
       
   150         {
       
   151         iParent->InternalRelease();
       
   152         }
       
   153         
       
   154     // Set the parent to be NULL, because this will act as its own parent after this.
       
   155     iParent = NULL;
       
   156 
       
   157     DLTRACEOUT((""));
       
   158     }
       
   159  
       
   160     
       
   161 void CCatalogsInterfaceBase::AddInterfaceL( CCatalogsInterfaceIdentifier* aInterface )
       
   162     {
       
   163     DLTRACE(("this-ptr: %x", this));
       
   164 
       
   165     if ( aInterface != NULL )
       
   166         {
       
   167         // All the interfaces exist in the top parent. So, if this object 
       
   168         // has parent, delegate query to it.
       
   169         if ( iParent != NULL )
       
   170             {
       
   171             iParent->AddInterfaceL( aInterface );
       
   172             }
       
   173         else
       
   174             {
       
   175             // This is a top parent.
       
   176             // If old interface of the same type exists, then remove
       
   177             // it because only one of the kind is accepted.
       
   178             RemoveInterface( aInterface->InterfaceId() );
       
   179             CleanupStack::PushL( aInterface );
       
   180             iInterfaces.AppendL( aInterface );
       
   181             CleanupStack::Pop( aInterface );
       
   182             }
       
   183         }
       
   184     }
       
   185     
       
   186     
       
   187 void CCatalogsInterfaceBase::RemoveInterface( TInt aId )
       
   188     {
       
   189     DLTRACE(("this-ptr: %x", this));
       
   190 
       
   191     if ( iParent != NULL )
       
   192         {
       
   193         // All the interfaces exist in the top parent. So, if this 
       
   194         // has parent, delegate query to it.
       
   195         iParent->RemoveInterface( aId );
       
   196         }
       
   197     else
       
   198         {
       
   199         // This is a top parent
       
   200         for ( TInt i = 0; i < iInterfaces.Count(); ++i )
       
   201             {
       
   202             if ( iInterfaces[ i ]->InterfaceId() == aId )
       
   203                 {
       
   204                 delete iInterfaces[i];
       
   205                 iInterfaces.Remove( i );
       
   206                 break;
       
   207                 }
       
   208             }        
       
   209         }    
       
   210     }
       
   211 
       
   212 TInt CCatalogsInterfaceBase::TotalRefCount() const
       
   213     {
       
   214     DLTRACE(("this-ptr: %x", this));
       
   215 
       
   216     if ( iParent != NULL )
       
   217         {
       
   218         // Parent has all the information
       
   219         return iParent->TotalRefCount();
       
   220         }
       
   221     else
       
   222         {
       
   223         // This is a top parent. So, return the information.
       
   224         return RefCount() + InternalRefCount();        
       
   225         }
       
   226     }
       
   227 
       
   228 TInt CCatalogsInterfaceBase::InternalRefCount() const
       
   229     {
       
   230     DLTRACEIN(("this-ptr: %x", this));
       
   231 
       
   232     if ( iParent != NULL )
       
   233         {
       
   234         // Parent has all the information.
       
   235         return iParent->InternalRefCount();
       
   236         }
       
   237     else
       
   238         {
       
   239         DLTRACEOUT(( "InternalRefCount: %d", iInternalRefCount ));
       
   240         // This is a top parent
       
   241         return iInternalRefCount;        
       
   242         }
       
   243     }
       
   244 
       
   245 TInt CCatalogsInterfaceBase::InternalAddRef()
       
   246     {
       
   247     DLTRACE(("this-ptr: %x", this));
       
   248 
       
   249     // Increase the reference counter for all the
       
   250     // objects starting from this object to the top parent.
       
   251     iInternalRefCount++;
       
   252     
       
   253     if ( iParent != NULL )
       
   254         {
       
   255         // Parent has all the information
       
   256         return iParent->InternalAddRef();
       
   257         }
       
   258     else
       
   259         {
       
   260         DLTRACE(( "InternalRefCount: %d", iInternalRefCount ));
       
   261         // This is a top parent.
       
   262         return iInternalRefCount;            
       
   263         }
       
   264     }
       
   265     
       
   266 TInt CCatalogsInterfaceBase::InternalRelease()
       
   267     {
       
   268     DLTRACE(("this-ptr: %x", this));
       
   269     DASSERT( iInternalRefCount > 0 );
       
   270 
       
   271     // Descrease the reference counter for all the
       
   272     // objects starting from this object to the top parent.
       
   273     if ( iInternalRefCount > 0 )
       
   274         {
       
   275         // Do not let the counter to go below zero.
       
   276         --iInternalRefCount;            
       
   277         }
       
   278         
       
   279     if ( iParent != NULL )
       
   280         {
       
   281         // Parent has all the information
       
   282         return iParent->InternalRelease();
       
   283         }
       
   284     else
       
   285         {
       
   286         // This is parent.
       
   287         if ( TotalRefCount() > 0 )
       
   288             {
       
   289             DLTRACE(( "InternalRefCount: %d", iInternalRefCount ));
       
   290             return iInternalRefCount;
       
   291             }
       
   292         else
       
   293             {
       
   294             DLTRACE(("Deleting this: %x", this));
       
   295             // No references are left for this object.
       
   296             delete this;
       
   297             return 0;
       
   298             }            
       
   299         }
       
   300     }
       
   301 
       
   302 TInt CCatalogsInterfaceBase::MyTotalRefCount() const
       
   303     {
       
   304     DLTRACE(("this-ptr: %x", this));
       
   305 
       
   306     return MyRefCount() + MyInternalRefCount();        
       
   307     }
       
   308 
       
   309 TInt CCatalogsInterfaceBase::MyRefCount() const
       
   310     {
       
   311     DLTRACE(("this-ptr: %x", this));
       
   312 
       
   313     return iRefCount;        
       
   314     }
       
   315 
       
   316 TInt CCatalogsInterfaceBase::MyInternalRefCount() const
       
   317     {
       
   318     DLTRACE(("this-ptr: %x", this));
       
   319     
       
   320     return iInternalRefCount;        
       
   321     }
       
   322 
       
   323 
       
   324 // MCatalogsBase functions
       
   325     
       
   326  
       
   327 TInt CCatalogsInterfaceBase::AddRef() const
       
   328     {    
       
   329 
       
   330     // Increase the reference counter for all the
       
   331     // objects starting from this object to the top parent.
       
   332     iRefCount++;
       
   333 
       
   334     if ( iParent != NULL )
       
   335         {
       
   336         // Parent has all the info.
       
   337         DLTRACE(( "this-ptr: %x, RefCount: %d", this, iRefCount ));
       
   338         return iParent->AddRef();
       
   339         }
       
   340     else
       
   341         {
       
   342         DLTRACE(( "this-ptr: %x, RefCount: %d", this, iRefCount ));
       
   343         return iRefCount;        
       
   344         }
       
   345     }
       
   346 
       
   347 TInt CCatalogsInterfaceBase::Release() const
       
   348     {    
       
   349     DASSERT( iRefCount > 0 );
       
   350 
       
   351     // Descrease the reference counter for all the
       
   352     // objects starting from this object to the top parent.
       
   353     if ( iRefCount > 0 )
       
   354         {
       
   355         // Do not let the counter to go below zero.
       
   356         --iRefCount;            
       
   357         }
       
   358         
       
   359     if ( iParent != NULL )
       
   360         {
       
   361         DLTRACE((("this-ptr: %x, RefCount: %d"), this, iRefCount ));
       
   362         // Parent has all the info.
       
   363         return iParent->Release();        
       
   364         }
       
   365     else
       
   366         {
       
   367         // This is parent
       
   368         if ( TotalRefCount() > 0 )
       
   369             {
       
   370             DLTRACE(( "RefCount: %d, this: %x", iRefCount, this ));
       
   371             return iRefCount;
       
   372             }
       
   373         else
       
   374             {
       
   375             DLTRACE(("Deleting this: %x", this));
       
   376             // No references are left for this object.
       
   377             delete this;
       
   378             return 0;
       
   379             }        
       
   380         }
       
   381     }
       
   382 
       
   383 TInt CCatalogsInterfaceBase::RefCount() const
       
   384     {    
       
   385     if ( iParent != NULL )
       
   386         {
       
   387         // Parent has all the info
       
   388         return iParent->RefCount();
       
   389         }
       
   390     else
       
   391         {
       
   392         DLTRACE(( "RefCount: %d", iRefCount ));
       
   393         // This is parent
       
   394         return iRefCount;    
       
   395         }
       
   396     }
       
   397 
       
   398 
       
   399 const TAny* CCatalogsInterfaceBase::QueryInterfaceL( TInt aInterfaceType ) const
       
   400     {
       
   401     DLTRACEIN(( "Interface: %d, this-ptr: %x", aInterfaceType, this ));
       
   402 
       
   403     // All the interfaces exist in the parent. So, if this 
       
   404     // has parent, delegate query to it.
       
   405     if ( iParent != NULL )
       
   406         {
       
   407         // The parent has all the info
       
   408         return iParent->QueryInterfaceL( aInterfaceType );
       
   409         }
       
   410     else
       
   411         {
       
   412         // This is parent.
       
   413         // Check the interfaces from the list and return the interface
       
   414         // that matches the given type
       
   415         for ( TInt i = 0; i < iInterfaces.Count(); ++i )
       
   416             {
       
   417             if ( iInterfaces[ i ]->InterfaceId() == aInterfaceType )
       
   418                 {
       
   419                 // Match was found
       
   420                 const TAny* interface( iInterfaces[ i ]->InterfaceObject() );
       
   421                 const CCatalogsInterfaceBase* interfaceBase( iInterfaces[ i ]->InterfaceBaseObject() );                
       
   422 
       
   423                 // Because interface is returned, the ref count has to be increased.
       
   424                 interfaceBase->AddRef();
       
   425 
       
   426                 DLTRACEOUT(( "Interface found: %X", interface ));
       
   427                 // Return interface, not interfaceBase, because interfaceBase is a pointer to
       
   428                 // the base class. They may have different pointer because of "public virtual <class>"
       
   429                 // inheritance.
       
   430                 return interface;            
       
   431                 }
       
   432             }
       
   433 
       
   434         // Interface was not found
       
   435         DLTRACEOUT( ( "return NULL" ) );        
       
   436         return NULL;        
       
   437         }
       
   438     }
       
   439 
       
   440