/**
 * @file
 * $Id$
 * $Revision$
 * $Author$
 * $Date$
 *
 * This file is part of The iWear Framework.
 *
 * The iWear Framework is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation as in version 2 of the License.

 * 
 * The iWear Framework is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * The iWear Framework; if not, write to the Free Software Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef __IWEAR_DELETER_H
#define __IWEAR_DELETER_H

namespace iwear
{

/**
 * This is a template helper class to use c-functions as deleter object for
 * boost::shared_ptr
 * example:
 * <pre>
 * Deleter<SDL_Surface> sdf(&SDL_FreeSurface);
 *
 * boost::shared_ptr<SDL_Surface> tb(tmp_menu_stop,sdf);
 * </pre>
 * It will call the Function passed in the deleter when the object has to be
 * destroyed (remember, its a shared pointer)
 * @note If the pointer to the function is 0 then a delete will be called. This
 * might lead to strange results, since for some compilers every function
 * pointer is == 0 ... dunno how to work around this yet...
 */
template<class T>
class Deleter
{
private:
    void (*t)(T*);
protected:
public:
    Deleter( void (*_t)(T*) ) : t(_t) { }
    void operator()( T* s );
};

template<class T>
class NoDelete
{
public:
    NoDelete( void ) { }
    void operator()( T* ) { }
};

template<class T>
void Deleter<T>::operator()( T* s )
{
    if( t != 0 )
    {
	t(s);
    }
    else
    {
	delete s;
    }
}

}

#endif
