Mantid
Loading...
Searching...
No Matches
ScopedWorkspace.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#include <utility>
8
13
15
16namespace Mantid::API {
17
18const size_t ScopedWorkspace::NAME_LENGTH = 16;
19
20//----------------------------------------------------------------------------------------------
24ScopedWorkspace::ScopedWorkspace() : m_name(generateUniqueName()) {}
25
29ScopedWorkspace::ScopedWorkspace(const Workspace_sptr &ws) : m_name(generateUniqueName()) { set(ws); }
30
31//----------------------------------------------------------------------------------------------
36
42ScopedWorkspace::operator bool() const { return AnalysisDataService::Instance().doesExist(m_name); }
43
49 AnalysisDataServiceImpl const &ads = AnalysisDataService::Instance();
50
51 if (ads.doesExist(m_name)) {
52 return ads.retrieveWS<Workspace>(m_name);
53 }
54
55 return Workspace_sptr();
56}
57
62 AnalysisDataServiceImpl &ads = AnalysisDataService::Instance();
63
64 // When destructed, remove workspace from the ADS if was added and still
65 // exists
66 if (ads.doesExist(m_name)) {
68 // If is a group, need to remove all the members as well
70 } else {
71 ads.remove(m_name);
72 }
73 }
74}
75
80 AnalysisDataServiceImpl &ads = AnalysisDataService::Instance();
81
82 if (!newWS->getName().empty() && ads.doesExist(newWS->getName()))
83 throw std::invalid_argument("Workspace is already in the ADS under the name " + newWS->getName());
84
85 // Remove previous workspace entry
86 remove();
87
88 ads.add(m_name, newWS);
89}
90
95 std::string newName;
96
97 do {
98 newName = "__ScopedWorkspace_" + randomString(NAME_LENGTH);
99 } while (AnalysisDataService::Instance().doesExist(newName));
100
101 return newName;
102}
103} // namespace Mantid::API
The Analysis data service stores instances of the Workspace objects and anything that derives from te...
void add(const std::string &name, const std::shared_ptr< API::Workspace > &workspace) override
Overridden add member to attach the name to the workspace when a workspace object is added to the ser...
void deepRemoveGroup(const std::string &name)
Remove a workspace group and all its members from the ADS.
virtual Workspace_sptr remove(const std::string &name)
Overridden remove member to delete its name held by the workspace itself.
std::shared_ptr< WSTYPE > retrieveWS(const std::string &name) const
Retrieve a workspace and cast it to the given WSTYPE.
void set(const Workspace_sptr &newWS)
Make ADS entry to point to the given workspace.
static std::string generateUniqueName()
Generates a tricky name which is unique within ADS.
virtual ~ScopedWorkspace()
Destructor.
Workspace_sptr retrieve() const
Retrieve workspace from the ADS.
ScopedWorkspace()
Empty constructor.
void remove()
Removes the workspace entry from the ADS.
const std::string m_name
ADS name of the workspace.
static const size_t NAME_LENGTH
Length of workspace names generated.
Class to hold a set of workspaces.
Base Workspace Abstract Class.
Definition Workspace.h:29
bool doesExist(const std::string &name) const
Check to see if a data object exists in the store.
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
MANTID_KERNEL_DLL std::string randomString(const size_t len)
Generates random alpha-numeric string.
Definition Strings.cpp:1190