/*======================================================================================

	Program:		Range Imaging Toolkit (RITK)
	Module:			ritkApplicationPlugin.cxx
	Version:		$Revision: 1791 $
	Date:			$Date: 2011-09-28 11:43:43 +0200 (Mi, 28 Sep 2011) $

	Copyright (c) 2010-2011
	Pattern Recognition Lab, 
    University Erlangen-Nuremberg, Germany
    All rights reserved.

	This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
	See RITKCopyright.txt for more information.

	http://www5.informatik.uni-erlangen.de/research/software/ritk

========================================================================================*/

#include "ritkApplicationPlugin.h"
#include "ritkManager.h"
#include "ritkDebugManager.h"

namespace ritk
{

//----------------------------------------------------------------------------
ApplicationPlugin::ApplicationPlugin()
{
	// Check if we're created from within the main thread context
	if ( QThread::currentThreadId() != Manager::GetInstance().GetMainThreadHandle() )
	{
		//throw std::exception("Cannot create plugin outside the main thread context!");
		LOG_DEB("Cannot create plugin outside the main thread context!");
		throw std::exception();
	}
}


//----------------------------------------------------------------------------
ApplicationPlugin::~ApplicationPlugin()
{
	// Note that there is no need to shutdown the ThreadedEventProcessor (TEP)
	// If the TEP was not shut down properly via the ShutDown() method a
	// warning will be printed...so its up to the user to do the job properly
}


//----------------------------------------------------------------------------
void
ApplicationPlugin::Initialize()
{
}


//----------------------------------------------------------------------------
bool
ApplicationPlugin::StartPlugin(void *p)
{
	// Call superclass' implementation
	if ( !PluginBase::StartPlugin(p) )
	{
		LOG_DEB("Starting plugin @" << this << " failed...");
		return false;
	}

	// Do some initialization
	Initialize();

	// Spawn thread. This will also include calling the Initialize() method
	start();

	return true;
}


//----------------------------------------------------------------------------
bool
ApplicationPlugin::StopPlugin()
{
	if ( !PluginBase::StopPlugin() )
	{
		return false;
	}

	// Invoke the StopPluginEvent
	Shutdown();

	return true;
}


} // end namespace ritk

