ximpfw/core/srcdatamodel/ximpobjectcollectionimp.cpp
changeset 51 61fad867f68e
equal deleted inserted replaced
-1:000000000000 51:61fad867f68e
       
     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:  MXIMPObjectCollection implementation
       
    15  *
       
    16 */
       
    17 
       
    18 #include "ximpobjectcollectionimp.h"
       
    19 
       
    20 
       
    21 // ============================ HELPER CLASS =============================
       
    22 
       
    23 // ---------------------------------------------------------------------------
       
    24 // CXIMPCollectionElement::NewL
       
    25 // ---------------------------------------------------------------------------
       
    26 //
       
    27 CXIMPObjectCollectionImp::CXIMPCollectionElement* 
       
    28 CXIMPObjectCollectionImp::CXIMPCollectionElement::NewL(
       
    29         CXIMPApiObjBase* aObject, const TDesC8& aName )
       
    30     {
       
    31     CXIMPCollectionElement* self = new ( ELeave ) CXIMPCollectionElement();
       
    32     CleanupStack::PushL( self );
       
    33     self->ConstructL( aObject, aName );
       
    34     CleanupStack::Pop( self ); 
       
    35     return self;
       
    36     }
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 // CXIMPCollectionElement::ConstructL
       
    40 // ---------------------------------------------------------------------------
       
    41 //
       
    42 void CXIMPObjectCollectionImp::CXIMPCollectionElement::ConstructL(
       
    43         CXIMPApiObjBase* aObject, const TDesC8& aName )
       
    44     {
       
    45     iObject = aObject;
       
    46 
       
    47     if ( aName.Length() > 0 )
       
    48         {
       
    49         iName = aName.AllocL();
       
    50         }
       
    51     else
       
    52         {
       
    53         // at least an empty one always exists
       
    54         iName = HBufC8::NewL( 0 );
       
    55         }
       
    56     }
       
    57 
       
    58 // ---------------------------------------------------------------------------
       
    59 // CXIMPCollectionElement::CXIMPCollectionElement
       
    60 // ---------------------------------------------------------------------------
       
    61 //
       
    62 CXIMPObjectCollectionImp::CXIMPCollectionElement::CXIMPCollectionElement()
       
    63     {
       
    64     }
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // CXIMPCollectionElement::~CXIMPCollectionElement
       
    68 // ---------------------------------------------------------------------------
       
    69 //
       
    70 CXIMPObjectCollectionImp::CXIMPCollectionElement::~CXIMPCollectionElement()
       
    71     {
       
    72     delete iObject;
       
    73     delete iName;
       
    74     }
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // CXIMPCollectionElement::GetObject
       
    78 // ---------------------------------------------------------------------------
       
    79 //
       
    80 CXIMPApiObjBase* CXIMPObjectCollectionImp::CXIMPCollectionElement::GetObject()
       
    81     {
       
    82     CXIMPApiObjBase* obj = iObject;
       
    83     iObject = NULL;
       
    84     return obj;
       
    85     }
       
    86 
       
    87 // ============================ MEMBER FUNCTIONS =============================
       
    88 
       
    89 // ---------------------------------------------------------------------------
       
    90 // CXIMPObjectCollectionImp::NewL()
       
    91 // ---------------------------------------------------------------------------
       
    92 //
       
    93 EXPORT_C CXIMPObjectCollectionImp* CXIMPObjectCollectionImp::NewL()
       
    94     {
       
    95     CXIMPObjectCollectionImp* self = NewLC();
       
    96     CleanupStack::Pop( self );
       
    97     return self;
       
    98     }
       
    99 
       
   100 
       
   101 // ---------------------------------------------------------------------------
       
   102 // CXIMPObjectCollectionImp::NewLC()
       
   103 // ---------------------------------------------------------------------------
       
   104 //
       
   105 EXPORT_C CXIMPObjectCollectionImp* CXIMPObjectCollectionImp::NewLC()
       
   106     {
       
   107     CXIMPObjectCollectionImp* self = new ( ELeave ) CXIMPObjectCollectionImp;
       
   108     CleanupStack::PushL( self );
       
   109     return self;
       
   110     }
       
   111 
       
   112 
       
   113 // ---------------------------------------------------------------------------
       
   114 // CXIMPObjectCollectionImp::~CXIMPObjectCollectionImp()
       
   115 // ---------------------------------------------------------------------------
       
   116 //
       
   117 CXIMPObjectCollectionImp::~CXIMPObjectCollectionImp()
       
   118     {
       
   119     iCollection.ResetAndDestroy();
       
   120     }
       
   121 
       
   122 // ---------------------------------------------------------------------------
       
   123 // CXIMPObjectCollectionImp::CXIMPObjectCollectionImp()
       
   124 // ---------------------------------------------------------------------------
       
   125 //
       
   126 CXIMPObjectCollectionImp::CXIMPObjectCollectionImp()
       
   127     {
       
   128     }
       
   129 
       
   130 
       
   131 // ---------------------------------------------------------------------------
       
   132 // Implement supported interface access.
       
   133 // ---------------------------------------------------------------------------
       
   134 //
       
   135 XIMPIMP_IF_BASE_GET_INTERFACE_BEGIN( CXIMPObjectCollectionImp,
       
   136                                      MXIMPObjectCollection )
       
   137 XIMPIMP_IF_BASE_GET_INTERFACE_END()
       
   138 
       
   139 
       
   140 XIMPIMP_IF_BASE_GET_CONST_INTERFACE_BEGIN( CXIMPObjectCollectionImp,
       
   141                                            MXIMPObjectCollection )
       
   142 XIMPIMP_IF_BASE_GET_INTERFACE_END()
       
   143 
       
   144 
       
   145 
       
   146 // ---------------------------------------------------------------------------
       
   147 // CXIMPObjectCollectionImp::LookupByType()
       
   148 // ---------------------------------------------------------------------------
       
   149 //
       
   150 void CXIMPObjectCollectionImp::LookupByType(
       
   151         const MXIMPBase*& aObject,
       
   152         TInt32 aInterfaceId ) const
       
   153     {
       
   154     TInt start = FindLRUIndex( aObject );
       
   155     
       
   156     aObject = NULL;
       
   157     
       
   158     // now find the object without name
       
   159     for ( TInt i = start; i < iCollection.Count(); i++ )
       
   160         {
       
   161         const CXIMPObjectCollectionImp::CXIMPCollectionElement* element = iCollection[ i ];
       
   162 
       
   163         if ( ! element->iName->Length() )
       
   164             {
       
   165             // no name, ok
       
   166             if ( aInterfaceId == element->iObject->Base().GetInterfaceId() )
       
   167                 {
       
   168                 // got a match!
       
   169                 aObject = &(element->iObject->Base());
       
   170                 return;
       
   171                 }
       
   172             }
       
   173         }
       
   174 
       
   175     return;
       
   176     }
       
   177 
       
   178 // ---------------------------------------------------------------------------
       
   179 // CXIMPObjectCollectionImp::LookupByTypeAndName()
       
   180 // ---------------------------------------------------------------------------
       
   181 //
       
   182 void CXIMPObjectCollectionImp::LookupByTypeAndName(
       
   183                 const MXIMPBase*& aObject,
       
   184                 TInt32 aInterfaceId,
       
   185                 const TDesC8& aMatch ) const
       
   186     {
       
   187     TInt start = FindLRUIndex( aObject );
       
   188 
       
   189     aObject = NULL;
       
   190 
       
   191     // now find the object with name
       
   192     for ( TInt i = start; i < iCollection.Count(); i++ )
       
   193         {
       
   194         const CXIMPObjectCollectionImp::CXIMPCollectionElement* element = iCollection[ i ];
       
   195 
       
   196         if ( ( KErrNotFound != element->iName->MatchF( aMatch ) ) 
       
   197                 && ( aInterfaceId == element->iObject->Base().GetInterfaceId() ) )
       
   198             {
       
   199             // got a match with correct name and interface id
       
   200             aObject = &(element->iObject->Base());
       
   201             return;
       
   202             }
       
   203         }
       
   204 
       
   205     return;
       
   206     }
       
   207 
       
   208 
       
   209 // ---------------------------------------------------------------------------
       
   210 // CXIMPObjectCollectionImp::AddObjectL
       
   211 // ---------------------------------------------------------------------------
       
   212 //
       
   213 void CXIMPObjectCollectionImp::AddObjectL(
       
   214         MXIMPBase* aObject )
       
   215     {
       
   216     AddObjectWithNameL( aObject, KNullDesC8 );
       
   217     }
       
   218 
       
   219 // ---------------------------------------------------------------------------
       
   220 // CXIMPObjectCollectionImp::AddObjectWithNameL
       
   221 // ---------------------------------------------------------------------------
       
   222 //
       
   223 void CXIMPObjectCollectionImp::AddObjectWithNameL(
       
   224         MXIMPBase* aObject,
       
   225         const TDesC8& aName )
       
   226     {
       
   227     // NULL is not accepted
       
   228     User::LeaveIfError( aObject == NULL ? KErrArgument : KErrNone );
       
   229     
       
   230     // name must not contain wildcards
       
   231     User::LeaveIfError( ValidateName( aName ) );
       
   232 
       
   233     CXIMPApiObjBase* base = ( CXIMPApiObjBase* ) 
       
   234         aObject->GetInterface( XIMPIMP_CLSID_CXIMPAPIOBJBASE, EPanicIfUnknown ); 
       
   235     CXIMPObjectCollectionImp::CXIMPCollectionElement* element = 
       
   236         CXIMPObjectCollectionImp::CXIMPCollectionElement::NewL( base, aName );
       
   237     CleanupStack::PushL( element );
       
   238     iCollection.AppendL( element );
       
   239     CleanupStack::Pop( element ); 
       
   240     }
       
   241 
       
   242 // ---------------------------------------------------------------------------
       
   243 // CXIMPObjectCollectionImp::GetByType
       
   244 // ---------------------------------------------------------------------------
       
   245 //
       
   246 TBool CXIMPObjectCollectionImp::GetByType(
       
   247         MXIMPBase*& aObject,
       
   248         TInt32 aInterfaceId )
       
   249     {
       
   250     aObject = NULL;
       
   251 
       
   252     // now find the object without name
       
   253     for ( TInt i = 0; i < iCollection.Count(); i++ )
       
   254         {
       
   255         CXIMPObjectCollectionImp::CXIMPCollectionElement* element = iCollection[ i ];
       
   256 
       
   257         if ( ! element->iName->Length() )
       
   258             {
       
   259             // no name, ok
       
   260             if ( aInterfaceId == element->iObject->Base().GetInterfaceId() )
       
   261                 {
       
   262                 // got a match!
       
   263                 CXIMPApiObjBase* object = element->GetObject(); 
       
   264                 iCollection.Remove( i );
       
   265                 delete element;
       
   266                 aObject = &(object->Base());
       
   267                 return ETrue;
       
   268                 }
       
   269             }
       
   270         }
       
   271 
       
   272     return EFalse;
       
   273     }
       
   274 
       
   275 // ---------------------------------------------------------------------------
       
   276 // CXIMPObjectCollectionImp::GetByTypeAndName
       
   277 // ---------------------------------------------------------------------------
       
   278 //
       
   279 TBool CXIMPObjectCollectionImp::GetByTypeAndName(
       
   280         MXIMPBase*& aObject,
       
   281         TInt32 aInterfaceId,
       
   282         const TDesC8& aMatch )
       
   283     {
       
   284     aObject = NULL;
       
   285 
       
   286     // now find the object with name
       
   287     for ( TInt i = 0; i < iCollection.Count(); i++ )
       
   288         {
       
   289         CXIMPObjectCollectionImp::CXIMPCollectionElement* element = iCollection[ i ];
       
   290 
       
   291         if ( ( KErrNotFound != element->iName->MatchF( aMatch ) ) 
       
   292                 && ( aInterfaceId == element->iObject->Base().GetInterfaceId() ) )
       
   293             {
       
   294             // got a match with correct name and interface id
       
   295             CXIMPApiObjBase* object = element->GetObject(); 
       
   296             iCollection.Remove( i );
       
   297             delete element;
       
   298             aObject = &(object->Base());
       
   299             return ETrue;
       
   300             }
       
   301         }
       
   302 
       
   303     return EFalse;
       
   304     }
       
   305 
       
   306 // ---------------------------------------------------------------------------
       
   307 // CXIMPObjectCollectionImp::FindLRUIndex()
       
   308 // ---------------------------------------------------------------------------
       
   309 //
       
   310 TInt CXIMPObjectCollectionImp::FindLRUIndex( const MXIMPBase*& aObject ) const
       
   311     {
       
   312     TInt start = 0;
       
   313 
       
   314     // naive lookup, least recently used (from beginning to the end).
       
   315     // find starting position
       
   316     if ( aObject != NULL )
       
   317         {
       
   318         TBool found = EFalse;
       
   319         for ( TInt i = 0; ( i < iCollection.Count()-1 ) && ( ! found ); i++ )
       
   320             {
       
   321             const CXIMPObjectCollectionImp::CXIMPCollectionElement* element = iCollection[ i ];
       
   322             if ( aObject == &(element->iObject->Base()) )
       
   323                 {
       
   324                 found = ETrue;
       
   325                 start = i;
       
   326                 }
       
   327             }
       
   328         }
       
   329 
       
   330     return start;
       
   331     }
       
   332 
       
   333 // ---------------------------------------------------------------------------
       
   334 // CXIMPObjectCollectionImp::ValidateName()
       
   335 // ---------------------------------------------------------------------------
       
   336 //
       
   337 TInt CXIMPObjectCollectionImp::ValidateName( const TDesC8& aName ) const
       
   338     {
       
   339     const TUint8* pName = aName.Ptr();
       
   340 
       
   341     TInt pNameLen = aName.Length();
       
   342     for( ; pNameLen; pName++, pNameLen-- )
       
   343         {
       
   344         if( *pName > 0x7e || 
       
   345             *pName < 0x20 || 
       
   346             *pName == '*' || 
       
   347             *pName == '?' || 
       
   348             *pName == ':' )
       
   349             {
       
   350             return KErrBadName;                
       
   351             }
       
   352         }
       
   353 
       
   354 	return KErrNone;
       
   355 	}
       
   356 
       
   357 
       
   358 
       
   359 // End of file