#ifndef _xbEXCEPTIONLOW_
#define _xbEXCEPTIONLOW_
/*<<<***************************************************************************
The MIT License (MIT)

Copyright(C) 2013 - 2015 Marc Mirti Mancinelli, Christophe Lardau and Philippe Pamart (xdcube.org)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
////////////////////////////////////////////////////////////////////////////////
#Title: xbExceptionLow
#Project: xdcBase
////////////////////////////////////////////////////////////////////////////////
#Author: Marc MIRTI MANCINELLI
#History:
<15/09/2013: creation.
12/02/2014: this notice inserted.
***************************************************************************>>>*/

#ifndef _xbHEAPLOW_
#	include "xbHeapLow.h"
#endif

#ifdef xbOS_MSWIN
#	include "osdep/mswin/xbODexceptionLow.h"
#else
#	pragma message("XDCBASE: ERROR: platform not supported")
#endif
enum xbEctorMode
{
	xbEctor_final,
	xbEctor_base
};

class xbEXPORT xbEinfoSt
{
protected:
	const char	*m_pszName;
	xbEinfoSt	*m_pBase;
public:
	xbEinfoSt(const char *pszName,xbEinfoSt *pBase)
	{
		m_pszName = pszName;
		m_pBase = pBase;
	}
	const xbEinfoSt *GetBaseInfo() const {return m_pBase;}
	const char *GetEname() const {return m_pszName;}
};

class xbEXPORT __declspec(uuid("{4EEAA3FB-8DE9-400d-8529-A239A3B6EC18}"))
	xbEbase:public sIxbBase
{
protected:
	xbEbase()
	{
		m_hr = xbHR_FAIL;
		m_bHRmsgGet = false;
		m_pszHRmsg = NULL;
	}
	xbEbase(HRESULT hr)
	{
		m_hr = hr;
		m_bHRmsgGet = false;
		m_pszHRmsg = NULL;
	}
public:
	static xbEinfoSt s_info;
	virtual const xbEinfoSt *GetInfo() const {return &s_info;}

	virtual bool GetInterface(REFIID riid, void ** ppInterf)
	{
		xbImplInterface(xbEbase);
		return false;
	}

	template<class ta_type> bool Is()
	{
		ta_type *pItf;
		return GetInterface(__uuidof(ta_type),(void**)&pItf);
	}
	template<class ta_type> ta_type *As()
	{
		ta_type *pItf;
		if (!GetInterface(__uuidof(ta_type),(void**)&pItf))
			return false;
		return pItf;
	}

	virtual LPCSTR GetFullEname() const {return NULL;}
	virtual xbHR	GetHR() const ;
	virtual xbHR	GetRealHR() const 
	{
		return m_hr;
	}
/*	virtual ushort	GetHRcode() const 
	{
		return ushort(HRESULT_CODE(m_hr));
	}
	virtual ulong	GetHRfacCode() const 
	{
		return ulong(m_hr) & 0x1fffffff;
	}
*/
	virtual const char *GetMsg() const = 0;
	virtual bool IsToWriteOnLogFile() const = 0;

	virtual xbMBiconType GetMBoxIcon() const = 0;
	virtual const char *GetMBoxTitle() const = 0;

	virtual const char *GetHRmsg() const;
	virtual bool onSubstVarExpand(const xbString &varName,xbString *pVarValue,xbOpMode opm) const
	{
		return false;
	}
	virtual void Delete()
	{
		delete this;
	}
	virtual void exceptionProcess(bool bDtor=false) 
	{
	}

	static void TraceExcept(const char *pszEtype, xbHR hr,const char *pszMsg,xbEctorMode ctorMode);

protected:
	void traceExcept(xbEctorMode ctorMode);

	virtual ~xbEbase() 
	{
		try
		{
			freeHRmsg();
		}
		catch(xbEbase *pE)
		{
			pE->Delete();
		}
		catch(...)
		{
		}
	}

	void freeHRmsg();

	LPCSTR			m_pszClassName;
	HRESULT			m_hr;
	mutable const char	*m_pszHRmsg;
	mutable bool	m_bHRmsgGet;
private:
	xbDclNoNewForObject(xbEbase);
	xbDclNoCopyForObject(xbEbase);
};

