Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
29 @brief Utility class templates that provide RAII-based automatic
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
43 // Not for Client Use , Only to be used Internally.
45 inline void CallCleanupFunction(T* aObjPtr)
52 Utility macro that can be used for defining the cleanup member
53 function for a class (typically a R-class).
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.
60 class RDestroyableClass
64 void Destroy(); // member function used for cleanup and releasing the resources owned by a RDestroyableClass object
68 DEFINE_CLEANUP_FUNCTION(RDestroyableClass, Destroy)
70 @param AClass the name of the class
71 @param CleanupMemFun the name of the cleanup member function of the class
73 #define DEFINE_CLEANUP_FUNCTION(AClass, CleanupMemFun) \
74 inline void CallCleanupFunction(AClass* aObjPtr) \
76 aObjPtr->CleanupMemFun(); \
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.
86 This macro must be used in the same namespace in which the R-class is
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.
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.
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
106 #define CONSTRUCTORS_MAY_LEAVE \
107 static void operator delete(TAny* aPtr) __NO_THROW \
109 ::operator delete(aPtr); \
112 static void operator delete(TAny*, TAny*) __NO_THROW \
116 static void operator delete(TAny* aPtr, TLeave) __NO_THROW \
118 ::operator delete(aPtr); \
121 static void operator delete(TAny* aPtr, TUint) __NO_THROW \
123 ::operator delete(aPtr); \
126 static void operator delete(TAny* aPtr, TLeave, TUint) __NO_THROW \
128 ::operator delete(aPtr); \
131 static void operator delete[](TAny* aPtr) __NO_THROW \
133 ::operator delete[](aPtr); \
136 static void operator delete[](TAny* aPtr, TLeave) __NO_THROW \
138 ::operator delete[](aPtr); \
142 // Implementation function.
144 void ManagedPopCleanupStackItem(T aIsManaged)
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
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())
156 CleanupStack::PopAndDestroy();
167 Strategy (policy) class that defines the default cleanup strategy
168 for managed resource class objects.
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.
177 // Not for Client Use , Only to be used Internally.
178 class TResourceCleanupStrategy
182 static void Cleanup(T* aObjPtr)
184 CallCleanupFunction(aObjPtr);
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.
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
202 static void Cleanup(T* aObjPtr)
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.
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
222 static void Cleanup(T* aObjPtr)
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.
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
242 static void Cleanup(T* aObjPtr)
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.
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
262 static void Cleanup(T* aObjPtr)
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.
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
278 class TResetAndDestroy
282 static void Cleanup(T* aObjPtr)
284 aObjPtr->ResetAndDestroy();
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
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
302 class TPtrCleanupStrategy
306 static void Cleanup(T* aPtr)
311 static void Cleanup(CBase* aPtr)
319 Strategy (policy) class that defines a cleanup strategy for pointer
320 types. This cleanup strategy deletes the managed pointer by using
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
328 class TPointerDeleteStrategy
332 static void Cleanup(T* aPtr)
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.
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
349 class TCBaseDeleteStrategy
352 static void Cleanup(CBase* aPtr)
360 Strategy (policy) class that defines a cleanup strategy for pointer
361 types. This cleanup strategy calls User::Free with the managed
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
372 static void Cleanup(TAny* aPtr)
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.
388 static void Cleanup(T* aPtr)
395 // enum type used for identifying the categories of managed pointer types
403 // macro used for determining whether a pointer is special
404 #define IS_PTR_SPECIAL(T) IS_BASE_OF(CBase, T)
407 // enum type used for identifying the categories of resource handle types
410 EAutoHandleNonSpecial,
411 EAutoRHandleBaseDerived,
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 )
421 Implementation base class - not designed for public inheritance or
426 // Not for Client Use , Only to be used Internally.
428 TInt isHandleSpecial = IS_HANDLE_SPECIAL(T)>
429 class LAutoHandleBase
437 template<typename Param1>
438 explicit LAutoHandleBase(const Param1& aParam1)
444 template<typename Param1>
445 explicit LAutoHandleBase(Param1& aParam1)
451 template<typename Param1,
453 LAutoHandleBase(const Param1& aParam1,
454 const Param2& aParam2)
461 template<typename Param1,
463 LAutoHandleBase(Param1& aParam1,
464 const Param2& aParam2)
471 template<typename Param1,
473 LAutoHandleBase(const Param1& aParam1,
481 template<typename Param1,
483 LAutoHandleBase(Param1& aParam1,
492 LAutoHandleBase& operator=(const U& aHandle)
514 const T& operator*() const
524 const T* operator->() const
535 TBool IsEnabled() const
545 void Swap(LAutoHandleBase& aAutoHandle)
547 ::Swap(iHandle, aAutoHandle.iHandle);
548 ::Swap(iEnabled, aAutoHandle.iEnabled);
556 LAutoHandleBase(const LAutoHandleBase&);
557 LAutoHandleBase& operator=(const LAutoHandleBase&);
562 Implementation base class - not designed for public inheritance or
563 direct use. Specialization for types derived from RHandleBase.
566 class LAutoHandleBase<T, EAutoRHandleBaseDerived>
573 template<typename Param1>
574 explicit LAutoHandleBase(const Param1& aParam1)
579 template<typename Param1>
580 explicit LAutoHandleBase(Param1& aParam1)
585 template<typename Param1,
587 LAutoHandleBase(const Param1& aParam1,
588 const Param2& aParam2)
594 template<typename Param1,
596 LAutoHandleBase(Param1& aParam1,
597 const Param2& aParam2)
603 template<typename Param1,
605 LAutoHandleBase(const Param1& aParam1,
612 template<typename Param1,
614 LAutoHandleBase(Param1& aParam1,
622 LAutoHandleBase& operator=(const U& aHandle)
643 const T& operator*() const
653 const T* operator->() const
661 iHandle.SetHandle(KNullHandle);
665 TBool IsEnabled() const
667 return iHandle.Handle() != KNullHandle;
672 iHandle.SetHandle(KNullHandle);
675 void Swap(LAutoHandleBase& aAutoHandle)
677 ::Swap(iHandle, aAutoHandle.iHandle);
684 LAutoHandleBase(const LAutoHandleBase&);
685 LAutoHandleBase& operator=(const LAutoHandleBase&);
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.
693 class LAutoHandleBase<T, EAutoHandleRBuf>: protected T
707 A class template for the creation and automatic management of
708 resource handles (typically R-class instances) held in the data
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
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.
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.
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.
733 Any arguments supplied when initializing an instance of this class
734 are automatically passed through to T's constructors.
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
743 Automatic cleanup may be disabled at any time by calling
744 Unmanage(), while cleanup may be forced at any time by calling
749 class CComposite : public CBase
752 CONSTRUCTORS_MAY_LEAVE
756 iFileServ->Connect() OR_LEAVE;
757 iFile->Open(*iFileServ, ...);
762 // the handles are automatically closed
767 LManagedHandle<RFs> iFileServ;
768 LManagedHandle<RFile> iFile;
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
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.
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).
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
796 @see TDestroy which implements an alternative Destroy() calling
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
804 class CleanupStrategyType = TResourceCleanupStrategy>
805 class LManagedHandle: protected LAutoHandleBase<T, IS_HANDLE_SPECIAL(T)>
807 typedef LAutoHandleBase<T, IS_HANDLE_SPECIAL(T)> LAutoHandleBase;
810 typedef T ManagedType;
811 typedef CleanupStrategyType CleanupStrategy;
820 template<typename Param1>
821 explicit LManagedHandle(const Param1& aParam1)
822 : LAutoHandleBase(aParam1)
826 template<typename Param1>
827 explicit LManagedHandle(Param1& aParam1)
828 : LAutoHandleBase(aParam1)
832 template<typename Param1,
834 LManagedHandle(const Param1& aParam1,
835 const Param2& aParam2)
836 : LAutoHandleBase(aParam1,
841 template<typename Param1,
843 LManagedHandle(const Param1& aParam1,
845 : LAutoHandleBase(aParam1,
850 template<typename Param1,
852 LManagedHandle(Param1& aParam1,
853 const Param2& aParam2)
854 : LAutoHandleBase(aParam1,
859 template<typename Param1,
861 LManagedHandle(Param1& aParam1,
863 : LAutoHandleBase(aParam1,
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.
874 @param aHandle a reference to a handle object of a type that can be assigned to a handle object of type T
877 LManagedHandle& operator=(const U& aHandle)
880 LAutoHandleBase::operator=(aHandle);
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.
893 CleanupStrategy::Cleanup(&Get());
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.
908 void ReleaseResource()
913 CleanupStrategy::Cleanup(&Get());
914 LAutoHandleBase::Disable();
918 Disables the automatic resource management for this object and
919 returns a copy of the resource handle.
921 @return A copy of the resource handle.
923 using LAutoHandleBase::Unmanage;
926 Returns ETrue if automatic resource management is enabled; EFalse
929 @return ETrue if automatic resource management is enabled; EFalse
932 using LAutoHandleBase::IsEnabled;
935 Returns a reference to the resource handle.
937 @return A reference to the resource handle.
939 using LAutoHandleBase::Get;
942 Overloaded indirection operator function.
944 @return A reference to the resource handle.
946 using LAutoHandleBase::operator*;
949 Overloaded class member access operator function.
951 @return A pointer to the resource handle.
953 using LAutoHandleBase::operator->;
955 using LAutoHandleBase::Disable;
957 void Swap(LManagedHandle& aManagedHandle)
959 LAutoHandleBase::Swap(aManagedHandle);
965 Implementation base class - not designed for public inheritance or
970 // Not for Client Use , Only to be used Internally.
980 explicit LAutoPtrBase(T* aPtr)
985 LAutoPtrBase& operator=(T* aPtr)
998 TBool IsEnabled() const
1000 return iPtr != NULL;
1008 T* operator->() const
1018 void Swap(LAutoPtrBase& aAutoPtr)
1020 ::Swap(iPtr, aAutoPtr.iPtr);
1027 LAutoPtrBase(const LAutoPtrBase&);
1028 LAutoPtrBase& operator=(const LAutoPtrBase&);
1032 // Cleanup traits class template
1033 template<typename T,
1034 class CleanupStrategyType,
1035 TInt isPtrSpecial = IS_PTR_SPECIAL(T)>
1036 struct TPtrCleanupTraits
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>
1047 typedef T ManagedType;
1048 typedef T BaseManagedType;
1049 typedef CleanupStrategyType CleanupStrategy;
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>
1058 typedef T ManagedType;
1059 typedef CBase BaseManagedType;
1060 typedef CleanupStrategyType CleanupStrategy;
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>
1069 typedef CBase ManagedType;
1070 typedef CBase BaseManagedType;
1071 typedef TPtrCleanupStrategy CleanupStrategy;
1076 Implementation base class - not designed for public inheritance or
1079 template<typename T,
1080 class CleanupStrategyType>
1081 class LManagedPtrBase: protected LAutoPtrBase<typename TPtrCleanupTraits<T, CleanupStrategyType>::BaseManagedType>
1083 typedef LAutoPtrBase<typename TPtrCleanupTraits<T, CleanupStrategyType>::BaseManagedType> LAutoPtrBase;
1086 typedef typename TPtrCleanupTraits<T, CleanupStrategyType>::ManagedType ManagedType;
1087 typedef typename TPtrCleanupTraits<T, CleanupStrategyType>::BaseManagedType BaseManagedType;
1088 typedef typename TPtrCleanupTraits<T, CleanupStrategyType>::CleanupStrategy CleanupStrategy;
1094 template<typename U>
1095 explicit LManagedPtrBase(U* aPtr)
1096 : LAutoPtrBase(aPtr)
1101 Destructor. When automatic resource management is enabled, the
1102 destructor invokes the specified cleanup strategy for the managed
1109 CleanupStrategy::Cleanup(static_cast<ManagedType*>(iPtr));
1113 template<typename U>
1114 LManagedPtrBase& operator=(U* aPtr)
1117 LAutoPtrBase::operator=(aPtr);
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
1129 void ReleaseResource()
1134 CleanupStrategy::Cleanup(static_cast<ManagedType*>(iPtr));
1135 LAutoPtrBase::Disable();
1138 using LAutoPtrBase::Unmanage;
1140 using LAutoPtrBase::IsEnabled;
1142 using LAutoPtrBase::Get;
1144 using LAutoPtrBase::operator->;
1146 using LAutoPtrBase::Disable;
1148 using LAutoPtrBase::iPtr;
1150 void Swap(LManagedPtrBase& aManagedPtr)
1152 LAutoPtrBase::Swap(aManagedPtr);
1158 A class template that provides automatic management of pointers
1159 held in the data members of objects.
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
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.
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).
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.
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
1188 Automatic cleanup may be disabled at any time by calling
1189 Unmanage(), while cleanup may be forced at any time by calling
1194 class CComposite : public CBase
1197 CONSTRUCTORS_MAY_LEAVE
1200 : iComponent(CComponent::NewL())
1207 // the pointer to the CComponent object is automatically
1212 LManagedPtr<CComponent> iComponent;
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
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.
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).
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
1241 template<typename T,
1242 class CleanupStrategyType = TPtrCleanupStrategy>
1243 class LManagedPtr: protected LManagedPtrBase<T, CleanupStrategyType>
1245 typedef LManagedPtrBase<T, CleanupStrategyType> LManagedPtrBase;
1248 typedef T ManagedType;
1249 typedef CleanupStrategyType CleanupStrategy;
1253 Default constructor. Constructs an empty LManagedPtr object.
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.
1270 @param aPtr A pointer of a type that is convertible to T* that can
1271 be cleaned up using the cleanup strategy.
1273 @pre aPtr is of a type convertible to T* and can be cleaned up
1274 using the cleanup strategy.
1278 explicit LManagedPtr(T* aPtr)
1279 : LManagedPtrBase(aPtr)
1284 Destructor. When automatic resource management is enabled, the
1285 destructor invokes the specified cleanup strategy for the managed
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.
1298 @param aPtr A pointer of a type that is convertible to T* that can
1299 be cleaned up using the cleanup strategy.
1301 @pre aPtr is a pointer of a type that is convertible to T* and can
1302 be cleaned up using the cleanup strategy.
1306 LManagedPtr& operator=(T* aPtr)
1308 LManagedPtrBase::operator=(aPtr);
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.
1320 @param aPtr A pointer of a type that is convertible to T* that can
1321 be cleaned up using the cleanup strategy.
1323 @pre aPtr is a pointer of a type that is convertible to T* and can
1324 be cleaned up using the cleanup strategy.
1328 template<typename U>
1329 LManagedPtr& operator=(U* aPtr)
1331 LManagedPtrBase::operator=(aPtr);
1335 using LManagedPtrBase::ReleaseResource;
1338 Disables the automatic resource management for this object and
1339 returns a pointer to the object of type T.
1341 @return A pointer to the object of type T.
1345 return static_cast<T*>(LManagedPtrBase::Unmanage());
1349 Returns ETrue if automatic resource management is enabled; EFalse
1352 @return ETrue if automatic resource management is enabled; EFalse
1355 using LManagedPtrBase::IsEnabled;
1358 Returns a pointer to the managed object of type T.
1360 @return A pointer to the managed object of type T.
1364 return static_cast<T*>(iPtr);
1368 Overloaded indirection operator function.
1370 @return A reference to the managed object of type T.
1372 T& operator*() const
1374 return *(static_cast<T*>(iPtr));
1378 Overloaded class member access operator function.
1380 @return A pointer to the managed object of type T.
1382 T* operator->() const
1384 return static_cast<T*>(iPtr);
1388 // Implementation type - do not use
1389 typedef typename LManagedPtrBase::BaseManagedType* LManagedPtr<T, CleanupStrategy>::*TUnspecifiedBoolType;
1392 Conversion operator that enables LCleanedupPtr objects to be used
1393 in boolean contexts.
1395 @return An unspecified value of an unspecified type convertible to
1396 boolean, which has a boolean value equal to Get() != NULL
1398 operator TUnspecifiedBoolType()
1400 return iPtr ? &LManagedPtr::iPtr : NULL;
1404 using LManagedPtrBase::Disable;
1406 void Swap(LManagedPtr& aManagedPtr)
1408 LManagedPtrBase::Swap(aManagedPtr);
1412 using LManagedPtrBase::iPtr;
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)
1421 return aPtr1.Get() == aPtr2.Get();
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)
1429 return aPtr1.Get() != aPtr2.Get();
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)
1437 return aPtr1.Get() < aPtr2.Get();
1442 A class template that provides automatic management of arrays. Such
1443 managed arrays can be data members of composite classes.
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
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).
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.
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
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.
1473 As a convenience, the elements of the managed array may be accessed
1474 via "[]" notation directly on the management object.
1476 Automatic cleanup may be disabled at any time by calling
1477 Unmanage(), while cleanup may be forced at any time by calling
1483 class CComposite : public CBase
1486 CONSTRUCTORS_MAY_LEAVE
1489 : iComponents(new(ELeave) CComponent[KNumComponents])
1496 // the array is automatically deleted
1500 LManagedArray<CComponent> iComponents;
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
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.
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).
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
1528 template<typename T,
1529 class CleanupStrategyType = TArrayDelete>
1530 class LManagedArray: protected LAutoPtrBase<T>
1532 typedef LAutoPtrBase<T> LAutoPtrBase;
1535 typedef T ManagedType;
1536 typedef CleanupStrategyType CleanupStrategy;
1539 Default constructor. Constructs an empty LManagedArray object.
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
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.
1561 @pre The array can be cleaned up using the cleanup strategy.
1565 explicit LManagedArray(T* aPtr)
1566 : LAutoPtrBase(aPtr)
1571 Destructor. When automatic resource management is enabled, the
1572 destructor invokes the specified cleanup strategy for the managed
1577 if (LAutoPtrBase::IsEnabled())
1579 CleanupStrategy::Cleanup(iPtr);
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.
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
1596 @pre The new array to be managed can be cleaned up using the
1601 LManagedArray& operator=(T* aPtr)
1604 LAutoPtrBase::operator=(aPtr);
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
1616 void ReleaseResource()
1618 if (!LAutoPtrBase::IsEnabled())
1621 CleanupStrategy::Cleanup(iPtr);
1622 LAutoPtrBase::Disable();
1626 Disables the automatic resource management for this object and
1627 returns a pointer to the first element of the array of objects of
1630 @return A pointer to the first element of the array of objects of
1635 return static_cast<T*>(LAutoPtrBase::Unmanage());
1639 Returns ETrue if automatic resource management is enabled; EFalse
1642 @return ETrue if automatic resource management is enabled; EFalse
1645 using LAutoPtrBase::IsEnabled;
1648 Returns a pointer to the first element of the managed array of
1651 @return A pointer to the first element of the managed array of
1654 using LAutoPtrBase::Get;
1657 Overloaded subscript operator.
1659 @return A reference to the object of type T at the position aIndex.
1661 T& operator[](TInt aIndex) const
1663 return iPtr[aIndex];
1666 using LAutoPtrBase::Disable;
1668 void Swap(LManagedArray& aArray)
1670 LAutoPtrBase::Swap(aArray);
1674 using LAutoPtrBase::iPtr;
1679 Implementation base class - not designed for public inheritance or
1684 // Not for Client Use , Only to be used Internally.
1685 template<typename T>
1689 template<typename U>
1690 explicit LAutoRefBase(U& aRef)
1695 template<typename U>
1696 LAutoRefBase& operator=(U& aRef)
1709 TBool IsEnabled() const
1711 return iPtr != NULL;
1719 T& operator*() const
1724 T* operator->() const
1734 void Swap(LAutoRefBase& aAutoRef)
1736 ::Swap(iPtr, aAutoRef.iPtr);
1743 LAutoRefBase(const LAutoRefBase&);
1744 LAutoRefBase& operator=(const LAutoRefBase&);
1749 A class template that provides automatic management of references
1750 to resource handles (often R-class instances) held in the data
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
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.
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.
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.
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
1783 Automatic cleanup may be disabled at any time by calling
1784 Unmanage(), while cleanup may be forced at any time by calling
1789 class CComposite : public CBase
1792 CONSTRUCTORS_MAY_LEAVE
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)
1799 iFileServ->Connect() OR_LEAVE;
1800 iFile->Open(*iFileServ, ...);
1805 // the handles are automatically closed
1810 LManagedRef<RFs> iFileServ;
1811 LManagedHandle<RFile> iFile;
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
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.
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).
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
1839 @see TDestroy which implements an alternative Destroy() calling
1841 @see TRelease which implements an alternative Release() calling
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
1847 @see CONSTRUCTORS_MAY_LEAVE
1849 template<typename T,
1850 class CleanupStrategyType = TResourceCleanupStrategy>
1851 class LManagedRef: protected LAutoRefBase<T>
1853 typedef LAutoRefBase<T> LAutoRefBase;
1856 typedef T ManagedType;
1857 typedef CleanupStrategyType CleanupStrategy;
1860 Explicit constructor.
1862 template<typename U>
1863 explicit LManagedRef(U& aRef)
1864 : LAutoRefBase(aRef)
1869 Destructor. When automatic resource management is enabled, the
1870 destructor invokes the specified cleanup strategy for the managed
1875 if (LAutoRefBase::IsEnabled())
1877 CleanupStrategy::Cleanup(iPtr);
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.
1887 template<typename U>
1888 LManagedRef& operator=(U& aRef)
1891 LAutoRefBase::operator=(aRef);
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.
1900 void ReleaseResource()
1902 if (!LAutoRefBase::IsEnabled())
1905 CleanupStrategy::Cleanup(iPtr);
1906 LAutoRefBase::Disable();
1910 Disables the automatic resource management for this object and
1911 returns a reference to the object of type T.
1913 @return A reference to the object of type T.
1915 using LAutoRefBase::Unmanage;
1918 Returns ETrue if automatic resource management is enabled; EFalse
1921 @return ETrue if automatic resource management is enabled; EFalse
1924 using LAutoRefBase::IsEnabled;
1927 Returns a reference to the managed object of type T.
1929 @return A reference to the managed object of type T.
1931 using LAutoRefBase::Get;
1934 Overloaded indirection operator function.
1936 @return A reference to the managed object of type T.
1938 using LAutoRefBase::operator*;
1941 Overloaded class member access operator function.
1943 @return A pointer to the managed object of type T.
1945 using LAutoRefBase::operator->;
1947 using LAutoRefBase::Disable;
1949 void Swap(LManagedRef& aRef)
1951 LAutoRefBase::Swap(aRef);
1955 using LAutoRefBase::iPtr;
1960 A class template for the creation and CleanupStack-based
1961 local-scope automatic management of resource handles (typically
1962 instances of R-classes).
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
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
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
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.
1987 The constructors of this class may leave.
1989 Any arguments supplied when initializing an instance of this class
1990 are automatically passed through to T's constructors.
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
1999 Automatic cleanup may be disabled at any time by calling
2000 Unmanage(), while cleanup may be forced at any time by calling
2005 // block scope example
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
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.
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).
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
2031 @see TDestroy which implements an alternative Destroy() calling
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
2037 template<typename T,
2038 class CleanupStrategyType = TResourceCleanupStrategy>
2039 class LCleanedupHandle: protected LAutoHandleBase<T, IS_HANDLE_SPECIAL(T)>
2041 typedef LAutoHandleBase<T, IS_HANDLE_SPECIAL(T)> LAutoHandleBase;
2044 typedef T ManagedType;
2045 typedef CleanupStrategyType CleanupStrategy;
2049 Default constructor.
2053 CleanupStack::PushL(TCleanupItem(Cleanup, this));
2056 template<typename Param1>
2057 explicit LCleanedupHandle(const Param1& aParam1)
2058 : LAutoHandleBase(aParam1)
2060 CleanupStack::PushL(TCleanupItem(Cleanup, this));
2063 template<typename Param1>
2064 explicit LCleanedupHandle(Param1& aParam1)
2065 : LAutoHandleBase(aParam1)
2067 CleanupStack::PushL(TCleanupItem(Cleanup, this));
2070 template<typename Param1,
2072 LCleanedupHandle(const Param1& aParam1,
2073 const Param2& aParam2)
2074 : LAutoHandleBase(aParam1,
2077 CleanupStack::PushL(TCleanupItem(Cleanup, this));
2080 template<typename Param1,
2082 LCleanedupHandle(const Param1& aParam1,
2084 : LAutoHandleBase(aParam1,
2087 CleanupStack::PushL(TCleanupItem(Cleanup, this));
2090 template<typename Param1,
2092 LCleanedupHandle(Param1& aParam1,
2093 const Param2& aParam2)
2094 : LAutoHandleBase(aParam1,
2097 CleanupStack::PushL(TCleanupItem(Cleanup, this));
2100 template<typename Param1,
2102 LCleanedupHandle(Param1& aParam1,
2104 : LAutoHandleBase(aParam1,
2107 CleanupStack::PushL(TCleanupItem(Cleanup, this));
2113 ManagedPopCleanupStackItem(IsEnabled());
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.
2122 template<typename U>
2123 LCleanedupHandle& operator=(const U& aHandle)
2126 LAutoHandleBase::operator=(aHandle);
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.
2142 void ReleaseResource()
2147 CleanupStrategy::Cleanup(&Get());
2148 LAutoHandleBase::Disable();
2152 Disables the automatic resource management for this obkect and
2153 returns a copy of the resource handle.
2155 @return A copy of the resource handle.
2157 using LAutoHandleBase::Unmanage;
2160 Returns ETrue if automatic resource management is enabled; EFalse
2163 @return ETrue if automatic resource management is enabled; EFalse
2166 using LAutoHandleBase::IsEnabled;
2170 Returns a reference to the resource handle.
2172 @return A reference to the resource handle.
2174 using LAutoHandleBase::Get;
2178 Overloaded indirection operator function.
2180 @return A reference to the resource handle.
2182 using LAutoHandleBase::operator*;
2185 Overloaded class member access operator function.
2187 @return A pointer to the resource handle.
2189 using LAutoHandleBase::operator->;
2191 static void Cleanup(TAny* aPtr)
2193 LCleanedupHandle* autoh = static_cast<LCleanedupHandle*>(aPtr);
2195 if (autoh->IsEnabled())
2197 CleanupStrategy::Cleanup(&autoh->Get());
2201 using LAutoHandleBase::Disable;
2203 void Swap(LCleanedupHandle& aCleanedupHandle)
2205 LAutoHandleBase::Swap(aCleanedupHandle);
2211 Implementation base class - not designed for public inheritance or
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>
2221 typedef LAutoPtrBase<typename TPtrCleanupTraits<T, CleanupStrategyType>::BaseManagedType> LAutoPtrBase;
2224 typedef typename TPtrCleanupTraits<T, CleanupStrategyType>::ManagedType ManagedType;
2225 typedef typename TPtrCleanupTraits<T, CleanupStrategyType>::BaseManagedType BaseManagedType;
2226 typedef typename TPtrCleanupTraits<T, CleanupStrategyType>::CleanupStrategy CleanupStrategy;
2230 CleanupStack::PushL(TCleanupItem(Cleanup, this));
2233 template<typename U>
2234 explicit LCleanedupPtrBase(U* aPtr)
2235 : LAutoPtrBase(aPtr)
2237 CleanupStack::PushL(TCleanupItem(Cleanup, this));
2240 ~LCleanedupPtrBase()
2242 ManagedPopCleanupStackItem(LAutoPtrBase::IsEnabled());
2245 template<typename U>
2246 LCleanedupPtrBase& operator=(U* aPtr)
2249 LAutoPtrBase::operator=(aPtr);
2253 void ReleaseResource()
2255 if (!LAutoPtrBase::IsEnabled())
2258 CleanupStrategy::Cleanup(static_cast<ManagedType*>(iPtr));
2259 LAutoPtrBase::Disable();
2262 using LAutoPtrBase::Unmanage;
2264 using LAutoPtrBase::IsEnabled;
2266 using LAutoPtrBase::Get;
2268 using LAutoPtrBase::operator->;
2270 static void Cleanup(TAny* aPtr)
2272 LCleanedupPtrBase* cleanupPtr = static_cast<LCleanedupPtrBase*>(aPtr);
2274 if (cleanupPtr->IsEnabled())
2276 CleanupStrategy::Cleanup(static_cast<ManagedType*>(cleanupPtr->iPtr));
2280 using LAutoPtrBase::iPtr;
2282 void Swap(LCleanedupPtrBase& aCleanedupPtr)
2284 LAutoPtrBase::Swap(aCleanedupPtr);
2290 A class template that provides CleanupStack-based local-scope
2291 automatic management of pointers.
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
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:
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
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.
2313 The constructors of this class may leave.
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
2322 Automatic cleanup may be disabled at any time by calling
2323 Unmanage(), while cleanup may be forced at any time by calling
2328 // block scope example
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
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.
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).
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
2354 template<typename T,
2355 class CleanupStrategyType = TPtrCleanupStrategy>
2356 class LCleanedupPtr: protected LCleanedupPtrBase<T, CleanupStrategyType>
2358 typedef LCleanedupPtrBase<T, CleanupStrategyType> LCleanedupPtrBase;
2361 typedef T ManagedType;
2362 typedef CleanupStrategyType CleanupStrategy;
2366 Default constructor. Constructs an empty LCleanedupPtr object.
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.
2383 @param aPtr A pointer of a type that is convertible to T* that can
2384 be cleaned up using the cleanup strategy.
2386 @pre aPtr is of a type convertible to T* and can be cleaned up
2387 using the cleanup strategy.
2391 explicit LCleanedupPtr(T* aPtr)
2392 : LCleanedupPtrBase(aPtr)
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.
2404 @param aPtr A pointer of a type that is convertible to T* that can
2405 be cleaned up using the cleanup strategy.
2407 @pre aPtr is a pointer of a type that is convertible to T* and can
2408 be cleaned up using the cleanup strategy.
2412 LCleanedupPtr& operator=(T* aPtr)
2414 LCleanedupPtrBase::operator=(aPtr);
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.
2426 @param aPtr A pointer of a type that is convertible to T* that can
2427 be cleaned up using the cleanup strategy.
2429 @pre aPtr is a pointer of a type that is convertible to T* and can
2430 be cleaned up using the cleanup strategy.
2434 template<typename U>
2435 LCleanedupPtr& operator=(U* aPtr)
2437 LCleanedupPtrBase::operator=(aPtr);
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
2450 using LCleanedupPtrBase::ReleaseResource;
2453 Disables the automatic resource management for this object and
2454 returns a pointer to the object of type T.
2456 @return A pointer to the object of type T.
2460 return static_cast<T*>(LCleanedupPtrBase::Unmanage());
2464 Returns ETrue if automatic resource management is enabled; EFalse
2467 @return ETrue if automatic resource management is enabled; EFalse
2470 using LCleanedupPtrBase::IsEnabled;
2473 Returns a pointer to the managed object of type T.
2475 @return A pointer to the managed object of type T.
2479 return static_cast<T*>(iPtr);
2483 Overloaded indirection operator function.
2485 @return A reference to the managed object of type T.
2487 T& operator*() const
2489 return *(static_cast<T*>(iPtr));
2493 Overloaded class member access operator function.
2495 @return A pointer to the managed object of type T.
2497 T* operator->() const
2499 return static_cast<T*>(iPtr);
2502 // Implementation type - do not use
2503 typedef typename LCleanedupPtrBase::BaseManagedType* LCleanedupPtr<T, CleanupStrategy>::*TUnspecifiedBoolType;
2506 Conversion operator that enables LCleanedupPtr objects to be used
2507 in boolean contexts.
2509 @return An unspecified value of an unspecified type convertible to
2510 boolean, which has a boolean value equal to Get() != NULL
2512 operator TUnspecifiedBoolType()
2514 return iPtr ? &LCleanedupPtr::iPtr : NULL;
2517 using LCleanedupPtrBase::Disable;
2519 void Swap(LCleanedupPtr& aCleanedupPtr)
2521 LCleanedupPtrBase::Swap(aCleanedupPtr);
2525 using LCleanedupPtrBase::iPtr;
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)
2534 return aPtr1.Get() == aPtr2.Get();
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)
2542 return aPtr1.Get() != aPtr2.Get();
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)
2550 return aPtr1.Get() < aPtr2.Get();
2555 A class template that provides CleanupStack-based local-scope
2556 automatic management of arrays.
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
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).
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:
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
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
2588 The constructors of this class may leave.
2590 As a convenience, the elements of the managed array may be accessed
2591 via "[]" notation directly on the management object.
2593 Automatic cleanup may be disabled at any time by calling
2594 Unmanage(), while cleanup may be forced at any time by calling
2598 // block scope example
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
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.
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).
2619 @see LManagedArray which has the same interface, but does not use
2620 the cleanup stack and is suitable for protecting the data members
2623 template<typename T,
2624 class CleanupStrategyType = TArrayDelete>
2625 class LCleanedupArray: protected LAutoPtrBase<T>
2627 typedef LAutoPtrBase<T> LAutoPtrBase;
2630 typedef T ManagedType;
2631 typedef CleanupStrategyType CleanupStrategy;
2634 Default constructor. Constructs an empty LCleanedupArray object.
2640 CleanupStack::PushL(TCleanupItem(Cleanup, this));
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
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.
2657 @pre The array can be cleaned up using the cleanup strategy.
2661 explicit LCleanedupArray(T* aPtr)
2662 : LAutoPtrBase(aPtr)
2664 CleanupStack::PushL(TCleanupItem(Cleanup, this));
2669 Destructor. When automatic resource management is enabled, the
2670 destructor invokes the specified cleanup strategy for the managed
2675 ManagedPopCleanupStackItem(LAutoPtrBase::IsEnabled());
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.
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
2691 @pre The new array to be managed can be cleaned up using the
2696 LCleanedupArray& operator=(T* aPtr)
2699 LAutoPtrBase::operator=(aPtr);
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
2711 void ReleaseResource()
2713 if (!LAutoPtrBase::IsEnabled())
2716 CleanupStrategy::Cleanup(iPtr);
2722 Disables the automatic resource management for this object and
2723 returns a pointer to the first element of the array of objects of
2726 @return A pointer to the first element of the array of objects of
2729 using LAutoPtrBase::Unmanage;
2732 Returns ETrue if automatic resource management is enabled; EFalse
2735 @return ETrue if automatic resource management is enabled; EFalse
2738 using LAutoPtrBase::IsEnabled;
2741 Returns a pointer to the first element of the managed array of
2744 @return A pointer to the first element of the managed array of
2747 using LAutoPtrBase::Get;
2750 Overloaded subscript operator.
2752 @return A reference to the object of type T at the position aIndex.
2754 T& operator[](TInt aIndex) const
2756 return iPtr[aIndex];
2759 static void Cleanup(TAny* aPtr)
2761 LCleanedupArray* cleanupPtr = static_cast<LCleanedupArray*>(aPtr);
2763 if (cleanupPtr->IsEnabled())
2765 CleanupStrategy::Cleanup(cleanupPtr->iPtr);
2769 using LAutoPtrBase::Disable;
2771 void Swap(LCleanedupArray& aArray)
2773 LAutoPtrBase::Swap(aArray);
2777 using LAutoPtrBase::iPtr;
2782 A class template that provides CleanupStack-based local-scope
2783 automatic management of references to resource handles (often
2784 instances of R-classes).
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
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:
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
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.
2810 The constructors of this class may leave.
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
2819 Automatic cleanup may be disabled at any time by calling
2820 Unmanage(), while cleanup may be forced at any time by calling
2825 // block scope example
2826 void DoWithClosable(RClosable& aObj)
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
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.
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).
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
2852 @see TDestroy which implements an alternative Destroy() calling
2854 @see TRelease which implements an alternative Release() calling
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
2859 @see LCleanedupHandle which has a similar interface but creates a
2860 fresh local instance of T
2862 template<typename T,
2863 class CleanupStrategyType = TResourceCleanupStrategy>
2864 class LCleanedupRef: protected LAutoRefBase<T>
2866 typedef LAutoRefBase<T> LAutoRefBase;
2869 typedef T ManagedType;
2870 typedef CleanupStrategyType CleanupStrategy;
2873 Explicit constructor.
2875 template<typename U>
2876 explicit LCleanedupRef(U& aRef)
2877 : LAutoRefBase(aRef)
2879 CleanupStack::PushL(TCleanupItem(Cleanup, this));
2883 Destructor. When automatic resource management is enabled, the
2884 destructor invokes the specified cleanup strategy for the managed
2889 ManagedPopCleanupStackItem(LAutoRefBase::IsEnabled());
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.
2898 template<typename U>
2899 LCleanedupRef& operator=(U& aRef)
2902 LAutoRefBase::operator=(aRef);
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.
2911 void ReleaseResource()
2913 if (!LAutoRefBase::IsEnabled())
2916 CleanupStrategy::Cleanup(iPtr);
2921 Disables the automatic resource management for this object and
2922 returns a reference to the object of type T.
2924 @return A reference to the object of type T.
2926 using LAutoRefBase::Unmanage;
2929 Returns ETrue if automatic resource management is enabled; EFalse
2932 @return ETrue if automatic resource management is enabled; EFalse
2935 using LAutoRefBase::IsEnabled;
2938 Returns a reference to the managed object of type T.
2940 @return A reference to the managed object of type T.
2942 using LAutoRefBase::Get;
2945 Overloaded indirection operator function.
2947 @return A reference to the managed object of type T.
2949 using LAutoRefBase::operator*;
2952 Overloaded class member access operator function.
2954 @return A pointer to the managed object of type T.
2956 using LAutoRefBase::operator->;
2959 static void Cleanup(TAny* aPtr)
2961 LCleanedupRef* cleanupRef = static_cast<LCleanedupRef*>(aPtr);
2963 if (cleanupRef->IsEnabled())
2965 CleanupStrategy::Cleanup(cleanupRef->iPtr);
2969 using LAutoRefBase::Disable;
2971 void Swap(LCleanedupRef& aRef)
2973 LAutoRefBase::Swap(aRef);
2977 using LAutoRefBase::iPtr;
2982 A class that provides automatic cleanup using a TCleanupOperation
2983 on the destruction of the LManagedGuard object.
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.
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.
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.
2999 Automatic cleanup may be disabled at any time by calling
3000 Dismiss(), while cleanup may be forced at any time by calling
3004 class CComposite : public CBase
3007 CONSTRUCTORS_MAY_LEAVE
3009 CComposite(RCleanable* aObj)
3010 : iObj(RCleanable::Cleanup, aObj)
3016 // RCleanable::Cleanup(iObj) is automatically invoked
3020 LManagedGuard<RCleanable> iObj;
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
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.
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
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.
3052 @param aCleanupOperation A cleanup operation.
3053 @param aData Pointer to data to be passed to the cleanup operation
3055 LManagedGuard(TCleanupOperation aCleanupOperation, TAny* aData = 0)
3056 : iCleanupOperation(aCleanupOperation),
3070 Executes the guard cleanup operation.
3074 if (iCleanupOperation)
3076 iCleanupOperation(iData);
3085 iCleanupOperation = NULL;
3089 LManagedGuard(const LManagedGuard&);
3090 LManagedGuard& operator=(const LManagedGuard&);
3092 TCleanupOperation iCleanupOperation;
3098 A class that provides CleanupStack-based local-scope automatic
3099 cleanup using a TCleanupOperation on the destruction of the
3100 LManagedGuard object.
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.
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:
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
3115 The constructors of this class may leave.
3117 Automatic cleanup may be disabled at any time by calling
3118 Dismiss(), while cleanup may be forced at any time by calling
3122 // block scope example
3125 LCleanedupGuard cleanGuard(RCleanable::Cleanup, &obj);
3127 obj.DoSomethingL(); // leave-safe
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
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.
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
3145 class LCleanedupGuard
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.
3154 @param aCleanupOperation A cleanup operation.
3155 @param aData Pointer to data to be passed to the cleanup operation
3157 LCleanedupGuard(TCleanupOperation aCleanupOperation, TAny* aData = 0)
3158 : iCleanupOperation(aCleanupOperation),
3161 CleanupStack::PushL(TCleanupItem(Cleanup, this));
3169 ManagedPopCleanupStackItem(iCleanupOperation);
3173 Executes the guard cleanup operation.
3177 if (iCleanupOperation)
3179 iCleanupOperation(iData);
3188 iCleanupOperation = NULL;
3191 static void Cleanup(TAny* aPtr)
3193 LCleanedupGuard* guard = static_cast<LCleanedupGuard*>(aPtr);
3198 LCleanedupGuard(const LCleanedupGuard&);
3199 LCleanedupGuard& operator=(const LCleanedupGuard&);
3202 TCleanupOperation iCleanupOperation;
3206 #endif // !EMANAGED_H