javauis/m2g_akn/inc/JcfAutoPtr.h
branchRCL_3
changeset 19 04becd199f91
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 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 #ifndef JCFAUTOPTR_H
       
    19 #define JCFAUTOPTR_H
       
    20 
       
    21 #include <e32base.h>
       
    22 
       
    23 /**
       
    24  * Functionality of this auto ptr class is same as stl's auto_ptr class
       
    25  * except this works with cleanup stack.
       
    26  * Implementation of the example operation with "traditional" way:
       
    27  * void bar()
       
    28  *   {
       
    29  *   CFoo* myFoo = CFoo::NewL();
       
    30  *   CleanupStack::PushL(myFoo);
       
    31  *   myFoo->DoSomeThingL();
       
    32  *   CleanupStack::PopAndDestroy(1); //myFoo
       
    33  *   }
       
    34  * Implementation of the same function with auto pointer class:
       
    35  * void bar()
       
    36  *   {
       
    37  *   jcfcommon::auto_ptr<CFoo> myFoo(CFoo::NewL());
       
    38  *   myFoo->DoSomeThingL();
       
    39  *   }
       
    40  *
       
    41  */
       
    42 namespace jcfcommon
       
    43 {
       
    44 
       
    45 template<class X> class auto_ptr
       
    46 {
       
    47 public:
       
    48     typedef X element_type;
       
    49 
       
    50     auto_ptr(X* aPtr = 0): iPtr(aPtr),iCloseCalledFlag(EFalse)
       
    51     {
       
    52         CleanupStack::PushL(TCleanupItem(Close, (void*)this));
       
    53     }
       
    54 
       
    55     /**
       
    56      * Copy constructor was commented out because current
       
    57      * implementation doesn't support situation where auto_ptr
       
    58      * is returned as return value of the function. Problem is that
       
    59      * orig. auto_ptr is deleted when getBar() operation has been
       
    60      * executed and this deleted local reference has been stored to the
       
    61      * cleanup stack.
       
    62      * E.g:
       
    63      * void foo()
       
    64      *   {
       
    65      *   auto_ptr<bar> barObj = getBar();
       
    66      *       .
       
    67      *       .
       
    68      *       .
       
    69      *   User::Leave(KErrNotFound);
       
    70      *   }
       
    71      */
       
    72     /*auto_ptr(auto_ptr& aPtr): iPtr(aPtr.release())
       
    73     {
       
    74       CleanupStack::PushL(TCleanupItem(Close, (void*)this));
       
    75     }*/
       
    76 
       
    77     auto_ptr<X>& operator=(auto_ptr<X>& aRhs)
       
    78     {
       
    79         if (&aRhs != this)
       
    80         {
       
    81             delete iPtr;
       
    82             iPtr = aRhs.release();
       
    83         }
       
    84         return (*this);
       
    85     }
       
    86 
       
    87     ~auto_ptr()
       
    88     {
       
    89         if (!iCloseCalledFlag)
       
    90         {
       
    91             CleanupStack::Pop();
       
    92             delete iPtr;
       
    93         }
       
    94     }
       
    95 
       
    96     X& operator *() const
       
    97     {
       
    98         return *iPtr;
       
    99     }
       
   100     X* operator ->() const
       
   101     {
       
   102         return iPtr;
       
   103     }
       
   104 
       
   105     X* get() const
       
   106     {
       
   107         return iPtr;
       
   108     }
       
   109 
       
   110     X* release()
       
   111     {
       
   112         X* result = iPtr;
       
   113         iPtr = 0;
       
   114         return result;
       
   115     }
       
   116 
       
   117     void reset(X* aPtr = 0)
       
   118     {
       
   119         if (aPtr != iPtr)
       
   120         {
       
   121             delete iPtr;
       
   122             iPtr = aPtr;
       
   123         }
       
   124     }
       
   125 
       
   126 private:
       
   127     static void Close(void* aPtr)
       
   128     {
       
   129         auto_ptr<X>* self = (auto_ptr<X>*)aPtr;
       
   130         delete self->iPtr;
       
   131         self->iPtr = 0;
       
   132         self->iCloseCalledFlag = ETrue;
       
   133     }
       
   134 
       
   135     //Not implemented.
       
   136     auto_ptr(auto_ptr& aPtr);
       
   137 
       
   138 private:
       
   139     X* iPtr;
       
   140     TBool iCloseCalledFlag;
       
   141 };
       
   142 
       
   143 }//end jcfcommon
       
   144 
       
   145 #endif // JCFAUTOPTR_H
       
   146 
       
   147