Skip to content
Snippets Groups Projects
AlgorithmManager.cpp 2.6 KiB
Newer Older
Matt Clarke's avatar
Matt Clarke committed
#include <iomanip>
#include <iostream>
#include <vector>
Matt Clarke's avatar
Matt Clarke committed
#include "IAlgorithm.h"
#include "StatusCode.h"
#include "AlgorithmManager.h"
Nick Draper's avatar
Nick Draper committed
#include "Exception.h"
Matt Clarke's avatar
Matt Clarke committed
	Logger& AlgorithmManager::g_log = Logger::get("AlgorithmManager");
	AlgorithmManager* AlgorithmManager::m_instance = 0;
	
Matt Clarke's avatar
Matt Clarke committed
	AlgorithmManager::AlgorithmManager() : DynamicFactory<IAlgorithm>(),
	   no_of_alg(0)
Matt Clarke's avatar
Matt Clarke committed
		std::cout<<"AlgorithmManager == "<<std::setbase(16)
			<<reinterpret_cast<long>(this)
		        <<std::endl;	
Matt Clarke's avatar
Matt Clarke committed
		std::cout<<"AlgorithmManager Delete == "<<std::setbase(16)
			<<reinterpret_cast<long>(this)
		        <<std::endl;	
		clear();
	}
        
	IAlgorithm* 
	AlgorithmManager::createUnmanaged(const std::string& algName) const
	  /*!
    	   Creates an instance of an algorithm
	  
	   @param algName The name of the algorithm required
	   @return A pointer to the created algorithm
Nick Draper's avatar
Nick Draper committed
	   @throw NotFoundError Thrown if algorithm requested is not registered
Matt Clarke's avatar
Matt Clarke committed
	 */
	{
	    return DynamicFactory<IAlgorithm>::create(algName);                // Throws on fail:
Matt Clarke's avatar
Matt Clarke committed
	IAlgorithm* AlgorithmManager::create(const std::string& algName)
	  /*!
Nick Draper's avatar
Nick Draper committed
		Creates an instance of an algorithm
Matt Clarke's avatar
Matt Clarke committed
	  
	   @param algName The name of the algorithm required
	   @return A pointer to the created algorithm
Nick Draper's avatar
Nick Draper committed
       @throw NotFoundError Thrown if algorithm requested is not registered
	   @throw std::runtime_error Thrown if properties string is ill-formed
Matt Clarke's avatar
Matt Clarke committed
	 */
Matt Clarke's avatar
Matt Clarke committed
	   regAlg.push_back(DynamicFactory<IAlgorithm>::create(algName));                // Throws on fail:
	   StatusCode status = regAlg.back()->initialize();
	    if (status.isFailure())
Nick Draper's avatar
Nick Draper committed
		    throw std::runtime_error("AglorithmManager:: Unable to initialise algorithm " + algName); 
Matt Clarke's avatar
Matt Clarke committed
	    no_of_alg++;		
	    return regAlg.back();
Matt Clarke's avatar
Matt Clarke committed
	     if (!m_instance) 
		m_instance=new AlgorithmManager;	 
		
	  return m_instance;
	}

	void AlgorithmManager::clear()
Matt Clarke's avatar
Matt Clarke committed
	    /// finalizes and deletes all registered algorithms
Matt Clarke's avatar
Matt Clarke committed
	     int errOut(0);
	     std::vector<IAlgorithm*>::iterator vc;
	     for(vc=regAlg.begin();vc!=regAlg.end();vc++)
	        {
		      // no test for zero since impossible 
		      StatusCode status = (*vc)->finalize();
		       errOut+= status.isFailure();
		       delete (*vc);
Matt Clarke's avatar
Matt Clarke committed
	     regAlg.clear();
	     no_of_alg=0;
	     if (errOut)
                throw std::runtime_error("AlgorithmManager:: Unable to finalise algorithm " ); 
	return;
     }