#ifndef _xbBASELOW_
#define _xbBASELOW_
/*<<<***************************************************************************
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: xbODbase
#Project: xdcBase
////////////////////////////////////////////////////////////////////////////////
#Author: Marc MIRTI MANCINELLI
#History:
<10/02/2014: creation.
12/02/2014: this notice inserted.
***************************************************************************>>>*/

//_MSC_VER = 1200 -> visual studio 6
//_MSC_VER = 1400 -> visual studio 8
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#ifdef _M_X64
#	define	XDCBASE_X64
#	pragma message("XDCBASE: platform = x64")
#elif defined(_M_IX86)
#	define	XDCBASE_X86
#	pragma message("XDCBASE: platform = x86")
#else
#	pragma message("XDCBASE: ERROR: platform not supported")
#endif

#pragma warning( disable : 4100 4245 4510 4610 4251 4661)
//warning C4510: '_xbNoAddRefRelease<T>' : le constructeur par défaut n'a pas pu être généré
//warning C4610: class '_xbNoAddRefRelease<T>' ne peut jamais être instancié(e) - constructeur défini par l'utilisateur requis

//warning C4251: 'xbPtrNotif::m_arrObj' : class 'xbArray<ta_Obj>' needs to have dll-interface to be used by clients of class 'xbPtrNotif'
//warning C4661: 'xbCfgEngine::LCIDitem &xbArray<ta_Obj,ta_bDelObj,ta_bAllocObj>::operator [](int) const' : no suitable definition provided for explicit template instantiation request

#pragma warning( 3 : 4239 4554 4927 )
//warning C4927: conversion non conforme ; plusieurs conversions définies par l'utilisateur ont été appliquées implicitement

#define xbSTRING2(x)	#x
#define xbSTRING(x)	xbSTRING2(x)

#ifndef _MSC_VER
#	pragma message("XDCBASE: ERROR: compiler not supported...")
#else
#	pragma message("XDCBASE: MSC_VER=" xbSTRING(_MSC_VER) )
#endif

#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS

// Modifiez les définitions suivantes si vous devez cibler une plate-forme avant celles spécifiées ci-dessous.
// Reportez-vous à MSDN pour obtenir les dernières informations sur les valeurs correspondantes pour les différentes plates-formes.
#ifndef WINVER				// Autorise l'utilisation des fonctionnalités spécifiques à Windows XP ou version ultérieure.
#define WINVER 0x0501		// Attribuez la valeur appropriée à cet élément pour cibler d'autres versions de Windows.
#endif

#ifndef _WIN32_WINNT		// Autorise l'utilisation des fonctionnalités spécifiques à Windows XP ou version ultérieure.                   
#define _WIN32_WINNT 0x0501	// Attribuez la valeur appropriée à cet élément pour cibler d'autres versions de Windows.
#endif						

#ifndef _WIN32_WINDOWS		// Autorise l'utilisation des fonctionnalités spécifiques à Windows 98 ou version ultérieure.
#define _WIN32_WINDOWS 0x0410 // Attribuez la valeur appropriée à cet élément pour cibler Windows Me ou version ultérieure.
#endif

#ifndef _WIN32_IE			// Autorise l'utilisation des fonctionnalités spécifiques à Internet Explorer 6.0 ou version ultérieure.
#define _WIN32_IE 0x0600	// Attribuez la valeur appropriée à cet élément pour cibler d'autres versions d'Internet Explorer.
#endif

#define VC_EXTRALEAN		// Exclude rarely-used stuff from Windows headers
#define WIN32_LEAN_AND_MEAN		// Exclure les en-têtes Windows rarement utilisés
// Fichiers d'en-tête Windows :
#include <windows.h>
#include <objbase.h>

