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
12
13namespace Mantid::API {
14
15const size_t ScopedWorkspace::NAME_LENGTH = 16;
16
17//----------------------------------------------------------------------------------------------
21ScopedWorkspace::ScopedWorkspace() : m_name(generateUniqueName()) {}
22
26ScopedWorkspace::ScopedWorkspace(const Workspace_sptr &ws) : m_name(generateUniqueName()) { set(ws); }
27
28//----------------------------------------------------------------------------------------------
33
39ScopedWorkspace::operator bool() const { return AnalysisDataService::Instance().doesExist(m_name); }
40
47
48 if (ads.doesExist(m_name)) {
49 return ads.retrieveWS<Workspace>(m_name);
50 }
51
52 return Workspace_sptr();
53}
54
60
61 // When destructed, remove workspace from the ADS if was added and still
62 // exists
63 if (ads.doesExist(m_name)) {
65 // If is a group, need to remove all the members as well
67 } else {
68 ads.remove(m_name);
69 }
70 }
71}
72
78
79 if (!newWS->getName().empty() && ads.doesExist(newWS->getName()))
80 throw std::invalid_argument("Workspace is already in the ADS under the name " + newWS->getName());
81
82 // Remove previous workspace entry
83 remove();
84
85 ads.add(m_name, newWS);
86}
87
92 std::string newName;
93
94 do {
95 // __ makes it hidden in the MantidPlot
96 newName = "__ScopedWorkspace_" + randomString(NAME_LENGTH);
97 } while (AnalysisDataService::Instance().doesExist(newName));
98
99 return newName;
100}
101
107std::string ScopedWorkspace::randomString(size_t len) {
108 static const std::string alphabet = "0123456789abcdefghijklmnopqrstuvwxyz";
109
110 std::string result;
111 result.reserve(len);
112
113 while (result.size() != len) {
114 size_t randPos = ((rand() % (alphabet.size() - 1)));
115 result.push_back(alphabet[randPos]);
116 }
117
118 return result;
119}
120
121} // 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 void 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.
static std::string randomString(size_t len)
Generates a random alpha-numeric string.
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:30
bool doesExist(const std::string &name) const
Check to see if a data object exists in the store.
Definition: DataService.h:370
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20