/*
 * The UDS Collection
 * Copyright (C) 1999, 2000, 2001  Julian v. Bock
 *
 * This program 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *	(See file COPYING for more information)
 */

#ifndef _uds_check_ptr_hh
#define _uds_check_ptr_hh


// includes

#include <uds/stdexcpt.hh>


/** pointer wrapper class that checks at each call to the class member
	access (->) and indirection (*) operators whether the
	wrapped pointer is non-zero. If it is, a bad_call exception is thrown.
	For performance reasons all function are inline.
	\author Julian v. Bock */
template < class T >
class check_ptr
{	
public:
	/** \param p pointer */
	explicit
	check_ptr( T* p = 0 ) : pointee( p ) {}
	
	/** copy constructor */
	template < class C >
	check_ptr( const check_ptr< C >& rhs ) : pointee( rhs.pointee ) {}
	
	/** returns the pointee if it is non-zero. Otherwise a runtime_error
		exception is thrown
		\return pointee */
	const T*
	safe_get() const { return sg(); }
	
	/** returns the pointee if it is non-zero. Otherwise a runtime_error
		exception is thrown
		\return pointee */
	T*
	safe_get() { return sg(); }
	
	/** \return pointee */
	T*
	get() { return pointee; }
	
	/** returns the dereferenced pointer if it is non-zero. Otherwise a
		runtime_error exception is thrown
		\return dereferenced pointer */
	const T&
	operator * () const { return *sg(); }
	
	/** returns the dereferenced pointer if it is non-zero. Otherwise a
		runtime_error exception is thrown
		\return dereferenced pointer */
	T&
	operator * () { return *sg(); }
	
	/** if the pointee is zero a runtime_error exception is thrown
		\return pointee */
	const T*
	operator -> () const { return sg(); }
	
	/** if the pointee is zero a runtime_error exception is thrown
		\return pointee */
	T*
	operator -> () { return sg(); }
	
	operator const T* () const { return pointee; }
	
	T*
	operator = ( T* rhs ) { return ( pointee = rhs ); }
	
private:
	/// wrapped pointer
	T*	pointee;
	
	/** returns the pointee if it is non-zero. Otherwise a runtime_error
		exception is thrown
		\return pointee
		\arg throws uds::runtime_error */
	T*
	sg() const
	{
		if ( !pointee ) throw uds::runtime_error( "zero pointer\n" );
		return pointee;
	}
};


/** const pointer wrapper class that checks at each call to the class member
	access (->) and indirection (*) operators whether the
	wrapped pointer is non-zero. If it is, a bad_call exception is thrown.
	For performance reasons all function are inline.
	\author Julian v. Bock */
template < class T >
class const_check_ptr
{	
public:
	/** \param p pointer */
	explicit
	const_check_ptr( const T* p = 0 ) : pointee( p ) {}
	
	/** returns the pointee if it is non-zero. Otherwise a runtime_error
		exception is thrown
		\return pointee */
	const T*
	safe_get() const { return sg(); }
	
	/** returns the dereferenced pointer if it is non-zero. Otherwise a
		runtime_error exception is thrown
		\return dereferenced pointer */
	const T&
	operator * () const { return *sg(); }
	
	/** if the pointee is zero a runtime_error exception is thrown
		\return pointee */
	const T*
	operator -> () const { return sg(); }
	
	operator const T* () const { return pointee; }
	
private:
	/// wrapped pointer
	const T*	pointee;
	
	/** returns the pointee if it is non-zero. Otherwise a runtime_error
		exception is thrown
		\return pointee
		\arg throws uds::runtime_error */
	const T*
	sg() const
	{
		if ( !pointee ) throw uds::runtime_error( "zero pointer\n" );
		return pointee;
	}
};

#endif // _uds_check_ptr_hh
