#ifndef __EXCEPTION_H__
#define __EXCEPTION_H__

#ifdef WIN32
#ifdef V2_CLASS_EXPORTS
#define V2_CLASS_API __declspec(dllexport)
#else
#define V2_CLASS_API __declspec(dllimport)
#endif
#else
#define V2_CLASS_API
#endif

#if defined(OS_Linux) || defined(WIN32)
#include <new>
#else
#include <new.h>
#endif

class V2_CLASS_API CException
{
private:
	int m_iCode;
	char m_szMessage[1024]; // exception message

public:
	CException();
	CException(const char *p_szMsg, int p_iCode=0);
	CException(int p_iCode, const char *p_fmt, ...);

	char *GetText();
	int GetCode();
};


/*
 ITS_new() 
 : if exception bad_alloc occurs then it makes pointer to NULL

 ITS_new_throw()
 : if exception bad_alloc occurs then it throws CException()

 ITS_delete()
 : delete allocated memory

 ITS_delete_arr()
 : delete array allocated memory
*/


// catch bad_alloc exception
#define ITS_new(pvar, type_str) \
	try { \
		pvar = new type_str; \
	} catch (...) { \
		pvar = NULL; \
	}

#define ITS_new_throw(pvar, type_str) \
	try { \
		pvar = new type_str; \
	} catch (std::bad_alloc &e) { \
		pvar = NULL; \
		throw CException(-11, "at %s(%d lines), (new %s) failed(%s)", __FILE__, __LINE__, #type_str, e.what()); \
	}

#define ITS_delete(pvar) \
	delete pvar; \
	pvar = NULL;

#define ITS_delete_arr(pvar) \
	delete [] pvar; \
	pvar = NULL;


#endif  /* __EXCEPTION_H__ */


