Code Search for Developers
 
 
  

XErr.h from redshed at Krugle


Show XErr.h syntax highlighted

/****************************************************************************************
	XErr.h $Revision: 46 $
		<http://rentzsch.com/xerr>
	
	Copyright © 1999-2002 Red Shed Software. All rights reserved.
	by Jonathan 'Wolf' Rentzsch (jon at redshed dot net)
	
	************************************************************************************/

#ifndef		_XErr_
#define		_XErr_

#include	"stdio.h"
#include	<exception>

/*
	You can use the following code to detect and print out the extra debugging
	information carried by a XErr exception, even if you can't be sure it was an XErr
	that was initially thrown (say a std::bad_alloc):
	
	try {
		...
	} catch( const std::exception &x ) {
		const XErr *xerr = dynamic_cast<const XErr*>( &x );
		if( xerr )
			xerr->Debug();
	}
	
	This requires Runtime Type Information (RTTI) be turned on.
*/

class	XErr : public std::exception	{
	public:
		//	Constructors.
		XErr()
		:	file_( NULL ),
			line_( 0 ),
			code_( 0 ),
			msg_( NULL ),
			thrown_( false ){}
		
			explicit
		XErr(
					long	code,
			const	char	*msg )
		:	file_( NULL ),
			line_( 0 ),
			code_( code ),
			msg_( msg ),
			thrown_( true ){}
		
		XErr(
			const	char			*file,
					unsigned long	line,
					long			code,
			const	char			*msg )
		:	file_( file ),
			line_( line ),
			code_( code ),
			msg_( msg ),
			thrown_( true ){}
		
		//	Copy Constructor.
		XErr(
			const	XErr	&rhs )
		:	file_( rhs.file_ ),
			line_( rhs.line_ ),
			code_( rhs.code_ ),
			msg_( rhs.msg_ ),
			thrown_( rhs.thrown_ ){}
		
		//	"=" operator.
			XErr&
		operator=(
			const	XErr	&rhs )
		{	file_ = rhs.file_;
			line_ = rhs.line_;
			code_ = rhs.code_;
			msg_ = rhs.msg_;
			thrown_ = rhs.thrown_;
			return( *this );
		}
		
		//	File from where exception was thrown.
		
			const	char*
		file() const
		{	return( file_ ); }
		
			void
		file(
			const	char	*file )
		{	file_ = file; }
		
		//	Line in File from where exception was thrown.
		
			unsigned long
		line() const
		{	return( line_ ); }
		
			void
		line(
			long	line )
		{	line_ = (unsigned long) line; }
		
		//	Error Code of exception.
		
			long
		code() const
		{	return( code_ ); }
		
			void
		code(
			long	code )
		{	code_ = code; }
		
		//	Error Message of exception.
		
			const char*
		msg() const
		{	return( msg_ ); }
		
			void
		msg(
			const	char	*msg )
		{	msg_ = msg; }
		
		//	Whether this exception was set to a thrown exception.
		
			bool
		thrown() const
		{	return( thrown_ ); }
		
			void
		thrown(
			bool	thrown )
		{	thrown_ = thrown; }
		
		//	Debug
			void 
		Debug() const
		{
#if	TARGET_OS_MAC && !TARGET_API_MAC_OSX
			Str255	out = "\p";
			if( line() ) {
				if( msg() ) {
					out[0] = (UInt8) snprintf( (char*) out+1, sizeof(out)-1,
										"XErr %s #%ld, file Ò%sÓ line %ld",
										msg(), code(), file(), line() );
				} else {
					out[0] = (UInt8) snprintf( (char*) out+1, sizeof(out)-1,
										"XErr #%ld, file Ò%sÓ line %ld",
										code(), file(), line() );
				}
			} else {
				if( msg() ) {
					out[0] = (UInt8) snprintf( (char*) out+1, sizeof(out)-1,
										"XErr %s #%ld", msg(), code() );
				} else {
					out[0] = (UInt8) snprintf( (char*) out+1, sizeof(out)-1,
										"XErr #%ld", code() );
				}
			}
			DebugStr( out );
#else	//	TARGET_OS_MAC
			if( line() ) {
				if( msg() ) {
					printf( "XErr %s (%ld in %s, line %ld)\n", msg(), code(), file(), line() );
				} else {
					printf( "XErr %ld (%s, line %ld)\n", code(), file(), line() );
				}
			} else {
				if( msg() ) {
					printf( "XErr %s (%ld)\n", msg(), code() );
				} else {
					printf( "XErr %ld\n", code() );
				}
				
			}
#endif	//	TARGET_OS_MAC
		}
	private:
		const	char			*file_;
				unsigned long	line_;
				long			code_;
		const	char			*msg_;
				bool			thrown_;
};

#ifndef	RecordXErrThrowLocation
#define	RecordXErrThrowLocation	1
#endif

#ifndef	XErrThrowsByReference
#define	XErrThrowsByReference	1
#endif

#if	XErrThrowsByReference
#define	OPTIONAL_NEW
#else
#define	OPTIONAL_NEW new
#endif

