// Exception.h: interface for the CException class.
//
//////////////////////////////////////////////////////////////////////
//========================================================================
// Class Name	: CException
// Purpose		: 
// Autor		: Richard Chuang
// Email		: 
// Description	: 
// Reference    : 
// Copyright    : Copyright (c) 2000-2007 Davicom Inc.  All rights reserved.
// -----------------------------------------------------------------------
// Version		: 1.0.0.0
// Update       : 2007-10-23
// Modified		: Richard Chung
// Description	: 
// -----------------------------------------------------------------------
// How to use: 
//  
#if !defined(_DAVICOM_EXCEPTION_H_)
#define _DAVICOM_EXCEPTION_H_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "stdafx.h"

#define	MAX_EXCEPTIONS	256
#define	ERR_STRING(p)	(PUCHAR)(p)

class CException  
{
public:
	CException();
	virtual ~CException();
	CException(DWORD dwErr);
	CException(PUCHAR ptrErr, DWORD dwErr=NDIS_STATUS_FAILURE);

	void	Constructor(PUCHAR ptrErr, DWORD dwError);
	void	PrintErrorMessage(void);
	DWORD	GetErrorCode(void);
protected:
	DWORD			m_dwErrorCode;
	NDIS_STRING		m_strNdisErrorMessage;
};

typedef	struct	_EXCEPTION_DATA_
{
	int				nIndex;
	jmp_buf			szJumps[MAX_EXCEPTIONS];
	CException		*szpExceptions[MAX_EXCEPTIONS];
}	EXCEPTION_DATA,	*PEXCEPTION_DATA;

extern PEXCEPTION_DATA		g_pExpData;

#define	INIT_EXCEPTION()	(g_pExpData = (PEXCEPTION_DATA)malloc(sizeof(EXCEPTION_DATA)) \
							, memset((void*)g_pExpData,0,sizeof(EXCEPTION_DATA)),g_pExpData->nIndex = -1)

#define	TERM_EXCEPTION()	free(g_pExpData)

#define	TRY					if(setjmp(g_pExpData->szJumps[++g_pExpData->nIndex]) == 0)

#define	FI					--(g_pExpData->nIndex)

#define	CATCH(exp) 			else if(((exp) = g_pExpData->szpExceptions[g_pExpData->nIndex--]))

#define	THROW(x) 			( g_pExpData->szpExceptions[g_pExpData->nIndex] = new CException x, \
							longjmp(g_pExpData->szJumps[g_pExpData->nIndex],-1))

#define	CLEAN(x)			delete x

#endif // !defined(_DAVICOM_EXCEPTION_H_)