#	define xbStrcmp		strcmp
#	define xbStricmp	_stricmp
#	define xbStrnicmp	_strnicmp
#	define xbStrncmp	strncmp
#	define xbStrcmpA	strcmp
#	define xbStricmpA	_stricmp
#	define xbStrnicmpA	_strnicmp
#	define xbStrncmpA	strncmp
#	define xbStrncpy	strncpy
#	define xbStrcpy		strcpy
#	define xbStrlen		strlen
#	define xbStrlenA	strlen
#	define xbStrlenW	wcslen
#	define xbStrlwr		_strlwr_s
#	define xbStrupr		_strupr_s
#	define xbStrlwrA	_strlwr_s
#	define xbStruprA	_strupr_s
#	define xbWcslwr		_wcslwr_s
#	define xbWcsupr		_wcsupr_s
#	define xbStrlwrW	_wcslwr_s
#	define xbStruprW	_wcsupr_s
#	define xbWcscmp		wcscmp
#	define xbWcsicmp	_wcsicmp
#	define xbWcsncmp	wcsncmp
#	define xbWcsnicmp	_wcsnicmp
#	define xbStrnicmpW	_wcsnicmp
#	define xbStrncmpW	wcsncmp
#	define xbStrcmpW	wcscmp
#	define xbStricmpW	_wcsicmp
#	define xbWcsncpy	wcsncpy
#	define xbVsnprintf	_vsnprintf
#	define xbSnprintf	_snprintf
#	define xbVsnprintfA	_vsnprintf
#	define xbSnprintfA	_snprintf
#	define xbVsnprintfW	_vsnwprintf
#	define xbSnprintfW	_snwprintf
#	define xbItoa		_itoa
#	define xbLtoa		_ltoa
#	define xbItot		_itot
#	define xbLtot		_ltot
#	define xbFopen		fopen
#	define xbStrtod		strtod
#	define xbStrtodA	strtod
#	define xbStrtodW	wcstod
#	define xbStrchr		strchr
#	define xbStrchrA	strchr
#	define xbStrchrW	wcschr
#	define xbStrrchr	strrchr
#	define xbStrrchrA	strrchr
#	define xbStrrchrW	wcsrchr
#include <string.h>
#include <stdio.h>

#define xbPropertyRW(type,name) __declspec( property( get=Get##name, put=Set##name ) ) type name
#define xbPropertyR(type,name) __declspec( property( get=Get##name ) ) type name
#define xbPropertyW(type,name) __declspec( property( put=Set##name ) ) type name

#define xbPropertyExtRW(type,mr,mw,name) __declspec( property( get=mr, put=mw ) ) type name
#define xbPropertyExtR(type,mr,name) __declspec( property( get=mr ) ) type name
#define xbPropertyExtW(type,mw,name) __declspec( property( put=mw ) ) type name

#define xbArrayPropertyRW(type,typeix,name) __declspec( property( get=Get##name, put=Set##name ) ) type name[]
#define xbArrayPropertyR(type,typeix,name) __declspec( property( get=Get##name ) ) type name[]
#define xbArrayPropertyW(type,typeix,name) __declspec( property( put=Set##name ) ) type name[]

#define xbArrayPropertyExtRW(type,typeix,mr,mw,name) __declspec( property( get=mr, put=mw ) ) type name[]
#define xbArrayPropertyExtR(type,typeix,mr,name) __declspec( property( get=mr ) ) type name[]
#define xbArrayPropertyExtW(type,typeix,mw,name) __declspec( property( put=mw ) ) type name[]

//types to convert a pointer in a number
#ifdef XDCBASE_X64
	typedef unsigned __int64	xbNptr;
	typedef __int64				xbint;
	typedef unsigned __int64	xbuint;
#else
	typedef unsigned int		xbNptr;
	typedef __int32				xbint;
	typedef unsigned __int32	xbuint;
#endif
typedef unsigned __int64	xbNptrExt;

typedef __int64				int64;
typedef unsigned __int64	uint64;
typedef __int32				int32;
typedef unsigned __int32	uint32;
typedef unsigned int		uint;
typedef unsigned long		ulong;
typedef unsigned short		ushort;
typedef unsigned char		uchar;
typedef wchar_t				wchar;

typedef short				int16;
typedef char				int8;
typedef unsigned short		uint16;
typedef unsigned char		uint8;
/*
//messages use :
//xbWM_LIB to xbWM_CTRL - 1 : reserved for lib : messages commons to lib controls.
//xbWM_CTRL : reserved for lib or control specific.
//WM_APP: for app -> use only xbMsg.

#define xbWM_LIB		WM_USER
#define xbWM_CTRL		(WM_USER + 0xFF)

//Library messages intra control
#define xbWMLIB_ANIM	xbWM_LIB	//message sent for play anim in controls
*/

//#define interface struct

#define xbLCID_US		MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT)
#define xbLCID_USER		LOCALE_USER_DEFAULT
#define xbLCID_SYS		LOCALE_SYSTEM_DEFAULT
#define xbLCID_NEUTRAL	LOCALE_NEUTRAL

