Mantid
Loading...
Searching...
No Matches
AnalysisDataService.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 +
14
15#include <boost/python/enum.hpp>
16
17#include <boost/python/list.hpp>
18#include <boost/python/overloads.hpp>
19#include <boost/python/return_value_policy.hpp>
20
21using namespace Mantid::API;
22using namespace Mantid::Kernel;
23using namespace Mantid::PythonInterface;
24using namespace boost::python;
25
27
28namespace {
29std::once_flag INIT_FLAG;
30
37AnalysisDataServiceImpl &instance() {
38 // start the framework (if necessary)
40 std::call_once(INIT_FLAG, []() {
41 PyRun_SimpleString("import atexit\n"
42 "from mantid.api import AnalysisDataService\n"
43 "atexit.register(lambda: AnalysisDataService.clear())");
44 });
45 return ads;
46}
47
55list retrieveWorkspaces(AnalysisDataServiceImpl const *const self, const list &names, bool unrollGroups = false) {
56 using WeakPtr = std::weak_ptr<Workspace>;
57 const auto wsSharedPtrs =
59 std::vector<WeakPtr> wsWeakPtrs;
60 wsWeakPtrs.reserve(wsSharedPtrs.size());
61 std::transform(wsSharedPtrs.cbegin(), wsSharedPtrs.cend(), std::back_inserter(wsWeakPtrs),
62 [](const Workspace_sptr &wksp) -> WeakPtr { return WeakPtr(wksp); });
63 return Converters::ToPyList<WeakPtr>()(wsWeakPtrs);
64}
65
66GNU_DIAG_OFF("unused-local-typedef")
67// Ignore -Wconversion warnings coming from boost::python
68// Seen with GCC 7.1.1 and Boost 1.63.0
69GNU_DIAG_OFF("conversion")
70BOOST_PYTHON_FUNCTION_OVERLOADS(AdsRetrieveWorkspacesOverloads, retrieveWorkspaces, 2, 3)
71GNU_DIAG_ON("conversion")
72GNU_DIAG_ON("unused-local-typedef")
73} // namespace
74
77 auto pythonClass = ADSExporter::define("AnalysisDataServiceImpl");
78 pythonClass
79 .def("Instance", instance, return_value_policy<reference_existing_object>(),
80 "Return a reference to the singleton instance")
81 .staticmethod("Instance")
82 .def("retrieveWorkspaces", retrieveWorkspaces,
83 AdsRetrieveWorkspacesOverloads("Retrieve a list of workspaces by name",
84 (arg("self"), arg("names"), arg("unrollGroups") = false)))
85 .def("addToGroup", &AnalysisDataServiceImpl::addToGroup, (arg("groupName"), arg("wsName")),
86 "Add a workspace in the ADS to a group in the ADS")
87 .def("removeFromGroup", &AnalysisDataServiceImpl::removeFromGroup, (arg("groupName"), arg("wsName")),
88 "Remove a workspace from a group in the ADS");
89}
#define GET_POINTER_SPECIALIZATION(TYPE)
Definition: GetPointer.h:17
void export_AnalysisDataService()
#define GNU_DIAG_ON(x)
#define GNU_DIAG_OFF(x)
This is a collection of macros for turning compiler warnings off in a controlled manner.
The Analysis data service stores instances of the Workspace objects and anything that derives from te...
void addToGroup(const std::string &groupName, const std::string &wsName)
Add a workspace to a group.
std::vector< Workspace_sptr > retrieveWorkspaces(const std::vector< std::string > &names, bool unrollGroups=false) const
Given a list of names retrieve the corresponding workspace handles.
void removeFromGroup(const std::string &groupName, const std::string &wsName)
Remove a workspace from a group but not from the ADS.
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
Converts a Python sequence type to a C++ std::vector, where the element type is defined by the templa...
Converter that takes a std::vector and converts it into a python list.
Definition: ToPyList.h:22
A helper struct to export templated DataService<> types to Python.