#ifndef _xbLIBTOOLS_
#define _xbLIBTOOLS_
/*<<<***************************************************************************
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: xbLibTools
#Project: xdcBase
////////////////////////////////////////////////////////////////////////////////
#Author: Marc MIRTI MANCINELLI
#History:
<16/08/2013: creation.
12/02/2014: this notice inserted.
***************************************************************************>>>*/

#ifndef _xbSTRING_
#	include "xbString.h"
#endif

#ifndef _xbHEAPBASE_
#	include "xbHeapBase.h"
#endif

#ifndef XDCBASE_EMBED
class xbXdcLibEnum;
class xbEXPORT xbXdcLib : public xbBase
{
public:
	xbXdcLib(const char *pName,const char *pVer);
	virtual ~xbXdcLib();


	static volatile DWORD		s_cChg;
	static xbSectShareSimple	s_share;

	xbString GetName() {return m_pName;}
	xbString GetCompleteName() 
	{
		return xbString(m_pName) + 'V' + xbString(m_pVer)
#ifdef _DEBUG
			+ 'D';
#endif
			;
	}

	HMODULE	GetInstance() const {return m_hModule;}

	virtual void dllMainCall(HMODULE hModule,DWORD dwReason);
	virtual void dllStart(HMODULE hModule);
	virtual void dllStop(HMODULE hModule);

	xbString GetFilePath() const;
protected:
	const char			*m_pName,*m_pVer;
	HMODULE				m_hModule;
	static xbXdcLib		*s_pFirst;
	xbXdcLib			*m_pNext,*m_pPrev;

	friend xbXdcLibEnum;
private:
	xbDclNoCopyForObject(xbXdcLib);
	xbDclNoNewForObject(xbXdcLib);
};
#endif

class xbEXPORT xbXdcLibEnum
{
public:
	xbXdcLibEnum();

	HMODULE			NextHlib(bool bUnlockLib = true);
#ifndef XDCBASE_EMBED
	xbXdcLib		*NextLib();
	xbXdcLib		*GetLib();
#endif

	void			Reset();
	void			LockLib();
	void			UnlockLib();

	~xbXdcLibEnum();


#ifndef XDCBASE_EMBED
	xbXdcLib		*Find(const xbString &name);
#endif
	HMODULE			FindHlib(const xbString &name,bool bUnlockLib = true);
protected:
	void			next();

	int				m_ix;
#ifndef XDCBASE_EMBED
	xbXdcLib	*m_pDll;
#endif
protected:
	xbSectLockSimple m_lck;
private:
	xbDclNoCopyForObject(xbXdcLibEnum);
	xbDclNoNewForObject(xbXdcLibEnum);
};

#endif
