Skip to content

Commit

Permalink
Refs #8550. ScopedWorkspace: explicit remove method.
Browse files Browse the repository at this point in the history
Plus fixed setting logic and test for it.
  • Loading branch information
arturbekasov committed Dec 9, 2013
1 parent 8b16f3c commit 50a5b02
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/API/inc/MantidAPI/ScopedWorkspace.h
Expand Up @@ -60,6 +60,9 @@ namespace API
/// Retrieve workspace from the ADS
Workspace_sptr retrieve() const;

/// Removes the workspace entry from the ADS
void remove();

/// Operator for conversion to boolean
operator bool() const;

Expand Down
41 changes: 26 additions & 15 deletions Code/Mantid/Framework/API/src/ScopedWorkspace.cpp
Expand Up @@ -32,21 +32,7 @@ namespace API
*/
ScopedWorkspace::~ScopedWorkspace()
{
AnalysisDataServiceImpl& ads = AnalysisDataService::Instance();

// When destructed, remove workspace from the ADS if was added and still exists
if ( ads.doesExist(m_name) )
{
if ( ads.retrieveWS<WorkspaceGroup>(m_name) )
{
// If is a group, need to remove all the members as well
ads.deepRemoveGroup(m_name);
}
else
{
ads.remove(m_name);
}
}
remove();
}

/**
Expand All @@ -73,6 +59,28 @@ namespace API

return Workspace_sptr();
}

/**
* Removes the workspace entry from the ADS.
*/
void ScopedWorkspace::remove()
{
AnalysisDataServiceImpl& ads = AnalysisDataService::Instance();

// When destructed, remove workspace from the ADS if was added and still exists
if ( ads.doesExist(m_name) )
{
if ( ads.retrieveWS<WorkspaceGroup>(m_name) )
{
// If is a group, need to remove all the members as well
ads.deepRemoveGroup(m_name);
}
else
{
ads.remove(m_name);
}
}
}

/**
* Make ADS entry to point to the given workspace.
Expand All @@ -84,6 +92,9 @@ namespace API
if ( ! newWS->name().empty() && ads.doesExist( newWS->name() ) )
throw std::invalid_argument( "Workspace is already in the ADS under the name " + newWS->name() );

// Remove previous workspace entry
remove();

ads.add(m_name, newWS);
}

Expand Down
17 changes: 17 additions & 0 deletions Code/Mantid/Framework/API/test/ScopedWorkspaceTest.h
Expand Up @@ -161,6 +161,23 @@ class ScopedWorkspaceTest : public CxxTest::TestSuite
TS_ASSERT( test );
}

void test_settingTwice()
{
ScopedWorkspace test;

MockWorkspace_sptr ws1 = MockWorkspace_sptr(new MockWorkspace);
test.set(ws1);

TS_ASSERT_EQUALS( ws1->name(), test.name() );

MockWorkspace_sptr ws2 = MockWorkspace_sptr(new MockWorkspace);
test.set(ws2);

TS_ASSERT_EQUALS( ws2->name(), test.name() );
TS_ASSERT( ws1->name().empty() );
TS_ASSERT_EQUALS( m_ads.getObjectNamesInclHidden().size(), 1 );
}

private:
AnalysisDataServiceImpl& m_ads;
};
Expand Down

0 comments on commit 50a5b02

Please sign in to comment.