//-------------------------------------------------
#if	RecordXErrThrowLocation

		inline
		void
	ThrowErr__(
		const	char			*file,
				unsigned long	line,
				long			code,
		const	char			*msg = NULL )
	{
		throw( OPTIONAL_NEW XErr( file, line, code, msg ) );
	}

		inline
		void
	ThrowIfErr__(
		const	char			*file,
				unsigned long	line,
				long			code,
		const	char			*msg = NULL )
	{
		if( code )
			throw( OPTIONAL_NEW XErr( file, line, code, msg ) );
	}

	#define	ThrowErr( ERR )				ThrowErr__( __FILE__, __LINE__, (ERR) )
	#define	ThrowErrMsg( ERR, MSG )		ThrowErr__( __FILE__, __LINE__, (ERR), (MSG) )
	#define	ThrowIfErr( ERR )			ThrowIfErr__( __FILE__, __LINE__, (ERR) )
	#define	ThrowIfErrMsg( ERR, MSG )	ThrowIfErr__( __FILE__, __LINE__, (ERR), (MSG) )

#if	TARGET_OS_MAC

		inline
		void
	ThrowIfMemErr__(
		const	char			*file,
				unsigned long	line,
		const	char			*msg = NULL )
	{
		if( MemError() )
			throw( OPTIONAL_NEW XErr( file, line, MemError(), msg ) );
	}

		inline
		void
	ThrowIfResErr__(
		const	char			*file,
				unsigned long	line,
		const	char			*msg = NULL )
	{
		if( ResError() )
			throw( OPTIONAL_NEW XErr( file, line, ResError(), msg ) );
	}

		inline
		void
	ThrowIfMemFail__(
		const	char			*file,
				unsigned long	line,
		const	void			*mem,
		const	char			*msg = NULL )
	{
		if( !mem ) {
			OSErr	err = MemError();
			if( !err )
				err = memFullErr;
			throw( OPTIONAL_NEW XErr( file, line, err, msg ) );
		}
	}

		inline
		void
	ThrowIfResFail__(
		const	char			*file,
				unsigned long	line,
		const	void			*res,
		const	char			*msg = NULL )
	{
		if( !res ) {
			OSErr	err = ResError();
			if( !err )
				err = resNotFound;
			throw( OPTIONAL_NEW XErr( file, line, err, msg ) );
		}
	}
	
	#define	ThrowIfMemErr()					ThrowIfMemErr__( __FILE__, __LINE__ )
	#define	ThrowIfMemErrMsg( MSG )			ThrowIfMemErr__( __FILE__, __LINE__, (MSG) )
	#define	ThrowIfResErr()					ThrowIfResErr__( __FILE__, __LINE__ )
	#define	ThrowIfResErrMsg( MSG )			ThrowIfResErr__( __FILE__, __LINE__, (MSG) )
	#define	ThrowIfMemFail( MEM )			ThrowIfMemFail__( __FILE__, __LINE__, (MEM) )
	#define	ThrowIfMemFailMsg( MEM, MSG )	ThrowIfMemFail__( __FILE__, __LINE__, (MEM), (MSG) )
	#define	ThrowIfResFail( RES )			ThrowIfResFail__( __FILE__, __LINE__, (RES) )
	#define	ThrowIfResFailMsg( RES, MSG )	ThrowIfResFail__( __FILE__, __LINE__, (RES), (MSG) )
	
#endif	//	TARGET_OS_MAC

//-------------------------------------------------
#else	//	RecordXErrThrowLocation

		inline
		void
	ThrowErr(
		long	code )
	{
		throw( OPTIONAL_NEW XErr( code ) );
	}

		inline
		void
	ThrowErrMsg(
				long	code,
		const	char	*msg )
	{
		throw( OPTIONAL_NEW XErr( code, msg ) );
	}

		inline
		void
	ThrowIfErr(
		long	code )
	{
		if( code )
			throw( OPTIONAL_NEW XErr( code ) );
	}

		inline
		void
	ThrowIfErrMsg(
				long	code
		const	char	*msg )
	{
		if( code )
			throw( OPTIONAL_NEW XErr( code, msg ) );
	}

#if	TARGET_OS_MAC

		inline
		void
	ThrowIfMemErr()
	{
		if( MemError() )
			throw( OPTIONAL_NEW XErr( MemError() ) );
	}

		inline
		void
	ThrowIfMemErrMsg(
		const	char	*msg )
	{
		if( MemError() )
			throw( OPTIONAL_NEW XErr( MemError(), msg ) );
	}

		inline
		void
	ThrowIfResErr()
	{
		if( ResError() )
			throw( OPTIONAL_NEW XErr( ResError() ) );
	}

		inline
		void
	ThrowIfResErrMsg(
		const	char	*msg )
	{
		if( ResError() )
			throw( OPTIONAL_NEW XErr( ResError(), msg ) );
	}

		inline
		void
	ThrowIfMemFail(
		const	void	*mem )
	{
		ThrowIfMemFailMsg( mem, NULL );
	}

		inline
		void
	ThrowIfMemFailMsg(
		const	void	*mem,
		const	char	*msg )
	{
		if( !mem ) {
			OSErr	err = MemError();
			if( !err )
				err = memFullErr;
			throw( OPTIONAL_NEW XErr( err ), msg );
		}
	}

		inline
		void
	ThrowIfResFail(
		const	void	*res )
	{
		ThrowIfResFailMsg( res, NULL );
	}

		inline
		void
	ThrowIfResFailMsg(
		const	void	*res,
		const	char	*msg )
	{
		if( !res ) {
			OSErr	err = ResError();
			if( !err )
				err = resNotFound;
			throw( OPTIONAL_NEW XErr( err ), msg );
		}
	}

#endif	//	TARGET_OS_MAC

#endif	//	RecordXErrThrowLocation

#endif	//	_XErr_



See more files for this project here

redshed

Code for Mac+WebObjects.

Project homepage: http://sourceforge.net/projects/redshed
Programming language(s): C,Java,Objective C
License: other

  XErr.h