ScopedWorkspace.cpp 3.33 KB
Newer Older
1
2
3
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4
5
//   NScD Oak Ridge National Laboratory, European Spallation Source,
//   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6
// SPDX - License - Identifier: GPL - 3.0 +
David Fairbrother's avatar
David Fairbrother committed
7
8
#include <utility>

9
#include "MantidAPI/AnalysisDataService.h"
David Fairbrother's avatar
David Fairbrother committed
10
#include "MantidAPI/ScopedWorkspace.h"
11
#include "MantidAPI/WorkspaceGroup.h"
12

13
14
15
16
17
18
19
20
21
22
23
24
25
26
namespace Mantid {
namespace API {

const size_t ScopedWorkspace::NAME_LENGTH = 16;

//----------------------------------------------------------------------------------------------
/**
 * Empty constructor
 */
ScopedWorkspace::ScopedWorkspace() : m_name(generateUniqueName()) {}

/**
 * Workspace constructor
 */
David Fairbrother's avatar
David Fairbrother committed
27
ScopedWorkspace::ScopedWorkspace(const Workspace_sptr &ws)
28
    : m_name(generateUniqueName()) {
David Fairbrother's avatar
David Fairbrother committed
29
  set(std::move(ws));
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
}

//----------------------------------------------------------------------------------------------
/**
 * Destructor
 */
ScopedWorkspace::~ScopedWorkspace() { remove(); }

/**
 * Operator for conversion to boolean. Returns true if workspace was created for
 * the
 * name and it is not null workspace.
 */
ScopedWorkspace::operator bool() const {
  return AnalysisDataService::Instance().doesExist(m_name);
}

/**
 * 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);
56
  }
57

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  return Workspace_sptr();
}

/**
 * Removes the workspace entry from the ADS.
 */
void ScopedWorkspace::remove() {
  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);
75
76
    }
  }
77
78
79
80
81
}

/**
 * Make ADS entry to point to the given workspace.
 */
David Fairbrother's avatar
David Fairbrother committed
82
void ScopedWorkspace::set(const Workspace_sptr &newWS) {
83
84
  AnalysisDataServiceImpl &ads = AnalysisDataService::Instance();

85
  if (!newWS->getName().empty() && ads.doesExist(newWS->getName()))
86
    throw std::invalid_argument(
87
        "Workspace is already in the ADS under the name " + newWS->getName());
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

  // Remove previous workspace entry
  remove();

  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]);
123
  }
124

125
126
  return result;
}
127

128
129
} // namespace API
} // namespace Mantid