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

	Program:		Range Imaging Toolkit (RITK)
	Module:			ritkRImageIOManager.txx
	Version:		$Revision: 1852 $
	Date:			$Date: 2011-10-12 18:04:35 +0200 (Mi, 12 Okt 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 "ritkRImageIOManager.h"
#include <string.h>
#include <iostream>

namespace ritk
{


template<class RImageBase>
RImageIOManager<RImageBase>& 
RImageIOManager<RImageBase>::GetInstance()
{
	static RImageIOManager Instance;
	return Instance;
}


//----------------------------------------------------------------------------
template<class RImageBase>
RImageIOManager<RImageBase>::RImageIOManager()
{
}


//----------------------------------------------------------------------------
template<class RImageBase>
RImageIOManager<RImageBase>::~RImageIOManager()
{
}


//----------------------------------------------------------------------------
template<class RImageBase>
bool
RImageIOManager<RImageBase>::RegisterIOObject(RImageIOBaseType *Object)
{
	// Check if the identifier is already registered
	typename IOObjectContainer::iterator it = m_IOObjects.begin();
	while ( it != m_IOObjects.end() )
	{
		if ( strcmp((*it)->GetRImageClassName(), Object->GetRImageClassName()) == 0 )
		{
			std::cout << "Warning: Trying to register IOObject <" << Object->GetRImageClassName() << "> that is already registered!" << std::endl;
			return false;
		}
		++it;
	}

	// Append the new object
	RImageIOBasePointer ObjectPointer = Object;
	m_IOObjects.push_back(ObjectPointer);
	return true;
}


//----------------------------------------------------------------------------
template<class RImageBase>
bool
RImageIOManager<RImageBase>::UnRegisterIOObject(RImageIOBaseType *Object)
{
	// Iterate through the list and remove the object
	typename IOObjectContainer::iterator it = m_IOObjects.begin();
	while ( it != m_IOObjects.end() )
	{
		if ( strcmp((*it)->GetRImageClassName(), Object->GetRImageClassName()) == 0 )
		{
			m_IOObjects.erase(it);
			return true;
		}
		++it;
	}

	std::cout << "Warning: could not unregistering IO object <" << Object->GetRImageClassName() << ">" << std::endl;
	return false;
}


//----------------------------------------------------------------------------
template<class RImageBase>
typename RImageIOManager<RImageBase>::RImageIOBasePointer
RImageIOManager<RImageBase>::RequestIOObject(const char *RImageIdentifier)
{
	// Iterate through the list and return the object
	typename IOObjectContainer::iterator it = m_IOObjects.begin();
	while ( it != m_IOObjects.end() )
	{
		if ( strcmp((*it)->GetRImageClassName(), RImageIdentifier) == 0 )
		{
			// Create a clone. This ensures multiple instances of one IOObject! 
			RImageIOBasePointer Clone = dynamic_cast<RImageIOBaseType*>((itk::LightObject*)(*it)->CreateAnother());
			return Clone;
		}
		++it;
	}
	return NULL;
}

} // end namespace ritk
