/*////////////////////////////////////////////////////////////////////////////

NAME:
	Basic defines for Qualcomm PureVoice Library

FILE:		QplCom.h
AUTHOR:	Livingstone Song
COMPANY:	QUALCOMM, Inc. Copyright (C) 1997, 1998

DESCRIPTION:
	Contains basic COM defines/functions and other misc defines for
	Qualcomm PureVoice Library.

REVISION:

-08/04/97	Inital

/*////////////////////////////////////////////////////////////////////////////
// TAB = 3

#ifndef _QPLCOM_H_
#define _QPLCOM_H_

#include <memory.h>

#if defined( WIN32 )
#define STDCALL			__stdcall
#include <windows.h>
#include <objbase.h>
#endif

#ifndef INT8
#	define INT8				char
#endif
#ifndef UINT8
#	define UINT8			unsigned char
#endif
#ifndef INT16
#	define INT16			short
#endif
#ifndef UINT16
#	define UINT16			unsigned short
#endif
#ifndef INT32
#	define INT32			long
#endif
#ifndef UINT32
#	define UINT32			unsigned long
#endif

#ifndef BYTE
#	define BYTE				UINT8
#endif

#ifndef HIBYTE
#	define HIBYTE(w)		((BYTE) (((UINT16) (w) >> 8) & 0xFF))  
#endif

#ifndef LOBYTE
#	define LOBYTE(w)		((BYTE) (w)) 
#endif

#ifndef MAKEUINT16
#	define MAKEUINT16(a, b) \
					((UINT16) (((BYTE) (a)) | ((UINT16) ((BYTE) (b))) << 8)) 
#endif

#if defined(BIG_ENDIAN)
#	define CORRECT_ORDER32(x)	((((UINT32)(x) & 0x000000FF) << 24) \
										|(((UINT32)(x) & 0x0000FF00) << 8) \
										|(((UINT32)(x) & 0x00FF0000) >> 8) \
										|(((UINT32)(x) & 0xFF000000) >> 24))

#	define CORRECT_ORDER16(x)	((((UINT16)(x) & 0x00FF) << 8) \
										|(((UINT16)(x) & 0xFF00) >> 8))
#else 
#	define CORRECT_ORDER32(x)	x
#	define CORRECT_ORDER16(x)	x
#endif

#if !defined( _WINDOWS_ )
#define WORD				UINT16
#define DWORD				UINT32
#define ULONG				UINT32
#define LONG				INT32
#define HRESULT			INT32
#define BOOL				INT32
#define STDCALL		
#define WINAPI				STDCALL
#define EXTERN_C			extern "C"

#ifndef NULL
#	define NULL				0
#endif
#ifndef TRUE
#	define TRUE				1
#endif
#ifndef FALSE
#	define FALSE			0
#endif

typedef struct  _GUID
{
    DWORD Data1;
    WORD Data2;
    WORD Data3;
    BYTE Data4[ 8 ];
}	GUID;

typedef GUID		*LPGUID;
#define REFGUID	const GUID&

typedef GUID		CLSID;
typedef CLSID		*LPCLSID;
#define REFCLSID	const CLSID&

typedef GUID		IID;
typedef IID			*LPIID;
#define REFIID		const IID&

// macros to define byte pattern for a GUID.
//      Example: DEFINE_GUID(GUID_XXX, a, b, c, ...);
//

#ifndef INITGUID
#define DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
    EXTERN_C const GUID name
#else
#define DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
        EXTERN_C const GUID name \
                = { l, w1, w2, { b1, b2,  b3,  b4,  b5,  b6,  b7,  b8 } }
#endif // INITGUID

inline BOOL IsEqualGUID(const GUID& rguid1, const GUID& rguid2)
{
#ifdef BIG_ENDIAN
	return ( (rguid1.Data1 == rguid2.Data1)
				&& (rguid1.Data2 == rguid2.Data2)
				&& (rguid1.Data3 == rguid2.Data3)
				&& !memcmp(&rguid1.Data4, &rguid2.Data4, sizeof(rguid1.Data4)) );
#else
	return !memcmp(&rguid1, &rguid2, sizeof(GUID));
#endif
}

#define IsEqualIID(riid1, riid2) IsEqualGUID(riid1, riid2)
#define IsEqualCLSID(rclsid1, rclsid2) IsEqualGUID(rclsid1, rclsid2)

inline BOOL operator==(const GUID& guidOne, const GUID& guidOther)
{
	return IsEqualGUID(guidOne,guidOther);
}

inline BOOL operator!=(const GUID& guidOne, const GUID& guidOther)
{
	return !(guidOne == guidOther);
}

////////////////////////////////////////////////////////////////////////////// 
//	IUnknown
//
//	Base class of all interfaces. Defines life time management and
//	support for dynamic cast.
//
//  IID_IUnknown:
//
//		{00000000-0000-0000-C000000000000046}
//
//DEFINE_GUID(IID_IUnknown,
//0x00000000, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);

class IUnknown
{
public:
    virtual ULONG STDCALL		AddRef() = 0;
    virtual ULONG STDCALL		Release() = 0;
    virtual HRESULT STDCALL	QueryInterface( REFIID riid, void** ppvObj ) = 0;
};

#define S_OK					(0x00000000L)
#define S_FALSE				(0x00000001L)

//
// HRESULT definitions
//

#define NOERROR				S_OK
#define E_UNEXPECTED			0x8000FFFFL	// Unexpected failure
#define E_NOTIMPL				0x80004001L	// Not implemented
#define E_OUTOFMEMORY		0x8007000EL	// Ran out of memory
#define E_INVALIDARG			0x80070057L	// One or more arguments are invalid
#define E_NOINTERFACE		0x80004002L	// No such interface supported
#define E_POINTER				0x80004003L	// Invalid pointer
#define E_HANDLE				0x80070006L	// Invalid handle
#define E_ABORT				0x80004004L	// Operation aborted
#define E_FAIL					0x80004005L	// Unspecified error
#define E_ACCESSDENIED		0x80070005L	// General access denied error

#endif // #ifdef _WINDOW #else

inline void SetGUID(GUID& rguidDest, const GUID& rguidSrc)
{
#ifdef BIG_ENDIAN
	rguidDest.Data1 = rguidSrc.Data1;
	rguidDest.Data2 = rguidSrc.Data2;
	rguidDest.Data3 = rguidSrc.Data3;
	memcpy(&rguidDest.Data4, &rguidSrc.Data4, sizeof(rguidSrc.Data4));
#else
	memcpy(&rguidDest, &rguidSrc, sizeof(GUID));
#endif
}

DEFINE_GUID(IID_NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

DEFINE_GUID(IID_IUnknown,
0x00000000, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);

#endif // #ifdef _QPLCOM_H_