#include <emanaged.h>
class LManagedPtr : protected LManagedPtrBase |
Public Member Type Definitions | |
---|---|
typedef | CleanupStrategyType CleanupStrategy |
typedef | LManagedPtrBase::BaseManagedType * LManagedPtr< T, CleanupStrategy >TUnspecifiedBoolType |
typedef | T ManagedType |
Public Member Functions | |
---|---|
LManagedPtr() | |
LManagedPtr(T *) | |
T * | Get() |
void | Swap(LManagedPtr &) |
T * | Unmanage() |
operator TUnspecifiedBoolType() | |
T & | operator*() |
T * | operator->() |
LManagedPtr & | operator=(T *) |
LManagedPtr & | operator=(U *) |
Inherited Attributes | |
---|---|
LAutoPtrBase< TPtrCleanupTraits< T, CleanupStrategyType >::BaseManagedType >::iPtr |
Inherited Type Definitions | |
---|---|
LManagedPtrBase::BaseManagedType |
A class template that provides automatic management of pointers held in the data members of objects.
This class should not used to define locals. See below for an explanation and links to management classes suitable for use in that context.
By default, the cleanup action is to delete the managed pointer using a (non-array) delete operation. An alternative cleanup strategy can be specified using the optional CleanupStrategy class template parameter of the LManagedPtr class template. The most common alternative cleanup strategies are predefined (e.g. TPointerFree).
The constructors of this class never leave, so data members defined with this type may be initialized safely during any phase of construction of the owning class.
As a convenience, the methods of the managed pointer may be accessed via "->" notation directly on the management object, while "." notation is used to access the interface of the management object itself. Using "*" to dereference the management object yields a T&, and is often useful when passing the managed object as an argument.
Automatic cleanup may be disabled at any time by calling Unmanage(), while cleanup may be forced at any time by calling ReleaseResource().
class CComposite : public CBase { public: CONSTRUCTORS_MAY_LEAVE CComposite() : iComponent(CComponent::NewL()) { //... } ~CComposite() { // the pointer to the CComponent object is automatically // deleted } private: LManagedPtr<CComponent> iComponent; };
Behind the scenes, this class template simply relies on reliable execution of its destructor. If used for a local variable rather than a data member, cleanup will occur but out-of-order compared to objects protected using the LCleanupXxx variants or the CleanupStack directly. Therefore it is not recommended for use in that context.
These management classes may be used as the basis for implementing leave-safe single-phase construction, since fully initialized data members protected in this way will get destroyed (so reliably triggering cleanup) if their containing classes leave during execution of their constructors. Note, however, that single-phase construction must be explicitly enabled in the containing class using the CONSTRUCTORS_MAY_LEAVE macro.
This class template together with the cleanup strategy class templates provide a template-based implementation of the Strategy design pattern (See also: Policy-based design).
See also: TPointerDelete which implements the default deleting cleanup strategy TPointerFree which implements the alternative User::Free() cleanup strategy LCleanedupPtr which has the same interface, but uses the cleanup stack and is suitable for protecting locals CONSTRUCTORS_MAY_LEAVE
typedef LManagedPtrBase::BaseManagedType * | LManagedPtr< T, CleanupStrategy >TUnspecifiedBoolType |
LManagedPtr | ( | ) | [inline] |
Default constructor. Constructs an empty LManagedPtr object.
LManagedPtr | ( | T * | aPtr | ) | [inline, explicit] |
Explicit constructor template. Constructs a LManagedPtr object that manages the pointer aPtr of a type convertible to T* that can be cleaned up using the cleanup strategy of the LManagedPtr class. The default cleanup strategy is to delete the pointer to a heap-allocated object by using non-array delete. Alternative cleanup strategies can be specified by using the CleanupStrategy template parameter of the LManagedPtr class template.
Parameter | Description |
---|---|
aPtr | A pointer of a type that is convertible to T* that can be cleaned up using the cleanup strategy. |
T * | Get | ( | ) | const [inline] |
Reimplemented from LAutoPtrBase< TPtrCleanupTraits< T, CleanupStrategyType >::BaseManagedType >::Get()const
Returns a pointer to the managed object of type T.
Returns: A pointer to the managed object of type T.
T * | Unmanage | ( | ) | [inline] |
Reimplemented from LAutoPtrBase< TPtrCleanupTraits< T, CleanupStrategyType >::BaseManagedType >::Unmanage()
Disables the automatic resource management for this object and returns a pointer to the object of type T.
Returns: A pointer to the object of type T.
operator TUnspecifiedBoolType | ( | ) | [inline] |
Conversion operator that enables LCleanedupPtr objects to be used in boolean contexts.
Returns: An unspecified value of an unspecified type convertible to boolean, which has a boolean value equal to Get() != NULL
T & | operator* | ( | ) | const [inline] |
Overloaded indirection operator function.
Returns: A reference to the managed object of type T.
T * | operator-> | ( | ) | const [inline] |
Reimplemented from LAutoPtrBase< TPtrCleanupTraits< T, CleanupStrategyType >::BaseManagedType >::operator->()const
Overloaded class member access operator function.
Returns: A pointer to the managed object of type T.
LManagedPtr & | operator= | ( | T * | aPtr | ) | [inline] |
Destructor. When automatic resource management is enabled, the destructor invokes the specified cleanup strategy for the managed pointer. Assigns a new pointer to be managed. The new pointer must be of a type convertible to T* and it must be possible to use the cleanup strategy of the LManagedPtr object for the cleanup of the new managed pointer. If the LManagedPtr object already contains a managed pointer, then the cleanup strategy is invoked with the managed pointer before assigning the new managed pointer.
Parameter | Description |
---|---|
aPtr | A pointer of a type that is convertible to T* that can be cleaned up using the cleanup strategy. |
LManagedPtr & | operator= | ( | U * | aPtr | ) | [inline] |
Reimplemented from LManagedPtrBase::operator=(U *)
Assigns a new pointer to be managed. The new pointer must be of a type convertible to T* and it must be possible to use the cleanup strategy of the LManagedPtr object for the cleanup of the new managed pointer. If the LManagedPtr object already contains a managed pointer, then the cleanup strategy is invoked with the managed pointer before assigning the new managed pointer.
Parameter | Description |
---|---|
aPtr | A pointer of a type that is convertible to T* that can be cleaned up using the cleanup strategy. |