Skip to content

Commit

Permalink
Re #6811. Add method to return newest instance of an algorithm.
Browse files Browse the repository at this point in the history
  • Loading branch information
RussellTaylor committed Apr 1, 2013
1 parent 9ae37a9 commit 5357765
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Code/Mantid/Framework/API/inc/MantidAPI/AlgorithmManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ class MANTID_API_DLL AlgorithmManagerImpl

void notifyAlgorithmStarting(AlgorithmID id);

std::vector<IAlgorithm_const_sptr> runningInstancesOf(const std::string algorithmName) const;
IAlgorithm_sptr newestInstanceOf(const std::string& algorithmName) const;
std::vector<IAlgorithm_const_sptr> runningInstancesOf(const std::string& algorithmName) const;

void cancelAll();

Expand Down
13 changes: 12 additions & 1 deletion Code/Mantid/Framework/API/src/AlgorithmManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,19 @@ namespace Mantid
notificationCenter.postNotification(new AlgorithmStartingNotification(alg));
}

/// Returns the most recently created instance of the named algorithm (or null if not found)
IAlgorithm_sptr AlgorithmManagerImpl::newestInstanceOf(const std::string& algorithmName) const
{
for ( auto it = m_managed_algs.rbegin(); it != m_managed_algs.rend(); ++it )
{
if ( (*it)->name() == algorithmName ) return *it;
}

return IAlgorithm_sptr();
}

/// Returns all running (& managed) occurances of the named algorithm, oldest first
std::vector<IAlgorithm_const_sptr> AlgorithmManagerImpl::runningInstancesOf(const std::string algorithmName) const
std::vector<IAlgorithm_const_sptr> AlgorithmManagerImpl::runningInstancesOf(const std::string& algorithmName) const
{
std::vector<IAlgorithm_const_sptr> theRunningInstances;
Mutex::ScopedLock _lock(this->m_managedMutex);
Expand Down
15 changes: 15 additions & 0 deletions Code/Mantid/Framework/API/test/AlgorithmManagerTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,21 @@ class AlgorithmManagerTest : public CxxTest::TestSuite
}
}

void test_newestInstanceOf()
{
auto& am = AlgorithmManager::Instance();
am.clear();
auto first = am.create("AlgTest");
TS_ASSERT_EQUALS( am.newestInstanceOf("AlgTest"), first );
auto second = am.create("AlgTest");
TS_ASSERT_EQUALS( am.newestInstanceOf("AlgTest"), second );
TS_ASSERT( !am.newestInstanceOf("AlgTestSecond") );
// Create a different algorithm
am.create("AlgTestSecond");
// Make sure we still get back the latest instance of other algorithm
TS_ASSERT_EQUALS( am.newestInstanceOf("AlgTest"), second );
}

void test_runningInstancesOf()
{
AlgorithmManager::Instance().clear();
Expand Down

0 comments on commit 5357765

Please sign in to comment.