From 2167ec8df527ce49d18319ad1c5391e160501678 Mon Sep 17 00:00:00 2001 From: Martyn Gigg Date: Mon, 25 May 2015 21:02:08 +0100 Subject: [PATCH] Checkpoint removing downcasting policies The tests still don't work yet. Refs #9807 --- .../kernel/DataServiceExporter.h | 21 ++-- .../kernel/Policies/DowncastingPolicies.h | 2 +- .../kernel/Registry/DataItemInterface.h | 5 - .../kernel/Registry/PropertyValueHandler.h | 3 +- .../Registry/TypedPropertyValueHandler.h | 20 +++- .../api/src/Exports/AnalysisDataService.cpp | 100 +++++++++--------- .../api/src/Exports/MatrixWorkspace.cpp | 73 ++++++------- .../Exports/PropertyManagerDataService.cpp | 1 + .../mantid/api/src/Exports/Workspace.cpp | 50 +++++---- .../api/src/Exports/WorkspaceFactory.cpp | 12 +-- .../mantid/dataobjects/CMakeLists.txt | 5 +- .../src/Exports/TableWorkspace.cpp | 19 ++++ .../dataobjects/src/Exports/Workspace2D.cpp | 7 ++ 13 files changed, 172 insertions(+), 146 deletions(-) create mode 100644 Code/Mantid/Framework/PythonInterface/mantid/dataobjects/src/Exports/TableWorkspace.cpp diff --git a/Code/Mantid/Framework/PythonInterface/inc/MantidPythonInterface/kernel/DataServiceExporter.h b/Code/Mantid/Framework/PythonInterface/inc/MantidPythonInterface/kernel/DataServiceExporter.h index 2390df8ce2b6..d47c36fb49b5 100644 --- a/Code/Mantid/Framework/PythonInterface/inc/MantidPythonInterface/kernel/DataServiceExporter.h +++ b/Code/Mantid/Framework/PythonInterface/inc/MantidPythonInterface/kernel/DataServiceExporter.h @@ -23,8 +23,8 @@ File change history is stored at: Code Documentation is available at: */ -#include "MantidPythonInterface/kernel/Policies/DowncastingPolicies.h" #include "MantidKernel/Exception.h" +#include "MantidPythonInterface/kernel/WeakPtr.h" #include #include @@ -38,9 +38,11 @@ namespace PythonInterface { * @tparam SvcType Type of DataService to export * @tparam SvcHeldType The type held within the DataService map */ -template struct DataServiceExporter { - /// typedef the type created by boost.python +template +struct DataServiceExporter { + // typedef the type created by boost.python typedef boost::python::class_ PythonType; + typedef boost::weak_ptr WeakPtr; /** * Define the necessary boost.python framework to expor the templated @@ -60,7 +62,6 @@ template struct DataServiceExporter { static PythonType define(const char *pythonClassName) { using namespace boost::python; using namespace Mantid::Kernel; - namespace Policies = Mantid::PythonInterface::Policies; auto classType = PythonType(pythonClassName, no_init) @@ -73,7 +74,6 @@ template struct DataServiceExporter { .def("doesExist", &SvcType::doesExist, "Returns True if the object is found in the service.") .def("retrieve", &DataServiceExporter::retrieveOrKeyError, - return_value_policy(), "Retrieve the named object. Raises an exception if the name " "does not exist") .def("remove", &SvcType::remove, "Remove a named object") @@ -86,8 +86,7 @@ template struct DataServiceExporter { // Make it act like a dictionary .def("__len__", &SvcType::size) - .def("__getitem__", &DataServiceExporter::retrieveOrKeyError, - return_value_policy()) + .def("__getitem__", &DataServiceExporter::retrieveOrKeyError) .def("__setitem__", &SvcType::addOrReplace) .def("__contains__", &SvcType::doesExist) .def("__delitem__", &SvcType::remove); @@ -105,10 +104,10 @@ template struct DataServiceExporter { * @return A shared_ptr to the named object. If the name does not exist it * sets a KeyError error indicator. */ - static SvcHeldType retrieveOrKeyError(SvcType &self, - const std::string &name) { + static WeakPtr retrieveOrKeyError(SvcType &self, + const std::string &name) { using namespace Mantid::Kernel; - SvcHeldType item; + SvcPtrType item; try { item = self.retrieve(name); } catch (Exception::NotFoundError &) { @@ -117,7 +116,7 @@ template struct DataServiceExporter { PyErr_SetString(PyExc_KeyError, err.c_str()); throw boost::python::error_already_set(); } - return item; + return WeakPtr(item); } /** diff --git a/Code/Mantid/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Policies/DowncastingPolicies.h b/Code/Mantid/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Policies/DowncastingPolicies.h index 31817d765574..d13f07a7e0ab 100644 --- a/Code/Mantid/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Policies/DowncastingPolicies.h +++ b/Code/Mantid/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Policies/DowncastingPolicies.h @@ -1,7 +1,7 @@ #ifndef MANITD_PYTHONINTERFACE_TOWEAKPTRWITHDOWNCASTIMPL_H_ #define MANITD_PYTHONINTERFACE_TOWEAKPTRWITHDOWNCASTIMPL_H_ /** - Copyright © 2012 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge + Copyright © 2012 ISIS Rutherford Appleton Laboratory, NScD Oak Ridg National Laboratory & European Spallation Source This file is part of Mantid. diff --git a/Code/Mantid/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Registry/DataItemInterface.h b/Code/Mantid/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Registry/DataItemInterface.h index 1d3519d18b69..8250d812b784 100644 --- a/Code/Mantid/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Registry/DataItemInterface.h +++ b/Code/Mantid/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Registry/DataItemInterface.h @@ -24,7 +24,6 @@ */ #include "MantidPythonInterface/kernel/Registry/TypedPropertyValueHandler.h" #include "MantidPythonInterface/kernel/Registry/TypeRegistry.h" -#include "MantidPythonInterface/kernel/Registry/DowncastRegistry.h" #include namespace Mantid { @@ -37,9 +36,6 @@ namespace Registry { * - Calls register_ptr_to_python> * - Calls register_ptr_to_python> * - Registers a new PropertyValueHandler for a boost::shared_ptr - * - * ID strings can then be mapped to the template type by calling the - * insert method. */ template struct DLLExport DataItemInterface { typedef boost::shared_ptr IType_sptr; @@ -57,7 +53,6 @@ template struct DLLExport DataItemInterface { /// Register a downcast for this ID DataItemInterface &castFromID(const std::string &id) { using namespace Registry; - DowncastRegistry::subscribe(id); return *this; } }; diff --git a/Code/Mantid/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Registry/PropertyValueHandler.h b/Code/Mantid/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Registry/PropertyValueHandler.h index 1838e0a0996f..d9c68118ce78 100644 --- a/Code/Mantid/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Registry/PropertyValueHandler.h +++ b/Code/Mantid/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Registry/PropertyValueHandler.h @@ -29,6 +29,7 @@ namespace Mantid { namespace Kernel { // Forward declarations +class DataItem; class IPropertyManager; class Property; } @@ -43,7 +44,7 @@ namespace Registry { */ struct DLLExport PropertyValueHandler { /// Virtual Destructor - virtual ~PropertyValueHandler(){}; + virtual ~PropertyValueHandler() {}; /// Overload to set the named property's value on the property manager virtual void set(Kernel::IPropertyManager *alg, const std::string &name, const boost::python::object &value) const = 0; diff --git a/Code/Mantid/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Registry/TypedPropertyValueHandler.h b/Code/Mantid/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Registry/TypedPropertyValueHandler.h index 5ed554e79405..fbb6a7bfbb5f 100644 --- a/Code/Mantid/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Registry/TypedPropertyValueHandler.h +++ b/Code/Mantid/Framework/PythonInterface/inc/MantidPythonInterface/kernel/Registry/TypedPropertyValueHandler.h @@ -26,6 +26,7 @@ #include "MantidPythonInterface/kernel/Registry/DowncastRegistry.h" #include "MantidPythonInterface/kernel/IsNone.h" // includes object.hpp +#include "MantidAPI/Workspace.h" #include "MantidKernel/PropertyWithValue.h" #include "MantidKernel/IPropertyManager.h" @@ -109,12 +110,20 @@ struct DLLExport TypedPropertyValueHandler> void set(Kernel::IPropertyManager *alg, const std::string &name, const boost::python::object &value) const { using namespace boost::python; - using Registry::DowncastRegistry; + using Mantid::API::Workspace_sptr; + typedef boost::weak_ptr Workspace_wptr; - const auto &entry = - DowncastRegistry::retrieve(call_method(value.ptr(), "id")); - alg->setProperty(name, boost::dynamic_pointer_cast( - entry.fromPythonAsSharedPtr(value))); + Workspace_sptr p; + extract weakExtract(value); + extract sharedExtract(value); + if(weakExtract.check()) { + p = weakExtract().lock(); + } else if(sharedExtract.check()) { + p = sharedExtract(); + } else { + throw std::runtime_error("Unable to add unknown object type to ADS"); + } + alg->setProperty(name, boost::dynamic_pointer_cast(p)); } /** @@ -147,6 +156,7 @@ struct DLLExport TypedPropertyValueHandler> } return valueProp; } + }; } } diff --git a/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/AnalysisDataService.cpp b/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/AnalysisDataService.cpp index 7723bf3eb02e..90af321c1614 100644 --- a/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/AnalysisDataService.cpp +++ b/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/AnalysisDataService.cpp @@ -1,70 +1,68 @@ #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 +#include using namespace Mantid::API; +using namespace Mantid::Kernel; using Mantid::PythonInterface::DataServiceExporter; using Mantid::PythonInterface::TrackingInstanceMethod; -using namespace Mantid::PythonInterface::Registry; using namespace boost::python; +namespace { -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(item.ptr(), "id")); +typedef boost::weak_ptr Workspace_wptr; + +/** + * 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) { + extract weakExtract(item); + if(weakExtract.check()) { + self.add(name, weakExtract().lock()); + return; + } - 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(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(); - } + extract sharedExtract(item); + if(sharedExtract.check()) { + self.add(name, sharedExtract()); + } else { + throw std::runtime_error("Unable to add unknown object type to ADS"); } - /** - * 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(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(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) { + extract weakExtract(item); + if(weakExtract.check()) { + self.add(name, weakExtract().lock()); + return; } + extract sharedExtract(item); + if(sharedExtract.check()) { + self.add(name, sharedExtract()); + } else { + throw std::runtime_error("Unable to add unknown object type to ADS"); + } } +} // clang-format off void export_AnalysisDataService() // clang-format on @@ -73,14 +71,16 @@ void export_AnalysisDataService() 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."); + // replace the add/addOrReplace,__setitem__ methods as we need to extract 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."); + "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::define(pythonClass); } - diff --git a/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/MatrixWorkspace.cpp b/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/MatrixWorkspace.cpp index 326886edf0d7..726a9e7db745 100644 --- a/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/MatrixWorkspace.cpp +++ b/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/MatrixWorkspace.cpp @@ -12,25 +12,25 @@ #include #include #include +#include using namespace Mantid::API; using namespace Mantid::Geometry; using namespace Mantid::Kernel; +using namespace Mantid::PythonInterface::Converters; +using namespace Mantid::PythonInterface::Policies; +using namespace Mantid::PythonInterface::Registry; using namespace boost::python; -namespace Converters = Mantid::PythonInterface::Converters; -namespace Policies = Mantid::PythonInterface::Policies; -namespace Registry = Mantid::PythonInterface::Registry; - namespace { /// Typedef for data access, i.e. dataX,Y,E members typedef Mantid::MantidVec&(MatrixWorkspace::*data_modifier)(const std::size_t); /// return_value_policy for read-only numpy array - typedef return_value_policy > return_readonly_numpy; + typedef return_value_policy > return_readonly_numpy; /// return_value_policy for read-write numpy array - typedef return_value_policy > return_readwrite_numpy; + typedef return_value_policy > return_readwrite_numpy; //------------------------------- Overload macros --------------------------- // Overloads for binIndexOf function which has 1 optional argument @@ -137,7 +137,7 @@ void export_MatrixWorkspace() class_, boost::noncopyable>("MatrixWorkspace", no_init) //--------------------------------------- Meta information ----------------------------------------------------------------------- .def("blocksize", &MatrixWorkspace::blocksize, args("self"), "Returns size of the Y data array") - .def("getNumberHistograms", &MatrixWorkspace::getNumberHistograms, args("self"), + .def("getNumberHistograms", &MatrixWorkspace::getNumberHistograms, args("self"), "Returns the number of spectra in the workspace") .def("binIndexOf", &MatrixWorkspace::binIndexOf, MatrixWorkspace_binIndexOfOverloads((arg("self"), arg("xvalue"), arg("workspaceIndex")), @@ -149,73 +149,73 @@ void export_MatrixWorkspace() args("self", "det"), "Returns the signed two theta value for given detector") .def("getSpectrum", (ISpectrum * (MatrixWorkspace::*)(const size_t))&MatrixWorkspace::getSpectrum, - return_internal_reference<>(), args("self", "workspaceIndex"), + return_internal_reference<>(), args("self", "workspaceIndex"), "Return the spectra at the given workspace index.") .def("getIndexFromSpectrumNumber",&MatrixWorkspace::getIndexFromSpectrumNumber,args("self"), "Returns workspace index correspondent to the given spectrum number. Throws if no such spectrum is present in the workspace") - .def("getDetector", &MatrixWorkspace::getDetector, return_value_policy(), - args("self", "workspaceIndex"), + .def("getDetector", &MatrixWorkspace::getDetector, return_value_policy(), + args("self", "workspaceIndex"), "Return the Detector or DetectorGroup that is linked to the given workspace index") .def("getRun", &MatrixWorkspace::mutableRun, return_internal_reference<>(), args("self"), "Return the Run object for this workspace") .def("axes", &MatrixWorkspace::axes, args("self"), "Returns the number of axes attached to the workspace") .def("getAxis", &MatrixWorkspace::getAxis, return_internal_reference<>(), args("self", "axis_index")) - .def("isHistogramData", &MatrixWorkspace::isHistogramData, args("self"), + .def("isHistogramData", &MatrixWorkspace::isHistogramData, args("self"), "Returns True if this is considered to be binned data.") .def("isDistribution", (const bool& (MatrixWorkspace::*)() const)&MatrixWorkspace::isDistribution, return_value_policy(), args("self"), "Returns the status of the distribution flag") - .def("YUnit", &MatrixWorkspace::YUnit, args("self"), + .def("YUnit", &MatrixWorkspace::YUnit, args("self"), "Returns the current Y unit for the data (Y axis) in the workspace") .def("YUnitLabel", &MatrixWorkspace::YUnitLabel, args("self"), "Returns the caption for the Y axis") // Deprecated - .def("getNumberBins", &getNumberBinsDeprecated, args("self"), + .def("getNumberBins", &getNumberBinsDeprecated, args("self"), "Returns size of the Y data array (deprecated, use blocksize instead)") - .def("getSampleDetails", &getSampleDetailsDeprecated, return_internal_reference<>(), args("self"), + .def("getSampleDetails", &getSampleDetailsDeprecated, return_internal_reference<>(), args("self"), "Return the Run object for this workspace (deprecated, use getRun instead)") //--------------------------------------- Setters ------------------------------------ - .def("setYUnitLabel", &MatrixWorkspace::setYUnitLabel, args("self", "newLabel"), + .def("setYUnitLabel", &MatrixWorkspace::setYUnitLabel, args("self", "newLabel"), "Sets a new caption for the data (Y axis) in the workspace") - .def("setYUnit", &MatrixWorkspace::setYUnit, args("self", "newUnit"), + .def("setYUnit", &MatrixWorkspace::setYUnit, args("self", "newUnit"), "Sets a new unit for the data (Y axis) in the workspace") .def("setDistribution", (bool& (MatrixWorkspace::*)(const bool))&MatrixWorkspace::isDistribution, - return_value_policy(), args("self", "newVal"), + return_value_policy(), args("self", "newVal"), "Set distribution flag. If True the workspace has been divided by the bin-width.") .def("replaceAxis", &MatrixWorkspace::replaceAxis, args("self", "axisIndex", "newAxis")) //--------------------------------------- Read spectrum data ------------------------- .def("readX", &MatrixWorkspace::readX, return_readonly_numpy(), - args("self", "workspaceIndex"), + args("self", "workspaceIndex"), "Creates a read-only numpy wrapper around the original X data at the given index") .def("readY", &MatrixWorkspace::readY, return_readonly_numpy(), - args("self", "workspaceIndex"), + args("self", "workspaceIndex"), "Creates a read-only numpy wrapper around the original Y data at the given index") .def("readE", &MatrixWorkspace::readE, return_readonly_numpy(), - args("self", "workspaceIndex"), + args("self", "workspaceIndex"), "Creates a read-only numpy wrapper around the original E data at the given index") .def("readDx", &MatrixWorkspace::readDx, return_readonly_numpy(), - args("self", "workspaceIndex"), + args("self", "workspaceIndex"), "Creates a read-only numpy wrapper around the original Dx data at the given index") //--------------------------------------- Write spectrum data ------------------------ .def("dataX", (data_modifier)&MatrixWorkspace::dataX, return_readwrite_numpy(), - args("self", "workspaceIndex"), + args("self", "workspaceIndex"), "Creates a writable numpy wrapper around the original X data at the given index") .def("dataY", (data_modifier)&MatrixWorkspace::dataY, return_readwrite_numpy(), - args("self", "workspaceIndex"), + args("self", "workspaceIndex"), "Creates a writable numpy wrapper around the original Y data at the given index") .def("dataE", (data_modifier)&MatrixWorkspace::dataE, return_readwrite_numpy(), - args("self", "workspaceIndex"), + args("self", "workspaceIndex"), "Creates a writable numpy wrapper around the original E data at the given index") .def("dataDx", (data_modifier)&MatrixWorkspace::dataDx, return_readwrite_numpy(), - args("self", "workspaceIndex"), + args("self", "workspaceIndex"), "Creates a writable numpy wrapper around the original Dx data at the given index") - .def("setX", &setXFromPyObject, args("self", "workspaceIndex", "x"), + .def("setX", &setXFromPyObject, args("self", "workspaceIndex", "x"), "Set X values from a python list or numpy array. It performs a simple copy into the array.") - .def("setY", &setYFromPyObject, args("self", "workspaceIndex", "y"), + .def("setY", &setYFromPyObject, args("self", "workspaceIndex", "y"), "Set Y values from a python list or numpy array. It performs a simple copy into the array.") - .def("setE", &setEFromPyObject, args("self", "workspaceIndex", "e"), + .def("setE", &setEFromPyObject, args("self", "workspaceIndex", "e"), "Set E values from a python list or numpy array. It performs a simple copy into the array.") // --------------------------------------- Extract data ------------------------------ @@ -236,23 +236,10 @@ void export_MatrixWorkspace() "Note: This can fail for large workspaces as numpy will require a block " "of memory free that will fit all of the data.") //-------------------------------------- Operators ----------------------------------- - .def("equals", &Mantid::API::equals, args("self", "other", "tolerance"), + .def("equals", &Mantid::API::equals, args("self", "other", "tolerance"), "Performs a comparison operation on two workspaces, using the " "CheckWorkspacesMatch algorithm") ; - - //------------------------------------------------------------------------------------------------- - - static const int NUM_IDS = 7; - static const char * WORKSPACE_IDS[NUM_IDS] = {\ - "GroupingWorkspace", "MaskWorkspace", "OffsetsWorkspace", - "RebinnedOutput", "SpecialWorkspace2D", "Workspace2D", "WorkspaceSingleValue" - }; - Registry::DataItemInterface entry; - for(int i = 0; i < NUM_IDS; ++i) - { - entry.castFromID(WORKSPACE_IDS[i]); - } - ; + DataItemInterface(); } diff --git a/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/PropertyManagerDataService.cpp b/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/PropertyManagerDataService.cpp index 157a454a24c6..7ad4431f87de 100644 --- a/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/PropertyManagerDataService.cpp +++ b/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/PropertyManagerDataService.cpp @@ -5,6 +5,7 @@ #include "MantidKernel/PropertyManager.h" #include +#include using namespace Mantid::API; using namespace Mantid::Kernel; diff --git a/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/Workspace.cpp b/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/Workspace.cpp index 20da316f1509..83a024efd0a9 100644 --- a/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/Workspace.cpp +++ b/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/Workspace.cpp @@ -1,4 +1,5 @@ #include "MantidAPI/Workspace.h" + #include "MantidPythonInterface/kernel/Registry/DataItemInterface.h" #include @@ -6,15 +7,16 @@ #include using namespace Mantid::API; -using Mantid::Kernel::DataItem; -using Mantid::PythonInterface::Registry::DataItemInterface; +using namespace Mantid::Kernel; +using namespace Mantid::PythonInterface; +using namespace Mantid::PythonInterface::Registry; using namespace boost::python; -namespace -{ - ///@cond - BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(Workspace_isDirtyOverloads, Workspace::isDirty, 0, 1) - ///@endcond +namespace { +///@cond +BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(Workspace_isDirtyOverloads, + Workspace::isDirty, 0, 1) +///@endcond } // clang-format off @@ -22,19 +24,27 @@ void export_Workspace() // clang-format on { class_, boost::noncopyable>("Workspace", no_init) - .def("getName", &Workspace::getName, return_value_policy(), - args("self"), "Returns the name of the workspace. This could be an empty string") - .def("getTitle", &Workspace::getTitle, args("self"), "Returns the title of the workspace") - .def("setTitle", &Workspace::setTitle, args("self", "title")) - .def("getComment", &Workspace::getComment, return_value_policy(), "Returns the comment field on the workspace") - .def("setComment", &Workspace::setComment, args("self", "comment")) - .def("isDirty", &Workspace::isDirty, Workspace_isDirtyOverloads(arg("n"), "True if the workspace has run more than n algorithms (Default=1)")) - .def("getMemorySize", &Workspace::getMemorySize, args("self"), "Returns the memory footprint of the workspace in KB") - .def("getHistory", (const WorkspaceHistory &(Workspace::*)() const)&Workspace::getHistory, return_value_policy(), - args("self"), "Return read-only access to the workspace history") - ; - - //------------------------------------------------------------------------------------------------- + .def("getName", &Workspace::getName, + return_value_policy(), args("self"), + "Returns the name of the workspace. This could be an empty string") + .def("getTitle", &Workspace::getTitle, args("self"), + "Returns the title of the workspace") + .def("setTitle", &Workspace::setTitle, args("self", "title")) + .def("getComment", &Workspace::getComment, + return_value_policy(), + "Returns the comment field on the workspace") + .def("setComment", &Workspace::setComment, args("self", "comment")) + .def("isDirty", &Workspace::isDirty, + Workspace_isDirtyOverloads(arg("n"), "True if the workspace has run " + "more than n algorithms " + "(Default=1)")) + .def("getMemorySize", &Workspace::getMemorySize, args("self"), + "Returns the memory footprint of the workspace in KB") + .def("getHistory", (const WorkspaceHistory &(Workspace::*)() const) & + Workspace::getHistory, + return_value_policy(), args("self"), + "Return read-only access to the workspace history"); + // register pointers DataItemInterface(); } diff --git a/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/WorkspaceFactory.cpp b/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/WorkspaceFactory.cpp index 775bcaa2dcf6..2476a2aa89c1 100644 --- a/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/WorkspaceFactory.cpp +++ b/Code/Mantid/Framework/PythonInterface/mantid/api/src/Exports/WorkspaceFactory.cpp @@ -1,14 +1,12 @@ #include "MantidAPI/WorkspaceFactory.h" #include "MantidAPI/ITableWorkspace.h" #include "MantidAPI/IPeaksWorkspace.h" -#include "MantidPythonInterface/kernel/Policies/DowncastingPolicies.h" #include #include using namespace boost::python; using namespace Mantid::API; -namespace Policies = Mantid::PythonInterface::Policies; namespace { @@ -27,7 +25,7 @@ namespace * @throw std::out_of_range If invalid (0 or less) size arguments are given * @throw NotFoundException If the class is not registered in the factory **/ - MatrixWorkspace_sptr createFromParentPtr(WorkspaceFactoryImpl & self, const MatrixWorkspace_sptr& parent, + Workspace_sptr createFromParentPtr(WorkspaceFactoryImpl & self, const MatrixWorkspace_sptr& parent, size_t NVectors = size_t(-1), size_t XLength = size_t(-1), size_t YLength = size_t(-1)) { return self.create(parent, NVectors, XLength, YLength); @@ -50,15 +48,14 @@ void export_WorkspaceFactory() typedef MatrixWorkspace_sptr (WorkspaceFactoryImpl::*createFromScratchPtr) (const std::string&,const size_t&, const size_t&, const size_t&) const; - class_("WorkspaceFactoryImpl", no_init) + class_("WorkspaceFactoryImpl", no_init) .def("create", &createFromParentPtr, createFromParent_Overload(createFromParentDoc, (arg("parent"), arg("NVectors")=-1, arg("XLength")=-1, - arg("YLength")=-1))[return_value_policy()]) + arg("YLength")=-1))) - .def("create", (createFromScratchPtr)&WorkspaceFactoryImpl::create, - return_value_policy(), + .def("create", (createFromScratchPtr)&WorkspaceFactoryImpl::create, createFromScratchDoc, (arg("className"), arg("NVectors"), arg("XLength"), arg("YLength"))) .def("createTable", &WorkspaceFactoryImpl::createTable, @@ -72,4 +69,3 @@ void export_WorkspaceFactory() .staticmethod("Instance") ; } - diff --git a/Code/Mantid/Framework/PythonInterface/mantid/dataobjects/CMakeLists.txt b/Code/Mantid/Framework/PythonInterface/mantid/dataobjects/CMakeLists.txt index 522415384f17..b2b43e8c5d58 100644 --- a/Code/Mantid/Framework/PythonInterface/mantid/dataobjects/CMakeLists.txt +++ b/Code/Mantid/Framework/PythonInterface/mantid/dataobjects/CMakeLists.txt @@ -8,6 +8,7 @@ set ( MODULE_TEMPLATE src/dataobjects.cpp.in ) # -- Do NOT sort this list. The order defines the order in which the export # definitions occur and some depend on their base classes being exported first -- set ( EXPORT_FILES + src/Exports/TableWorkspace.cpp src/Exports/Workspace2D.cpp ) @@ -53,11 +54,11 @@ set_python_properties( PythonDataObjectsModule _dataobjects ) set_target_output_directory ( PythonDataObjectsModule ${OUTPUT_DIR} .pyd ) # Add the required dependencies -target_link_libraries ( PythonDataObjectsModule PythonAPIModule PythonGeometryModule +target_link_libraries ( PythonDataObjectsModule PythonAPIModule PythonGeometryModule PythonKernelModule DataObjects ) if (OSX_VERSION VERSION_GREATER 10.8) - set_target_properties( PythonDataObjectsModule PROPERTIES + set_target_properties( PythonDataObjectsModule PROPERTIES INSTALL_RPATH "@loader_path/../../../MacOS;@loader_path/../kernel/;@loader_path/../geometry/;@loader_path/../api/") endif () ########################################################################### diff --git a/Code/Mantid/Framework/PythonInterface/mantid/dataobjects/src/Exports/TableWorkspace.cpp b/Code/Mantid/Framework/PythonInterface/mantid/dataobjects/src/Exports/TableWorkspace.cpp new file mode 100644 index 000000000000..bfda43f40655 --- /dev/null +++ b/Code/Mantid/Framework/PythonInterface/mantid/dataobjects/src/Exports/TableWorkspace.cpp @@ -0,0 +1,19 @@ +#include "MantidDataObjects/TableWorkspace.h" +#include + +#include "MantidPythonInterface/kernel/Registry/DataItemInterface.h" + +using Mantid::API::Workspace; +using Mantid::DataObjects::TableWorkspace; +using namespace Mantid::PythonInterface::Registry; +using namespace boost::python; + +void export_TableWorkspace() +{ +class_, + boost::noncopyable>("TableWorkspace", no_init) + ; + + // register pointers + DataItemInterface(); +} diff --git a/Code/Mantid/Framework/PythonInterface/mantid/dataobjects/src/Exports/Workspace2D.cpp b/Code/Mantid/Framework/PythonInterface/mantid/dataobjects/src/Exports/Workspace2D.cpp index e8c12737ed4b..ad2410f06c87 100644 --- a/Code/Mantid/Framework/PythonInterface/mantid/dataobjects/src/Exports/Workspace2D.cpp +++ b/Code/Mantid/Framework/PythonInterface/mantid/dataobjects/src/Exports/Workspace2D.cpp @@ -1,8 +1,12 @@ #include "MantidDataObjects/Workspace2D.h" +#include "MantidPythonInterface/kernel/Registry/DataItemInterface.h" + #include +#include using Mantid::API::MatrixWorkspace; using Mantid::DataObjects::Workspace2D; +using namespace Mantid::PythonInterface::Registry; using namespace boost::python; void export_Workspace2D() @@ -10,4 +14,7 @@ void export_Workspace2D() class_, boost::noncopyable>("Workspace2D", no_init) ; + + // register pointers + DataItemInterface(); }