AnalysisDataService.h 8.9 KB
Newer Older
1
2
3
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2007 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 +
7
#pragma once
8
9
10
11

//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
12
#include "MantidAPI/DllConfig.h"
13
#include "MantidAPI/Workspace.h"
LamarMoore's avatar
LamarMoore committed
14
15
#include "MantidKernel/DataService.h"
#include "MantidKernel/SingletonHolder.h"
16
17
18

#include <Poco/AutoPtr.h>

19
namespace Mantid {
20

21
namespace API {
22

23
24
25
26
27
28
//----------------------------------------------------------------------
// Forward declaration
//----------------------------------------------------------------------

class WorkspaceGroup;

29
/** The Analysis data service stores instances of the Workspace objects and
30
    anything that derives from template class
31
32
    DynamicFactory<Mantid::Kernel::IAlgorithm>. This is the primary data service
    that the users will interact with either through writing scripts or directly
33
34
35
36
37
38
39
40
41
42
    through the API. It is implemented as a singleton class.

    This is the manager/owner of Workspace* when registered.

    @author Russell Taylor, Tessella Support Services plc
    @date 01/10/2007
    @author L C Chapon, ISIS, Rutherford Appleton Laboratory

    Modified to inherit from DataService
*/
43
class MANTID_API_DLL AnalysisDataServiceImpl final
44
45
    : public Kernel::DataService<API::Workspace> {
public:
46
47
48
  /** @name Extra notifications only applicable to the ADS */
  //@{
  /// GroupWorkspaces notification is send from GroupWorkspaces algorithm
49
  class GroupWorkspacesNotification : public DataServiceNotification {
50
51
  public:
    /// Constructor
52
53
54
    GroupWorkspacesNotification(const std::vector<std::string> &wsnames)
        : DataServiceNotification("", boost::shared_ptr<API::Workspace>()),
          m_wsnames(wsnames) {}
55
    /// returns the workspace names
56
57
58
59
    const std::vector<std::string> &inputworkspacenames() const {
      return m_wsnames;
    }

60
  private:
61
62
63
64
    std::vector<std::string> m_wsnames; ///< cache of ws names
  };

  /// UnGroupingWorkspace notification is sent from UnGroupWorkspace algorithm
65
  /// before the WorkspaceGroup is removed from the DataService
66
67
68
69
70
71
72
73
  class UnGroupingWorkspaceNotification : public DataServiceNotification {
  public:
    /// Constructor
    UnGroupingWorkspaceNotification(const std::string &name,
                                    const boost::shared_ptr<Workspace> &obj)
        : DataServiceNotification(name, obj) {}
  };

74
75
  /// GroupWorkspaces notification is send when a group is updated by adding
  /// or removing members.
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
  /// Disable observing the ADS by a group
  /// (WorkspaceGroup::observeADSNotifications(false))
  /// to prevent sending this notification.
  class GroupUpdatedNotification : public DataServiceNotification {
  public:
    /// Constructor
    GroupUpdatedNotification(const std::string &name);
    /// Returns the workspace pointer cast to WorkspaceGroup
    boost::shared_ptr<const WorkspaceGroup> getWorkspaceGroup() const;
  };

  //@}

public:
  /// Return the list of illegal characters as one string
  const std::string &illegalCharacters() const;
92
  /// Set the list of illegal characters
93
94
95
  void setIllegalCharacterList(const std::string &);
  /// Is the given name a valid name for an object in the ADS
  const std::string isValid(const std::string &name) const;
96
97
  /// Overridden add member to attach the name to the workspace when a
  /// workspace object is added to the service
98
99
  void add(const std::string &name,
           const boost::shared_ptr<API::Workspace> &workspace) override;
100
101
  /// Overridden addOrReplace member to attach the name to the workspace when
  /// a workspace object is added to the service
102
103
104
  void
  addOrReplace(const std::string &name,
               const boost::shared_ptr<API::Workspace> &workspace) override;
105
106
107
108
109
110
111
112
113
  /// Overridden rename member to attach the new name to the workspace when a
  /// workspace object is renamed
  virtual void rename(const std::string &oldName, const std::string &newName);
  /// Overridden remove member to delete its name held by the workspace itself
  virtual void remove(const std::string &name);

  /** Retrieve a workspace and cast it to the given WSTYPE
   *
   * @param name :: name of the workspace
114
115
   * @tparam WSTYPE :: type of workspace to cast to. Should sub-class
   * Workspace
116
117
118
119
120
   * @return a shared pointer of WSTYPE
   */
  template <typename WSTYPE>
  boost::shared_ptr<WSTYPE> retrieveWS(const std::string &name) const {
    // Get as a bare workspace
121
122
    try {
      // Cast to the desired type and return that.
123
124
      return boost::dynamic_pointer_cast<WSTYPE>(
          Kernel::DataService<API::Workspace>::retrieve(name));
125
126

    } catch (Kernel::Exception::NotFoundError &) {
127
      throw;
128
    }
129
130
  }

131
132
133
134
  std::vector<Workspace_sptr>
  retrieveWorkspaces(const std::vector<std::string> &names,
                     bool unrollGroups = false) const;

135
136
  /** @name Methods to work with workspace groups */
  //@{
137
  void sortGroupByName(const std::string &groupName);
138
139
140
141
142
143
144
  void addToGroup(const std::string &groupName, const std::string &wsName);
  void deepRemoveGroup(const std::string &name);
  void removeFromGroup(const std::string &groupName, const std::string &wsName);
  //@}

  /// Return a lookup of the top level items
  std::map<std::string, Workspace_sptr> topLevelItems() const;
145
  void shutdown() override;
Nick Draper's avatar
Nick Draper committed
146

147
private:
148
  /// Checks the name is valid, throwing if not
149
150
  void verifyName(const std::string &name,
                  const boost::shared_ptr<API::WorkspaceGroup> &workspace);
151

152
  friend struct Mantid::Kernel::CreateUsingNew<AnalysisDataServiceImpl>;
153
  /// Constructor
154
155
  AnalysisDataServiceImpl();
  /// Private, unimplemented copy constructor
156
  AnalysisDataServiceImpl(const AnalysisDataServiceImpl &) = delete;
157
  /// Private, unimplemented copy assignment operator
158
  AnalysisDataServiceImpl &operator=(const AnalysisDataServiceImpl &) = delete;
159
  /// Private destructor
160
  ~AnalysisDataServiceImpl() override = default;
161
162
163

  /// The string of illegal characters
  std::string m_illegalChars;
164
165
};

166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using AnalysisDataService =
    Mantid::Kernel::SingletonHolder<AnalysisDataServiceImpl>;

using WorkspaceAddNotification =
    Mantid::Kernel::DataService<Mantid::API::Workspace>::AddNotification;
using WorkspaceAddNotification_ptr = const Poco::AutoPtr<
    Mantid::Kernel::DataService<Mantid::API::Workspace>::AddNotification> &;

using WorkspaceBeforeReplaceNotification = Mantid::Kernel::DataService<
    Mantid::API::Workspace>::BeforeReplaceNotification;
using WorkspaceBeforeReplaceNotification_ptr =
    const Poco::AutoPtr<Mantid::Kernel::DataService<
        Mantid::API::Workspace>::BeforeReplaceNotification> &;

using WorkspaceAfterReplaceNotification = Mantid::Kernel::DataService<
    Mantid::API::Workspace>::AfterReplaceNotification;
using WorkspaceAfterReplaceNotification_ptr =
    const Poco::AutoPtr<Mantid::Kernel::DataService<
        Mantid::API::Workspace>::AfterReplaceNotification> &;

using WorkspacePreDeleteNotification =
    Mantid::Kernel::DataService<Mantid::API::Workspace>::PreDeleteNotification;
LamarMoore's avatar
LamarMoore committed
188
189
190
using WorkspacePreDeleteNotification_ptr = const Poco::AutoPtr<
    Mantid::Kernel::DataService<Mantid::API::Workspace>::PreDeleteNotification>
    &;
191
192
193

using WorkspacePostDeleteNotification =
    Mantid::Kernel::DataService<Mantid::API::Workspace>::PostDeleteNotification;
LamarMoore's avatar
LamarMoore committed
194
195
196
using WorkspacePostDeleteNotification_ptr = const Poco::AutoPtr<
    Mantid::Kernel::DataService<Mantid::API::Workspace>::PostDeleteNotification>
    &;
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221

using ClearADSNotification =
    Mantid::Kernel::DataService<Mantid::API::Workspace>::ClearNotification;
using ClearADSNotification_ptr = const Poco::AutoPtr<
    Mantid::Kernel::DataService<Mantid::API::Workspace>::ClearNotification> &;

using WorkspaceRenameNotification =
    Mantid::Kernel::DataService<Mantid::API::Workspace>::RenameNotification;
using WorkspaceRenameNotification_ptr = const Poco::AutoPtr<
    Mantid::Kernel::DataService<Mantid::API::Workspace>::RenameNotification> &;

using WorkspacesGroupedNotification =
    AnalysisDataServiceImpl::GroupWorkspacesNotification;
using WorkspacesGroupedNotification_ptr =
    const Poco::AutoPtr<AnalysisDataServiceImpl::GroupWorkspacesNotification> &;

using WorkspaceUnGroupingNotification =
    AnalysisDataServiceImpl::UnGroupingWorkspaceNotification;
using WorkspaceUnGroupingNotification_ptr = const Poco::AutoPtr<
    AnalysisDataServiceImpl::UnGroupingWorkspaceNotification> &;

using GroupUpdatedNotification =
    AnalysisDataServiceImpl::GroupUpdatedNotification;
using GroupUpdatedNotification_ptr =
    const Poco::AutoPtr<AnalysisDataServiceImpl::GroupUpdatedNotification> &;
222
223
224
225

} // Namespace API
} // Namespace Mantid

226
227
namespace Mantid {
namespace Kernel {
228
229
EXTERN_MANTID_API template class MANTID_API_DLL
    Mantid::Kernel::SingletonHolder<Mantid::API::AnalysisDataServiceImpl>;
230
}
LamarMoore's avatar
LamarMoore committed
231
} // namespace Mantid