RAII Idiom

The Resource Acquisition Is Initialization (RAII) idiom is the basis of implementation of the smart pointer class template. It is also used by the implementation of the file stream classes provided by C++ Standard Library and by many others. The Symbian C++ class templates for automatic resource management are based on the C++ RAII idiom, in order to provide exception-safe automatic cleanup on normal or exceptional exit from a scope.

Need for RAII Idiom

The current Symbian C++ resource management mechanism is the Symbian Cleanup Stack. The CleanupStack class and the associated functions provides Symbian C++ exception-safe (leave safe) cleanup when used in conjunction with the Symbian Leave mechanism. The RAII idiom can free resources automatically. The idiom consists of a class with a constructor that allocates a resource and a destructor that frees the resource. When a local variable of that class type is declared, it will automatically call the destructor to free the resource when leaving the scope. This also protects against leaks caused by thrown exceptions.

RAII APIs

The LCleanedupX and LManagedX class templates share a common API as described below:

  • ReleaseResource()

             
              
             
             void ReleaseResource()
            

    Description: This method forces cleanup of a managed resource. If automatic resource management is enabled, the specified cleanup strategy is invoked for the managed reference and the automatic resource management is then disabled.

  • Unmanage()

             
              
             
             T& Unmanage()
            

    Description: This method disables automatic cleanup of a managed resource and returns a reference to the object of type T.

  • IsEnabled()

             
              
             
             TBool IsEnabled()
            

    Description: This method returns ETrue if automatic resource management is enabled, otherwise returns EFalse.

  • Get()

             
              
             
             T& Get()
            

    Description: This method returns a reference to the managed object of type T for Ref, Handle and Guard variants of LCleanedupX and LManagedX.

             
              
             
             T* Get()
            

    Description: This method returns a pointer to the managed object of type T for Ptr and Array variants of LCleanedupX and LManagedX.

  • Operators

    The . operator gives access to the public members of the managing class.

    The -> operator gives access to the public members of the managed class.

    The * operator is used to get a reference or pointer to the underlying managed object.