lowlevellibsandfws/genericusabilitylib/inc/emanaged.h
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #ifndef EMANAGED_H
       
    17 #define EMANAGED_H
       
    18 
       
    19 #include <e32base.h>
       
    20 
       
    21 #include <typerel.h>
       
    22 #include <swap.h>
       
    23 
       
    24 
       
    25 
       
    26 
       
    27 /**
       
    28    @file
       
    29    @brief Utility class templates that provide RAII-based automatic
       
    30    resource management.
       
    31 
       
    32 	 @publishedAll
       
    33 	 @released
       
    34 */
       
    35 
       
    36 
       
    37   /**
       
    38      Implementation function.In order to override the default cleanup
       
    39      strategy for a particular type, use the provided
       
    40      DEFINE_CLEANUP_FUNCTION utility macro
       
    41      @internalComponent
       
    42   */
       
    43 // Not for Client Use , Only to be used Internally.
       
    44 template<class T>
       
    45 inline void CallCleanupFunction(T* aObjPtr)
       
    46 	{
       
    47 	aObjPtr->Close();
       
    48 	}
       
    49 
       
    50 
       
    51 /**
       
    52 Utility macro that can be used for defining the cleanup member
       
    53 function for a class (typically a R-class).
       
    54 
       
    55 This macro can be used in the same namespace in which the R-class is
       
    56 defined or in a namespace in which the R-class is used.
       
    57 
       
    58 Example:
       
    59 
       
    60 class RDestroyableClass
       
    61 	{
       
    62   public:
       
    63 	// ...
       
    64 	void Destroy(); // member function used for cleanup and releasing the resources owned by a RDestroyableClass object
       
    65 	// ...
       
    66 	};
       
    67 
       
    68 DEFINE_CLEANUP_FUNCTION(RDestroyableClass, Destroy)
       
    69 
       
    70 @param AClass the name of the class
       
    71 @param CleanupMemFun the name of the cleanup member function of the class
       
    72  */
       
    73 #define DEFINE_CLEANUP_FUNCTION(AClass, CleanupMemFun)	\
       
    74 	inline void CallCleanupFunction(AClass* aObjPtr)	\
       
    75 		{												\
       
    76 		aObjPtr->CleanupMemFun();						\
       
    77 		}
       
    78 
       
    79 /**
       
    80 Utility macro that can be used for specializing the default cleanup
       
    81 strategy class template TResourceCleanupStrategy for a particular
       
    82 class (typically a R-class).  The default cleanup strategy for a class
       
    83 specified using DEFINE_CLEANUP_STRATEGY overrides any other cleanup
       
    84 strategy specified using DEFINE_CLEANUP_FUNCTION for that class.
       
    85 
       
    86 This macro must be used in the same namespace in which the R-class is
       
    87 defined.
       
    88 
       
    89 
       
    90    Utility macro that can be used for enabling single phase
       
    91    construction for CBase-derived classes. This is necessary because
       
    92    Symbian OS currently lacks the placement delete operator
       
    93    counterparts corresponding to the placement new operators that take
       
    94    a TLeave parameter (new(ELeave)), which will result in memory leaks
       
    95    if a class constructor leaves.
       
    96 
       
    97    This macro must be used within a public section of a class
       
    98    definition, if the single phase construction is part of the public
       
    99    interface of the class.
       
   100 
       
   101    Current Limitation CONSTRUCTORS_MAY_LEAVE is an unfortunate blight on the
       
   102    usability of single-phase construction, but we have yet to come up
       
   103    with a better alternative in the face of the legacy handling of
       
   104    ELeave.
       
   105 */
       
   106 #define CONSTRUCTORS_MAY_LEAVE											\
       
   107 	static void operator delete(TAny* aPtr) __NO_THROW					\
       
   108 		{																\
       
   109 		::operator delete(aPtr);										\
       
   110 		}																\
       
   111 																		\
       
   112 	static void operator delete(TAny*, TAny*) __NO_THROW				\
       
   113 		{																\
       
   114 		}																\
       
   115 																		\
       
   116 	static void operator delete(TAny* aPtr, TLeave) __NO_THROW			\
       
   117 		{																\
       
   118 		::operator delete(aPtr);										\
       
   119 		}																\
       
   120 																		\
       
   121 	static void operator delete(TAny* aPtr, TUint) __NO_THROW			\
       
   122 		{																\
       
   123 		::operator delete(aPtr);										\
       
   124 		}																\
       
   125 																		\
       
   126 	static void operator delete(TAny* aPtr, TLeave, TUint) __NO_THROW	\
       
   127 		{																\
       
   128 		::operator delete(aPtr);										\
       
   129 		}																\
       
   130 																		\
       
   131 	static void operator delete[](TAny* aPtr) __NO_THROW				\
       
   132 		{																\
       
   133 		::operator delete[](aPtr);										\
       
   134 		}																\
       
   135 																		\
       
   136 	static void operator delete[](TAny* aPtr, TLeave) __NO_THROW		\
       
   137 		{																\
       
   138 		::operator delete[](aPtr);										\
       
   139 		}
       
   140 
       
   141 
       
   142 // Implementation function.
       
   143 template<typename T>
       
   144 void ManagedPopCleanupStackItem(T aIsManaged)
       
   145 	{
       
   146 // CleanupStack-based cleanup is automatically triggered by a Leave,
       
   147 // so, in the case when __LEAVE_EQUALS_THROW__,
       
   148 // CleanupStack::PopAndDestroy must not be called again here
       
   149 #ifndef __GCCXML__
       
   150 // for gccxml builds the std::uncaught_exception function is not listed in std name space
       
   151 // to supress GCCXML error
       
   152 	if (!std::uncaught_exception())
       
   153 		{
       
   154 		if (aIsManaged)
       
   155 			{
       
   156 			CleanupStack::PopAndDestroy();
       
   157 			}
       
   158 		else
       
   159 			{
       
   160 			CleanupStack::Pop();
       
   161 			}
       
   162 		}
       
   163 #endif		
       
   164 	}
       
   165 
       
   166 /**
       
   167    Strategy (policy) class that defines the default cleanup strategy
       
   168    for managed resource class objects.
       
   169 
       
   170    The default cleanup strategy is to call the cleanup member function
       
   171    of the managed class, which is the Close() member function of the
       
   172    managed class, unless explicitly defined otherwise, for example by
       
   173    using the provided DEFINE_CLEANUP_FUNCTION macro.
       
   174    
       
   175    @internalComponent
       
   176 */
       
   177 // Not for Client Use , Only to be used Internally.
       
   178 class TResourceCleanupStrategy
       
   179 	{
       
   180   public:
       
   181 	template<typename T>
       
   182 	static void Cleanup(T* aObjPtr)
       
   183 		{
       
   184 		CallCleanupFunction(aObjPtr);
       
   185 		}
       
   186 	};
       
   187 
       
   188 /**
       
   189    Strategy (policy) class that defines a cleanup strategy for managed
       
   190    resource class objects.  This cleanup strategy calls the Close()
       
   191    member function of the managed class.
       
   192 
       
   193    @see LCleanedupHandle to which this strategy type may be supplied as
       
   194    an (optional) second tamplate parameter
       
   195    @see LManagedHandle to which this strategy type may be supplied as
       
   196    an (optional) second tamplate parameter
       
   197 */
       
   198 class TClose
       
   199 	{
       
   200   public:
       
   201 	template<class T>
       
   202 	static void Cleanup(T* aObjPtr)
       
   203 		{
       
   204 		aObjPtr->Close();
       
   205 		}
       
   206 	};
       
   207 
       
   208 /**
       
   209    Strategy (policy) class that defines a cleanup strategy for managed
       
   210    resource class objects.  This cleanup strategy calls the Release()
       
   211    member function of the managed class.
       
   212 
       
   213    @see LCleanedupHandle to which this strategy type may be supplied as
       
   214    an (optional) second tamplate parameter
       
   215    @see LManagedHandle to which this strategy type may be supplied as
       
   216    an (optional) second tamplate parameter
       
   217 */
       
   218 class TRelease
       
   219 	{
       
   220   public:
       
   221 	template<class T>
       
   222 	static void Cleanup(T* aObjPtr)
       
   223 		{
       
   224 		aObjPtr->Release();
       
   225 		}
       
   226 	};
       
   227 
       
   228 /**
       
   229    Strategy (policy) class that defines a cleanup strategy for managed
       
   230    resource class objects.  This cleanup strategy calls the Destroy()
       
   231    member function of the managed class.
       
   232 
       
   233    @see LCleanedupHandle to which this strategy type may be supplied as
       
   234    an (optional) second tamplate parameter
       
   235    @see LManagedHandle to which this strategy type may be supplied as
       
   236    an (optional) second tamplate parameter
       
   237 */
       
   238 class TDestroy
       
   239 	{
       
   240   public:
       
   241 	template<class T>
       
   242 	static void Cleanup(T* aObjPtr)
       
   243 		{
       
   244 		aObjPtr->Destroy();
       
   245 		}
       
   246 	};
       
   247 
       
   248 /**
       
   249    Strategy (policy) class that defines a cleanup strategy for managed
       
   250    resource class objects.  This cleanup strategy calls the Free()
       
   251    member function of the managed class.
       
   252 
       
   253    @see LCleanedupHandle to which this strategy type may be supplied as
       
   254    an (optional) second tamplate parameter
       
   255    @see LManagedHandle to which this strategy type may be supplied as
       
   256    an (optional) second tamplate parameter
       
   257 */
       
   258 class TFree
       
   259 	{
       
   260   public:
       
   261 	template<class T>
       
   262 	static void Cleanup(T* aObjPtr)
       
   263 		{
       
   264 		aObjPtr->Free();
       
   265 		}
       
   266 	};
       
   267 
       
   268 /**
       
   269    Strategy (policy) class that defines a cleanup strategy for managed
       
   270    resource class objects.  This cleanup strategy calls the
       
   271    ResetAndDestroy() member function of the managed class.
       
   272 
       
   273    @see LCleanedupHandle to which this strategy type may be supplied as
       
   274    an (optional) second tamplate parameter
       
   275    @see LManagedHandle to which this strategy type may be supplied as
       
   276    an (optional) second tamplate parameter
       
   277 */
       
   278 class TResetAndDestroy
       
   279 	{
       
   280   public:
       
   281 	template<class T>
       
   282 	static void Cleanup(T* aObjPtr)
       
   283 		{
       
   284 		aObjPtr->ResetAndDestroy();
       
   285 		}
       
   286 	};
       
   287 
       
   288 
       
   289 /**
       
   290    Strategy (policy) class that defines the default cleanup strategy
       
   291    for pointer types.  For pointers to CBase-derived types, the
       
   292    default cleanup strategy is to call CBase::Delete with the managed
       
   293    pointer.  For pointers to types that are not derived from CBase,
       
   294    the default cleanup strategy is to delete the managed pointer using
       
   295    non-array delete.
       
   296 
       
   297    @see LCleanedupPtr to which this strategy type may be supplied as
       
   298    an (optional) second tamplate parameter
       
   299    @see LManagedPtr to which this strategy type may be supplied as
       
   300    an (optional) second tamplate parameter
       
   301 */
       
   302 class TPtrCleanupStrategy
       
   303 	{
       
   304   public:
       
   305 	template<typename T>
       
   306 	static void Cleanup(T* aPtr)
       
   307 		{
       
   308 		delete aPtr;
       
   309 		}
       
   310 
       
   311 	static void Cleanup(CBase* aPtr)
       
   312 		{
       
   313 		CBase::Delete(aPtr);
       
   314 		}
       
   315 	};
       
   316 
       
   317 
       
   318 /**
       
   319    Strategy (policy) class that defines a cleanup strategy for pointer
       
   320    types.  This cleanup strategy deletes the managed pointer by using
       
   321    non-array delete.
       
   322 
       
   323    @see LCleanedupPtr to which this strategy type may be supplied as
       
   324    an (optional) second tamplate parameter
       
   325    @see LManagedPtr to which this strategy type may be supplied as
       
   326    an (optional) second tamplate parameter
       
   327 */
       
   328 class TPointerDeleteStrategy
       
   329 	{
       
   330   public:
       
   331 	template<typename T>
       
   332 	static void Cleanup(T* aPtr)
       
   333 		{
       
   334 		delete aPtr;
       
   335 		}
       
   336 	};
       
   337 
       
   338 
       
   339 /**
       
   340    Strategy (policy) class that defines a cleanup strategy for
       
   341    pointers to CBase-derived types.  This cleanup strategy calls
       
   342    CBase::Delete with the managed pointer.
       
   343 
       
   344    @see LCleanedupPtr to which this strategy type may be supplied as
       
   345    an (optional) second tamplate parameter
       
   346    @see LManagedPtr to which this strategy type may be supplied as
       
   347    an (optional) second tamplate parameter
       
   348 */
       
   349 class TCBaseDeleteStrategy
       
   350 	{
       
   351   public:
       
   352 	static void Cleanup(CBase* aPtr)
       
   353 		{
       
   354 		CBase::Delete(aPtr);
       
   355 		}
       
   356 	};
       
   357 
       
   358 
       
   359 /**
       
   360    Strategy (policy) class that defines a cleanup strategy for pointer
       
   361    types.  This cleanup strategy calls User::Free with the managed
       
   362    pointer.
       
   363 
       
   364    @see LCleanedupPtr to which this strategy type may be supplied as
       
   365    an (optional) second tamplate parameter
       
   366    @see LManagedPtr to which this strategy type may be supplied as
       
   367    an (optional) second tamplate parameter
       
   368 */
       
   369 class TPointerFree
       
   370 	{
       
   371   public:
       
   372 	static void Cleanup(TAny* aPtr)
       
   373 		{
       
   374 		User::Free(aPtr);
       
   375 		}
       
   376 	};
       
   377 
       
   378 
       
   379 /**
       
   380    Strategy (policy) class that defines the default cleanup strategy
       
   381    for heap-allocated arrays.  This cleanup strategy deallocates the
       
   382    managed array by using array delete.
       
   383 */
       
   384 class TArrayDelete
       
   385 	{
       
   386   public:
       
   387 	template<typename T>
       
   388 	static void Cleanup(T* aPtr)
       
   389 		{
       
   390 		delete[] aPtr;
       
   391 		}
       
   392 	};
       
   393 
       
   394 
       
   395 // enum type used for identifying the categories of managed pointer types
       
   396 enum TManagedPtrType
       
   397 {
       
   398 	EPtrNonSpecial,
       
   399 	EPtrCBaseDerived
       
   400 };
       
   401 
       
   402 
       
   403 // macro used for determining whether a pointer is special
       
   404 #define IS_PTR_SPECIAL(T) IS_BASE_OF(CBase, T)
       
   405 
       
   406 
       
   407 // enum type used for identifying the categories of resource handle types
       
   408 enum TAutoHandleType
       
   409 {
       
   410 	EAutoHandleNonSpecial,
       
   411 	EAutoRHandleBaseDerived,
       
   412 	EAutoHandleRBuf
       
   413 };
       
   414 
       
   415 
       
   416 // macro used for determining whether a resource handle type is special
       
   417 #define IS_HANDLE_SPECIAL(T) IS_BASE_OF(RHandleBase, T) ? EAutoRHandleBaseDerived : ( (IS_SAME(RBuf8, T) || IS_SAME(RBuf16, T)) ? EAutoHandleRBuf : EAutoHandleNonSpecial )
       
   418 
       
   419 
       
   420 /**
       
   421    Implementation base class - not designed for public inheritance or
       
   422    direct use.
       
   423    
       
   424    @internalComponent
       
   425 */
       
   426 // Not for Client Use , Only to be used Internally.
       
   427 template<typename T,
       
   428 		 TInt isHandleSpecial = IS_HANDLE_SPECIAL(T)>
       
   429 class LAutoHandleBase
       
   430 	{
       
   431   protected:
       
   432 	LAutoHandleBase()
       
   433 		: iEnabled(ETrue)
       
   434 		{
       
   435 		}
       
   436 
       
   437 	template<typename Param1>
       
   438 	explicit LAutoHandleBase(const Param1& aParam1)
       
   439 		: iHandle(aParam1),
       
   440 		  iEnabled(ETrue)
       
   441 		{
       
   442 		}
       
   443 
       
   444 	template<typename Param1>
       
   445 	explicit LAutoHandleBase(Param1& aParam1)
       
   446 		: iHandle(aParam1),
       
   447 		  iEnabled(ETrue)
       
   448 		{
       
   449 		}
       
   450 
       
   451 	template<typename Param1,
       
   452 			 typename Param2>
       
   453 	LAutoHandleBase(const Param1& aParam1,
       
   454 					const Param2& aParam2)
       
   455 		: iHandle(aParam1,
       
   456 				  aParam2),
       
   457 		  iEnabled(ETrue)
       
   458 		{
       
   459 		}
       
   460 
       
   461 	template<typename Param1,
       
   462 			 typename Param2>
       
   463 	LAutoHandleBase(Param1& aParam1,
       
   464 					const Param2& aParam2)
       
   465 		: iHandle(aParam1,
       
   466 				  aParam2),
       
   467 		  iEnabled(ETrue)
       
   468 		{
       
   469 		}
       
   470 
       
   471 	template<typename Param1,
       
   472 			 typename Param2>
       
   473 	LAutoHandleBase(const Param1& aParam1,
       
   474 					Param2& aParam2)
       
   475 		: iHandle(aParam1,
       
   476 				  aParam2),
       
   477 		  iEnabled(ETrue)
       
   478 		{
       
   479 		}
       
   480 
       
   481 	template<typename Param1,
       
   482 			 typename Param2>
       
   483 	LAutoHandleBase(Param1& aParam1,
       
   484 					Param2& aParam2)
       
   485 		: iHandle(aParam1,
       
   486 				  aParam2),
       
   487 		  iEnabled(ETrue)
       
   488 		{
       
   489 		}
       
   490 
       
   491 	template<typename U>
       
   492 	LAutoHandleBase& operator=(const U& aHandle)
       
   493 		{
       
   494 		iHandle = aHandle;
       
   495 		iEnabled = ETrue;
       
   496 		return *this;
       
   497 		}
       
   498 
       
   499 	T& Get()
       
   500 		{
       
   501 		return iHandle;
       
   502 		}
       
   503 
       
   504 	const T& Get() const
       
   505 		{
       
   506 		return iHandle;
       
   507 		}
       
   508 
       
   509 	T& operator*()
       
   510 		{
       
   511 		return iHandle;
       
   512 		}
       
   513 
       
   514 	const T& operator*() const
       
   515 		{
       
   516 		return iHandle;
       
   517 		}
       
   518 
       
   519 	T* operator->()
       
   520 		{
       
   521 		return &iHandle;
       
   522 		}
       
   523 
       
   524 	const T* operator->() const
       
   525 		{
       
   526 		return &iHandle;
       
   527 		}
       
   528 
       
   529 	T Unmanage()
       
   530 		{
       
   531 		iEnabled = EFalse;
       
   532 		return iHandle;
       
   533 		}
       
   534 
       
   535 	TBool IsEnabled() const
       
   536 		{
       
   537 		return iEnabled;
       
   538 		}
       
   539 
       
   540 	void Disable()
       
   541 		{
       
   542 		iEnabled = EFalse;
       
   543 		}
       
   544 
       
   545 	void Swap(LAutoHandleBase& aAutoHandle)
       
   546 		{
       
   547 		::Swap(iHandle, aAutoHandle.iHandle);
       
   548 		::Swap(iEnabled, aAutoHandle.iEnabled);
       
   549 		}
       
   550 
       
   551   protected:
       
   552 	T iHandle;
       
   553 	TBool iEnabled;
       
   554 
       
   555   private:
       
   556 	LAutoHandleBase(const LAutoHandleBase&);
       
   557 	LAutoHandleBase& operator=(const LAutoHandleBase&);
       
   558 	};
       
   559 
       
   560 
       
   561 /**
       
   562    Implementation base class - not designed for public inheritance or
       
   563    direct use.  Specialization for types derived from RHandleBase.
       
   564 */
       
   565 template<typename T>
       
   566 class LAutoHandleBase<T, EAutoRHandleBaseDerived>
       
   567 	{
       
   568   protected:
       
   569 	LAutoHandleBase()
       
   570 		{
       
   571 		}
       
   572 
       
   573 	template<typename Param1>
       
   574 	explicit LAutoHandleBase(const Param1& aParam1)
       
   575 		: iHandle(aParam1)
       
   576 		{
       
   577 		}
       
   578 
       
   579 	template<typename Param1>
       
   580 	explicit LAutoHandleBase(Param1& aParam1)
       
   581 		: iHandle(aParam1)
       
   582 		{
       
   583 		}
       
   584 
       
   585 	template<typename Param1,
       
   586 			 typename Param2>
       
   587 	LAutoHandleBase(const Param1& aParam1,
       
   588 					const Param2& aParam2)
       
   589 		: iHandle(aParam1,
       
   590 				  aParam2)
       
   591 		{
       
   592 		}
       
   593 
       
   594 	template<typename Param1,
       
   595 			 typename Param2>
       
   596 	LAutoHandleBase(Param1& aParam1,
       
   597 					const Param2& aParam2)
       
   598 		: iHandle(aParam1,
       
   599 				  aParam2)
       
   600 		{
       
   601 		}
       
   602 
       
   603 	template<typename Param1,
       
   604 			 typename Param2>
       
   605 	LAutoHandleBase(const Param1& aParam1,
       
   606 					Param2& aParam2)
       
   607 		: iHandle(aParam1,
       
   608 				  aParam2)
       
   609 		{
       
   610 		}
       
   611 
       
   612 	template<typename Param1,
       
   613 			 typename Param2>
       
   614 	LAutoHandleBase(Param1& aParam1,
       
   615 					Param2& aParam2)
       
   616 		: iHandle(aParam1,
       
   617 				  aParam2)
       
   618 		{
       
   619 		}
       
   620 
       
   621 	template<typename U>
       
   622 	LAutoHandleBase& operator=(const U& aHandle)
       
   623 		{
       
   624 		iHandle = aHandle;
       
   625 		return *this;
       
   626 		}
       
   627 
       
   628 	T& Get()
       
   629 		{
       
   630 		return iHandle;
       
   631 		}
       
   632 
       
   633 	const T& Get() const
       
   634 		{
       
   635 		return iHandle;
       
   636 		}
       
   637 
       
   638 	T& operator*()
       
   639 		{
       
   640 		return iHandle;
       
   641 		}
       
   642 
       
   643 	const T& operator*() const
       
   644 		{
       
   645 		return iHandle;
       
   646 		}
       
   647 
       
   648 	T* operator->()
       
   649 		{
       
   650 		return &iHandle;
       
   651 		}
       
   652 
       
   653 	const T* operator->() const
       
   654 		{
       
   655 		return &iHandle;
       
   656 		}
       
   657 
       
   658 	T Unmanage()
       
   659 		{
       
   660 		T handle = iHandle;
       
   661 		iHandle.SetHandle(KNullHandle);
       
   662 		return handle;
       
   663 		}
       
   664 
       
   665 	TBool IsEnabled() const
       
   666 		{
       
   667 		return iHandle.Handle() != KNullHandle;
       
   668 		}
       
   669 
       
   670 	void Disable()
       
   671 		{
       
   672 		iHandle.SetHandle(KNullHandle);
       
   673 		}
       
   674 
       
   675 	void Swap(LAutoHandleBase& aAutoHandle)
       
   676 		{
       
   677 		::Swap(iHandle, aAutoHandle.iHandle);
       
   678 		}
       
   679 
       
   680   protected:
       
   681 	T iHandle;
       
   682 
       
   683   private:
       
   684 	LAutoHandleBase(const LAutoHandleBase&);
       
   685 	LAutoHandleBase& operator=(const LAutoHandleBase&);
       
   686 	};
       
   687 
       
   688 
       
   689 // N.B. RBuf8, RBuf16 and RBuf cannot be used with LManagedHandle and
       
   690 // LCleanedupHandle.  Use LString or managed references instead.
       
   691 // The following specialization must not be used.
       
   692 template<typename T>
       
   693 class LAutoHandleBase<T, EAutoHandleRBuf>: protected T
       
   694 	{
       
   695   private:
       
   696 	LAutoHandleBase()
       
   697 		{
       
   698 		}
       
   699 
       
   700 	~LAutoHandleBase()
       
   701 		{
       
   702 		}
       
   703 	};
       
   704 
       
   705 
       
   706 /**
       
   707    A class template for the creation and automatic management of
       
   708    resource handles (typically R-class instances) held in the data
       
   709    members of objects.
       
   710 
       
   711    @note This class should not used to define locals. See below for
       
   712    an explanation and links to management classes suitable for use in
       
   713    that context.
       
   714 
       
   715    This class template can be used to protect a resource handle of
       
   716    type T (typically an R-class instance) such that the instance of T
       
   717    protected is automatically cleaned up when the management object is
       
   718    destroyed; typically when the object containing it is deleted.
       
   719 
       
   720    By default, the cleanup action is to call the Close() member
       
   721    function of the managed handle. An alternative cleanup strategy may
       
   722    be selected by specifying a cleanup strategy template class in the
       
   723    optional second template parameter position. The most common
       
   724    alternative cleanup strategies are predefined. It is also possible
       
   725    to specialize the default cleanup action for a given class using
       
   726    the DEFINE_CLEANUP_FUNCTION macro.
       
   727 
       
   728    The constructors of this class never leave (unless construction of
       
   729    the underlying T instance can leave, which is rare), so data
       
   730    members defined with this type may be initialized safely during any
       
   731    phase of construction of the owning class.
       
   732 
       
   733    Any arguments supplied when initializing an instance of this class
       
   734    are automatically passed through to T's constructors.
       
   735 
       
   736    As a convenience, the methods of the managed pointer may be
       
   737    accessed via "->" notation directly on the management object, while
       
   738    "." notation is used to access the interface of the management
       
   739    object itself. Using "*" to dereference the management object
       
   740    yields a T&, and is often useful when passing the managed object as
       
   741    an argument.
       
   742 
       
   743    Automatic cleanup may be disabled at any time by calling
       
   744    Unmanage(), while cleanup may be forced at any time by calling
       
   745    ReleaseResource().
       
   746 
       
   747    Example:
       
   748    @code
       
   749    class CComposite : public CBase
       
   750 	   {
       
   751 	 public:
       
   752 	   CONSTRUCTORS_MAY_LEAVE
       
   753 
       
   754 	   CComposite()
       
   755 		   {
       
   756 		   iFileServ->Connect() OR_LEAVE;
       
   757 		   iFile->Open(*iFileServ, ...);
       
   758 		   }
       
   759 
       
   760 	   ~CComposite()
       
   761 		   {
       
   762 		   // the handles are automatically closed
       
   763 		   }
       
   764 
       
   765 	 private:
       
   766 
       
   767 	   LManagedHandle<RFs> iFileServ;
       
   768 	   LManagedHandle<RFile> iFile;
       
   769 	   };
       
   770    @endcode
       
   771 
       
   772    Behind the scenes, this class template simply relies on reliable
       
   773    execution of its destructor. If used for a local variable rather
       
   774    than a data member, cleanup will occur but out-of-order compared to
       
   775    objects protected using the LCleanupXxx variants or the
       
   776    CleanupStack directly. Therefore it is not recommended for use in
       
   777    that context.
       
   778 
       
   779    These management classes may be used as the basis for implementing
       
   780    leave-safe single-phase construction, since fully initialized
       
   781    data members protected in this way will get destroyed (so reliably
       
   782    triggering cleanup) if their containing classes leave during
       
   783    execution of their constructors. Note, however, that single-phase
       
   784    construction must be explicitly enabled in the containing class
       
   785    using the CONSTRUCTORS_MAY_LEAVE macro.
       
   786 
       
   787    This class template together with the cleanup strategy class
       
   788    templates provide a template-based implementation of the Strategy
       
   789    design pattern (See also: Policy-based design).
       
   790 
       
   791    @see TClose which implements the default Close() calling cleanup strategy
       
   792    @see TResetAndDestroy which implements an alternative
       
   793    ResetAndDestroy() calling cleanup strategy
       
   794    @see TFree which implements an alternative Free() calling cleanup
       
   795    strategy
       
   796    @see TDestroy which implements an alternative Destroy() calling
       
   797    cleanup strategy
       
   798    @see TRelease which implements an alternative Release() calling cleanup strategy
       
   799    @see LCleanedupHandle which has the same interface, but uses the cleanup
       
   800    stack and is suitable for protecting locals
       
   801    @see CONSTRUCTORS_MAY_LEAVE
       
   802 */
       
   803 template<typename T,
       
   804 		 class CleanupStrategyType = TResourceCleanupStrategy>
       
   805 class LManagedHandle: protected LAutoHandleBase<T, IS_HANDLE_SPECIAL(T)>
       
   806 	{
       
   807 	typedef LAutoHandleBase<T, IS_HANDLE_SPECIAL(T)> LAutoHandleBase;
       
   808 
       
   809   public:
       
   810 	typedef T ManagedType;
       
   811 	typedef CleanupStrategyType CleanupStrategy;
       
   812 
       
   813 /**
       
   814    Default constructor.
       
   815 */
       
   816 	LManagedHandle()
       
   817 		{
       
   818 		}
       
   819 
       
   820 	template<typename Param1>
       
   821 	explicit LManagedHandle(const Param1& aParam1)
       
   822 		: LAutoHandleBase(aParam1)
       
   823 		{
       
   824 		}
       
   825 
       
   826 	template<typename Param1>
       
   827 	explicit LManagedHandle(Param1& aParam1)
       
   828 		: LAutoHandleBase(aParam1)
       
   829 		{
       
   830 		}
       
   831 
       
   832 	template<typename Param1,
       
   833 			 typename Param2>
       
   834 	LManagedHandle(const Param1& aParam1,
       
   835 				   const Param2& aParam2)
       
   836 		: LAutoHandleBase(aParam1,
       
   837 					   aParam2)
       
   838 		{
       
   839 		}
       
   840 
       
   841 	template<typename Param1,
       
   842 			 typename Param2>
       
   843 	LManagedHandle(const Param1& aParam1,
       
   844 				   Param2& aParam2)
       
   845 		: LAutoHandleBase(aParam1,
       
   846 					   aParam2)
       
   847 		{
       
   848 		}
       
   849 
       
   850 	template<typename Param1,
       
   851 			 typename Param2>
       
   852 	LManagedHandle(Param1& aParam1,
       
   853 				   const Param2& aParam2)
       
   854 		: LAutoHandleBase(aParam1,
       
   855 					   aParam2)
       
   856 		{
       
   857 		}
       
   858 
       
   859 	template<typename Param1,
       
   860 			 typename Param2>
       
   861 	LManagedHandle(Param1& aParam1,
       
   862 				   Param2& aParam2)
       
   863 		: LAutoHandleBase(aParam1,
       
   864 					   aParam2)
       
   865 		{
       
   866 		}
       
   867 
       
   868 /**
       
   869    Assigns a new resource to be managed.  If the LManagedHandle object
       
   870    already contains a managed resource handle, then the managed
       
   871    resource is released using the specified cleanup strategy before
       
   872    assigning the new managed resource.
       
   873 
       
   874    @param aHandle a reference to a handle object of a type that can be assigned to a handle object of type T
       
   875  */
       
   876 	template<typename U>
       
   877 	LManagedHandle& operator=(const U& aHandle)
       
   878 		{
       
   879 		ReleaseResource();
       
   880 		LAutoHandleBase::operator=(aHandle);
       
   881 		return *this;
       
   882 		}
       
   883 
       
   884 /**
       
   885    Destructor.	When automatic resource management is enabled, the
       
   886    destructor calls the cleanup function defined by the cleanup
       
   887    strategy with the contained resource handle object.
       
   888  */
       
   889 	~LManagedHandle()
       
   890 		{
       
   891 		if (IsEnabled())
       
   892 			{
       
   893 			CleanupStrategy::Cleanup(&Get());
       
   894 			}
       
   895 		}
       
   896 
       
   897 /**
       
   898    If automatic resource management is enabled, calls the cleanup
       
   899    function defined by the cleanup strategy with the managed resource
       
   900    handle object and then disables the automatic resource management
       
   901    for this object.	 The cleanup strategy is specified by the
       
   902    CleanupStrategy template template parameter.	 The default cleanup
       
   903    strategy is to call the cleanup member function on the contained
       
   904    resource handle object. which is a member function named Close(),
       
   905    unless explicitly defined otherwise for the class of the object,
       
   906    for example by using the provided DEFINE_CLEANUP_FUNCTION macro.
       
   907 */
       
   908 	void ReleaseResource()
       
   909 		{
       
   910 		if (!IsEnabled())
       
   911 			return;
       
   912 
       
   913 		CleanupStrategy::Cleanup(&Get());
       
   914 		LAutoHandleBase::Disable();
       
   915 		}
       
   916 
       
   917 /**
       
   918    Disables the automatic resource management for this object and
       
   919    returns a copy of the resource handle.
       
   920 
       
   921    @return A copy of the resource handle.
       
   922 */
       
   923 	using LAutoHandleBase::Unmanage;
       
   924 
       
   925 /**
       
   926    Returns ETrue if automatic resource management is enabled; EFalse
       
   927    otherwise.
       
   928 
       
   929    @return ETrue if automatic resource management is enabled; EFalse
       
   930    otherwise.
       
   931 */
       
   932 	using LAutoHandleBase::IsEnabled;
       
   933 
       
   934 /**
       
   935    Returns a reference to the resource handle.
       
   936 
       
   937    @return A reference to the resource handle.
       
   938 */
       
   939 	using LAutoHandleBase::Get;
       
   940 
       
   941 /**
       
   942    Overloaded indirection operator function.
       
   943 
       
   944    @return A reference to the resource handle.
       
   945 */
       
   946 	using LAutoHandleBase::operator*;
       
   947 
       
   948 /**
       
   949    Overloaded class member access operator function.
       
   950 
       
   951    @return A pointer to the resource handle.
       
   952 */
       
   953 	using LAutoHandleBase::operator->;
       
   954 
       
   955 	using LAutoHandleBase::Disable;
       
   956 
       
   957 	void Swap(LManagedHandle& aManagedHandle)
       
   958 		{
       
   959 		LAutoHandleBase::Swap(aManagedHandle);
       
   960 		}
       
   961 	};
       
   962 
       
   963 
       
   964 /**
       
   965    Implementation base class - not designed for public inheritance or
       
   966    direct use.
       
   967    
       
   968    @internalComponent
       
   969 */
       
   970 // Not for Client Use , Only to be used Internally.
       
   971 template<typename T>
       
   972 class LAutoPtrBase
       
   973 	{
       
   974   protected:
       
   975 	LAutoPtrBase()
       
   976 		: iPtr(NULL)
       
   977 		{
       
   978 		}
       
   979 
       
   980 	explicit LAutoPtrBase(T* aPtr)
       
   981 		: iPtr(aPtr)
       
   982 		{
       
   983 		}
       
   984 
       
   985 	LAutoPtrBase& operator=(T* aPtr)
       
   986 		{
       
   987 		iPtr = aPtr;
       
   988 		return *this;
       
   989 		}
       
   990 
       
   991 	T* Unmanage()
       
   992 		{
       
   993 		T* ptr = iPtr;
       
   994 		iPtr = NULL;
       
   995 		return ptr;
       
   996 		}
       
   997 
       
   998 	TBool IsEnabled() const
       
   999 		{
       
  1000 		return iPtr != NULL;
       
  1001 		}
       
  1002 
       
  1003 	T* Get() const
       
  1004 		{
       
  1005 		return iPtr;
       
  1006 		}
       
  1007 
       
  1008 	T* operator->() const
       
  1009 		{
       
  1010 		return iPtr;
       
  1011 		}
       
  1012 
       
  1013 	void Disable()
       
  1014 		{
       
  1015 		iPtr = NULL;
       
  1016 		}
       
  1017 
       
  1018 	void Swap(LAutoPtrBase& aAutoPtr)
       
  1019 		{
       
  1020 		::Swap(iPtr, aAutoPtr.iPtr);
       
  1021 		}
       
  1022 
       
  1023   protected:
       
  1024 	T* iPtr;
       
  1025 
       
  1026   private:
       
  1027 	LAutoPtrBase(const LAutoPtrBase&);
       
  1028 	LAutoPtrBase& operator=(const LAutoPtrBase&);
       
  1029 	};
       
  1030 
       
  1031 
       
  1032 // Cleanup traits class template
       
  1033 template<typename T,
       
  1034 		 class CleanupStrategyType,
       
  1035 		 TInt isPtrSpecial = IS_PTR_SPECIAL(T)>
       
  1036 struct TPtrCleanupTraits
       
  1037 	{
       
  1038 	};
       
  1039 
       
  1040 
       
  1041 // Cleanup traits class template specialization for pointers to types
       
  1042 // that are not derived from CBase
       
  1043 template<typename T,
       
  1044 		 class CleanupStrategyType>
       
  1045 struct TPtrCleanupTraits<T, CleanupStrategyType, EPtrNonSpecial>
       
  1046 	{
       
  1047 	typedef T ManagedType;
       
  1048 	typedef T BaseManagedType;
       
  1049 	typedef CleanupStrategyType CleanupStrategy;
       
  1050 	};
       
  1051 
       
  1052 // Cleanup traits class template specialization for pointers to types
       
  1053 // that are derived from CBase
       
  1054 template<typename T,
       
  1055 		 class CleanupStrategyType>
       
  1056 struct TPtrCleanupTraits<T, CleanupStrategyType, EPtrCBaseDerived>
       
  1057 	{
       
  1058 	typedef T ManagedType;
       
  1059 	typedef CBase BaseManagedType;
       
  1060 	typedef CleanupStrategyType CleanupStrategy;
       
  1061 	};
       
  1062 
       
  1063 // Cleanup traits class template specialization for pointers to types
       
  1064 // that are derived from CBase and the default pointer cleanup
       
  1065 // strategy (TPtrCleanupStrategy)
       
  1066 template<typename T>
       
  1067 struct TPtrCleanupTraits<T, TPtrCleanupStrategy, EPtrCBaseDerived>
       
  1068 	{
       
  1069 	typedef CBase ManagedType;
       
  1070 	typedef CBase BaseManagedType;
       
  1071 	typedef TPtrCleanupStrategy CleanupStrategy;
       
  1072 	};
       
  1073 
       
  1074 
       
  1075 /**
       
  1076    Implementation base class - not designed for public inheritance or
       
  1077    direct use.
       
  1078 */
       
  1079 template<typename T,
       
  1080 		 class CleanupStrategyType>
       
  1081 class LManagedPtrBase: protected LAutoPtrBase<typename TPtrCleanupTraits<T, CleanupStrategyType>::BaseManagedType>
       
  1082 	{
       
  1083 	typedef LAutoPtrBase<typename TPtrCleanupTraits<T, CleanupStrategyType>::BaseManagedType> LAutoPtrBase;
       
  1084 
       
  1085   protected:
       
  1086 	typedef typename TPtrCleanupTraits<T, CleanupStrategyType>::ManagedType ManagedType;
       
  1087 	typedef typename TPtrCleanupTraits<T, CleanupStrategyType>::BaseManagedType BaseManagedType;
       
  1088 	typedef typename TPtrCleanupTraits<T, CleanupStrategyType>::CleanupStrategy CleanupStrategy;
       
  1089 
       
  1090 	LManagedPtrBase()
       
  1091 		{
       
  1092 		}
       
  1093 
       
  1094 	template<typename U>
       
  1095 	explicit LManagedPtrBase(U* aPtr)
       
  1096 		: LAutoPtrBase(aPtr)
       
  1097 		{
       
  1098 		}
       
  1099 
       
  1100 /**
       
  1101    Destructor.	When automatic resource management is enabled, the
       
  1102    destructor invokes the specified cleanup strategy for the managed
       
  1103    pointer.
       
  1104  */
       
  1105 	~LManagedPtrBase()
       
  1106 		{
       
  1107 		if (IsEnabled())
       
  1108 			{
       
  1109 			CleanupStrategy::Cleanup(static_cast<ManagedType*>(iPtr));
       
  1110 			}
       
  1111 		}
       
  1112 
       
  1113 	template<typename U>
       
  1114 	LManagedPtrBase& operator=(U* aPtr)
       
  1115 		{
       
  1116 		ReleaseResource();
       
  1117 		LAutoPtrBase::operator=(aPtr);
       
  1118 		return *this;
       
  1119 		}
       
  1120 
       
  1121 /**
       
  1122    If automatic resource management is enabled, the specified cleanup
       
  1123    strategy is invoked for the managed pointer and the automatic
       
  1124    resource management is then disabled.  The underlying pointer is
       
  1125    reset to NULL.
       
  1126 
       
  1127    @post Get() == NULL
       
  1128 */
       
  1129 	void ReleaseResource()
       
  1130 		{
       
  1131 		if (!IsEnabled())
       
  1132 			return;
       
  1133 
       
  1134 		CleanupStrategy::Cleanup(static_cast<ManagedType*>(iPtr));
       
  1135 		LAutoPtrBase::Disable();
       
  1136 		}
       
  1137 
       
  1138 	using LAutoPtrBase::Unmanage;
       
  1139 
       
  1140 	using LAutoPtrBase::IsEnabled;
       
  1141 
       
  1142 	using LAutoPtrBase::Get;
       
  1143 
       
  1144 	using LAutoPtrBase::operator->;
       
  1145 
       
  1146 	using LAutoPtrBase::Disable;
       
  1147 
       
  1148 	using LAutoPtrBase::iPtr;
       
  1149 
       
  1150 	void Swap(LManagedPtrBase& aManagedPtr)
       
  1151 		{
       
  1152 		LAutoPtrBase::Swap(aManagedPtr);
       
  1153 		}
       
  1154 	};
       
  1155 
       
  1156 
       
  1157 /**
       
  1158    A class template that provides automatic management of pointers
       
  1159    held in the data members of objects.
       
  1160 
       
  1161    @note This class should not used to define locals. See below for
       
  1162    an explanation and links to management classes suitable for use in
       
  1163    that context.
       
  1164 
       
  1165    This class template can be used to protect a pointer to type T such
       
  1166    that the instance of T referred to is automatically cleaned up when
       
  1167    the management object is destroyed; typically when the object
       
  1168    containing it is deleted.
       
  1169 
       
  1170    By default, the cleanup action is to delete the managed pointer
       
  1171    using a (non-array) delete operation. An alternative cleanup
       
  1172    strategy can be specified using the optional CleanupStrategy class
       
  1173    template parameter of the LManagedPtr class template. The most
       
  1174    common alternative cleanup strategies are predefined
       
  1175    (e.g. TPointerFree).
       
  1176 
       
  1177    The constructors of this class never leave, so data members defined with
       
  1178    this type may be initialized safely during any phase of
       
  1179    construction of the owning class.
       
  1180 
       
  1181    As a convenience, the methods of the managed pointer may be
       
  1182    accessed via "->" notation directly on the management object, while
       
  1183    "." notation is used to access the interface of the management
       
  1184    object itself. Using "*" to dereference the management object
       
  1185    yields a T&, and is often useful when passing the managed object as
       
  1186    an argument.
       
  1187 
       
  1188    Automatic cleanup may be disabled at any time by calling
       
  1189    Unmanage(), while cleanup may be forced at any time by calling
       
  1190    ReleaseResource().
       
  1191 
       
  1192    Example:
       
  1193    @code
       
  1194    class CComposite : public CBase
       
  1195 	   {
       
  1196 	 public:
       
  1197 	   CONSTRUCTORS_MAY_LEAVE
       
  1198 
       
  1199 	   CComposite()
       
  1200 		   : iComponent(CComponent::NewL())
       
  1201 		   {
       
  1202 		   //...
       
  1203 		   }
       
  1204 
       
  1205 	   ~CComposite()
       
  1206 		   {
       
  1207 		   // the pointer to the CComponent object is automatically
       
  1208 		   // deleted
       
  1209 		   }
       
  1210 
       
  1211 	 private:
       
  1212 	   LManagedPtr<CComponent> iComponent;
       
  1213 	   };
       
  1214 	@endcode
       
  1215 
       
  1216    Behind the scenes, this class template simply relies on reliable
       
  1217    execution of its destructor. If used for a local variable rather
       
  1218    than a data member, cleanup will occur but out-of-order compared to
       
  1219    objects protected using the LCleanupXxx variants or the
       
  1220    CleanupStack directly. Therefore it is not recommended for use in
       
  1221    that context.
       
  1222 
       
  1223    These management classes may be used as the basis for implementing
       
  1224    leave-safe single-phase construction, since fully initialized
       
  1225    data members protected in this way will get destroyed (so reliably
       
  1226    triggering cleanup) if their containing classes leave during
       
  1227    execution of their constructors. Note, however, that single-phase
       
  1228    construction must be explicitly enabled in the containing class
       
  1229    using the CONSTRUCTORS_MAY_LEAVE macro.
       
  1230 
       
  1231    This class template together with the cleanup strategy class
       
  1232    templates provide a template-based implementation of the Strategy
       
  1233    design pattern (See also: Policy-based design).
       
  1234 
       
  1235    @see TPointerDelete which implements the default deleting cleanup strategy
       
  1236    @see TPointerFree which implements the alternative User::Free() cleanup strategy
       
  1237    @see LCleanedupPtr which has the same interface, but uses the cleanup
       
  1238    stack and is suitable for protecting locals
       
  1239    @see CONSTRUCTORS_MAY_LEAVE
       
  1240 */
       
  1241 template<typename T,
       
  1242 		 class CleanupStrategyType = TPtrCleanupStrategy>
       
  1243 class LManagedPtr: protected LManagedPtrBase<T, CleanupStrategyType>
       
  1244 	{
       
  1245 	typedef LManagedPtrBase<T, CleanupStrategyType> LManagedPtrBase;
       
  1246 
       
  1247   public:
       
  1248 	typedef T ManagedType;
       
  1249 	typedef CleanupStrategyType CleanupStrategy;
       
  1250 
       
  1251 
       
  1252 /**
       
  1253    Default constructor.	 Constructs an empty LManagedPtr object.
       
  1254 
       
  1255    @post Get() == NULL
       
  1256  */
       
  1257 	LManagedPtr()
       
  1258 		{
       
  1259 		}
       
  1260 
       
  1261 /**
       
  1262    Explicit constructor template.  Constructs a LManagedPtr object
       
  1263    that manages the pointer aPtr of a type convertible to T* that can
       
  1264    be cleaned up using the cleanup strategy of the LManagedPtr class.
       
  1265    The default cleanup strategy is to delete the pointer to a
       
  1266    heap-allocated object by using non-array delete.	 Alternative
       
  1267    cleanup strategies can be specified by using the CleanupStrategy
       
  1268    template parameter of the LManagedPtr class template.
       
  1269 
       
  1270    @param aPtr A pointer of a type that is convertible to T* that can
       
  1271    be cleaned up using the cleanup strategy.
       
  1272 
       
  1273    @pre aPtr is of a type convertible to T* and can be cleaned up
       
  1274    using the cleanup strategy.
       
  1275 
       
  1276    @post Get() == aPtr
       
  1277  */
       
  1278 	explicit LManagedPtr(T* aPtr)
       
  1279 		: LManagedPtrBase(aPtr)
       
  1280 		{
       
  1281 		}
       
  1282 
       
  1283 /**
       
  1284    Destructor.	When automatic resource management is enabled, the
       
  1285    destructor invokes the specified cleanup strategy for the managed
       
  1286    pointer.
       
  1287  */
       
  1288 
       
  1289 
       
  1290 /**
       
  1291    Assigns a new pointer to be managed.	 The new pointer must be of a
       
  1292    type convertible to T* and it must be possible to use the cleanup
       
  1293    strategy of the LManagedPtr object for the cleanup of the new
       
  1294    managed pointer.	 If the LManagedPtr object already contains a
       
  1295    managed pointer, then the cleanup strategy is invoked with the
       
  1296    managed pointer before assigning the new managed pointer.
       
  1297 
       
  1298    @param aPtr A pointer of a type that is convertible to T* that can
       
  1299    be cleaned up using the cleanup strategy.
       
  1300 
       
  1301    @pre aPtr is a pointer of a type that is convertible to T* and can
       
  1302    be cleaned up using the cleanup strategy.
       
  1303 
       
  1304    @post Get() == aPtr
       
  1305  */
       
  1306 	LManagedPtr& operator=(T* aPtr)
       
  1307 		{
       
  1308 		LManagedPtrBase::operator=(aPtr);
       
  1309 		return *this;
       
  1310 		}
       
  1311 
       
  1312 /**
       
  1313    Assigns a new pointer to be managed.	 The new pointer must be of a
       
  1314    type convertible to T* and it must be possible to use the cleanup
       
  1315    strategy of the LManagedPtr object for the cleanup of the new
       
  1316    managed pointer.	 If the LManagedPtr object already contains a
       
  1317    managed pointer, then the cleanup strategy is invoked with the
       
  1318    managed pointer before assigning the new managed pointer.
       
  1319 
       
  1320    @param aPtr A pointer of a type that is convertible to T* that can
       
  1321    be cleaned up using the cleanup strategy.
       
  1322 
       
  1323    @pre aPtr is a pointer of a type that is convertible to T* and can
       
  1324    be cleaned up using the cleanup strategy.
       
  1325 
       
  1326    @post Get() == aPtr
       
  1327  */
       
  1328 	template<typename U>
       
  1329 	LManagedPtr& operator=(U* aPtr)
       
  1330 		{
       
  1331 		LManagedPtrBase::operator=(aPtr);
       
  1332 		return *this;
       
  1333 		}
       
  1334 
       
  1335 	using LManagedPtrBase::ReleaseResource;
       
  1336 
       
  1337 /**
       
  1338    Disables the automatic resource management for this object and
       
  1339    returns a pointer to the object of type T.
       
  1340 
       
  1341    @return A pointer to the object of type T.
       
  1342 */
       
  1343 	T* Unmanage()
       
  1344 		{
       
  1345 		return static_cast<T*>(LManagedPtrBase::Unmanage());
       
  1346 		}
       
  1347 
       
  1348 /**
       
  1349    Returns ETrue if automatic resource management is enabled; EFalse
       
  1350    otherwise.
       
  1351 
       
  1352    @return ETrue if automatic resource management is enabled; EFalse
       
  1353    otherwise.
       
  1354 */
       
  1355 	using LManagedPtrBase::IsEnabled;
       
  1356 
       
  1357 /**
       
  1358    Returns a pointer to the managed object of type T.
       
  1359 
       
  1360    @return A pointer to the managed object of type T.
       
  1361 */
       
  1362 	T* Get() const
       
  1363 		{
       
  1364 		return static_cast<T*>(iPtr);
       
  1365 		}
       
  1366 
       
  1367 /**
       
  1368    Overloaded indirection operator function.
       
  1369 
       
  1370    @return A reference to the managed object of type T.
       
  1371 */
       
  1372 	T& operator*() const
       
  1373 		{
       
  1374 		return *(static_cast<T*>(iPtr));
       
  1375 		}
       
  1376 
       
  1377 /**
       
  1378    Overloaded class member access operator function.
       
  1379 
       
  1380    @return A pointer to the managed object of type T.
       
  1381 */
       
  1382 	T* operator->() const
       
  1383 		{
       
  1384 		return static_cast<T*>(iPtr);
       
  1385 		}
       
  1386 
       
  1387 
       
  1388 // Implementation type - do not use
       
  1389 	typedef typename LManagedPtrBase::BaseManagedType* LManagedPtr<T, CleanupStrategy>::*TUnspecifiedBoolType;
       
  1390 
       
  1391 /**
       
  1392    Conversion operator that enables LCleanedupPtr objects to be used
       
  1393    in boolean contexts.
       
  1394 
       
  1395    @return An unspecified value of an unspecified type convertible to
       
  1396    boolean, which has a boolean value equal to Get() != NULL
       
  1397  */
       
  1398 	operator TUnspecifiedBoolType()
       
  1399 		{
       
  1400 		return iPtr ? &LManagedPtr::iPtr : NULL;
       
  1401 		}
       
  1402 
       
  1403 
       
  1404 	using LManagedPtrBase::Disable;
       
  1405 
       
  1406 	void Swap(LManagedPtr& aManagedPtr)
       
  1407 		{
       
  1408 		LManagedPtrBase::Swap(aManagedPtr);
       
  1409 		}
       
  1410 
       
  1411   private:
       
  1412 	using LManagedPtrBase::iPtr;
       
  1413 	};
       
  1414 
       
  1415 
       
  1416 // function template used for comparing two LManagedPtr-managed
       
  1417 // pointers for equality
       
  1418 template<typename T, typename U>
       
  1419 TBool operator==(const LManagedPtr<T>& aPtr1, const LManagedPtr<U>& aPtr2)
       
  1420 	{
       
  1421 	return aPtr1.Get() == aPtr2.Get();
       
  1422 	}
       
  1423 
       
  1424 // function template used for comparing two LManagedPtr-managed
       
  1425 // pointers for inequality
       
  1426 template<typename T, typename U>
       
  1427 TBool operator!=(const LManagedPtr<T>& aPtr1, const LManagedPtr<U>& aPtr2)
       
  1428 	{
       
  1429 	return aPtr1.Get() != aPtr2.Get();
       
  1430 	}
       
  1431 
       
  1432 // function template used for testing the ordering of two
       
  1433 // LManagedPtr-managed pointers
       
  1434 template<typename T, typename U>
       
  1435 TBool operator<(const LManagedPtr<T>& aPtr1, const LManagedPtr<U>& aPtr2)
       
  1436 	{
       
  1437 	return aPtr1.Get() < aPtr2.Get();
       
  1438 	}
       
  1439 
       
  1440 
       
  1441 /**
       
  1442    A class template that provides automatic management of arrays. Such
       
  1443    managed arrays can be data members of composite classes.
       
  1444 
       
  1445    @note This class should not used to define locals. See below for
       
  1446    an explanation and links to management classes suitable for use in
       
  1447    that context.
       
  1448 
       
  1449    @par
       
  1450 
       
  1451    @note This class can only be used with raw arrays, which are used
       
  1452    only rarely on Symbian OS.  Instances of Symbian array container
       
  1453    classes (e.g. RArray, RPointerArray) should be managed using the
       
  1454    automatic management template classes appropriate for the array's
       
  1455    type (LManagedHandle template classes for Symbian R arrays or
       
  1456    LManagedPtr template classes for Symbian C arrays).
       
  1457 
       
  1458    This class template can be used to protect a heap-allocated array
       
  1459    of objects of type T such that the managed array is automatically
       
  1460    deallocated when the management object is destroyed.
       
  1461 
       
  1462    The default cleanup strategy is to deallocate the managed array
       
  1463    using arrray delete (delete[]), assuming that the array is
       
  1464    heap-allocated.	An alternative cleanup strategy can be selected by
       
  1465    specifying a cleanup strategy template class as the optional second
       
  1466    template argument (corresponding to the CleanupStrategy template
       
  1467    parameter).
       
  1468 
       
  1469    The constructors of this class never leave, so data members defined with
       
  1470    this type may be initialized safely during any phase of
       
  1471    construction of the owning class.
       
  1472 
       
  1473    As a convenience, the elements of the managed array may be accessed
       
  1474    via "[]" notation directly on the management object.
       
  1475 
       
  1476    Automatic cleanup may be disabled at any time by calling
       
  1477    Unmanage(), while cleanup may be forced at any time by calling
       
  1478    ReleaseResource().
       
  1479 
       
  1480 
       
  1481    Example:
       
  1482    @code
       
  1483    class CComposite : public CBase
       
  1484 	   {
       
  1485 	 public:
       
  1486 	   CONSTRUCTORS_MAY_LEAVE
       
  1487 
       
  1488 	   CComposite()
       
  1489 		   : iComponents(new(ELeave) CComponent[KNumComponents])
       
  1490 		   {
       
  1491 		   //...
       
  1492 		   }
       
  1493 
       
  1494 	   ~CComposite()
       
  1495 		   {
       
  1496 		   // the array is automatically deleted
       
  1497 		   }
       
  1498 
       
  1499 	 private:
       
  1500 	   LManagedArray<CComponent> iComponents;
       
  1501 	   };
       
  1502    @endcode
       
  1503 
       
  1504 
       
  1505    Behind the scenes, this class template simply relies on reliable
       
  1506    execution of its destructor. If used for a local variable rather
       
  1507    than a data member, cleanup will occur but out-of-order compared to
       
  1508    objects protected using the LCleanupXxx variants or the
       
  1509    CleanupStack directly. Therefore it is not recommended for use in
       
  1510    that context.
       
  1511 
       
  1512    These management classes may be used as the basis for implementing
       
  1513    leave-safe single-phase construction, since fully initialized
       
  1514    data members protected in this way will get destroyed (so reliably
       
  1515    triggering cleanup) if their containing classes leave during
       
  1516    execution of their constructors. Note, however, that single-phase
       
  1517    construction must be explicitly enabled in the containing class
       
  1518    using the CONSTRUCTORS_MAY_LEAVE macro.
       
  1519 
       
  1520    This class template together with the cleanup strategy class
       
  1521    templates provide a template-based implementation of the Strategy
       
  1522    design pattern (See also: Policy-based design).
       
  1523 
       
  1524    @see LCleanedupArray which has the same interface, but uses the cleanup
       
  1525    stack and is suitable for protecting locals
       
  1526    @see CONSTRUCTORS_MAY_LEAVE
       
  1527 */
       
  1528 template<typename T,
       
  1529 		 class CleanupStrategyType = TArrayDelete>
       
  1530 class LManagedArray: protected LAutoPtrBase<T>
       
  1531 	{
       
  1532 	typedef LAutoPtrBase<T> LAutoPtrBase;
       
  1533 
       
  1534   public:
       
  1535 	typedef T ManagedType;
       
  1536 	typedef CleanupStrategyType CleanupStrategy;
       
  1537 
       
  1538 /**
       
  1539    Default constructor.	 Constructs an empty LManagedArray object.
       
  1540 
       
  1541    @post Get() == NULL
       
  1542  */
       
  1543 	LManagedArray()
       
  1544 		{
       
  1545 		}
       
  1546 
       
  1547 /**
       
  1548    Explicit constructor.  Constructs a LManagedArray object that
       
  1549    manages an array of objects of type T that can be cleaned up using
       
  1550    the cleanup strategy of the LManagedArray class.	 The default
       
  1551    cleanup strategy is to deallocate the managed array by using array
       
  1552    delete (delete[]), assuming that the array is heap-allocated.
       
  1553    Alternative cleanup strategies can be specified by using the
       
  1554    CleanupStrategy template parameter of the LManagedArray class
       
  1555    template.
       
  1556 
       
  1557    @param aPtr A pointer to the first element of an array of objects
       
  1558    of type T - array that can be cleaned up using the cleanup strategy
       
  1559    of the the LManagedArray class.
       
  1560 
       
  1561    @pre The array can be cleaned up using the cleanup strategy.
       
  1562 
       
  1563    @post Get() == aPtr
       
  1564  */
       
  1565 	explicit LManagedArray(T* aPtr)
       
  1566 		: LAutoPtrBase(aPtr)
       
  1567 		{
       
  1568 		}
       
  1569 
       
  1570 /**
       
  1571    Destructor.	When automatic resource management is enabled, the
       
  1572    destructor invokes the specified cleanup strategy for the managed
       
  1573    pointer.
       
  1574  */
       
  1575 	~LManagedArray()
       
  1576 		{
       
  1577 		if (LAutoPtrBase::IsEnabled())
       
  1578 			{
       
  1579 			CleanupStrategy::Cleanup(iPtr);
       
  1580 			}
       
  1581 		}
       
  1582 
       
  1583 /**
       
  1584    Assigns a new array of objects of type T to be managed.	It needs
       
  1585    to be possible use the cleanup strategy of the LManagedArray object
       
  1586    for the cleanup of the new managed array.  The default cleanup
       
  1587    strategy is to delete the heap-allocated array by using array
       
  1588    delete (delete[]). If the LManagedArray object already manages an
       
  1589    array, then the cleanup strategy is invoked with the managed array
       
  1590    before assigning the new managed array.
       
  1591 
       
  1592    @param aPtr A pointer to the first element of the array of objects
       
  1593    of type T - array that can be cleaned up using the cleanup
       
  1594    strategy.
       
  1595 
       
  1596    @pre The new array to be managed can be cleaned up using the
       
  1597    cleanup strategy.
       
  1598 
       
  1599    @post Get() == aPtr
       
  1600  */
       
  1601 	LManagedArray& operator=(T* aPtr)
       
  1602 		{
       
  1603 		ReleaseResource();
       
  1604 		LAutoPtrBase::operator=(aPtr);
       
  1605 		return *this;
       
  1606 		}
       
  1607 
       
  1608 /**
       
  1609    If automatic resource management is enabled, the specified cleanup
       
  1610    strategy is invoked for the managed pointer and the automatic
       
  1611    resource management is then disabled.  The underlying pointer is
       
  1612    reset to NULL.
       
  1613 
       
  1614    @post Get() == NULL
       
  1615 */
       
  1616 	void ReleaseResource()
       
  1617 		{
       
  1618 		if (!LAutoPtrBase::IsEnabled())
       
  1619 			return;
       
  1620 
       
  1621 		CleanupStrategy::Cleanup(iPtr);
       
  1622 		LAutoPtrBase::Disable();
       
  1623 		}
       
  1624 
       
  1625 /**
       
  1626    Disables the automatic resource management for this object and
       
  1627    returns a pointer to the first element of the array of objects of
       
  1628    type T.
       
  1629 
       
  1630    @return A pointer to the first element of the array of objects of
       
  1631    type T.
       
  1632 */
       
  1633 	T* Unmanage()
       
  1634 		{
       
  1635 		return static_cast<T*>(LAutoPtrBase::Unmanage());
       
  1636 		}
       
  1637 
       
  1638 /**
       
  1639    Returns ETrue if automatic resource management is enabled; EFalse
       
  1640    otherwise.
       
  1641 
       
  1642    @return ETrue if automatic resource management is enabled; EFalse
       
  1643    otherwise.
       
  1644 */
       
  1645 	using LAutoPtrBase::IsEnabled;
       
  1646 
       
  1647 /**
       
  1648    Returns a pointer to the first element of the managed array of
       
  1649    objects of type T.
       
  1650 
       
  1651    @return A pointer to the first element of the managed array of
       
  1652    objects of type T.
       
  1653 */
       
  1654 	using LAutoPtrBase::Get;
       
  1655 
       
  1656 /**
       
  1657    Overloaded subscript operator.
       
  1658 
       
  1659    @return A reference to the object of type T at the position aIndex.
       
  1660  */
       
  1661 	T& operator[](TInt aIndex) const
       
  1662 		{
       
  1663 		return iPtr[aIndex];
       
  1664 		}
       
  1665 
       
  1666 	using LAutoPtrBase::Disable;
       
  1667 
       
  1668 	void Swap(LManagedArray& aArray)
       
  1669 		{
       
  1670 		LAutoPtrBase::Swap(aArray);
       
  1671 		}
       
  1672 
       
  1673   private:
       
  1674 	using LAutoPtrBase::iPtr;
       
  1675 	};
       
  1676 
       
  1677 
       
  1678 /**
       
  1679    Implementation base class - not designed for public inheritance or
       
  1680    direct use.
       
  1681    
       
  1682    @internalComponent
       
  1683 */
       
  1684 // Not for Client Use , Only to be used Internally.
       
  1685 template<typename T>
       
  1686 class LAutoRefBase
       
  1687 	{
       
  1688   protected:
       
  1689 	template<typename U>
       
  1690 	explicit LAutoRefBase(U& aRef)
       
  1691 		: iPtr(&aRef)
       
  1692 		{
       
  1693 		}
       
  1694 
       
  1695 	template<typename U>
       
  1696 	LAutoRefBase& operator=(U& aRef)
       
  1697 		{
       
  1698 		iPtr = &aRef;
       
  1699 		return *this;
       
  1700 		}
       
  1701 
       
  1702 	T& Unmanage()
       
  1703 		{
       
  1704 		T* ptr = iPtr;
       
  1705 		iPtr = NULL;
       
  1706 		return *ptr;
       
  1707 		}
       
  1708 
       
  1709 	TBool IsEnabled() const
       
  1710 		{
       
  1711 		return iPtr != NULL;
       
  1712 		}
       
  1713 
       
  1714 	T& Get() const
       
  1715 		{
       
  1716 		return *iPtr;
       
  1717 		}
       
  1718 
       
  1719 	T& operator*() const
       
  1720 		{
       
  1721 		return *iPtr;
       
  1722 		}
       
  1723 
       
  1724 	T* operator->() const
       
  1725 		{
       
  1726 		return iPtr;
       
  1727 		}
       
  1728 
       
  1729 	void Disable()
       
  1730 		{
       
  1731 		iPtr = NULL;
       
  1732 		}
       
  1733 
       
  1734 	void Swap(LAutoRefBase& aAutoRef)
       
  1735 		{
       
  1736 		::Swap(iPtr, aAutoRef.iPtr);
       
  1737 		}
       
  1738 
       
  1739   protected:
       
  1740 	T* iPtr;
       
  1741 
       
  1742   private:
       
  1743 	LAutoRefBase(const LAutoRefBase&);
       
  1744 	LAutoRefBase& operator=(const LAutoRefBase&);
       
  1745 	};
       
  1746 
       
  1747 
       
  1748 /**
       
  1749    A class template that provides automatic management of references
       
  1750    to resource handles (often R-class instances) held in the data
       
  1751    members of objects.
       
  1752 
       
  1753    @note This class should not used to define locals. See below for
       
  1754    an explanation and links to management classes suitable for use in
       
  1755    that context.
       
  1756 
       
  1757    Unlike LManagedHandle which creates a fresh instance of its managed
       
  1758    type, this class template can be used to protect an existing
       
  1759    resource handle of type T (typically an R-class instance). The
       
  1760    instance of T referred to has a cleanup operation run on it
       
  1761    automatically when the management object is destroyed; typically
       
  1762    when the object containing it is deleted.
       
  1763 
       
  1764    By default, the cleanup action is to call the Close() member
       
  1765    function of the referenced handle. An alternative cleanup strategy may
       
  1766    be selected by specifying a cleanup strategy template class in the
       
  1767    optional second template parameter position. The most common
       
  1768    alternative cleanup strategies are predefined. It is also possible
       
  1769    to specialize the default cleanup action for a given class using
       
  1770    the DEFINE_CLEANUP_FUNCTION macro.
       
  1771 
       
  1772    The constructors of this class never leave, so data members defined with
       
  1773    this type may be initialized safely during any phase of
       
  1774    construction of the owning class.
       
  1775 
       
  1776    As a convenience, the methods of the managed pointer may be
       
  1777    accessed via "->" notation directly on the management object, while
       
  1778    "." notation is used to access the interface of the management
       
  1779    object itself. Using "*" to dereference the management object
       
  1780    yields a T&, and is often useful when passing the managed object as
       
  1781    an argument.
       
  1782 
       
  1783    Automatic cleanup may be disabled at any time by calling
       
  1784    Unmanage(), while cleanup may be forced at any time by calling
       
  1785    ReleaseResource().
       
  1786 
       
  1787    Example:
       
  1788    @code
       
  1789    class CComposite : public CBase
       
  1790 	   {
       
  1791 	 public:
       
  1792 	   CONSTRUCTORS_MAY_LEAVE
       
  1793 
       
  1794 	   // An existing RFs instance is given to us to reuse, but
       
  1795 	   // we are responsible for calling Close() when we're done
       
  1796 	   CComposite(RFs& aFs)
       
  1797 		   : iFileServ(aFs)
       
  1798 		   {
       
  1799 		   iFileServ->Connect() OR_LEAVE;
       
  1800 		   iFile->Open(*iFileServ, ...);
       
  1801 		   }
       
  1802 
       
  1803 	   ~CComposite()
       
  1804 		   {
       
  1805 		   // the handles are automatically closed
       
  1806 		   }
       
  1807 
       
  1808 	 private:
       
  1809 
       
  1810 	   LManagedRef<RFs> iFileServ;
       
  1811 	   LManagedHandle<RFile> iFile;
       
  1812 	   };
       
  1813    @endcode
       
  1814 
       
  1815    Behind the scenes, this class template simply relies on reliable
       
  1816    execution of its destructor. If used for a local variable rather
       
  1817    than a data member, cleanup will occur but out-of-order compared to
       
  1818    objects protected using the LCleanupXxx variants or the
       
  1819    CleanupStack directly. Therefore it is not recommended for use in
       
  1820    that context.
       
  1821 
       
  1822    These management classes may be used as the basis for implementing
       
  1823    leave-safe single-phase construction, since fully initialized
       
  1824    data members protected in this way will get destroyed (so reliably
       
  1825    triggering cleanup) if their containing classes leave during
       
  1826    execution of their constructors. Note, however, that single-phase
       
  1827    construction must be explicitly enabled in the containing class
       
  1828    using the CONSTRUCTORS_MAY_LEAVE macro.
       
  1829 
       
  1830    This class template together with the cleanup strategy class
       
  1831    templates provide a template-based implementation of the Strategy
       
  1832    design pattern (See also: Policy-based design).
       
  1833 
       
  1834    @see TClose which implements the default Close() calling cleanup strategy
       
  1835    @see TResetAndDestroy which implements an alternative
       
  1836    ResetAndDestroy() calling cleanup strategy
       
  1837    @see TFree which implements an alternative Free() calling cleanup
       
  1838    strategy
       
  1839    @see TDestroy which implements an alternative Destroy() calling
       
  1840    cleanup strategy
       
  1841    @see TRelease which implements an alternative Release() calling
       
  1842    cleanup strategy
       
  1843    @see LCleanedupRef which has the same interface, but uses the cleanup
       
  1844    stack and is suitable for protecting locals
       
  1845    @see LManagedHandle which has a similar interface but creates a fresh
       
  1846    local instance of T
       
  1847    @see CONSTRUCTORS_MAY_LEAVE
       
  1848 */
       
  1849 template<typename T,
       
  1850 		 class CleanupStrategyType = TResourceCleanupStrategy>
       
  1851 class LManagedRef: protected LAutoRefBase<T>
       
  1852 	{
       
  1853 	typedef LAutoRefBase<T> LAutoRefBase;
       
  1854 
       
  1855   public:
       
  1856 	typedef T ManagedType;
       
  1857 	typedef CleanupStrategyType CleanupStrategy;
       
  1858 
       
  1859 /**
       
  1860    Explicit constructor.
       
  1861  */
       
  1862 	template<typename U>
       
  1863 	explicit LManagedRef(U& aRef)
       
  1864 		: LAutoRefBase(aRef)
       
  1865 		{
       
  1866 		}
       
  1867 
       
  1868 /**
       
  1869    Destructor.	When automatic resource management is enabled, the
       
  1870    destructor invokes the specified cleanup strategy for the managed
       
  1871    reference.
       
  1872  */
       
  1873 	~LManagedRef()
       
  1874 		{
       
  1875 		if (LAutoRefBase::IsEnabled())
       
  1876 			{
       
  1877 			CleanupStrategy::Cleanup(iPtr);
       
  1878 			}
       
  1879 		}
       
  1880 
       
  1881 /**
       
  1882    Assigns a new reference to be managed.  If the LManagedRef
       
  1883    object already contains a managed reference, then the specified
       
  1884    cleanup strategy is invoked for the managed reference before
       
  1885    assigning the new managed reference.
       
  1886  */
       
  1887 	template<typename U>
       
  1888 	LManagedRef& operator=(U& aRef)
       
  1889 		{
       
  1890 		ReleaseResource();
       
  1891 		LAutoRefBase::operator=(aRef);
       
  1892 		return *this;
       
  1893 		}
       
  1894 
       
  1895 /**
       
  1896    If automatic resource management is enabled, the specified cleanup
       
  1897    strategy is invoked for the managed reference and the automatic
       
  1898    resource management is then disabled for this object.
       
  1899 */
       
  1900 	void ReleaseResource()
       
  1901 		{
       
  1902 		if (!LAutoRefBase::IsEnabled())
       
  1903 			return;
       
  1904 
       
  1905 		CleanupStrategy::Cleanup(iPtr);
       
  1906 		LAutoRefBase::Disable();
       
  1907 		}
       
  1908 
       
  1909 /**
       
  1910    Disables the automatic resource management for this object and
       
  1911    returns a reference to the object of type T.
       
  1912 
       
  1913    @return A reference to the object of type T.
       
  1914 */
       
  1915 	using LAutoRefBase::Unmanage;
       
  1916 
       
  1917 /**
       
  1918    Returns ETrue if automatic resource management is enabled; EFalse
       
  1919    otherwise.
       
  1920 
       
  1921    @return ETrue if automatic resource management is enabled; EFalse
       
  1922    otherwise.
       
  1923 */
       
  1924 	using LAutoRefBase::IsEnabled;
       
  1925 
       
  1926 /**
       
  1927    Returns a reference to the managed object of type T.
       
  1928 
       
  1929    @return A reference to the managed object of type T.
       
  1930 */
       
  1931 	using LAutoRefBase::Get;
       
  1932 
       
  1933 /**
       
  1934    Overloaded indirection operator function.
       
  1935 
       
  1936    @return A reference to the managed object of type T.
       
  1937 */
       
  1938 	using LAutoRefBase::operator*;
       
  1939 
       
  1940 /**
       
  1941    Overloaded class member access operator function.
       
  1942 
       
  1943    @return A pointer to the managed object of type T.
       
  1944 */
       
  1945 	using LAutoRefBase::operator->;
       
  1946 
       
  1947 	using LAutoRefBase::Disable;
       
  1948 
       
  1949 	void Swap(LManagedRef& aRef)
       
  1950 		{
       
  1951 		LAutoRefBase::Swap(aRef);
       
  1952 		}
       
  1953 
       
  1954   private:
       
  1955 	using LAutoRefBase::iPtr;
       
  1956 	};
       
  1957 
       
  1958 
       
  1959 /**
       
  1960    A class template for the creation and CleanupStack-based
       
  1961    local-scope automatic management of resource handles (typically
       
  1962    instances of R-classes).
       
  1963 
       
  1964    @note This class can only be used to define locals, never
       
  1965    data members. See below for an explanation and links to management
       
  1966    classes suitable for use in different contexts. It should never be
       
  1967    used in the same function as code that uses the CleanupStack API
       
  1968    directly.
       
  1969 
       
  1970    This class template can be used to create and protect a resource
       
  1971    handle of type T (typically a R-class) such that the instance of T
       
  1972    referred to is automatically cleaned up when either of the
       
  1973    following occur:
       
  1974 
       
  1975    - The referring local variable goes out of scope normally
       
  1976    - The referring local variable goes out of scope due to an
       
  1977 	 untrapped leave causing the scope to be exited non-locally
       
  1978 
       
  1979    By default, the cleanup action is to call the Close() member
       
  1980    function of the managed handle. An alternative cleanup strategy may
       
  1981    be selected by specifying a cleanup strategy template class in the
       
  1982    optional second template parameter position. The most common
       
  1983    alternative cleanup strategies are predefined. It is also possible
       
  1984    to specialize the default cleanup action for a given class using
       
  1985    the DEFINE_CLEANUP_FUNCTION macro.
       
  1986 
       
  1987    The constructors of this class may leave.
       
  1988 
       
  1989    Any arguments supplied when initializing an instance of this class
       
  1990    are automatically passed through to T's constructors.
       
  1991 
       
  1992    As a convenience, the methods of the managed handle may be
       
  1993    accessed via "->" notation directly on the management object, while
       
  1994    "." notation is used to access the interface of the management
       
  1995    object itself. Using "*" to dereference the management object
       
  1996    yields a T&, and is often useful when passing the managed object as
       
  1997    an argument.
       
  1998 
       
  1999    Automatic cleanup may be disabled at any time by calling
       
  2000    Unmanage(), while cleanup may be forced at any time by calling
       
  2001    ReleaseResource().
       
  2002 
       
  2003    Example:
       
  2004    @code
       
  2005 	// block scope example
       
  2006 	{
       
  2007 	LCleanedupHandle<RClosable> obj;
       
  2008 	obj->DoSomethingL(); // leave-safe
       
  2009 	if (obj->Finished())
       
  2010 		return; // RClosable::Close is invoked automatically
       
  2011 	obj->DoSomethingElseL(); // leave-safe
       
  2012 	// RClosable::Close is invoked automatically
       
  2013 	}
       
  2014    @endcode
       
  2015 
       
  2016    Behind the scenes, this class template is implemented in terms of
       
  2017    the thread-local CleanupStack, restricting its use to locals on the
       
  2018    stack. This use of the CleanupStack ensures a consistent cleanup
       
  2019    order between functions that call one another, even if they use
       
  2020    different cleanup idioms.
       
  2021 
       
  2022    This class template together with the cleanup strategy class
       
  2023    templates provide a template-based implementation of the Strategy
       
  2024    design pattern (See also: Policy-based design).
       
  2025 
       
  2026    @see TClose which implements the default Close() calling cleanup strategy
       
  2027    @see TResetAndDestroy which implements an alternative
       
  2028    ResetAndDestroy() calling cleanup strategy
       
  2029    @see TFree which implements an alternative Free() calling cleanup
       
  2030    strategy
       
  2031    @see TDestroy which implements an alternative Destroy() calling
       
  2032    cleanup strategy
       
  2033    @see TRelease which implements an alternative Release() calling cleanup strategy
       
  2034    @see LManagedHandle which has the same interface, but does not use the cleanup
       
  2035    stack and is suitable for protecting the data members of classes
       
  2036 */
       
  2037 template<typename T,
       
  2038 		 class CleanupStrategyType = TResourceCleanupStrategy>
       
  2039 class LCleanedupHandle: protected LAutoHandleBase<T, IS_HANDLE_SPECIAL(T)>
       
  2040 	{
       
  2041 	typedef LAutoHandleBase<T, IS_HANDLE_SPECIAL(T)> LAutoHandleBase;
       
  2042 
       
  2043   public:
       
  2044 	typedef T ManagedType;
       
  2045 	typedef CleanupStrategyType CleanupStrategy;
       
  2046 
       
  2047 
       
  2048 /**
       
  2049    Default constructor.
       
  2050 */
       
  2051 	LCleanedupHandle()
       
  2052 		{
       
  2053 		CleanupStack::PushL(TCleanupItem(Cleanup, this));
       
  2054 		}
       
  2055 
       
  2056 	template<typename Param1>
       
  2057 	explicit LCleanedupHandle(const Param1& aParam1)
       
  2058 		: LAutoHandleBase(aParam1)
       
  2059 		{
       
  2060 		CleanupStack::PushL(TCleanupItem(Cleanup, this));
       
  2061 		}
       
  2062 
       
  2063 	template<typename Param1>
       
  2064 	explicit LCleanedupHandle(Param1& aParam1)
       
  2065 		: LAutoHandleBase(aParam1)
       
  2066 		{
       
  2067 		CleanupStack::PushL(TCleanupItem(Cleanup, this));
       
  2068 		}
       
  2069 
       
  2070 	template<typename Param1,
       
  2071 			 typename Param2>
       
  2072 	LCleanedupHandle(const Param1& aParam1,
       
  2073 					 const Param2& aParam2)
       
  2074 		: LAutoHandleBase(aParam1,
       
  2075 					   aParam2)
       
  2076 		{
       
  2077 		CleanupStack::PushL(TCleanupItem(Cleanup, this));
       
  2078 		}
       
  2079 
       
  2080 	template<typename Param1,
       
  2081 			 typename Param2>
       
  2082 	LCleanedupHandle(const Param1& aParam1,
       
  2083 					 Param2& aParam2)
       
  2084 		: LAutoHandleBase(aParam1,
       
  2085 					   aParam2)
       
  2086 		{
       
  2087 		CleanupStack::PushL(TCleanupItem(Cleanup, this));
       
  2088 		}
       
  2089 
       
  2090 	template<typename Param1,
       
  2091 			 typename Param2>
       
  2092 	LCleanedupHandle(Param1& aParam1,
       
  2093 					 const Param2& aParam2)
       
  2094 		: LAutoHandleBase(aParam1,
       
  2095 					   aParam2)
       
  2096 		{
       
  2097 		CleanupStack::PushL(TCleanupItem(Cleanup, this));
       
  2098 		}
       
  2099 
       
  2100 	template<typename Param1,
       
  2101 			 typename Param2>
       
  2102 	LCleanedupHandle(Param1& aParam1,
       
  2103 					 Param2& aParam2)
       
  2104 		: LAutoHandleBase(aParam1,
       
  2105 					   aParam2)
       
  2106 		{
       
  2107 		CleanupStack::PushL(TCleanupItem(Cleanup, this));
       
  2108 		}
       
  2109 
       
  2110 
       
  2111 	~LCleanedupHandle()
       
  2112 		{
       
  2113 		ManagedPopCleanupStackItem(IsEnabled());
       
  2114 		}
       
  2115 
       
  2116 /**
       
  2117    Assigns a new resource to be managed.  If the LCleanedupHandle
       
  2118    object already contains a managed resource handle, then the managed
       
  2119    resource is released using the specified cleanup strategy before
       
  2120    assigning the new managed resource.
       
  2121  */
       
  2122 	template<typename U>
       
  2123 	LCleanedupHandle& operator=(const U& aHandle)
       
  2124 		{
       
  2125 		ReleaseResource();
       
  2126 		LAutoHandleBase::operator=(aHandle);
       
  2127 		return *this;
       
  2128 		}
       
  2129 
       
  2130 
       
  2131 /**
       
  2132    If automatic resource management is enabled, calls the cleanup
       
  2133    function defined by the cleanup strategy with the managed resource
       
  2134    handle object and then disables the automatic resource management
       
  2135    for this object.	 The cleanup strategy is specified by the
       
  2136    CleanupStrategy template template parameter.	 The default cleanup
       
  2137    strategy is to call the cleanup member function on the contained
       
  2138    resource handle object. which is a member function named Close(),
       
  2139    unless explicitly defined otherwise for the class of the object,
       
  2140    for example by using the provided DEFINE_CLEANUP_FUNCTION macro.
       
  2141 */
       
  2142 	void ReleaseResource()
       
  2143 		{
       
  2144 		if (!IsEnabled())
       
  2145 			return;
       
  2146 
       
  2147 		CleanupStrategy::Cleanup(&Get());
       
  2148 		LAutoHandleBase::Disable();
       
  2149 		}
       
  2150 
       
  2151 /**
       
  2152    Disables the automatic resource management for this obkect and
       
  2153    returns a copy of the resource handle.
       
  2154 
       
  2155    @return A copy of the resource handle.
       
  2156 */
       
  2157 	using LAutoHandleBase::Unmanage;
       
  2158 
       
  2159 /**
       
  2160    Returns ETrue if automatic resource management is enabled; EFalse
       
  2161    otherwise.
       
  2162 
       
  2163    @return ETrue if automatic resource management is enabled; EFalse
       
  2164    otherwise.
       
  2165 */
       
  2166 	using LAutoHandleBase::IsEnabled;
       
  2167 
       
  2168 
       
  2169 /**
       
  2170    Returns a reference to the resource handle.
       
  2171 
       
  2172    @return A reference to the resource handle.
       
  2173 */
       
  2174 	using LAutoHandleBase::Get;
       
  2175 
       
  2176 
       
  2177 /**
       
  2178    Overloaded indirection operator function.
       
  2179 
       
  2180    @return A reference to the resource handle.
       
  2181 */
       
  2182 	using LAutoHandleBase::operator*;
       
  2183 
       
  2184 /**
       
  2185    Overloaded class member access operator function.
       
  2186 
       
  2187    @return A pointer to the resource handle.
       
  2188 */
       
  2189 	using LAutoHandleBase::operator->;
       
  2190 
       
  2191 	static void Cleanup(TAny* aPtr)
       
  2192 		{
       
  2193 		LCleanedupHandle* autoh = static_cast<LCleanedupHandle*>(aPtr);
       
  2194 
       
  2195 		if (autoh->IsEnabled())
       
  2196 			{
       
  2197 			CleanupStrategy::Cleanup(&autoh->Get());
       
  2198 			}
       
  2199 		}
       
  2200 
       
  2201 	using LAutoHandleBase::Disable;
       
  2202 
       
  2203 	void Swap(LCleanedupHandle& aCleanedupHandle)
       
  2204 		{
       
  2205 		LAutoHandleBase::Swap(aCleanedupHandle);
       
  2206 		}
       
  2207 	};
       
  2208 
       
  2209 
       
  2210 /**
       
  2211    Implementation base class - not designed for public inheritance or
       
  2212    direct use.
       
  2213    
       
  2214    @internalComponent
       
  2215 */
       
  2216 // Not for Client Use , Only to be used Internally.
       
  2217 template<typename T,
       
  2218 		 class CleanupStrategyType>
       
  2219 class LCleanedupPtrBase: protected LAutoPtrBase<typename TPtrCleanupTraits<T, CleanupStrategyType>::BaseManagedType>
       
  2220 	{
       
  2221 	typedef LAutoPtrBase<typename TPtrCleanupTraits<T, CleanupStrategyType>::BaseManagedType> LAutoPtrBase;
       
  2222 
       
  2223   protected:
       
  2224 	typedef typename TPtrCleanupTraits<T, CleanupStrategyType>::ManagedType ManagedType;
       
  2225 	typedef typename TPtrCleanupTraits<T, CleanupStrategyType>::BaseManagedType BaseManagedType;
       
  2226 	typedef typename TPtrCleanupTraits<T, CleanupStrategyType>::CleanupStrategy CleanupStrategy;
       
  2227 
       
  2228 	LCleanedupPtrBase()
       
  2229 		{
       
  2230 		CleanupStack::PushL(TCleanupItem(Cleanup, this));
       
  2231 		}
       
  2232 
       
  2233 	template<typename U>
       
  2234 	explicit LCleanedupPtrBase(U* aPtr)
       
  2235 		: LAutoPtrBase(aPtr)
       
  2236 		{
       
  2237 		CleanupStack::PushL(TCleanupItem(Cleanup, this));
       
  2238 		}
       
  2239 
       
  2240 	~LCleanedupPtrBase()
       
  2241 		{
       
  2242 		ManagedPopCleanupStackItem(LAutoPtrBase::IsEnabled());
       
  2243 		}
       
  2244 
       
  2245 	template<typename U>
       
  2246 	LCleanedupPtrBase& operator=(U* aPtr)
       
  2247 		{
       
  2248 		ReleaseResource();
       
  2249 		LAutoPtrBase::operator=(aPtr);
       
  2250 		return *this;
       
  2251 		}
       
  2252 
       
  2253 	void ReleaseResource()
       
  2254 		{
       
  2255 		if (!LAutoPtrBase::IsEnabled())
       
  2256 			return;
       
  2257 
       
  2258 		CleanupStrategy::Cleanup(static_cast<ManagedType*>(iPtr));
       
  2259 		LAutoPtrBase::Disable();
       
  2260 		}
       
  2261 
       
  2262 	using LAutoPtrBase::Unmanage;
       
  2263 
       
  2264 	using LAutoPtrBase::IsEnabled;
       
  2265 
       
  2266 	using LAutoPtrBase::Get;
       
  2267 
       
  2268 	using LAutoPtrBase::operator->;
       
  2269 
       
  2270 	static void Cleanup(TAny* aPtr)
       
  2271 		{
       
  2272 		LCleanedupPtrBase* cleanupPtr = static_cast<LCleanedupPtrBase*>(aPtr);
       
  2273 
       
  2274 		if (cleanupPtr->IsEnabled())
       
  2275 			{
       
  2276 			CleanupStrategy::Cleanup(static_cast<ManagedType*>(cleanupPtr->iPtr));
       
  2277 			}
       
  2278 		}
       
  2279 
       
  2280 	using LAutoPtrBase::iPtr;
       
  2281 
       
  2282 	void Swap(LCleanedupPtrBase& aCleanedupPtr)
       
  2283 		{
       
  2284 		LAutoPtrBase::Swap(aCleanedupPtr);
       
  2285 		}
       
  2286 	};
       
  2287 
       
  2288 
       
  2289 /**
       
  2290    A class template that provides CleanupStack-based local-scope
       
  2291    automatic management of pointers.
       
  2292 
       
  2293    @note This class can only be used to define locals, never
       
  2294    data members. See below for an explanation and links to management
       
  2295    classes suitable for use in different contexts. It should never be
       
  2296    used in the same function as code that uses the CleanupStack API
       
  2297    directly
       
  2298 
       
  2299    This class template can be used to protect a pointer to type T such
       
  2300    that the instance of T referred to is automatically cleaned up
       
  2301    when either of the following occur:
       
  2302 
       
  2303    - The referring local variable goes out of scope normally
       
  2304    - The referring local variable goes out of scope due to an
       
  2305 	 untrapped leave causing the scope to be exited non-locally
       
  2306 
       
  2307    By default, the cleanup action is to delete the managed pointer
       
  2308    using non-array delete. An alternative cleanup strategy may be
       
  2309    selected by specifying a cleanup strategy template class in the
       
  2310    optional second template parameter position. The most common
       
  2311    alternative cleanup strategies are predefined.
       
  2312 
       
  2313    The constructors of this class may leave.
       
  2314 
       
  2315    As a convenience, the methods of the managed pointer may be
       
  2316    accessed via "->" notation directly on the management object, while
       
  2317    "." notation is used to access the interface of the management
       
  2318    object itself. Using "*" to dereference the management object
       
  2319    yields a T&, and is often useful when passing the managed object as
       
  2320    an argument.
       
  2321 
       
  2322    Automatic cleanup may be disabled at any time by calling
       
  2323    Unmanage(), while cleanup may be forced at any time by calling
       
  2324    ReleaseResource().
       
  2325 
       
  2326    Example:
       
  2327    @code
       
  2328 	// block scope example
       
  2329 	{
       
  2330 	LCleanedupPtr<CDynamic> autop(new(ELeave) CDynamic);
       
  2331 	autop->DoSomethingL(); // leave-safe
       
  2332 	if (autop->Finished())
       
  2333 		return; //	the pointer is deleted automatically when exiting from scope
       
  2334 	autop->DoSomethingElseL(); // leave-safe
       
  2335 	//	the pointer is deleted automatically when exiting from scope
       
  2336 	}
       
  2337    @endcode
       
  2338 
       
  2339    Behind the scenes, this class template is implemented in terms of
       
  2340    the thread-local CleanupStack, restricting its use to locals on the
       
  2341    stack. This use of the CleanupStack ensures a consistent cleanup
       
  2342    order between functions that call one another, even if they use
       
  2343    different cleanup idioms.
       
  2344 
       
  2345    This class template together with the cleanup strategy class
       
  2346    templates provide a template-based implementation of the Strategy
       
  2347    design pattern (See also: Policy-based design).
       
  2348 
       
  2349    @see TPointerDelete which implements the default deleting cleanup strategy
       
  2350    @see TPointerFree which implements the alternative User::Free() cleanup strategy
       
  2351    @see LManagedPtr which has the same interface, but does not use the cleanup
       
  2352    stack and is suitable for protecting the data members of classes
       
  2353 */
       
  2354 template<typename T,
       
  2355 		 class CleanupStrategyType = TPtrCleanupStrategy>
       
  2356 class LCleanedupPtr: protected LCleanedupPtrBase<T, CleanupStrategyType>
       
  2357 	{
       
  2358 	typedef LCleanedupPtrBase<T, CleanupStrategyType> LCleanedupPtrBase;
       
  2359 
       
  2360   public:
       
  2361 	typedef T ManagedType;
       
  2362 	typedef CleanupStrategyType CleanupStrategy;
       
  2363 
       
  2364 
       
  2365 /**
       
  2366    Default constructor.	 Constructs an empty LCleanedupPtr object.
       
  2367 
       
  2368    @post Get() == NULL
       
  2369 */
       
  2370 	LCleanedupPtr()
       
  2371 		{
       
  2372 		}
       
  2373 
       
  2374 /**
       
  2375    Explicit constructor template.  Constructs a LCleanedupPtr object
       
  2376    that manages the pointer aPtr of a type convertible to T* that can
       
  2377    be cleaned up using the cleanup strategy of the LCleanedupPtr
       
  2378    class.  The default cleanup strategy is to delete the pointer to a
       
  2379    heap-allocated object by using non-array delete.	 Alternative
       
  2380    cleanup strategies can be specified by using the CleanupStrategy
       
  2381    template parameter of the LCleanedupPtr class template.
       
  2382 
       
  2383    @param aPtr A pointer of a type that is convertible to T* that can
       
  2384    be cleaned up using the cleanup strategy.
       
  2385 
       
  2386    @pre aPtr is of a type convertible to T* and can be cleaned up
       
  2387    using the cleanup strategy.
       
  2388 
       
  2389    @post Get() == aPtr
       
  2390 */
       
  2391 	explicit LCleanedupPtr(T* aPtr)
       
  2392 		: LCleanedupPtrBase(aPtr)
       
  2393 		{
       
  2394 		}
       
  2395 
       
  2396 /**
       
  2397    Assigns a new pointer to be managed.	 The new pointer must be of a
       
  2398    type convertible to T* and it must be possible to use the cleanup
       
  2399    strategy of the LCleanedupPtr object for the cleanup of the new
       
  2400    managed pointer.	 If the LCleanedupPtr object already contains a
       
  2401    managed pointer, then the cleanup strategy is invoked with the
       
  2402    managed pointer before assigning the new managed pointer.
       
  2403 
       
  2404    @param aPtr A pointer of a type that is convertible to T* that can
       
  2405    be cleaned up using the cleanup strategy.
       
  2406 
       
  2407    @pre aPtr is a pointer of a type that is convertible to T* and can
       
  2408    be cleaned up using the cleanup strategy.
       
  2409 
       
  2410    @post Get() == aPtr
       
  2411  */
       
  2412 	LCleanedupPtr& operator=(T* aPtr)
       
  2413 		{
       
  2414 		LCleanedupPtrBase::operator=(aPtr);
       
  2415 		return *this;
       
  2416 		}
       
  2417 
       
  2418 /**
       
  2419    Assigns a new pointer to be managed.	 The new pointer must be of a
       
  2420    type convertible to T* and it must be possible to use the cleanup
       
  2421    strategy of the LCleanedupPtr object for the cleanup of the new
       
  2422    managed pointer.	 If the LCleanedupPtr object already contains a
       
  2423    managed pointer, then the cleanup strategy is invoked with the
       
  2424    managed pointer before assigning the new managed pointer.
       
  2425 
       
  2426    @param aPtr A pointer of a type that is convertible to T* that can
       
  2427    be cleaned up using the cleanup strategy.
       
  2428 
       
  2429    @pre aPtr is a pointer of a type that is convertible to T* and can
       
  2430    be cleaned up using the cleanup strategy.
       
  2431 
       
  2432    @post Get() == aPtr
       
  2433  */
       
  2434 	template<typename U>
       
  2435 	LCleanedupPtr& operator=(U* aPtr)
       
  2436 		{
       
  2437 		LCleanedupPtrBase::operator=(aPtr);
       
  2438 		return *this;
       
  2439 		}
       
  2440 
       
  2441 
       
  2442 /**
       
  2443    If automatic resource management is enabled, the specified cleanup
       
  2444    strategy is invoked with the managed pointer and the automatic
       
  2445    resource management is then disabled.  The underlying pointer is
       
  2446    reset to NULL.
       
  2447 
       
  2448    @post Get() == NULL
       
  2449 */
       
  2450 	using LCleanedupPtrBase::ReleaseResource;
       
  2451 
       
  2452 /**
       
  2453    Disables the automatic resource management for this object and
       
  2454    returns a pointer to the object of type T.
       
  2455 
       
  2456    @return A pointer to the object of type T.
       
  2457 */
       
  2458 	T* Unmanage()
       
  2459 		{
       
  2460 		return static_cast<T*>(LCleanedupPtrBase::Unmanage());
       
  2461 		}
       
  2462 
       
  2463 /**
       
  2464    Returns ETrue if automatic resource management is enabled; EFalse
       
  2465    otherwise.
       
  2466 
       
  2467    @return ETrue if automatic resource management is enabled; EFalse
       
  2468    otherwise.
       
  2469 */
       
  2470 	using LCleanedupPtrBase::IsEnabled;
       
  2471 
       
  2472 /**
       
  2473    Returns a pointer to the managed object of type T.
       
  2474 
       
  2475    @return A pointer to the managed object of type T.
       
  2476 */
       
  2477 	T* Get() const
       
  2478 		{
       
  2479 		return static_cast<T*>(iPtr);
       
  2480 		}
       
  2481 
       
  2482 /**
       
  2483    Overloaded indirection operator function.
       
  2484 
       
  2485    @return A reference to the managed object of type T.
       
  2486 */
       
  2487 	T& operator*() const
       
  2488 		{
       
  2489 		return *(static_cast<T*>(iPtr));
       
  2490 		}
       
  2491 
       
  2492 /**
       
  2493    Overloaded class member access operator function.
       
  2494 
       
  2495    @return A pointer to the managed object of type T.
       
  2496 */
       
  2497 	T* operator->() const
       
  2498 		{
       
  2499 		return static_cast<T*>(iPtr);
       
  2500 		}
       
  2501 
       
  2502 // Implementation type - do not use
       
  2503 	typedef typename LCleanedupPtrBase::BaseManagedType* LCleanedupPtr<T, CleanupStrategy>::*TUnspecifiedBoolType;
       
  2504 
       
  2505 /**
       
  2506    Conversion operator that enables LCleanedupPtr objects to be used
       
  2507    in boolean contexts.
       
  2508 
       
  2509    @return An unspecified value of an unspecified type convertible to
       
  2510    boolean, which has a boolean value equal to Get() != NULL
       
  2511  */
       
  2512 	operator TUnspecifiedBoolType()
       
  2513 		{
       
  2514 		return iPtr ? &LCleanedupPtr::iPtr : NULL;
       
  2515 		}
       
  2516 
       
  2517 	using LCleanedupPtrBase::Disable;
       
  2518 
       
  2519 	void Swap(LCleanedupPtr& aCleanedupPtr)
       
  2520 		{
       
  2521 		LCleanedupPtrBase::Swap(aCleanedupPtr);
       
  2522 		}
       
  2523 
       
  2524   private:
       
  2525 	using LCleanedupPtrBase::iPtr;
       
  2526 	};
       
  2527 
       
  2528 
       
  2529 // function template used for comparing two LCleanedupPtr-managed
       
  2530 // pointers for equality
       
  2531 template<typename T, typename U>
       
  2532 TBool operator==(const LCleanedupPtr<T>& aPtr1, const LCleanedupPtr<U>& aPtr2)
       
  2533 	{
       
  2534 	return aPtr1.Get() == aPtr2.Get();
       
  2535 	}
       
  2536 
       
  2537 // function template used for comparing two LCleanedupPtr-managed
       
  2538 // pointers for inequality
       
  2539 template<typename T, typename U>
       
  2540 TBool operator!=(const LCleanedupPtr<T>& aPtr1, const LCleanedupPtr<U>& aPtr2)
       
  2541 	{
       
  2542 	return aPtr1.Get() != aPtr2.Get();
       
  2543 	}
       
  2544 
       
  2545 // function template used for testing the ordering of two
       
  2546 // LCleanedupPtr-managed pointers
       
  2547 template<typename T, typename U>
       
  2548 TBool operator<(const LCleanedupPtr<T>& aPtr1, const LCleanedupPtr<U>& aPtr2)
       
  2549 	{
       
  2550 	return aPtr1.Get() < aPtr2.Get();
       
  2551 	}
       
  2552 
       
  2553 
       
  2554 /**
       
  2555    A class template that provides CleanupStack-based local-scope
       
  2556    automatic management of arrays.
       
  2557 
       
  2558    @note This class can only be used to define locals, never
       
  2559    data members. See below for an explanation and links to management
       
  2560    classes suitable for use in different contexts. It should never be
       
  2561    used in the same function as code that uses the CleanupStack API
       
  2562    directly
       
  2563 
       
  2564    @par
       
  2565 
       
  2566    @note This class can only be used with raw arrays, which are used
       
  2567    only rarely on Symbian OS.  Instances of Symbian array container
       
  2568    classes (e.g. RArray, RPointerArray) should be managed using the
       
  2569    automatic management template classes appropriate for the array's
       
  2570    type (LCleanedupHandle template classes for Symbian R arrays or
       
  2571    LCleanedupPtr template classes for Symbian C arrays).
       
  2572 
       
  2573    This class template can be used to protect a heap-allocated array
       
  2574    of objects of type T such that the array of T referred to is
       
  2575    automatically cleaned up when either of the following occur:
       
  2576 
       
  2577    - The referring local variable goes out of scope normally
       
  2578    - The referring local variable goes out of scope due to an
       
  2579 	 untrapped leave causing the scope to be exited non-locally
       
  2580 
       
  2581    The default cleanup strategy is to deallocate the managed array
       
  2582    using arrray delete (delete[]), assuming that the array is
       
  2583    heap-allocated.	An alternative cleanup strategy can be selected by
       
  2584    specifying a cleanup strategy template class as the optional second
       
  2585    template argument (corresponding to the CleanupStrategy template
       
  2586    parameter).
       
  2587 
       
  2588    The constructors of this class may leave.
       
  2589 
       
  2590    As a convenience, the elements of the managed array may be accessed
       
  2591    via "[]" notation directly on the management object.
       
  2592 
       
  2593    Automatic cleanup may be disabled at any time by calling
       
  2594    Unmanage(), while cleanup may be forced at any time by calling
       
  2595    ReleaseResource().
       
  2596 
       
  2597    @code
       
  2598 	// block scope example
       
  2599 	{
       
  2600 	LCleanedupArray<TValue> arrayp(new(ELeave) TValue[KArraySize]);
       
  2601 	arrayp[0].DoSomethingL(); // leave-safe
       
  2602 	if (arrayp[0].Finished())
       
  2603 		return; //	the array is deleted automatically when exiting from scope
       
  2604 	arrayp[1].DoSomethingElseL(); // leave-safe
       
  2605 	//	the array is deleted automatically when exiting from scope
       
  2606 	}
       
  2607    @endcode
       
  2608 
       
  2609    Behind the scenes, this class template is implemented in terms of
       
  2610    the thread-local CleanupStack, restricting its use to locals on the
       
  2611    stack. This use of the CleanupStack ensures a consistent cleanup
       
  2612    order between functions that call one another, even if they use
       
  2613    different cleanup idioms.
       
  2614 
       
  2615    This class template together with the cleanup strategy class
       
  2616    templates provide a template-based implementation of the Strategy
       
  2617    design pattern (See also: Policy-based design).
       
  2618 
       
  2619    @see LManagedArray which has the same interface, but does not use
       
  2620    the cleanup stack and is suitable for protecting the data members
       
  2621    of classes
       
  2622 */
       
  2623 template<typename T,
       
  2624 		 class CleanupStrategyType = TArrayDelete>
       
  2625 class LCleanedupArray: protected LAutoPtrBase<T>
       
  2626 	{
       
  2627 	typedef LAutoPtrBase<T> LAutoPtrBase;
       
  2628 
       
  2629   public:
       
  2630 	typedef T ManagedType;
       
  2631 	typedef CleanupStrategyType CleanupStrategy;
       
  2632 
       
  2633 /**
       
  2634    Default constructor.	 Constructs an empty LCleanedupArray object.
       
  2635 
       
  2636    @post Get() == NULL
       
  2637  */
       
  2638 	LCleanedupArray()
       
  2639 		{
       
  2640 		CleanupStack::PushL(TCleanupItem(Cleanup, this));
       
  2641 		}
       
  2642 
       
  2643 /**
       
  2644    Explicit constructor.  Constructs a LCleanedupArray object that
       
  2645    manages an array of objects of type T that can be cleaned up using
       
  2646    the cleanup strategy of the LCleanedupArray class.  The default
       
  2647    cleanup strategy is to deallocate the heap-allocated array by using
       
  2648    array delete.  An alternative cleanup strategy can be selected by
       
  2649    specifying a cleanup strategy template class as the optional second
       
  2650    template argument (corresponding to the CleanupStrategy template
       
  2651    parameter).
       
  2652 
       
  2653    @param aPtr A pointer to the first element of an array of objects
       
  2654    of type T, array that can be cleaned up using the cleanup strategy
       
  2655    of the the LCleanedupArray class.
       
  2656 
       
  2657    @pre The array can be cleaned up using the cleanup strategy.
       
  2658 
       
  2659    @post Get() == aPtr
       
  2660  */
       
  2661 	explicit LCleanedupArray(T* aPtr)
       
  2662 		: LAutoPtrBase(aPtr)
       
  2663 		{
       
  2664 		CleanupStack::PushL(TCleanupItem(Cleanup, this));
       
  2665 		}
       
  2666 
       
  2667 
       
  2668 /**
       
  2669    Destructor.	When automatic resource management is enabled, the
       
  2670    destructor invokes the specified cleanup strategy for the managed
       
  2671    pointer.
       
  2672  */
       
  2673 	~LCleanedupArray()
       
  2674 		{
       
  2675 		ManagedPopCleanupStackItem(LAutoPtrBase::IsEnabled());
       
  2676 		}
       
  2677 
       
  2678 /**
       
  2679    Assigns a new array of objects of type T to be managed.	It needs
       
  2680    to be be possible to use the cleanup strategy of the
       
  2681    LCleanedupArray object for the cleanup of the new managed array.
       
  2682    The default cleanup strategy is to delete the heap-allocated array
       
  2683    by using array delete (delete[]).  If the LCleanedupArray object
       
  2684    already manages an array, then the cleanup strategy is invoked with
       
  2685    the managed array before assigning the new managed array.
       
  2686 
       
  2687    @param aPtr A pointer to the first element of the array of objects
       
  2688    of type T - array that can be cleaned up using the cleanup
       
  2689    strategy.
       
  2690 
       
  2691    @pre The new array to be managed can be cleaned up using the
       
  2692    cleanup strategy.
       
  2693 
       
  2694    @post Get() == aPtr
       
  2695  */
       
  2696 	LCleanedupArray& operator=(T* aPtr)
       
  2697 		{
       
  2698 		ReleaseResource();
       
  2699 		LAutoPtrBase::operator=(aPtr);
       
  2700 		return *this;
       
  2701 		}
       
  2702 
       
  2703 /**
       
  2704    If automatic resource management is enabled, the specified cleanup
       
  2705    strategy is invoked for the managed pointer and the automatic
       
  2706    resource management is then disabled.  The underlying pointer is
       
  2707    reset to NULL.
       
  2708 
       
  2709    @post Get() == NULL
       
  2710 */
       
  2711 	void ReleaseResource()
       
  2712 		{
       
  2713 		if (!LAutoPtrBase::IsEnabled())
       
  2714 			return;
       
  2715 
       
  2716 		CleanupStrategy::Cleanup(iPtr);
       
  2717 		iPtr = NULL;
       
  2718 		}
       
  2719 
       
  2720 
       
  2721 /**
       
  2722    Disables the automatic resource management for this object and
       
  2723    returns a pointer to the first element of the array of objects of
       
  2724    type T.
       
  2725 
       
  2726    @return A pointer to the first element of the array of objects of
       
  2727    type T.
       
  2728 */
       
  2729 	using LAutoPtrBase::Unmanage;
       
  2730 
       
  2731 /**
       
  2732    Returns ETrue if automatic resource management is enabled; EFalse
       
  2733    otherwise.
       
  2734 
       
  2735    @return ETrue if automatic resource management is enabled; EFalse
       
  2736    otherwise.
       
  2737 */
       
  2738 	using LAutoPtrBase::IsEnabled;
       
  2739 
       
  2740 /**
       
  2741    Returns a pointer to the first element of the managed array of
       
  2742    objects of type T.
       
  2743 
       
  2744    @return A pointer to the first element of the managed array of
       
  2745    objects of type T.
       
  2746 */
       
  2747 	using LAutoPtrBase::Get;
       
  2748 
       
  2749 /**
       
  2750    Overloaded subscript operator.
       
  2751 
       
  2752    @return A reference to the object of type T at the position aIndex.
       
  2753  */
       
  2754 	T& operator[](TInt aIndex) const
       
  2755 		{
       
  2756 		return iPtr[aIndex];
       
  2757 		}
       
  2758 
       
  2759 	static void Cleanup(TAny* aPtr)
       
  2760 		{
       
  2761 		LCleanedupArray* cleanupPtr = static_cast<LCleanedupArray*>(aPtr);
       
  2762 
       
  2763 		if (cleanupPtr->IsEnabled())
       
  2764 			{
       
  2765 			CleanupStrategy::Cleanup(cleanupPtr->iPtr);
       
  2766 			}
       
  2767 		}
       
  2768 
       
  2769 	using LAutoPtrBase::Disable;
       
  2770 
       
  2771 	void Swap(LCleanedupArray& aArray)
       
  2772 		{
       
  2773 		LAutoPtrBase::Swap(aArray);
       
  2774 		}
       
  2775 
       
  2776   private:
       
  2777 	using LAutoPtrBase::iPtr;
       
  2778 	};
       
  2779 
       
  2780 
       
  2781 /**
       
  2782    A class template that provides CleanupStack-based local-scope
       
  2783    automatic management of references to resource handles (often
       
  2784    instances of R-classes).
       
  2785 
       
  2786    @note This class can only be used to define locals, never
       
  2787    data members. See below for an explanation and links to management
       
  2788    classes suitable for use in different contexts. It should never be
       
  2789    used in the same function as code that uses the CleanupStack API
       
  2790    directly.
       
  2791 
       
  2792    Unlike LCleanedupHandle which creates a fresh instance of its
       
  2793    managed type, this class template can be used to reference and
       
  2794    protect an existing resource handle of type T (typically an
       
  2795    R-class). The instance of T referred to has a cleanup operation run
       
  2796    on it automatically when either of the following occur:
       
  2797 
       
  2798    - The referring local variable goes out of scope normally
       
  2799    - The referring local variable goes out of scope due to an
       
  2800 	 untrapped leave causing the scope to be exited non-locally
       
  2801 
       
  2802    By default, the cleanup action is to call the Close() member
       
  2803    function of the referenced handle. An alternative cleanup strategy
       
  2804    may be selected by specifying a cleanup strategy template class in
       
  2805    the optional second template parameter position. The most common
       
  2806    alternative cleanup strategies are predefined. It is also possible
       
  2807    to specialize the default cleanup action for a given class using
       
  2808    the DEFINE_CLEANUP_FUNCTION macro.
       
  2809 
       
  2810    The constructors of this class may leave.
       
  2811 
       
  2812    As a convenience, the methods of the managed handle may be
       
  2813    accessed via "->" notation directly on the management object, while
       
  2814    "." notation is used to access the interface of the management
       
  2815    object itself. Using "*" to dereference the management object
       
  2816    yields a T&, and is often useful when passing the managed object as
       
  2817    an argument.
       
  2818 
       
  2819    Automatic cleanup may be disabled at any time by calling
       
  2820    Unmanage(), while cleanup may be forced at any time by calling
       
  2821    ReleaseResource().
       
  2822 
       
  2823    Example:
       
  2824    @code
       
  2825 	// block scope example
       
  2826 	void DoWithClosable(RClosable& aObj)
       
  2827 	  {
       
  2828 	  LCleanedupRef<RClosable> obj(aObj);
       
  2829 	  obj->DoSomethingL(); // leave-safe
       
  2830 	  if (obj->Finished())
       
  2831 		return; // RClosable::Close is invoked automatically
       
  2832 	  obj->DoSomethingElseL(); // leave-safe
       
  2833 	  // RClosable::Close is invoked automatically
       
  2834 	  }
       
  2835    @endcode
       
  2836 
       
  2837    Behind the scenes, this class template is implemented in terms of
       
  2838    the thread-local CleanupStack, restricting its use to locals on the
       
  2839    stack. This use of the CleanupStack ensures a consistent cleanup
       
  2840    order between functions that call one another, even if they use
       
  2841    different cleanup idioms.
       
  2842 
       
  2843    This class template together with the cleanup strategy class
       
  2844    templates provide a template-based implementation of the Strategy
       
  2845    design pattern (See also: Policy-based design).
       
  2846 
       
  2847    @see TClose which implements the default Close() calling cleanup strategy
       
  2848    @see TResetAndDestroy which implements an alternative
       
  2849    ResetAndDestroy() calling cleanup strategy
       
  2850    @see TFree which implements an alternative Free() calling cleanup
       
  2851    strategy
       
  2852    @see TDestroy which implements an alternative Destroy() calling
       
  2853    cleanup strategy
       
  2854    @see TRelease which implements an alternative Release() calling
       
  2855    cleanup strategy
       
  2856    @see LManagedRef which has the same interface, but does not use
       
  2857    the cleanup stack and is suitable for protecting the data members of
       
  2858    classes
       
  2859    @see LCleanedupHandle which has a similar interface but creates a
       
  2860    fresh local instance of T
       
  2861 */
       
  2862 template<typename T,
       
  2863 		 class CleanupStrategyType = TResourceCleanupStrategy>
       
  2864 class LCleanedupRef: protected LAutoRefBase<T>
       
  2865 	{
       
  2866 	typedef LAutoRefBase<T> LAutoRefBase;
       
  2867 
       
  2868   public:
       
  2869 	typedef T ManagedType;
       
  2870 	typedef CleanupStrategyType CleanupStrategy;
       
  2871 
       
  2872 /**
       
  2873    Explicit constructor.
       
  2874  */
       
  2875 	template<typename U>
       
  2876 	explicit LCleanedupRef(U& aRef)
       
  2877 		: LAutoRefBase(aRef)
       
  2878 		{
       
  2879 		CleanupStack::PushL(TCleanupItem(Cleanup, this));
       
  2880 		}
       
  2881 
       
  2882 /**
       
  2883    Destructor.	When automatic resource management is enabled, the
       
  2884    destructor invokes the specified cleanup strategy for the managed
       
  2885    reference.
       
  2886  */
       
  2887 	~LCleanedupRef()
       
  2888 		{
       
  2889 		ManagedPopCleanupStackItem(LAutoRefBase::IsEnabled());
       
  2890 		}
       
  2891 
       
  2892 /**
       
  2893    Assigns a new reference to be managed.  If the LCleanedupRef
       
  2894    object already contains a managed reference, then the specified
       
  2895    cleanup strategy is invoked for the managed reference before
       
  2896    assigning the new managed reference.
       
  2897  */
       
  2898 	template<typename U>
       
  2899 	LCleanedupRef& operator=(U& aRef)
       
  2900 		{
       
  2901 		ReleaseResource();
       
  2902 		LAutoRefBase::operator=(aRef);
       
  2903 		return *this;
       
  2904 		}
       
  2905 
       
  2906 /**
       
  2907    If automatic resource management is enabled, the specified cleanup
       
  2908    strategy is invoked for the managed reference and the automatic
       
  2909    resource management is then disabled.
       
  2910 */
       
  2911 	void ReleaseResource()
       
  2912 		{
       
  2913 		if (!LAutoRefBase::IsEnabled())
       
  2914 			return;
       
  2915 
       
  2916 		CleanupStrategy::Cleanup(iPtr);
       
  2917 		iPtr = NULL;
       
  2918 		}
       
  2919 
       
  2920 /**
       
  2921    Disables the automatic resource management for this object and
       
  2922    returns a reference to the object of type T.
       
  2923 
       
  2924    @return A reference to the object of type T.
       
  2925 */
       
  2926 	using LAutoRefBase::Unmanage;
       
  2927 
       
  2928 /**
       
  2929    Returns ETrue if automatic resource management is enabled; EFalse
       
  2930    otherwise.
       
  2931 
       
  2932    @return ETrue if automatic resource management is enabled; EFalse
       
  2933    otherwise.
       
  2934 */
       
  2935 	using LAutoRefBase::IsEnabled;
       
  2936 
       
  2937 /**
       
  2938    Returns a reference to the managed object of type T.
       
  2939 
       
  2940    @return A reference to the managed object of type T.
       
  2941 */
       
  2942 	using LAutoRefBase::Get;
       
  2943 
       
  2944 /**
       
  2945    Overloaded indirection operator function.
       
  2946 
       
  2947    @return A reference to the managed object of type T.
       
  2948 */
       
  2949 	using LAutoRefBase::operator*;
       
  2950 
       
  2951 /**
       
  2952    Overloaded class member access operator function.
       
  2953 
       
  2954    @return A pointer to the managed object of type T.
       
  2955 */
       
  2956 	using LAutoRefBase::operator->;
       
  2957 
       
  2958 
       
  2959 	static void Cleanup(TAny* aPtr)
       
  2960 		{
       
  2961 		LCleanedupRef* cleanupRef = static_cast<LCleanedupRef*>(aPtr);
       
  2962 
       
  2963 		if (cleanupRef->IsEnabled())
       
  2964 			{
       
  2965 			CleanupStrategy::Cleanup(cleanupRef->iPtr);
       
  2966 			}
       
  2967 		}
       
  2968 
       
  2969 	using LAutoRefBase::Disable;
       
  2970 
       
  2971 	void Swap(LCleanedupRef& aRef)
       
  2972 		{
       
  2973 		LAutoRefBase::Swap(aRef);
       
  2974 		}
       
  2975 
       
  2976   private:
       
  2977 	using LAutoRefBase::iPtr;
       
  2978 	};
       
  2979 
       
  2980 
       
  2981 /**
       
  2982    A class that provides automatic cleanup using a TCleanupOperation
       
  2983    on the destruction of the LManagedGuard object.
       
  2984 
       
  2985    @note This class can only be used to define object scoped cleanup
       
  2986    to guard object destruction, never local stack scoped cleanup. See
       
  2987    below for an explanation and links to management classes suitable
       
  2988    for use in different contexts.
       
  2989 
       
  2990    This class can be used to manage a TCleanupOperation in such a way
       
  2991    that the specified cleanup operation is guaranteed to be called
       
  2992    when the guarding object is destroyed; typically when the object
       
  2993    containing it is deleted.
       
  2994 
       
  2995    The constructors of this class never leave, so data members defined with
       
  2996    this type may be initialized safely during any phase of
       
  2997    construction of the owning class.
       
  2998 
       
  2999    Automatic cleanup may be disabled at any time by calling
       
  3000    Dismiss(), while cleanup may be forced at any time by calling
       
  3001    Execute().
       
  3002 
       
  3003    @code
       
  3004    class CComposite : public CBase
       
  3005 	   {
       
  3006 	 public:
       
  3007 	   CONSTRUCTORS_MAY_LEAVE
       
  3008 
       
  3009 	   CComposite(RCleanable* aObj)
       
  3010 		   : iObj(RCleanable::Cleanup, aObj)
       
  3011 		   {
       
  3012 		   }
       
  3013 
       
  3014 	   ~CComposite()
       
  3015 		   {
       
  3016 		   // RCleanable::Cleanup(iObj) is automatically invoked
       
  3017 		   }
       
  3018 
       
  3019 	 private:
       
  3020 	   LManagedGuard<RCleanable> iObj;
       
  3021 	   };
       
  3022    @endcode
       
  3023 
       
  3024    Behind the scenes, this class template simply relies on reliable
       
  3025    execution of its destructor. If used for a local variable rather
       
  3026    than a data member, cleanup will occur but out-of-order compared to
       
  3027    objects protected using the LCleanupXxx variants or the
       
  3028    CleanupStack directly. Therefore it is not recommended for use in
       
  3029    that context.
       
  3030 
       
  3031    These management classes may be used as the basis for implementing
       
  3032    leave-safe single-phase construction, since fully initialized
       
  3033    data members protected in this way will get destroyed (so reliably
       
  3034    triggering cleanup) if their containing classes leave during
       
  3035    execution of their constructors. Note, however, that single-phase
       
  3036    construction must be explicitly enabled in the containing class
       
  3037    using the CONSTRUCTORS_MAY_LEAVE macro.
       
  3038 
       
  3039    @see LCleanedupGuard which has the same interface, but uses the cleanup
       
  3040    stack and is suitable for use as a local to guard local scope exit
       
  3041    @see CONSTRUCTORS_MAY_LEAVE
       
  3042 */
       
  3043 class LManagedGuard
       
  3044 	{
       
  3045   public:
       
  3046 /**
       
  3047    Constructor.	 Creates a LCleanedupGuard object that, when enabled,
       
  3048    automatically invokes upon destruction a cleanup operation
       
  3049    specified by the aCleanupOperation parameter with the pointer to
       
  3050    data specified by the aData parameter.
       
  3051 
       
  3052    @param aCleanupOperation A cleanup operation.
       
  3053    @param aData Pointer to data to be passed to the cleanup operation
       
  3054  */
       
  3055 	LManagedGuard(TCleanupOperation aCleanupOperation, TAny* aData = 0)
       
  3056 		: iCleanupOperation(aCleanupOperation),
       
  3057 		  iData(aData)
       
  3058 		{
       
  3059 		}
       
  3060 
       
  3061 /**
       
  3062    Destructor.
       
  3063  */
       
  3064 	~LManagedGuard()
       
  3065 		{
       
  3066 		Execute();
       
  3067 		}
       
  3068 
       
  3069 /**
       
  3070    Executes the guard cleanup operation.
       
  3071 */
       
  3072 	void Execute()
       
  3073 		{
       
  3074 		if (iCleanupOperation)
       
  3075 			{
       
  3076 			iCleanupOperation(iData);
       
  3077 			}
       
  3078 		}
       
  3079 
       
  3080 /**
       
  3081    Disables the guard.
       
  3082 */
       
  3083 	void Dismiss()
       
  3084 		{
       
  3085 		iCleanupOperation = NULL;
       
  3086 		}
       
  3087 
       
  3088   private:
       
  3089 	LManagedGuard(const LManagedGuard&);
       
  3090 	LManagedGuard& operator=(const LManagedGuard&);
       
  3091 
       
  3092 	TCleanupOperation iCleanupOperation;
       
  3093 	TAny* iData;
       
  3094 	};
       
  3095 
       
  3096 
       
  3097 /**
       
  3098    A class that provides CleanupStack-based local-scope automatic
       
  3099    cleanup using a TCleanupOperation on the destruction of the
       
  3100    LManagedGuard object.
       
  3101 
       
  3102    @note This class can only be used to define a local stack scoped
       
  3103    cleanup, never an object scoped cleanup to guard object
       
  3104    destruction. See below for an explanation and links to management
       
  3105    classes suitable for use in different contexts.
       
  3106 
       
  3107    This class can be used to manage a TCleanupOperation in such a way
       
  3108    that the specified cleanup operation is guaranteed to be called
       
  3109    when either of the following occur:
       
  3110 
       
  3111    - The guarding local variable goes out of scope normally
       
  3112    - The guarding local variable goes out of scope due to an
       
  3113 	 untrapped leave causing the scope to be exited non-locally
       
  3114 
       
  3115    The constructors of this class may leave.
       
  3116 
       
  3117    Automatic cleanup may be disabled at any time by calling
       
  3118    Dismiss(), while cleanup may be forced at any time by calling
       
  3119    Execute().
       
  3120 
       
  3121    @code
       
  3122 	// block scope example
       
  3123 	{
       
  3124 	RCleanable obj;
       
  3125 	LCleanedupGuard cleanGuard(RCleanable::Cleanup, &obj);
       
  3126 
       
  3127 	obj.DoSomethingL(); // leave-safe
       
  3128 	if (Finished())
       
  3129 		return; // RCleanable::Cleanup is invoked automatically when exiting from scope
       
  3130 	obj.DoSomethingElseL(); // leave-safe
       
  3131 	//	RCleanable::Cleanup is invoked automatically when exiting from scope
       
  3132 	}
       
  3133    @endcode
       
  3134 
       
  3135    Behind the scenes, this class template is implemented in terms of
       
  3136    the thread-local CleanupStack, restricting its use to local stack
       
  3137    scope. This use of the CleanupStack ensures a consistent cleanup
       
  3138    order between functions that call one another, even if they use
       
  3139    different cleanup idioms.
       
  3140 
       
  3141    @see LManagedGuard which has the same interface, but does not use the cleanup
       
  3142    stack and is suitable for use as the data member of a class to guard
       
  3143    object destruction.
       
  3144 */
       
  3145 class LCleanedupGuard
       
  3146 	{
       
  3147   public:
       
  3148 /**
       
  3149    Constructor.	 Creates a LCleanedupGuard object that, when enabled,
       
  3150    automatically invokes upon destruction a cleanup operation
       
  3151    specified by the aCleanupOperation parameter with the pointer to
       
  3152    data specified by the aData parameter.
       
  3153 
       
  3154    @param aCleanupOperation A cleanup operation.
       
  3155    @param aData Pointer to data to be passed to the cleanup operation
       
  3156  */
       
  3157 	LCleanedupGuard(TCleanupOperation aCleanupOperation, TAny* aData = 0)
       
  3158 		: iCleanupOperation(aCleanupOperation),
       
  3159 		  iData(aData)
       
  3160 		{
       
  3161 		CleanupStack::PushL(TCleanupItem(Cleanup, this));
       
  3162 		}
       
  3163 
       
  3164 /**
       
  3165    Destructor.
       
  3166  */
       
  3167 	~LCleanedupGuard()
       
  3168 		{
       
  3169 		ManagedPopCleanupStackItem(iCleanupOperation);
       
  3170 		}
       
  3171 
       
  3172 /**
       
  3173    Executes the guard cleanup operation.
       
  3174 */
       
  3175 	void Execute()
       
  3176 		{
       
  3177 		if (iCleanupOperation)
       
  3178 			{
       
  3179 			iCleanupOperation(iData);
       
  3180 			}
       
  3181 		}
       
  3182 
       
  3183 /**
       
  3184    Disables the guard.
       
  3185 */
       
  3186 	void Dismiss()
       
  3187 		{
       
  3188 		iCleanupOperation = NULL;
       
  3189 		}
       
  3190 
       
  3191 	static void Cleanup(TAny* aPtr)
       
  3192 		{
       
  3193 		LCleanedupGuard* guard = static_cast<LCleanedupGuard*>(aPtr);
       
  3194 		guard->Execute();
       
  3195 		}
       
  3196 
       
  3197   private:
       
  3198 	LCleanedupGuard(const LCleanedupGuard&);
       
  3199 	LCleanedupGuard& operator=(const LCleanedupGuard&);
       
  3200 
       
  3201 
       
  3202 	TCleanupOperation iCleanupOperation;
       
  3203 	TAny* iData;
       
  3204 	};
       
  3205 
       
  3206 #endif // !EMANAGED_H
       
  3207