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

	Program:		Range Imaging Toolkit (RITK)
	Module:			ritkThreadedWorker.cxx
	Version:		$Revision: 1761 $
	Date:			$Date: 2011-09-23 17:25:41 +0200 (Fr, 23 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 "ritkThreadedWorker.h"
#include <iostream>

namespace ritk
{


//----------------------------------------------------------------------------
ThreadedWorker::ThreadedWorker()
{
}


//----------------------------------------------------------------------------
ThreadedWorker::~ThreadedWorker()
{
}


//----------------------------------------------------------------------------
void 
ThreadedWorker::AddNewJob(ThreadedJob *job)
{

    // Wrap the job into an event
	ThreadedJobEvent::Pointer e = ThreadedJobEvent::Pointer(new ThreadedJobEvent());
	e->m_Job = job;

	// Enqueue the job
	EnqueueEvent(e);
}


//----------------------------------------------------------------------------
void
ThreadedWorker::ProcessEvent(ritk::Event::Pointer e)
{
	if ( e->type() == ThreadedJobEvent::EventType )
	{
		// Cast the event
		ThreadedJobEvent::Pointer TJE = qSharedPointerDynamicCast<ThreadedJobEvent,Event>(e);
		TJE->m_Job->Execute();
	}
}



// ================ //
// ThreadedJobEvent //
// ================ //
QEvent::Type ThreadedJobEvent::EventType = static_cast<QEvent::Type>(QEvent::registerEventType());

ThreadedJobEvent::ThreadedJobEvent() :
	Event(EventType)
{
	m_Job = 0;
}

ThreadedJobEvent::~ThreadedJobEvent()
{
	//std::cout << "Destructor called for TJE (" << m_Job << ")" << std::endl;
	/*if ( m_Job->DeleteAfterExecution() )
	{
		std::cout << "Deleting job..." << std::endl;
		delete m_Job;
	}*/
	//std::cout << "Destructor finished..." << std::endl;
}


} // end namespace ritk
