Skip to content
Snippets Groups Projects
AlgorithmManager.cpp 2.65 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;
	
  /// Private Constructor for singleton class
Matt Clarke's avatar
Matt Clarke committed
	AlgorithmManager::AlgorithmManager() : DynamicFactory<IAlgorithm>(),
	   no_of_alg(0)
  /** Private destructor
   *  Prevents client from calling 'delete' on the pointer handed 
   *  out by Instance
   */
Matt Clarke's avatar
Matt Clarke committed
		clear();
	}
        
  /** Creates an instance of an algorithm, but does not own that instance
   * 
   *  @param  algName The name of the algorithm required
   *  @return A pointer to the created algorithm
   *  @throw  NotFoundError Thrown if algorithm requested is not registered
   */
	IAlgorithm* AlgorithmManager::createUnmanaged(const std::string& algName) const
Matt Clarke's avatar
Matt Clarke committed
	{
	    return DynamicFactory<IAlgorithm>::create(algName);                // Throws on fail:
  /** Creates an instance of an algorithm
   *
   *  @param  algName The name of the algorithm required
   *  @return A pointer to the created algorithm
   *  @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
	IAlgorithm* AlgorithmManager::create(const std::string& algName)
	   regAlg.push_back(DynamicFactory<IAlgorithm>::create(algName));      // Throws on fail:
Matt Clarke's avatar
Matt Clarke committed
	   StatusCode status = regAlg.back()->initialize();
	   if (status.isFailure())
	   {
	     throw std::runtime_error("AlgorithmManager:: Unable to initialise algorithm " + algName); 
	   }
	   no_of_alg++;		
	   return regAlg.back();
  /** A static method which retrieves the single instance of the Algorithm Manager
  * 
  *  @returns A pointer to the Algorithm Manager instance
  */
	AlgorithmManager* AlgorithmManager::Instance()
	{
	     if (!m_instance) m_instance = new AlgorithmManager;	 		
	     return m_instance;
  /// Finalizes and deletes all registered algorithms
	  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);
	  regAlg.clear();
	  no_of_alg=0;
	  if (errOut) throw std::runtime_error("AlgorithmManager:: Unable to finalise algorithm " ); 
	  return;
	}
Matt Clarke's avatar
Matt Clarke committed