//
//	[!output PROJECT_NAME]Module.cpp
//
//	Main entry point for Vectorworks module code.
//

[!if ADD_SAMPLE_CODE]
[!endif]

#include "[!output PRECOMPILED_HEADER].h"

[!if ADD_MFC_SUPPORT]
#include "[!output PROJECT_NAME]MFCApp.h"

[!if ADD_SAMPLE_CODE]
#include "MFCTestDialog.h"
[!endif]
[!endif]


//------------------------------------------------------------------
template<class T> void REGISTER_Interface(const VWIID& iid, long action, void* pInfo, long& ioData)
{
	if ( action == kVCOMRegisterInterfaces ) {
		::GS_VWRegisterInterface(iid, pInfo);
	}
	else if ( action == kVCOMQueryInterface && pInfo ) {
		if ( *((VWIID*) pInfo) == iid ) {
			T*			pInstance	= new T();
			IVWUnknown*	pImpl		= dynamic_cast<IVWUnknown*>( pInstance );
			DSTOPIF(pImpl == NULL, ("Incorrect interface implementation specified."));
			ioData					= (long) pImpl;
		}
	}
	else if ( action == kVCOMReleaseInterface && pInfo ) {
		if ( *((VWIID*) pInfo) == iid ) {
			T* pImpl = dynamic_cast<T*>( (IVWUnknown*) ioData );
			if ( VERIFYN( kVStanev, pImpl ) ) {
				delete pImpl;
			}
		}
	}
}

template<class T> void REGISTER_ParametricApp(const VWIID& iid, long action, void* pInfo, long& ioData)
{
	using namespace VectorWorks::PluginSupport;
	if ( action == kVCOMRegisterInterfaces ) {
		::GS_VWRegisterInterface(iid, GROUPID_ParametricApp, pInfo);
	}
	else if ( action == kVCOMQueryInterface && pInfo ) {
		if ( *((VWIID*) pInfo) == iid ) {
			T*				pInstance	= new T();
			IParametricApp*	pImpl		= dynamic_cast<IParametricApp*>( pInstance );
			DSTOPIF(pImpl == NULL, (kVStanev, "Incorrect interface implementation specified."));
			ioData						= (long) pImpl;
		}
	}
	else if ( action == kVCOMReleaseInterface && pInfo ) {
		if ( *((VWIID*) pInfo) == iid ) {
			T* pImpl = dynamic_cast<T*>( (IVWUnknown*) ioData );
			if ( VERIFYN( kVStanev, pImpl ) ) {
				delete pImpl;
			}
		}
	}
}

template<class T> void REGISTER_MenuApp(const VWIID& iid, long action, void* pInfo, long& ioData)
{
	using namespace VectorWorks::PluginSupport;
	if ( action == kVCOMRegisterInterfaces ) {
		::GS_VWRegisterInterface(iid, GROUPID_MenuApp, pInfo);
	}
	else if ( action == kVCOMQueryInterface && pInfo ) {
		if ( *((VWIID*) pInfo) == iid ) {
			T*			pInstance	= new T();
			IMenuApp*	pImpl		= dynamic_cast<IMenuApp*>( pInstance );
			DSTOPIF(pImpl == NULL, (kVStanev, "Incorrect interface implementation specified."));
			ioData					= (long) pImpl;
		}
	}
	else if ( action == kVCOMReleaseInterface && pInfo ) {
		if ( *((VWIID*) pInfo) == iid ) {
			T* pImpl = dynamic_cast<T*>( (IVWUnknown*) ioData );
			if ( VERIFYN( kVStanev, pImpl ) ) {
				delete pImpl;
			}
		}
	}
}

template<class T> void REGISTER_ToolApp(const VWIID& iid, long action, void* pInfo, long& ioData)
{
	using namespace VectorWorks::PluginSupport;
	if ( action == kVCOMRegisterInterfaces ) {
		::GS_VWRegisterInterface(iid, GROUPID_ToolApp, pInfo);
	}
	else if ( action == kVCOMQueryInterface && pInfo ) {
		if ( *((VWIID*) pInfo) == iid ) {
			T*			pInstance	= new T();
			IToolApp*	pImpl		= dynamic_cast<IToolApp*>( pInstance );
			DSTOPIF(pImpl == NULL, (kVStanev, "Incorrect interface implementation specified."));
			ioData					= (long) pImpl;
		}
	}
	else if ( action == kVCOMReleaseInterface && pInfo ) {
		if ( *((VWIID*) pInfo) == iid ) {
			T* pImpl = dynamic_cast<T*>( (IVWUnknown*) ioData );
			if ( VERIFYN( kVStanev, pImpl ) ) {
				delete pImpl;
			}
		}
	}
}

//------------------------------------------------------------------
// Entry point of the module plug-in for Vectorworks
//
// This function is responsible for providing VCOM interface implementations
// for the objects, menu commands and tools that this module provides
//
// Template functions are used to simplify registering and providing of the VCOM interface implementations:
//    - REGISTER_Interface      - handles general VCOM interface implementation
//    - REGISTER_ParametricApp  - handles parametric application VCOM implementation
//    - REGISTER_MenuApp        - handles menu command application VCOM implementation
//    - REGISTER_ToolApp        - handles tool application VCOM implementation
//
// More info at: http://developer.vectorworks.net/index.php?title=SDK:Module_Plug-in
//
extern "C" long GS_EXTERNAL_ENTRY plugin_main(long action, void* pInfo, long& ioData, CallBackPtr cbp)
{
	// initialize VCOM mechanizm
	::GS_InitializeVCOM( cbp );

	long	reply	= 0L;

//	REGISTER_Interface<CXXX>( IID_XXX, action, pInfo, ioData );
//	REGISTER_ParametricApp<CXXX>( IID_XXX, action, pInfo, ioData );
//	REGISTER_MenuApp<CXXX>( IID_XXX, action, pInfo, ioData );
//	REGISTER_ToolApp<CXXX>( IID_XXX, action, pInfo, ioData );

	return reply;
}
