Skip to content

Commit

Permalink
Refs #8506. ScopedWorkspace: unique name generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
arturbekasov committed Nov 28, 2013
1 parent 7025158 commit 70735aa
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
20 changes: 18 additions & 2 deletions Code/Mantid/Framework/API/inc/MantidAPI/ScopedWorkspace.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#ifndef MANTID_API_SCOPEDWORKSPACE_H_
#define MANTID_API_SCOPEDWORKSPACE_H_

#include "MantidKernel/System.h"
#include <string>

#include "MantidKernel/System.h"

namespace Mantid
{
Expand Down Expand Up @@ -36,7 +37,22 @@ namespace API
public:
ScopedWorkspace();
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;

/// Generates a tricky name which is unique within ADS
static std::string generateUniqueName();

/// Generates a random alpha-numeric string
static std::string randomString(size_t len);

/// Length of workspace names generated
static const size_t NAME_LENGTH;
};


Expand Down
46 changes: 45 additions & 1 deletion Code/Mantid/Framework/API/src/ScopedWorkspace.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
#include "MantidAPI/ScopedWorkspace.h"
#include "MantidAPI/AnalysisDataService.h"

namespace Mantid
{
namespace API
{


const size_t ScopedWorkspace::NAME_LENGTH = 16;

//----------------------------------------------------------------------------------------------
/**
* Constructor
*/
ScopedWorkspace::ScopedWorkspace()
{
// Randomize
srand( static_cast<unsigned int>( time(0) ) );

m_name = generateUniqueName();
}

//----------------------------------------------------------------------------------------------
Expand All @@ -22,5 +28,43 @@ namespace API
{
}

/**
* Generates a tricky name which is unique within ADS.
*/
std::string ScopedWorkspace::generateUniqueName()
{
std::string newName;

do
{
// __ makes it hidden in the MantidPlot
newName = "__ScopedWorkspace_" + randomString(NAME_LENGTH);
}
while( AnalysisDataService::Instance().doesExist(newName) );

return newName;
}

/**
* Generates random alpha-numeric string.
* @param len :: Length of the string
* @return Random string of the given length
*/
std::string ScopedWorkspace::randomString(size_t len)
{
static const std::string alphabet = "0123456789abcdefghijklmnopqrstuvwxyz";

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

while(result.size() != len)
{
size_t randPos = ( (rand() % (alphabet.size() - 1)));
result.push_back(alphabet[randPos]);
}

return result;
}

} // namespace API
} // namespace Mantid
12 changes: 12 additions & 0 deletions Code/Mantid/Framework/API/test/ScopedWorkspaceTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "MantidAPI/ScopedWorkspace.h"
#include "MantidAPI/AnalysisDataService.h"

#include <boost/algorithm/string/predicate.hpp>

using namespace Mantid::API;

class ScopedWorkspaceTest : public CxxTest::TestSuite
Expand Down Expand Up @@ -36,6 +38,16 @@ class ScopedWorkspaceTest : public CxxTest::TestSuite
TS_ASSERT( ! ads.doesExist( test.name() ) );
}

void test_name()
{
ScopedWorkspace test;

const std::string prefix("__ScopedWorkspace_");

TS_ASSERT( boost::starts_with(test.name(), prefix) );
TS_ASSERT_EQUALS( test.name().size(), prefix.size() + 16 );
}

private:
AnalysisDataServiceImpl& ads;
};
Expand Down

0 comments on commit 70735aa

Please sign in to comment.