Skip to content

Commit

Permalink
Refs #8506. ScopedWorkspace: workspace constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
arturbekasov committed Nov 28, 2013
1 parent d0c7236 commit 1428880
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
9 changes: 8 additions & 1 deletion Code/Mantid/Framework/API/inc/MantidAPI/ScopedWorkspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string>

#include "MantidKernel/System.h"
#include "MantidAPI/Workspace.h"

namespace Mantid
{
Expand Down Expand Up @@ -35,15 +36,21 @@ namespace API
class DLLExport ScopedWorkspace
{
public:
/// Empty constructor
ScopedWorkspace();

/// Workspace constructor
ScopedWorkspace(Workspace_sptr ws);

/// Destructor
virtual ~ScopedWorkspace();

/// Returns ADS name of the workspace
std::string name() const { return m_name; }

private:
/// ADS name of the workspace
std::string m_name;
const std::string m_name;

/// Generates a tricky name which is unique within ADS
static std::string generateUniqueName();
Expand Down
20 changes: 14 additions & 6 deletions Code/Mantid/Framework/API/src/ScopedWorkspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ namespace API

//----------------------------------------------------------------------------------------------
/**
* Constructor
* Empty constructor
*/
ScopedWorkspace::ScopedWorkspace()
{
// Randomize
srand( static_cast<unsigned int>( time(0) ) );
ScopedWorkspace::ScopedWorkspace() :
m_name( generateUniqueName() )
{}

m_name = generateUniqueName();
/**
* Workspace constructor
*/
ScopedWorkspace::ScopedWorkspace(Workspace_sptr ws) :
m_name( generateUniqueName() )
{
AnalysisDataService::Instance().add(m_name, ws);
}

//----------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -70,6 +75,9 @@ namespace API
{
static const std::string alphabet = "0123456789abcdefghijklmnopqrstuvwxyz";

// Randomize
srand( static_cast<unsigned int>( time(0) ) );

std::string result;
result.reserve(len);

Expand Down
12 changes: 10 additions & 2 deletions Code/Mantid/Framework/API/test/ScopedWorkspaceTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,20 @@ class ScopedWorkspaceTest : public CxxTest::TestSuite
void test_emptyConstructor()
{
ScopedWorkspace test;
// Should have name created

TS_ASSERT( ! test.name().empty() );
// However, nothing should be added under that name yet
TS_ASSERT( ! m_ads.doesExist( test.name() ) );
}

void test_workspaceConstructor()
{
MockWorkspace_sptr ws = MockWorkspace_sptr(new MockWorkspace);
ScopedWorkspace test(ws);

TS_ASSERT( ! test.name().empty() );
TS_ASSERT( m_ads.doesExist( test.name() ) );
}

void test_name()
{
ScopedWorkspace test;
Expand Down

0 comments on commit 1428880

Please sign in to comment.