Skip to content

Commit

Permalink
Have AlgorithmFactory::subscribe return the class name subscribed.
Browse files Browse the repository at this point in the history
Refs #7263
  • Loading branch information
martyngigg committed Jul 4, 2013
1 parent a23d639 commit 483acce
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
19 changes: 12 additions & 7 deletions Code/Mantid/Framework/API/inc/MantidAPI/AlgorithmFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//----------------------------------------------------------------------
#include <vector>
#include <set>
#include <sstream>
#include "MantidAPI/DllConfig.h"
#include "MantidKernel/DynamicFactory.h"
#include "MantidKernel/SingletonHolder.h"
Expand Down Expand Up @@ -67,20 +68,21 @@ class MANTID_API_DLL AlgorithmFactoryImpl : public Kernel::DynamicFactory<Algori

/// algorithm factory specific function to subscribe algorithms, calls the dynamic factory subscribe function internally
template <class C>
void subscribe()
std::string subscribe()
{
Kernel::Instantiator<C, Algorithm>* newI = new Kernel::Instantiator<C, Algorithm>;
this->subscribe(newI);
return this->subscribe(newI);
}

/**
* Subscribes an algorithm using a custom instantiator. This
* object takes ownership of the instantiator
* @param instantiator - A pointer to a custom instantiator
* @param replaceExisting - Defines what happens if an algorithm of the same name/verson already exists, see SubscribeAction
* @param replaceExisting - Defines what happens if an algorithm of the same name/version already exists, see SubscribeAction
* @returns The classname that was registered
*/
template<class T>
void subscribe(Kernel::AbstractInstantiator<T> *instantiator, const SubscribeAction replaceExisting = ErrorIfExists)
std::string subscribe(Kernel::AbstractInstantiator<T> *instantiator, const SubscribeAction replaceExisting = ErrorIfExists)
{
boost::shared_ptr<IAlgorithm> tempAlg = instantiator-> createInstance();
const int version = extractAlgVersion(tempAlg);
Expand All @@ -97,16 +99,19 @@ class MANTID_API_DLL AlgorithmFactoryImpl : public Kernel::DynamicFactory<Algori
{
if(version == it->second && replaceExisting == ErrorIfExists)
{
g_log.fatal() << "Cannot register algorithm " << className << " twice with the same version\n";
return;
std::ostringstream os;
os << "Cannot register algorithm " << className << " twice with the same version\n";
throw std::runtime_error(os.str());
}
if(version > it->second)
{
m_vmap[className]=version;
}
}
}
Kernel::DynamicFactory<Algorithm>::subscribe(key, instantiator, replaceExisting);
}
else throw std::invalid_argument("Cannot register empty algorithm name");
return className;
}
/// Unsubscribe the given algorithm
void unsubscribe(const std::string & algorithmName, const int version);
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/API/test/AlgorithmManagerTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class AlgorithmManagerTest : public CxxTest::TestSuite
void testVersionFail()
{
const size_t nalgs = AlgorithmFactory::Instance().getKeys().size();
TS_ASSERT_THROWS_NOTHING(AlgorithmFactory::Instance().subscribe<AlgTestFail>());
TS_ASSERT_THROWS(AlgorithmFactory::Instance().subscribe<AlgTestFail>(), std::runtime_error);
// Size should be the same
TS_ASSERT_EQUALS(AlgorithmFactory::Instance().getKeys().size(), nalgs);
}
Expand Down

0 comments on commit 483acce

Please sign in to comment.