datacommsserver/esockserver/test/TE_EsockTestSteps/inc/SimpleMapTemplate.h
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 /**
       
     2 * Copyright (c) 2005-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:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 /**
       
    21  @file SimpleMapTemplate.h
       
    22 */
       
    23 #if (!defined SIMPLEMAPTEMPLATE_H)
       
    24 #define SIMPLEMAPTEMPLATE_H
       
    25 
       
    26 #include <e32base.h>
       
    27 #include <e32std.h>
       
    28 
       
    29 
       
    30 #ifdef _DEBUG
       
    31 // Panic category for "absolutely impossible!" vanilla ASSERT()-type panics from this module
       
    32 // (if it could happen through user error then you should give it an explicit, documented, category + code)
       
    33 _LIT(KSpecAssert_ESockTestSmplMpT, "ESockTestSmplMpT");
       
    34 #endif
       
    35 
       
    36 template<class T>
       
    37 class RPointerDesMap
       
    38 	{
       
    39 public:
       
    40     inline TInt Add(T* aElement, const TDesC& aName);
       
    41     inline T* Find(const TDesC& aName);
       
    42     inline T* operator[](TInt aIndex);
       
    43     inline void Remove(const TDesC& aName);
       
    44     inline void Reset();
       
    45     inline void ResetAndDestroy();
       
    46     inline TInt Count();
       
    47 
       
    48 private:
       
    49     static TBool CompareDescriptors(const TDesC& aLeft, const TDesC& aRight);
       
    50 
       
    51 private:
       
    52     RPointerArray<TDesC> iNames;
       
    53     RPointerArray<T> iElements;
       
    54 	};
       
    55 
       
    56 template<class T>
       
    57 inline TBool RPointerDesMap<T>::CompareDescriptors(const TDesC& aLeft, const TDesC& aRight)
       
    58     {
       
    59     return (aLeft.CompareF(aRight)==0);
       
    60     }
       
    61 
       
    62 template<class T>
       
    63 inline TInt RPointerDesMap<T>::Add(T* aElement, const TDesC& aName)
       
    64     {
       
    65     __ASSERT_DEBUG(aElement!=NULL,User::Panic(_L("NULL pointer"),KErrGeneral));
       
    66 
       
    67     if (Find(aName) != NULL)
       
    68         return KErrAlreadyExists;
       
    69 
       
    70     HBufC* copy = aName.Alloc();
       
    71     if (copy == NULL)
       
    72         return KErrNoMemory;
       
    73 
       
    74     TInt error = iNames.Append(copy);
       
    75     if (error == KErrNone)
       
    76         {
       
    77         error = iElements.Append(aElement);
       
    78         if (error != KErrNone)
       
    79             {
       
    80             iNames.Remove(iNames.Count()-1);
       
    81             delete copy;
       
    82             }
       
    83         }
       
    84     return error;
       
    85     }
       
    86 
       
    87 template<class T>
       
    88 inline T* RPointerDesMap<T>::Find(const TDesC& aName)
       
    89     {
       
    90     TInt idx = iNames.Find(&aName,TIdentityRelation<TDesC>(CompareDescriptors));
       
    91     T* element = NULL;
       
    92     if (idx>=0)
       
    93         {
       
    94         element = iElements[idx];
       
    95         __ASSERT_DEBUG(element!=NULL,User::Panic(_L("NULL pointer"),KErrGeneral));
       
    96         }
       
    97     return element;
       
    98     }
       
    99     
       
   100 template<class T>
       
   101 inline T* RPointerDesMap<T>::operator[](TInt aIndex)
       
   102     {
       
   103     return iElements[aIndex];
       
   104     }
       
   105 
       
   106 template<class T>
       
   107 inline void RPointerDesMap<T>::Remove(const TDesC& aName)
       
   108     {
       
   109     TInt idx = iNames.Find(&aName,TIdentityRelation<TDesC>(CompareDescriptors));
       
   110     __ASSERT_DEBUG(idx >= 0,User::Panic(_L("Element does not exist"),KErrGeneral));
       
   111     iNames.Remove(idx);
       
   112     iElements.Remove(idx);
       
   113     }
       
   114 
       
   115 template<class T>
       
   116 inline void RPointerDesMap<T>::Reset()
       
   117     {
       
   118     iNames.Reset();
       
   119     iElements.Reset();
       
   120     }
       
   121 
       
   122 template<class T>
       
   123 inline void RPointerDesMap<T>::ResetAndDestroy()
       
   124     {
       
   125     iNames.ResetAndDestroy();
       
   126     iElements.ResetAndDestroy();
       
   127     }
       
   128     
       
   129 template<class T>
       
   130 inline TInt RPointerDesMap<T>::Count()
       
   131     {
       
   132     __ASSERT_DEBUG(iNames.Count() == iElements.Count(), User::Panic(KSpecAssert_ESockTestSmplMpT, 1));
       
   133     return iElements.Count();
       
   134     }
       
   135 
       
   136 
       
   137 #endif //SIMPLEMAPTEMPLATE_H
       
   138 
       
   139