Skip to content

Commit

Permalink
Refs #8506. ScopedWorkspace: boolean operator.
Browse files Browse the repository at this point in the history
  • Loading branch information
arturbekasov committed Nov 29, 2013
1 parent bc00fb8 commit 00f0f78
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Code/Mantid/Framework/API/inc/MantidAPI/ScopedWorkspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ namespace API
/// Retrieve workspace from the ADS
Workspace_sptr retrieve() const;

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

/// Make ADS entry to point to the given workspace
void set(Workspace_sptr newWS);

private:
DISABLE_COPY_AND_ASSIGN(ScopedWorkspace);

Expand Down
14 changes: 14 additions & 0 deletions Code/Mantid/Framework/API/src/ScopedWorkspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ namespace API
}
}
}

/**
* Operator for conversion to boolean. Returns true if workspace was created for the
* name and it is not null workspace.
*/
ScopedWorkspace::operator bool() const
{
AnalysisDataServiceImpl& ads = AnalysisDataService::Instance();

if ( ads.doesExist(m_name) )
return ads.retrieveWS<Workspace>(m_name);
else
return false;
}

/**
* Retrieve workspace from the ADS. Null pointer returned if nothing was added
Expand Down
11 changes: 11 additions & 0 deletions Code/Mantid/Framework/API/test/ScopedWorkspaceTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,17 @@ class ScopedWorkspaceTest : public CxxTest::TestSuite
TS_ASSERT_THROWS( ScopedWorkspace test2(ws), std::invalid_argument );
}

void test_boolConversion()
{
ScopedWorkspace test;

TS_ASSERT( ! test );

test.set(MockWorkspace_sptr(new MockWorkspace));

TS_ASSERT( test );
}

private:
AnalysisDataServiceImpl& m_ads;
};
Expand Down

0 comments on commit 00f0f78

Please sign in to comment.