#if (!defined(WINVER)) || (!defined(_WIN32))
#	pragma message("XDCBASE: ERROR: OS not supported (WINVER or _WIN32 not defined)")
#else
#	if WINVER < 0x500
#		ifndef xbNOCWARNINGS
#			pragma message("XDCBASE: WARNING : Old standard C includes (WINVER < 0x500 : you should update your includes with ms platform sdk)")
#		endif
#	endif
#endif

/*
#if !defined(_INC_COMDEF)
#	include <comdef.h>
#endif
*/

#define xbHR_MEMORY		E_OUTOFMEMORY
#define xbHR_NOTFOUND	ERROR_FILE_NOT_FOUND
#define xbHR_FOUND		ERROR_FILE_EXISTS
#define xbHR_SHARE		ERROR_SHARING_VIOLATION
#define xbHR_DENIED		E_ACCESSDENIED
#define xbHR_ABORT		E_ABORT
#define xbHR_FAIL		E_FAIL
#define xbHR_OK			S_OK
#define xbHR_LAST		HRESULT(HRESULT_FROM_WIN32(::GetLastError())) //E_LASTERR
#define xbHR_TIMEOUT	MAKE_HRESULT(SEVERITY_ERROR,0,ERROR_TIMEOUT)
#define xbHR_INVALIDARG	E_INVALIDARG
#define xbHR_NOTIMPL	E_NOTIMPL

typedef HRESULT xbHR;

inline bool	xbIsHRaccessDenied(xbHR hr) 
{
	return hr == E_ACCESSDENIED || hr == STG_E_ACCESSDENIED 
		|| HRESULT_CODE(hr) == ERROR_ACCESS_DENIED;
}
inline bool	xbIsHRnotFound(xbHR hr) 
{
	return hr == STG_E_FILENOTFOUND || hr == STG_E_PATHNOTFOUND 
		|| HRESULT_CODE(hr) == ERROR_FILE_NOT_FOUND 
		|| HRESULT_CODE(hr) == ERROR_PATH_NOT_FOUND;
}
inline bool	xbIsHRsharedViolation(xbHR hr) 
{
	return hr == STG_E_SHAREVIOLATION || hr == STG_E_LOCKVIOLATION 
		|| HRESULT_CODE(hr) == ERROR_SHARING_VIOLATION 
		|| HRESULT_CODE(hr) == ERROR_LOCK_VIOLATION
		/*|| HRESULT_CODE(hr) == ERROR_ACCESS_DENIED*/; //XP bug: ???
}

inline bool	xbIsHRfound(xbHR hr) 
{
	return hr == STG_E_FILEALREADYEXISTS
		|| HRESULT_CODE(hr) == ERROR_FILE_EXISTS;
}

inline bool	xbIsHRmemory(xbHR hr) 
{
	return hr == E_OUTOFMEMORY
		|| hr == STG_E_INSUFFICIENTMEMORY
		|| HRESULT_CODE(hr) == ERROR_OUTOFMEMORY
		|| HRESULT_CODE(hr) == ERROR_NOT_ENOUGH_MEMORY;
}
inline bool	xbIsHRabort(xbHR hr) 
{
	return hr == E_ABORT;
}

inline bool	xbIsHRok(xbHR hr) 
{
	return SUCCEEDED(hr);
}

inline bool	xbIsHRfailed(xbHR hr) 
{
	return FAILED(hr);
}

#ifdef XDCBASE_X64
#	define xbInterlockIncrement32			_InterlockedIncrement
#	define xbInterlockIncrement64			_InterlockedIncrement64
#	define xbInterlockIncrementInt			_InterlockedIncrement64

#	define xbInterlockDecrement32			_InterlockedDecrement
#	define xbInterlockDecrement64			_InterlockedDecrement64
#	define xbInterlockDecrementInt			_InterlockedDecrement64

#	define xbInterlockCompareExchange32		_InterlockedCompareExchange
#	define xbInterlockCompareExchange64		_InterlockedCompareExchange64
#	define xbInterlockCompareExchangeInt	_InterlockedCompareExchange64