#define xbEsysFmt_SIZEMAX 256
class xbEXPORT __declspec(uuid("{E4046CA3-D136-47bc-AC06-DBE37C5FBEB2}"))
	xbEsysLow:public xbEbase
{
public:
	static xbEinfoSt s_info;
	virtual const xbEinfoSt *GetInfo() const {return &s_info;}
	virtual LPCSTR GetFullEname() const {return "xbEsysLow";}

	virtual bool IsToWriteOnLogFile() const {return true;}
	virtual xbMBiconType GetMBoxIcon() const {return xbMBicon_Stop;}
	virtual const char *GetMBoxTitle() const {return "System error";} 

	virtual bool GetInterface(REFIID riid, void ** ppInterf)
	{
		xbImplInterface(xbEsysLow);
		return xbEbase::GetInterface(riid,ppInterf);
	}

	xbEsysLow(const char *pszMsg,xbEctorMode ctorMode = xbEctor_final)
		:xbEbase()
	{
		m_pszMsg = g_xbLowExceptionHeap.xbAllocString(pszMsg);

		traceExcept(ctorMode);
	}
	xbEsysLow(xbHR hr,const char *pszMsg,xbEctorMode ctorMode = xbEctor_final)
		:xbEbase(hr)
	{
		m_pszMsg = g_xbLowExceptionHeap.xbAllocString(pszMsg);

		traceExcept(ctorMode);
	}
	xbEsysLow(xbEctorMode ctorMode,xbHR hr,const char *pszMsg,va_list ap)
		:xbEbase(hr)
	{
		msgFmt(pszMsg,ap);

		traceExcept(ctorMode);
	}
	xbEsysLow(xbEctorMode ctorMode,const char *pszMsg,va_list ap)
		:xbEbase()
	{
		msgFmt(pszMsg,ap);

		traceExcept(ctorMode);
	}
	static void Throw(const char *pszMsg)
	{
		xbThrow xbEsysLow(pszMsg);
	}
	static void Throw(xbHR hr,const char *pszMsg)
	{
		xbThrow xbEsysLow(hr,pszMsg);
	}
	static void ThrowV(const char *pszMsg,...)
	{
		va_list ap;
		va_start(ap,pszMsg);
		xbThrow xbEsysLow(xbEctor_final,pszMsg,ap);
		va_end(ap);
	}
	static void ThrowV(xbHR hr,const char *pszMsg,...)
	{
		va_list ap;
		va_start(ap,pszMsg);
		xbThrow xbEsysLow(xbEctor_final,hr,pszMsg,ap);
		va_end(ap);
	}

	virtual const char *GetMsg() const {return m_pszMsg;}

	virtual ~xbEsysLow()
	{
		eProtectBegin();
		if (m_pszMsg != NULL)
			g_xbLowExceptionHeap.Free(m_pszMsg);
		eProtectEndRdbgTrace();
	}
protected:
	xbEsysLow(HRESULT hr)
		:xbEbase(hr)
	{
		m_pszMsg = NULL;
	}

	void msgFmt(const char *pszMsg,va_list ap)
	{
		char buf[xbEsysFmt_SIZEMAX];
		xbVsnprintf(buf,sizeof(buf)-1,pszMsg,ap);
		buf[sizeof(buf)-1] = 0;

		m_pszMsg = g_xbLowExceptionHeap.xbAllocString(buf);
	}
	char	*m_pszMsg;
private:
	xbDclNoCopyForObject(xbEsysLow);
public:
	xbLowExceptionHeapDnewImpl(xbEsysLow);
};

