-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs #8534. Merge remote-tracking branch 'origin/master' into feature…
…/8534_muon_new_workflow_alg
- Loading branch information
Showing
30 changed files
with
971 additions
and
651 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#ifndef MANTID_API_SCOPEDWORKSPACE_H_ | ||
#define MANTID_API_SCOPEDWORKSPACE_H_ | ||
|
||
#include <string> | ||
|
||
#include "MantidKernel/ClassMacros.h" | ||
#include "MantidKernel/System.h" | ||
#include "MantidAPI/Workspace.h" | ||
|
||
namespace Mantid | ||
{ | ||
namespace API | ||
{ | ||
/** ScopedWorkspace : scoped workspace ADS entry. | ||
This class is provided for situations when you need a workspace to be in the ADS to run an | ||
algorithm, but you don't really need to keep it there after the algorithm has finished. In | ||
these circumstances you can create ScopedWorkspace, set it's name as a workspace property | ||
for the algorithm and retrieve it when algorithm has finished. The workspace will be | ||
removed from the ADS when the object goes out of scope, or exception is thrown. | ||
Primarily, it was created to overcome some limitations of WorkspaceProperties, but it can be | ||
useful in other places, e.g. tests. | ||
Copyright © 2013 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory | ||
This file is part of Mantid. | ||
Mantid is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 3 of the License, or | ||
(at your option) any later version. | ||
Mantid is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
File change history is stored at: <https://github.com/mantidproject/mantid> | ||
Code Documentation is available at: <http://doxygen.mantidproject.org> | ||
*/ | ||
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; } | ||
|
||
/// 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); | ||
|
||
/// ADS name of the workspace | ||
const 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; | ||
}; | ||
|
||
|
||
} // namespace API | ||
} // namespace Mantid | ||
|
||
#endif /* MANTID_API_SCOPEDWORKSPACE_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#include "MantidAPI/ScopedWorkspace.h" | ||
#include "MantidAPI/AnalysisDataService.h" | ||
#include "MantidAPI/WorkspaceGroup.h" | ||
|
||
namespace Mantid | ||
{ | ||
namespace API | ||
{ | ||
|
||
const size_t ScopedWorkspace::NAME_LENGTH = 16; | ||
|
||
//---------------------------------------------------------------------------------------------- | ||
/** | ||
* Empty constructor | ||
*/ | ||
ScopedWorkspace::ScopedWorkspace() : | ||
m_name( generateUniqueName() ) | ||
{} | ||
|
||
/** | ||
* Workspace constructor | ||
*/ | ||
ScopedWorkspace::ScopedWorkspace(Workspace_sptr ws) : | ||
m_name( generateUniqueName() ) | ||
{ | ||
set(ws); | ||
} | ||
|
||
//---------------------------------------------------------------------------------------------- | ||
/** | ||
* Destructor | ||
*/ | ||
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); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 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 | ||
* under the name. | ||
*/ | ||
Workspace_sptr ScopedWorkspace::retrieve() const | ||
{ | ||
AnalysisDataServiceImpl& ads = AnalysisDataService::Instance(); | ||
|
||
if ( ads.doesExist(m_name) ) | ||
{ | ||
return ads.retrieveWS<Workspace>(m_name); | ||
} | ||
|
||
return Workspace_sptr(); | ||
} | ||
|
||
/** | ||
* Make ADS entry to point to the given workspace. | ||
*/ | ||
void ScopedWorkspace::set(Workspace_sptr newWS) | ||
{ | ||
AnalysisDataServiceImpl& ads = AnalysisDataService::Instance(); | ||
|
||
if ( ! newWS->name().empty() && ads.doesExist( newWS->name() ) ) | ||
throw std::invalid_argument( "Workspace is already in the ADS under the name " + newWS->name() ); | ||
|
||
ads.add(m_name, newWS); | ||
} | ||
|
||
/** | ||
* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.