-
Notifications
You must be signed in to change notification settings - Fork 122
/
AnalysisDataService.cpp
87 lines (73 loc) · 3.4 KB
/
AnalysisDataService.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "MantidPythonInterface/kernel/DataServiceExporter.h"
#include "MantidPythonInterface/kernel/Registry/DowncastRegistry.h"
#include "MantidPythonInterface/kernel/TrackingInstanceMethod.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/Workspace.h"
#include <boost/python/call_method.hpp>
#include <boost/python/return_value_policy.hpp>
#include <boost/python/reference_existing_object.hpp>
using namespace Mantid::API;
using Mantid::PythonInterface::DataServiceExporter;
using Mantid::PythonInterface::TrackingInstanceMethod;
using namespace Mantid::PythonInterface::Registry;
using namespace boost::python;
namespace
{
/**
* Add an item into the ADS, if it exists then an error is raised
* @param self A reference to the calling object
* @param name The name to assign to this in the service
* @param item A boost.python wrapped SvcHeldType object
*/
void addItem(AnalysisDataServiceImpl& self, const std::string & name, const boost::python::object& item)
{
const auto & entry = DowncastRegistry::retrieve(call_method<std::string>(item.ptr(), "id"));
try
{
// It is VERY important that the extract type be a reference to SvcHeldType so that
// boost.python doesn't create a new shared_ptr and instead simply extracts the embedded one.
self.add(name, boost::dynamic_pointer_cast<Workspace>(entry.fromPythonAsSharedPtr(item)));
}
catch(std::exception& exc)
{
PyErr_SetString(PyExc_RuntimeError, exc.what()); // traditionally throws RuntimeError so don't break scripts
throw boost::python::error_already_set();
}
}
/**
* Add or replace an item into the service, if it exists then an error is raised
* @param self A reference to the calling object
* @param name The name to assign to this in the service
* @param item A boost.python wrapped SvcHeldType object
*/
void addOrReplaceItem(AnalysisDataServiceImpl& self, const std::string & name,
const boost::python::object& item)
{
const auto & entry = DowncastRegistry::retrieve(call_method<std::string>(item.ptr(), "id"));
try
{
// It is VERY important that the extract type be a reference to SvcHeldType so that
// boost.python doesn't create a new shared_ptr and instead simply extracts the embedded one.
self.addOrReplace(name, boost::dynamic_pointer_cast<Workspace>(entry.fromPythonAsSharedPtr(item)));
}
catch(std::exception& exc)
{
PyErr_SetString(PyExc_RuntimeError, exc.what()); // traditionally throws RuntimeError so don't break scripts
throw boost::python::error_already_set();
}
}
}
void export_AnalysisDataService()
{
typedef DataServiceExporter<AnalysisDataServiceImpl, Workspace_sptr> ADSExporter;
auto pythonClass = ADSExporter::define("AnalysisDataServiceImpl");
// -- special ADS behaviour --
// replace the add/addOrReplace,__setitem__ methods as we need to exact the exact stored type
pythonClass.def("add", &addItem,
"Adds the given object to the service with the given name. If the name/object exists it will raise an error.");
pythonClass.def("addOrReplace", &addOrReplaceItem,
"Adds the given object to the service with the given name. The the name exists the object is replaced.");
pythonClass.def("__setitem__", &addOrReplaceItem);
// Instance method
TrackingInstanceMethod<AnalysisDataService, typename ADSExporter::PythonType>::define(pythonClass);
}