#	define xbInterlockExchangeAdd32			_InterlockedExchangeAdd
#	define xbInterlockExchangeAdd64			_InterlockedExchangeAdd64
#	define xbInterlockExchangeAddInt		_InterlockedExchangeAdd64
#else
#	define xbInterlockIncrement32			_InterlockedIncrement
#	define xbInterlockIncrementInt			_InterlockedIncrement

#	define xbInterlockDecrement32			_InterlockedDecrement
#	define xbInterlockDecrementInt			_InterlockedDecrement

#	define xbInterlockCompareExchange32		_InterlockedCompareExchange
#	define xbInterlockCompareExchange64		_InterlockedCompareExchange64
#	define xbInterlockCompareExchangeInt	_InterlockedCompareExchange

#	define xbInterlockExchangeAdd32			_InterlockedExchangeAdd
#	define xbInterlockExchangeAdd64			_InterlockedExchangeAdd64
#	define xbInterlockExchangeAddInt		_InterlockedExchangeAdd
/*Pour utiliser les points d'entrée dll en 32
#	define xbInterlockIncrement32			InterlockedIncrement
#	define xbInterlockIncrementInt			InterlockedIncrement

#	define xbInterlockDecrement32			InterlockedDecrement
#	define xbInterlockDecrementInt			InterlockedDecrement

#	define xbInterlockCompareExchange32		InterlockedCompareExchange
#	define xbInterlockCompareExchange64		_InterlockedCompareExchange64 // point d'entrée inexistant sous XP
#	define xbInterlockCompareExchangeInt	InterlockedCompareExchange

#	define xbInterlockExchangeAdd32			InterlockedExchangeAdd
#	define xbInterlockExchangeAdd64			InterlockedExchangeAdd64
#	define xbInterlockExchangeAddInt		InterlockedExchangeAdd
*/
#endif

inline ulong xbThreadId()
{
	return ::GetCurrentThreadId();
}

typedef HWND	xbHWND;

enum xbMBiconType
{
	xbMBicon_Default = 0,
	xbMBicon_Stop = MB_ICONSTOP, //=MB_ICONHAND
	xbMBicon_Question = MB_ICONQUESTION,
	xbMBicon_Exclamation = MB_ICONEXCLAMATION,
	xbMBicon_Information = MB_ICONINFORMATION //=MB_ICONASTERISK
};
enum xbMBbtnType
{
	xbMBbtn_Default = 0,
	xbMBbtn_Ok = MB_OK, //=MB_ICONHAND
	xbMBbtn_OkCancel = MB_OKCANCEL, //=MB_ICONHAND
	xbMBbtn_AbortRetryIgnore = MB_ABORTRETRYIGNORE,
	xbMBbtn_YesNoCancel = MB_YESNOCANCEL,
	xbMBbtn_YesNo = MB_YESNO,
	xbMBbtn_RetryCancel = MB_RETRYCANCEL,
#if(WINVER >= 0x0500)
	xbMBbtn_CancelTryContinue = MB_CANCELTRYCONTINUE
#endif /* WINVER >= 0x0500 */
};
enum xbMBretType
{
	xbMBret_Undef = 0,
	xbMBret_Ok = IDOK,
	xbMBret_Cancel = IDCANCEL,
	xbMBret_Abort = IDABORT,
	xbMBret_Retry = IDRETRY,
	xbMBret_Ignore = IDIGNORE,
	xbMBret_Yes = IDYES,
	xbMBret_No = IDNO,
#if(WINVER >= 0x0400)
	xbMBret_Close = IDCLOSE,
	xbMBret_Help = IDHELP,
#endif /* WINVER >= 0x0400 */
#if(WINVER >= 0x0500)
	xbMBret_TryAgain = IDTRYAGAIN,
	xbMBret_Continue = IDCONTINUE,
#endif /* WINVER >= 0x0500 */
};

inline HMODULE xbGetInstanceHandle()
{
	return ::GetModuleHandle(NULL);
}

xbEXPORT void xbFreeLibrary(HMODULE hMod);
xbEXPORT HMODULE xbLoadLibrary(LPCSTR path);
xbEXPORT HMODULE xbLoadLibraryAsPlug(LPCSTR path);
xbEXPORT HMODULE xbLoadLibraryAsData(LPCSTR path);

#define xbPath_SLASH	"\\"
#define xbPath_SLASHch	'\\'
#define xbPath_SLASH2ch	'/'

#define xbLITTLEENDIAN
//#define xbBIGENDIAN
#endif
