photosgallery/slideshow/utils/shwcleanupwrapper.h
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Thu, 17 Dec 2009 08:45:44 +0200
changeset 0 4e91876724a2
child 15 191387a8b767
permissions -rw-r--r--
Revision: 200949 Kit: 200951

/*
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies). 
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:    Thin cleanup wrapper
 *
*/




// include guard for the whole file content
#ifndef __TSHWCLEANUPWRAPPER_H__
#define __TSHWCLEANUPWRAPPER_H__

/**
 * Helper class to construct a TCleanupItem from any 
 * public non static void (*method)() of a class
 * Usage:
 *      TShwCleanupWrapper< %ClassName%, %MethodName% >( 
 *          %ClassPointer% )
 * Example:
 *      RPointerArray< MInterface > array;
 *      CleanupStack::PushL( 
 *          TShwCleanupWrapper< 
 *              RPointerArray<MInterface>,
 *              RPointerArray<MInterface>::ResetAndDestroy >( array ) );
 */
template< class Object, void (Object::*Method)()>
NONSHARABLE_CLASS( TShwCleanupWrapper ) : public TCleanupItem
	{
	public:
        /**
		 * Constructor.
		 * @param aInstance the object instance of the class 
		 */
		inline TShwCleanupWrapper( Object& aInstance )
			: TCleanupItem( &Cleanup, &aInstance )
			{}

    private:
        /**
		 * The cleanup callback delegator. Inlined as we dont want
		 * extra method to be created, static so that base
		 * class can call it without pointer to this.
		 * @param aInstance the instance of TShwCallBack
		 * @return the return value for the TCallBack
		 */
		inline static void Cleanup( TAny* aInstance )
			{
			( static_cast< Object* >( aInstance )->*Method )();
			}
	};

/**
 * Helper function to create cleanup item without ugly template notations
 * @param aObject the object to call ResetAndDestroy in case of leave
 * 
 * Usage:
 *      ShwCleanupResetAndDestroyPushL( array );
 * Example:
 *   RPointerArray< MShwEffect > effects;
 *   // put the array in cleanupstack in case CreateEffectsL leaves
 *    ShwCleanupResetAndDestroyPushL( effects );
 */
template< class T >
void ShwCleanupResetAndDestroyPushL( T& aObject )
	{
	CleanupStack::PushL( 
		TShwCleanupWrapper< T, T::ResetAndDestroy >( aObject ) );
	}

#endif // __TSHWCLEANUPWRAPPER_H__