#define xbElowDclStart(ename,baseclass,euid,exportd) \
	class exportd __declspec(uuid( # euid)) \
		ename:public baseclass \
	{ \
	public: \
		static xbEinfoSt s_info; \
		virtual const xbEinfoSt *GetInfo() const {return &s_info;} \
		virtual LPCSTR GetFullEname() const {return # ename;} \
		virtual bool GetInterface(REFIID riid, void ** ppInterf) \
		{ \
			xbImplInterface(ename); \
			return baseclass::GetInterface(riid,ppInterf); \
		} \
		\
		ename(xbHR hr,const char *pszMsg,xbEctorMode ctorMode = xbEctor_final) \
			:baseclass(hr,pszMsg,xbEctor_base) \
		{ \
			traceExcept(ctorMode); \
		} \
		ename(xbEctorMode ctorMode,xbHR hr,const char *pszMsg,va_list ap) \
			:baseclass(xbEctor_base,hr,pszMsg,ap) \
		{ \
			traceExcept(ctorMode); \
		} \
		ename(const char *pszMsg,xbEctorMode ctorMode = xbEctor_final) \
			:baseclass(xbHR_FAIL,pszMsg,xbEctor_base) \
		{ \
			traceExcept(ctorMode); \
		} \
		ename(xbEctorMode ctorMode,const char *pszMsg,va_list ap) \
			:baseclass(xbEctor_base,xbHR_FAIL,pszMsg,ap) \
		{ \
			traceExcept(ctorMode); \
		} \
		static void Throw(const char *pszMsg) \
		{ \
			xbThrow ename(pszMsg); \
		} \
		static void Throw(xbHR hr,const char *pszMsg) \
		{ \
			xbThrow ename(hr,pszMsg); \
		} \
		static void ThrowV(const char *pszMsg,...) \
		{ \
			va_list ap; \
			va_start(ap,pszMsg); \
			xbThrow ename(xbEctor_final,pszMsg,ap); \
			va_end(ap); \
		} \
		static void ThrowV(xbHR hr,const char *pszMsg,...) \
		{ \
			va_list ap; \
			va_start(ap,pszMsg); \
			xbThrow ename(xbEctor_final,hr,pszMsg,ap); \
			va_end(ap); \
		}

#define xbElowDclHRstart(ename,baseclass,hr,euid,exportd) \
	class exportd __declspec(uuid(# euid)) \
		ename:public baseclass \
	{ \
	public: \
		static xbEinfoSt s_info; \
		virtual const xbEinfoSt *GetInfo() const {return &s_info;} \
		virtual LPCSTR GetFullEname() const {return # ename;} \
		virtual bool GetInterface(REFIID riid, void ** ppInterf) \
		{ \
			xbImplInterface(ename); \
			return baseclass::GetInterface(riid,ppInterf); \
		} \
		\
		ename(const char *pszMsg,xbEctorMode ctorMode = xbEctor_final) \
			:baseclass(hr,pszMsg,xbEctor_base) \
		{ \
			traceExcept(ctorMode); \
		} \
		ename(xbEctorMode ctorMode,const char *pszMsg,va_list ap) \
			:baseclass(xbEctor_base,hr,pszMsg,ap) \
		{ \
			traceExcept(ctorMode); \
		} \
		static void Throw(const char *pszMsg) \
		{ \
			xbThrow ename(pszMsg); \
		} \
		static void ThrowV(const char *pszMsg,...) \
		{ \
			va_list ap; \
			va_start(ap,pszMsg); \
			xbThrow ename(xbEctor_final,pszMsg,ap); \
			va_end(ap); \
		}


#define xbElowDclHRend(ename) xbElowDclEnd(ename) 
#define xbElowDclEnd(ename) \
	public: \
		xbLowExceptionHeapDnewImpl(ename); \
	private: \
		xbDclNoCopyForObject(ename); \
	};

#define xbElowImpl(ename,baseclass) \
	xbEinfoSt ename::s_info(#ename,&baseclass::s_info)

//Exception xbEsizeLimit: when allocations (strings, arrays, ...) exceed a fixed size (xbSIZE_LIMIT)
// will be used in some cases to detect stream/storage corruption
xbElowDclHRstart(xbEsizeLimit,xbEsysLow,xbHR_INVALIDARG,{3FD45120-8B67-4a6f-B864-71636BE48F1F},xbEXPORT)
xbElowDclHRend(xbEsizeLimit);

//Exception xbEmem: when allocations (strings, arrays, ...) exceed a fixed size (xbSIZE_LIMIT)
// will be used in some cases to detect stream/storage corruption
xbElowDclHRstart(xbEmem,xbEsysLow,xbHR_MEMORY,{0D5A4647-7F1A-4289-BC6B-7F303FAD3EC6},xbEXPORT)
	xbEmem()
		:xbEsysLow(xbHR_MEMORY,"Not enough memory",xbEctor_base)
	{
	}
xbElowDclHRend(xbEmem);

#endif
