wlan_bearer/wlanldd/wlan_common/osa_common/inc/osahandle.inl
changeset 0 c40eb8fe8501
equal deleted inserted replaced
-1:000000000000 0:c40eb8fe8501
       
     1 /*
       
     2 * Copyright (c) 2007 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 the License "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:   inline implementation of Handle
       
    15 *
       
    16 */
       
    17 
       
    18 /*
       
    19 * %version: 4 %
       
    20 */
       
    21 
       
    22 #include "algorithm.h"  // for operator !=
       
    23 #include <wlanosa.h>    // for assert
       
    24 
       
    25 // -----------------------------------------------------------------------------
       
    26 // 
       
    27 // -----------------------------------------------------------------------------
       
    28 //
       
    29 template<class T>
       
    30 inline Handle<T>::Handle( T& aElem ) 
       
    31     : iData( &aElem ), iRefCount( new TInt( 1 ) )
       
    32     {
       
    33     TraceDump(INFO_LEVEL, (("[WLAN] Handle ctor +: 0x%08x"), this));
       
    34     TraceDump(INFO_LEVEL, (("[WLAN] data: 0x%08x"), iData));
       
    35     TraceDump(INFO_LEVEL, (("[WLAN] refcount: 0x%08x"), iRefCount));
       
    36 
       
    37     if ( iRefCount )
       
    38         {
       
    39         // allocation success
       
    40         TraceDump(INFO_LEVEL, 
       
    41             (("[WLAN] reference count: %d"), *iRefCount));
       
    42         Validate();
       
    43         }
       
    44     else
       
    45         {
       
    46         // allocation failure
       
    47         TraceDump(ERROR_LEVEL, ("[WLAN] error: allocation"));
       
    48         Trace( ERROR_LEVEL, 
       
    49             reinterpret_cast<const TInt8*>(WLAN_FILE), __LINE__ );
       
    50 
       
    51         InValidate();
       
    52         }
       
    53 
       
    54     TraceDump(INFO_LEVEL, (("[WLAN] Handle ctor -: 0x%08x"), this));
       
    55     }
       
    56 
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // 
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 template<class T>
       
    63 inline Handle<T>::~Handle()
       
    64     {
       
    65     TraceDump(INFO_LEVEL, (("[WLAN] Handle dtor +: 0x%08x"), this));
       
    66 
       
    67     Dispose();
       
    68 
       
    69     TraceDump(INFO_LEVEL, (("[WLAN] Handle dtor -: 0x%08x"), this));
       
    70     }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // 
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 template<class T>
       
    77 inline Handle<T>::Handle( const Self& aElem ) 
       
    78     : iData( aElem.iData ), iRefCount( aElem.iRefCount )
       
    79     {
       
    80     TraceDump(INFO_LEVEL, (("[WLAN] Handle copy ctor +: 0x%08x"), this));
       
    81     TraceDump(INFO_LEVEL, (("[WLAN] data: 0x%08x"), iData));
       
    82     TraceDump(INFO_LEVEL, (("[WLAN] refcount: 0x%08x"), iRefCount));
       
    83 
       
    84 
       
    85     MWlanOsa::Assert( reinterpret_cast<const TInt8*>(WLAN_FILE), 
       
    86         __LINE__, (iRefCount != NULL) );
       
    87 
       
    88     ++(*iRefCount);
       
    89     TraceDump(INFO_LEVEL, (("[WLAN] reference count: %d"), *iRefCount));
       
    90 
       
    91     TraceDump(INFO_LEVEL, (("[WLAN] Handle copy ctor -: 0x%08x"), this));
       
    92     }
       
    93 
       
    94 // -----------------------------------------------------------------------------
       
    95 // 
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 template<class T>
       
    99 inline Handle<T>& Handle<T>::operator=( const Self& aElem )
       
   100     {
       
   101     TraceDump(INFO_LEVEL, 
       
   102         (("[WLAN] Handle assignement operator +: 0x%08x"), this));
       
   103 
       
   104     if ( *this != aElem )
       
   105         {
       
   106         Dispose();
       
   107 
       
   108         iData = aElem.iData;
       
   109         iRefCount = aElem.iRefCount;
       
   110 
       
   111         TraceDump(INFO_LEVEL, (("[WLAN] data: 0x%08x"), iData));
       
   112         TraceDump(INFO_LEVEL, (("[WLAN] refcount: 0x%08x"), iRefCount));
       
   113 
       
   114         MWlanOsa::Assert( reinterpret_cast<const TInt8*>(WLAN_FILE), 
       
   115             __LINE__, (iRefCount != NULL) );
       
   116 
       
   117         ++(*iRefCount);
       
   118         }
       
   119     else
       
   120         {
       
   121         // left intentionally empty
       
   122         }
       
   123 
       
   124     TraceDump(INFO_LEVEL, (("[WLAN] reference count: %d"), *iRefCount));
       
   125     TraceDump(INFO_LEVEL, 
       
   126         (("[WLAN] Handle assignement operator -: 0x%08x"), this));
       
   127 
       
   128     return *this;
       
   129     }
       
   130 
       
   131 // -----------------------------------------------------------------------------
       
   132 // 
       
   133 // -----------------------------------------------------------------------------
       
   134 //
       
   135 template<class T>
       
   136 inline T* Handle<T>::operator->()
       
   137     {
       
   138     return iData;
       
   139     }
       
   140 
       
   141 // -----------------------------------------------------------------------------
       
   142 // 
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 template<class T>
       
   146 inline T* Handle<T>::Data()
       
   147     {
       
   148     MWlanOsa::Assert( reinterpret_cast<const TInt8*>(WLAN_FILE), 
       
   149         __LINE__, (iData != NULL) );
       
   150     return iData;
       
   151     }
       
   152 
       
   153 // -----------------------------------------------------------------------------
       
   154 // 
       
   155 // -----------------------------------------------------------------------------
       
   156 //
       
   157 template<class T>
       
   158 inline const T* Handle<T>::Data() const
       
   159     {
       
   160     MWlanOsa::Assert( reinterpret_cast<const TInt8*>(WLAN_FILE), 
       
   161         __LINE__, (iData != NULL) );
       
   162     return iData;
       
   163     }
       
   164 
       
   165 // -----------------------------------------------------------------------------
       
   166 // only upon allocation failure return EFalse
       
   167 // -----------------------------------------------------------------------------
       
   168 //
       
   169 template<class T>
       
   170 inline TBool Handle<T>::Bind( T& aElem )
       
   171     {
       
   172     TraceDump(INFO_LEVEL, (("[WLAN] Handle::Bind +: 0x%08x"), this));
       
   173 
       
   174     TBool ret( ETrue );
       
   175 
       
   176     if ( aElem != *iData )
       
   177         {
       
   178         if ( --(*iRefCount) == 0 )
       
   179             {
       
   180             TraceDump(INFO_LEVEL, 
       
   181                 ("[WLAN] Handle::Bind deallocate referenced data"));
       
   182             TraceDump(INFO_LEVEL, 
       
   183                 (("[WLAN] old data delloac: 0x%08x"), iData));
       
   184             TraceDump(INFO_LEVEL, (("[WLAN] refcount: 0x%08x"), iRefCount));
       
   185 
       
   186             delete iData;
       
   187             ++(*iRefCount); // recycle counter
       
   188             }
       
   189         else
       
   190             {
       
   191             // references still exist after this critter is a goner
       
   192             // as we are doing a rebind we shall create a new counter
       
   193             iRefCount = new TInt( 1 );
       
   194             if ( !iRefCount )
       
   195                 {
       
   196                 TraceDump(ERROR_LEVEL, ("[WLAN] error: allocation"));
       
   197                 Trace( ERROR_LEVEL, 
       
   198                     reinterpret_cast<const TInt8*>(WLAN_FILE), __LINE__ );
       
   199 
       
   200                 ret = EFalse;
       
   201                 }
       
   202             else
       
   203                 {
       
   204                 // allocation success
       
   205                 // left intentionally empty
       
   206                 TraceDump(INFO_LEVEL, 
       
   207                     (("[WLAN] new refcount: 0x%08x"), iRefCount));
       
   208                 }
       
   209             }
       
   210 
       
   211         iData = &aElem;
       
   212         TraceDump(INFO_LEVEL, (("[WLAN] new data: 0x%08x"), iData));
       
   213         }
       
   214     else
       
   215         {
       
   216         // left intentionally empty
       
   217         }
       
   218 
       
   219     TraceDump(INFO_LEVEL, (("[WLAN] reference count: %d"), *iRefCount));
       
   220     TraceDump(INFO_LEVEL, (("[WLAN] Handle::Bind -: 0x%08x"), this));
       
   221 
       
   222     return ret;
       
   223     }
       
   224 
       
   225 // -----------------------------------------------------------------------------
       
   226 // 
       
   227 // -----------------------------------------------------------------------------
       
   228 //
       
   229 template<class T>
       
   230 inline void Handle<T>::Dispose()
       
   231     {
       
   232     TraceDump(INFO_LEVEL, (("[WLAN] Handle::Dispose +: 0x%08x"), this));
       
   233 
       
   234     if ( IsValid() )
       
   235         {
       
   236         // we are a valid object
       
   237         MWlanOsa::Assert( reinterpret_cast<const TInt8*>(WLAN_FILE), 
       
   238             __LINE__, (iRefCount != NULL) );
       
   239 
       
   240         if ( --(*iRefCount) == 0 )
       
   241             {
       
   242             TraceDump(INFO_LEVEL, 
       
   243                 ("[WLAN] Handle::Dispose deallocate referenced data"));
       
   244 
       
   245             TraceDump(INFO_LEVEL, (("[WLAN] data: 0x%08x"), iData));
       
   246             delete iData;
       
   247             TraceDump(INFO_LEVEL, (("[WLAN] refcount: 0x%08x"), iRefCount));
       
   248             delete iRefCount;
       
   249             }
       
   250         else
       
   251             {
       
   252             // references still exist after this critter is a goner
       
   253             // left intentionally empty
       
   254             TraceDump(INFO_LEVEL, 
       
   255                 (("[WLAN] reference count: %d"), *iRefCount));
       
   256             }
       
   257         }
       
   258     else
       
   259         {
       
   260         // we are an invalid object
       
   261         // left intentionally empty
       
   262         }
       
   263 
       
   264     TraceDump(INFO_LEVEL, (("[WLAN] Handle::Dispose -: 0x%08x"), this));
       
   265     }